目录



1.前言

我的话是微服务B调用微服务A的controller层

2.生产者

微服务A请求接口如下:

@GetMapping("/listUniteProdPrice")
public AjaxResult listUniteProdPrice(){
return AjaxResult.success(productService.listUniteProdPrice());
}


3.消费者

为了便于管理,创建一个文件夹名为client,与service、mapper层统计,接口命名规则为xxxFeignService,表明是远程调用的接口;​​FeignClient注解​​的值是微服务A的​​spring.application.name​

@FeignClient(value="MICRO-A",fallbackFactory = IProductFallback.class)
@Component
public interface IProductFeignService {
@GetMapping("/product/listProductByDeptId")
AjaxResult listProductByDeptId(@RequestParam("deptId") Long deptId);

@GetMapping("/product/listUniteProdPrice")
AjaxResult listUniteProdPrice();
}


同时需要在启动类添加注解,并标明远程调用接口的文件夹在哪个位置

​@EnableFeignClients({"com.workflow.oa.client"}) ​

4.扩展

加入hystrix/sentinel断路器...

如果接口长时间没有返回或报错,返回消息给前台

@Slf4j
public class IProductFallback implements FallbackFactory<IProductService> {
@Override
public IProductService create(Throwable cause) {
log.error("获取产品数据调用失败:{}", cause.getMessage());
return new IProductService() {

@Override
public AjaxResult listProductByDeptId(Long deptId) {
return AjaxResult.error("获取产品数据调用失败:{}", cause.getMessage());
}

@Override
public AjaxResult listUniteProdPrice() {
return AjaxResult.error("获取产品数据调用失败:{}", cause.getMessage());
}
};
}
}