Feign Client的超时时间

编程入门 行业动态 更新时间:2024-10-11 23:26:50

Feign Client的超时<a href=https://www.elefans.com/category/jswz/34/1771441.html style=时间"/>

Feign Client的超时时间

如何配置好 feign、ribbon、hystrix 的超时设置?

其实是有套路的,因为Feign的请求:其实是Hystrix+Ribbon。Hystrix在最外层,然后再到Ribbon,最后里面的是http请求。所以说。Hystrix的熔断时间必须大于Ribbon的 ( ConnectTimeout + ReadTimeout )。而如果Ribbon开启了重试机制,还需要乘以对应的重试次数,保证在Ribbon里的请求还没结束时,Hystrix的熔断时间不会超时。

Feign Client Configuration

# 默认开启
feign.httpclient.enabled=false
# 默认关闭
feign.okhttp.enabled=true
# 默认关闭
feign.hystrix.enabled=false
# 默认关闭
feign.sentinel.enabled=true# default context 连接超时时间
feign.client.config.default.connectTimeout = 5000
# default context 读超时时间
feign.client.config.default.readTimeout = 10000# 设置重试处理器,默认直接抛出异常
# feign.client.config.default.retryer = Class<Retryer>
# 设置日志级别,默认NONE
# feign.client.config.default.loggerLevel = FULL

Hystrix Configuration

hystrix在ribbon的外层处理。

# 全局设置超时:
hystrixmand.default.execution.isolation.thread.timeoutInMilliseconds: 30000

Ribbon Configuration

当Ribbon调用接口发送连接异常或者超时异常时会触发Ribbon 的重试机制。

# 连接超时时间,默认为1秒,该值会被FeignClient配置connectTimeout覆盖
ribbon.ConnectTimeout=5000
# 读超时时间,默认为1秒,该值会被FeignClient配置readTimeout覆盖
ribbon.ReadTimeout=5000
# 最大重试次数
ribbon.MaxAutoRetries=1

OKHttp 超时时间

所设置的连接时间和超时时间最后会动态设置到OkHttpClient中,最底层也就是Socket的连接时间和读超时时间。也就是说,直接配置OkHttpClient是无效的。

解决方案:添加OkHttp Client的请求Interceptor,动态设置超时时间。

@Bean("okHttpClient")
public OkHttpClient okHttpClient(ConnectionPool connectionPool) {return new OkHttpClient().newBuilder().connectionPool(connectionPool)// 改值在FeignClient体系中会被动态覆盖.connectTimeout(6, TimeUnit.SECONDS)// 改值在FeignClient体系中会被动态覆盖.readTimeout(10, TimeUnit.SECONDS)// 添加拦截器,支持动态设置超时时间.addInterceptor(new OkHttpClientDynamicTimeoutInterceptor()).eventListener(eventListener()).build();
}

Apache HttpClient 超时配置

OkHttpProperties

@Data
@ConfigurationProperties(prefix = "okhttp")
public class OkHttpProperties {/*** 连接超时,默认 10 秒,0 表示没有超时限制*/private Integer connectTimeout = 10;/*** 响应超时,默认 10 秒,0 表示没有超时限制*/private Integer readTimeout = 10;/*** 写超时,默认 10 秒,0 表示没有超时限制*/private Integer writeTimeout = 10;/*** 连接池中整体的空闲连接的最大数量,默认 5 个连接数*/private Integer maxIdleConnections = 5;/*** 连接空闲时间最大时间,单位秒,默认 300 秒*/private Long keepAliveDuration = 300L;
}

OkHttpConfig

@EnableConfigurationProperties(OkHttpProperties.class)
@Configuration
public class OkHttpConfig {@Autowiredprivate OkHttpProperties properties;@Beanpublic OkHttpClient okHttpClient() {return new OkHttpClient.Builder().sslSocketFactory(sslSocketFactory(), x509TrustManager())// 是否开启缓存.retryOnConnectionFailure(false).connectionPool(pool()).connectTimeout(properties.getConnectTimeout(), TimeUnit.SECONDS).readTimeout(properties.getReadTimeout(), TimeUnit.SECONDS).writeTimeout(properties.getWriteTimeout(), TimeUnit.SECONDS).hostnameVerifier((hostname, session) -> true)// 设置代理
//            	.proxy(new Proxy(Proxy.Type.HTTP, new InetSocketAddress("127.0.0.1", 8888)))// 拦截器
//                .addInterceptor().addNetworkInterceptor(new Interceptor() {@Overridepublic Response intercept(Chain chain) throws IOException {Request request = chain.request().newBuilder().addHeader("Connection", "close").build();return chain.proceed(request);}}).build();}@Beanpublic X509TrustManager x509TrustManager() {return new X509TrustManager() {@Overridepublic void checkClientTrusted(X509Certificate[] chain, String authType)throws CertificateException {}@Overridepublic void checkServerTrusted(X509Certificate[] chain, String authType)throws CertificateException {}@Overridepublic X509Certificate[] getAcceptedIssuers() {return new X509Certificate[0];}};}@Beanpublic SSLSocketFactory sslSocketFactory() {try {// 信任任何链接SSLContext sslContext = SSLContext.getInstance("TLS");sslContext.init(null, new TrustManager[]{x509TrustManager()}, new SecureRandom());return sslContext.getSocketFactory();} catch (NoSuchAlgorithmException | KeyManagementException e) {e.printStackTrace();}return null;}@Beanpublic ConnectionPool pool() {return new ConnectionPool(properties.getMaxIdleConnections(),properties.getKeepAliveDuration(),TimeUnit.SECONDS);}
}

更多推荐

Feign Client的超时时间

本文发布于:2024-02-05 10:54:38,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1745319.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:时间   Feign   Client

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!