本文目录

  • 一、背景简介
  • 二、调用方(项目A)
  • 第一步:添加Maven依赖
  • 第二步:添加@EnableFeignClients注解
  • 第三步:创建FeignClient接口
  • 第四步:调用方使用
  • 三、调用方(项目B)
  • 第一步:添加Maven依赖
  • 第二步:添加@EnableFeignClients注解
  • 第三步:创建Controller控制层


一、背景简介

最近做的一个公司项目,由于功能需求,需要在两个springboot项目间的进行远程调用,我使用的是通过Feign的方式。而springboot本身封装了两种方法HTTP调用方式:

  1. feign的远程调用(http接口调用)
  2. RestTemplate

下面记录一下我使用的过程(项目A调用项目B):

二、调用方(项目A)

首先是调用方(项目A):

项目技术架构:Spring boot(2.0.0.RELEASE) + Mybatis-plus(3.1.1) + druid(1.1.9)

第一步:添加Maven依赖

<!-- openfein的依赖 -->
<dependency>
	<groupId>org.springframework.cloud</groupId>
	<artifactId>spring-cloud-starter-openfeign</artifactId>
	<version>2.0.4.RELEASE</version>
</dependency>

第二步:添加@EnableFeignClients注解

在启动类上添加@EnableFeignClients(basePackages = {“com..”}),其中****号代表你实际的项目目录结构,可以根据你的项目实际填写;

第三步:创建FeignClient接口

package com.iot.flowapplication.feign;

import com.iot.flowapplication.common.domain.vo.JsonResult;
import com.iot.flowapplication.organization.entity.User;
import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

/**
 * <p>此类用于流程管理服务调用日报服务查询日报列表</p>
 * <p>@author:xzj</p>
 * <p>@date:2019/8/29</p>
 * <p>@remark</p>
 */
@FeignClient(url = "${dailyServer}", name = "daily")
public interface DailyService {

    /**
     * 日报统计
     *
     * @param currUser 当前登录用户
     * @return 日报列表
     */
    @RequestMapping(value = "/api/statistics/getNoSubmitList", method = RequestMethod.POST)
    JsonResult dailyStatistics(@RequestBody User currUser);
}

说明:
@FeignClient(url = “${dailyServer}”, name = “daily”),其中url的值是被调用方的域名加端口,例如:(dailyServer: http://localhost:8006),而name的值可以写成被调用方的服务名称等。
写法总结:

  1. 在resources目录下的yml配置文件里写:dailyServer: http://localhost:8006
  2. 直接在url后面写上,例如:@FeignClient(url = “http://localhost:8006”, name =
    “daily”)

推荐使用第一种写法。

第四步:调用方使用

package com.iot.flowapplication.feign;

import com.iot.flowapplication.basics.web.BaseController;
import com.iot.flowapplication.common.domain.vo.JsonResult;
import com.iot.flowapplication.organization.entity.User;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;

/**
 * <p>DailyStatisticsController 此类用于:本项目对前端提供的接口映射</p>
 * <p>@author:hujm</p>
 * <p>@date:2021年04月29日 14:37</p>
 * <p>@remark:</p>
 */
@Api(tags = "首页日报统计", value = "daily")
@RestController
@RequestMapping(value = "daily")
public class DailyStatisticsController extends BaseController {

    @Resource
    private DailyService dailyService;

    @ApiOperation(value = "获取日报未提交人员列表")
    @GetMapping(value = "/getNoSubmitList")
    public JsonResult getList() {
        User currUser = getCurrUser();
        JsonResult jsonResult = dailyService.dailyStatistics(currUser);
        return jsonResult;
    }
}

至此调用方的步骤全部完成。

三、调用方(项目B)

以下是被调用方(项目B):

项目技术架构:Spring boot(2.0.0.RELEASE) + Mybatis-plus(3.1.1) + druid(1.1.9)

第一步:添加Maven依赖

<!--openfein的依赖-->
<dependency>
	<groupId>org.springframework.cloud</groupId>
	<artifactId>spring-cloud-starter-openfeign</artifactId>
	<version>2.0.4.RELEASE</version>
</dependency>

第二步:添加@EnableFeignClients注解

在启动类上添加@EnableFeignClients(basePackages = {“com..”}),其中****号代表你实际的项目目录结构,可以根据你的项目实际填写;
@EnableFeignClients(basePackages = “com..”)

第三步:创建Controller控制层

此步骤按照正常的Spring MVC方式创建一个Controller路径映射即可。这个Controller和平时写的一样。

package com.iot.daily.module.web;

import com.iot.daily.common.domain.vo.JsonResult;
import com.iot.daily.common.web.BaseController;
import com.iot.daily.module.service.DailyStatisticsService;
import com.iot.daily.organization.entity.User;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;

/**
 * <p>DailyStatisticsController 此类用于:日报统计相关接口</p>
 * <p>@author:hujm</p>
 * <p>@date:2021年04月24日 9:14</p>
 * <p>@remark:</p>
 */
@Api(tags = "日报统计相关接口", value = "statistics")
@RestController
@RequestMapping(value = "/api/statistics")
public class DailyStatisticsController extends BaseController {

    @Resource
    private DailyStatisticsService dailyStatisticsService;

    @ApiOperation(value = "获取日报未提交人员列表,用于Portal页面日报统计未提交人员列表")
    @PostMapping(value = "/getNoSubmitList")
    public JsonResult getNoSubmitList(@RequestBody User user) {

        JsonResult jsonResult = dailyStatisticsService.getNoSubmitList(user);
        return jsonResult;
    }
}

说明:DailyStatisticsService接口,以及Mapper接口,都按照正常的业务流程处理即可。而方法接受的参数(User对象)则为调用方(项目A)传入过来的参数。

完结!