记一个openFeign使用拦截器RequestInterceptor熔断机制踩坑

feign配置如下

@Configuration
public class LoggingConfigure {
    @Bean
    public Logger.Level config(){
        return Logger.Level.FULL;
    }
}

yaml配置如下:

feign:
  hystrix:
    enabled: true
  client:
    config:
      default:
        connectTimeout: 100000
        readTimeout: 100000
hystrix:
  command:
    default:
      execution:
        isolation:
          thread:
            timeoutInMilliseconds: 30000
ribbon:
  ReadTimeout: 60000
  ConnectTimeout: 60000

此时feign接口熔断机制为:线程模式,熔断机制正常

配置文件修改为:(使用RequestInterceptor拦截器)

@Configuration
public class FeignConfiguration implements RequestInterceptor {

    @Override
    public void apply(RequestTemplate requestTemplate) {
        ServletRequestAttributes attributes = (ServletRequestAttributes)
                RequestContextHolder.getRequestAttributes();
        HttpServletRequest request = attributes.getRequest();
        Enumeration<String> headerNames = request.getHeaderNames();
        if (headerNames != null) {
            while (headerNames.hasMoreElements()) {
                String name = headerNames.nextElement();
                String values = request.getHeader(name);
                requestTemplate.header(name, values);
            }
        }
    }
    @Bean
    public Logger.Level config(){
        return Logger.Level.FULL;
    }
}

导致hystrix熔断机制失效,接口调用异常:
在这里插入图片描述
出现该错误原因:
在feign调用之前,我给他开启了一个拦截器 RequestInterceptor实现类
里面有使用到ServletRequestAttributes 获取请求数据

当feign开启熔断模式的时候,feign 调用会失败 (feign: hystrix: enabled: true)

原因:feign 使用的是线程池模式,当开启熔断的时候,feign 所在的服务端不在同一个线程,这是attributes取到的将会是空值

解决方案:
将hystrix熔断方式: 线程模式改为信号量模式

feign:
  hystrix:
    enabled: true
  client:
    config:
      default:
        connectTimeout: 100000
        readTimeout: 100000
hystrix:
 command:
   default:
     execution:
       isolation:
         thread:
           timeoutInMilliseconds: 30000
         strategy: SEMAPHORE  
ribbon:
  ReadTimeout: 60000
  ConnectTimeout: 60000

到此问题完美解决;
附带一个文章链接:
文章链接

  • 3
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值