Options > appConfig > defaultConfig > OKhttp
# Options参数
@PostMapping(value = "/api/test/options")Object testOptions(Request.Options options, @RequestBody List<Param> Params);
# xx应用 appConfig
tTimeout=2000
adTimeout=2000# feign全局默认 defaultConfig
tTimeout=1500
adTimeout=3000
# OKHttp的超时配置
return new OkHttpClient.Builder().readTimeout(readTimeout, TimeUnit.MILLISECONDS).connectTimeout(connectTimeout, TimeUnit.MILLISECONDS).writeTimeout(writeTimeout,TimeUnit.MILLISECONDS).build()
动态设置超时参数
feign 版本 >=10.3 支持
feign 版本 <=10.2.3 无法动态设置Options参数
详见SynchronousMethodHandler.invoke
@Overridepublic Object invoke(Object[] argv) throws Throwable {RequestTemplate template = ate(argv);Options options = findOptions(argv);Retryer retryer = r.clone();...}Options findOptions(Object[] argv) {if (argv == null || argv.length == 0) {return this.options;}return (Options) Stream.of(argv).filter(o -> o instanceof Options).findFirst().orElse(this.options);}
// 不生效@GetMapping(value = "/tool/sleep")@Headers("k2:v2")Result<String> sleep(@RequestParam(value = "ms") Integer ms);
测试后,不生效
核心关注下FeignClient 的 Contract
如果是 SpringMvcContract 则使用 @RequestMapping 中的 headers
如果是 Contract.Default 则使用 @Headers 注解即可
// 正解@GetMapping(value = "/tool/sleep", headers = "Retry=true")Result<String> sleep(@RequestParam(value = "ms") Integer ms);
ps: openFeign 会默认启用SpringMvcContract
// FeignClientsConfiguration@Bean@ConditionalOnMissingBeanpublic Contract feignContract(ConversionService feignConversionService) {return new SpringMvcContract(this.parameterProcessors, feignConversionService);}
原因
详见此文
我的解法1
底层使用 OkHttpClient
定义 OkhttpInterceptor
默认针对 IOException + Get/Put 操作做重试 (可结合header做定制扩展)
上游处理 RpcException + FeignException
我的解法2
底层使用 OkHttpClient
定义 OkhttpInterceptor
实现异常的捕获、包装与转换
上游只处理 RpcException
OKHTTP默认
OkHttpClient.Builder
maxIdleConnections=5
keepAliveTime= 5 mins
feign-okhttp配置
FeignAutoConfiguration.OkHttpFeignConfiguration.httpClientConnectionPool
maxIdleConnections=200
keepAliveTime= 900 s
# 可通过属性配置 FeignHttpClientProperties
feign.httpclient.maxConnections=50
feign.httpclient.timeToLive=900
多个FeignClient 里共用一个 OkHttpClient
可以在项目启动时查看
# Feign.Builder
public Feign build() 看client
public <T> T target(Target<T> target) 看 target 的type和name
回收原则:idle 数量多 或 idle的时间长
(idleCount> maxIdleConnections || longestIdleDurationNs >= this.keepAliveDurationNs)
详见代码 ConnectionPool.cleanup
# ConnectionPool.cleanupif (longestIdleDurationNs >= this.keepAliveDurationNs|| idleConnectionCount > this.maxIdleConnections) {// We've found a connection to evict. Remove it from the list, then close it below (outside// of the synchronized block).ve(longestIdleConnection);
}
本文发布于:2024-02-08 19:54:30,感谢您对本站的认可!
本文链接:https://www.4u4v.net/it/170739342768546.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
留言与评论(共有 0 条评论) |