SpringBoot项目中feignClient使用

1 在application.yml 中加入两个配置 

# feign client 配置
hystrix:
  threadpool:
    default:
      coreSize: 500
      maxQueueSize: -1
      queueSizeRejectionThreshold: 10000
  command:
    default:
      circuitBreaker:
        requestVolumeThreshold: 1500
      execution:
        #(超时时间)
        timeout:
          enabled: false
        #(上下文传递)
        isolation:
          strategy: SEMAPHORE
          semaphore:
            maxConcurrentRequests: 1500
            maxSemaphores: 1500
ribbon:
  ConnectTimeout: 50000000
  ReadTimeout: 500000000

2在主类上加入(scanBasePackageClasses = FeignInterceptor.class, scanBasePackages = {"com.example"}) 


FeignInterceptor用来扫描 传递上下文的拦截器
 {"com.example"}用来扫描自己的包
 @EnableFeignClients(主类上)扫描被@FeignClient注解的接口
  
@EnableFeignClients
@SpringBootApplication(scanBasePackageClasses = FeignInterceptor.class, scanBasePackages = {"com.example"})

3将xxxRestContextInterceptor加入到拦截器中

 @Configuration
public class  WebAppConfig extends WebMvcConfigurerAdapter {
 
    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        //注册自定义拦截器,添加拦截路径和排除拦截路径
        registry.addInterceptor(new XsyRestContextInterceptor());
    }
}

4接口调用说明

声明一个接口比如:PaasAggFeignClient,在其上显式加上@FeignClient注解
内部填入Eureka上已注册服务名称比如:@FeignClient("manager-service", configuration = FeignInterceptor.class)
其中configuration = FeignInterceptor.class是要加上的,保证FeignInterceptor生效
 
自定义方法,请求路径为该服务上对应的rest请求路径,如下
  
@FeignClient("manager-service")
public interface PaasAggFeignClient {
    @RequestMapping(value = "/api/xxxx/test/description", method = RequestMethod.GET)
    String testDescription();
 
    @RequestMapping(value = "/api/xxxx/test/{id}", method = RequestMethod.GET)
    String testInfo(@PathVariable("id") Long id);
 
    @RequestMapping(value = "/api/xxxx/test/", method = RequestMethod.POST, consumes = {MediaType.APPLICATION_JSON})
    String testCreate(@RequestBody JSONObject jsonObject);
 
    @RequestMapping(value = "/api/xxxx/test/{id}", method = RequestMethod.DELETE)
    String testDelete(@PathVariable("id") Long id);
 
    @RequestMapping(value = "/api/xxxx/test/{id}", method = RequestMethod.PATCH, consumes = {MediaType.APPLICATION_JSON})
    String testUpdate(@PathVariable("id") Long id, @RequestBody JSONObject jsonObject);
}

5在对应controller内引入自定义接口即可使用

@Resource
private PaasAggFeignClient paasAggFeignClient;
 
String paasAggResult = paasAggFeignClient.testDescription();
 
String paasAggResult = paasAggFeignClient.testCreate(entity);
 
String paasAggResult = paasAggFeignClient.testDelete(id);
 
.....

 

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值