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

阅读: 评论:0

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

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

feign配置如下

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

yaml配置如下:

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

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

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

@Configuration
public class FeignConfiguration implements RequestInterceptor {@Overridepublic void apply(RequestTemplate requestTemplate) {ServletRequestAttributes attributes = (RequestAttributes();HttpServletRequest request = Request();Enumeration<String> headerNames = HeaderNames();if (headerNames != null) {while (headerNames.hasMoreElements()) {String name = Element();String values = Header(name);requestTemplate.header(name, values);}}}@Beanpublic Logger.Level config(){return Logger.Level.FULL;}
}

导致hystrix熔断机制失效,接口调用异常:

出现该错误原因:
在feign调用之前,我给他开启了一个拦截器 RequestInterceptor实现类
里面有使用到ServletRequestAttributes 获取请求数据

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

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

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

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

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

本文发布于:2024-02-04 04:44:37,感谢您对本站的认可!

本文链接:https://www.4u4v.net/it/170699336852165.html

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。

留言与评论(共有 0 条评论)
   
验证码:

Copyright ©2019-2022 Comsenz Inc.Powered by ©

网站地图1 网站地图2 网站地图3 网站地图4 网站地图5 网站地图6 网站地图7 网站地图8 网站地图9 网站地图10 网站地图11 网站地图12 网站地图13 网站地图14 网站地图15 网站地图16 网站地图17 网站地图18 网站地图19 网站地图20 网站地图21 网站地图22/a> 网站地图23