SpringCloud:Feign调用接口不稳定问题以及如何设置超时
1. Feign调用接口不稳定报错
Caused by: java.net.SocketException: Software caused connection abort: recv failed
at java.net.SocketInputStream.socketRead0(Native Method)
at java.net.SocketInputStream.socketRead(SocketInputStream.java:116)
at java.net.SocketInputStream.read(SocketInputStream.java:170)
at java.net.SocketInputStream.read(SocketInputStream.java:141)
at org.apache.http.impl.io.SessionInputBufferImpl.streamRead(SessionInputBufferImpl.java:137)
at org.apache.http.impl.io.SessionInputBufferImpl.fillBuffer(SessionInputBufferImpl.java:153)
at org.apache.http.impl.io.SessionInputBufferImpl.readLine(SessionInputBufferImpl.java:282)
at org.apache.http.impl.conn.DefaultHttpResponseParser.parseHead(DefaultHttpResponseParser.java:140)
at org.apache.http.impl.conn.DefaultHttpResponseParser.parseHead(DefaultHttpResponseParser.java:57)
at org.apache.http.impl.io.AbstractMessageParser.parse(AbstractMessageParser.java:259)
at org.apache.http.impl.DefaultBHttpClientConnection.receiveResponseHeader(DefaultBHttpClientConnection.java:163)
at org.apache.http.impl.conn.CPoolProxy.receiveResponseHeader(CPoolProxy.java:167)
at org.apache.http.protocol.HttpRequestExecutor.doReceiveResponse(HttpRequestExecutor.java:273)
at org.apache.http.protocol.HttpRequestExecutor.execute(HttpRequestExecutor.java:125)
at org.apache.http.impl.execchain.MainClientExec.execute(MainClientExec.java:271)
at org.apache.http.impl.execchain.ProtocolExec.execute(ProtocolExec.java:184)
at org.apache.http.impl.execchain.RetryExec.execute(RetryExec.java:88)
at org.apache.http.impl.execchain.RedirectExec.execute(RedirectExec.java:110)
at org.apache.http.impl.client.InternalHttpClient.doExecute(InternalHttpClient.java:184)
at org.apache.http.impl.client.CloseableHttpClient.execute(CloseableHttpClient.java:82)
at org.apache.http.impl.client.CloseableHttpClient.execute(CloseableHttpClient.java:107)
at org.apache.http.impl.client.CloseableHttpClient.execute(CloseableHttpClient.java:55)
at feign.httpclient.ApacheHttpClient.execute(ApacheHttpClient.java:87)
at org.springframework.cloud.netflix.feign.ribbon.RetryableFeignLoadBalancer$1.doWithRetry(RetryableFeignLoadBalancer.java:92)
at org.springframework.cloud.netflix.feign.ribbon.RetryableFeignLoadBalancer$1.doWithRetry(RetryableFeignLoadBalancer.java:77)
at org.springframework.retry.support.RetryTemplate.doExecute(RetryTemplate.java:286)
at org.springframework.retry.support.RetryTemplate.execute(RetryTemplate.java:163)
at org.springframework.cloud.netflix.feign.ribbon.RetryableFeignLoadBalancer.execute(RetryableFeignLoadBalancer.java:77)
at org.springframework.cloud.netflix.feign.ribbon.RetryableFeignLoadBalancer.execute(RetryableFeignLoadBalancer.java:48)
at com.netflix.client.AbstractLoadBalancerAwareClient$1.call(AbstractLoadBalancerAwareClient.java:109)
at com.netflix.loadbalancer.reactive.LoadBalancerCommand$3$1.call(LoadBalancerCommand.java:303)
at com.netflix.loadbalancer.reactive.LoadBalancerCommand$3$1.call(LoadBalancerCommand.java:287)
at rx.internal.util.ScalarSynchronousObservable$3.call(ScalarSynchronousObservable.java:231)
at rx.internal.util.ScalarSynchronousObservable$3.call(ScalarSynchronousObservable.java:228)
at rx.Observable.unsafeSubscribe(Observable.java:10211)
at rx.internal.operators.OnSubscribeConcatMap$ConcatMapSubscriber.drain(OnSubscribeConcatMap.java:286)
at rx.internal.operators.OnSubscribeConcatMap$ConcatMapSubscriber.onNext(OnSubscribeConcatMap.java:144)
at com.netflix.loadbalancer.reactive.LoadBalancerCommand$1.call(LoadBalancerCommand.java:185)
at com.netflix.loadbalancer.reactive.LoadBalancerCommand$1.call(LoadBalancerCommand.java:180)
at rx.Observable.unsafeSubscribe(Observable.java:10211)
at rx.internal.operators.OnSubscribeConcatMap.call(OnSubscribeConcatMap.java:94)
at rx.internal.operators.OnSubscribeConcatMap.call(OnSubscribeConcatMap.java:42)
at rx.Observable.unsafeSubscribe(Observable.java:10211)
at rx.internal.operators.OperatorRetryWithPredicate$SourceSubscriber$1.call(OperatorRetryWithPredicate.java:127)
at rx.internal.schedulers.TrampolineScheduler$InnerCurrentThreadScheduler.enqueue(TrampolineScheduler.java:73)
at rx.internal.schedulers.TrampolineScheduler$InnerCurrentThreadScheduler.schedule(TrampolineScheduler.java:52)
at rx.internal.operators.OperatorRetryWithPredicate$SourceSubscriber.onNext(OperatorRetryWithPredicate.java:79)
at rx.internal.operators.OperatorRetryWithPredicate$SourceSubscriber.onNext(OperatorRetryWithPredicate.java:45)
at rx.internal.util.ScalarSynchronousObservable$WeakSingleProducer.request(ScalarSynchronousObservable.java:276)
at rx.Subscriber.setProducer(Subscriber.java:209)
at rx.internal.util.ScalarSynchronousObservable$JustOnSubscribe.call(ScalarSynchronousObservable.java:138)
at rx.internal.util.ScalarSynchronousObservable$JustOnSubscribe.call(ScalarSynchronousObservable.java:129)
at rx.internal.operators.OnSubscribeLift.call(OnSubscribeLift.java:48)
at rx.internal.operators.OnSubscribeLift.call(OnSubscribeLift.java:30)
at rx.internal.operators.OnSubscribeLift.call(OnSubscribeLift.java:48)
at rx.internal.operators.OnSubscribeLift.call(OnSubscribeLift.java:30)
at rx.internal.operators.OnSubscribeLift.call(OnSubscribeLift.java:48)
at rx.internal.operators.OnSubscribeLift.call(OnSubscribeLift.java:30)
at rx.Observable.subscribe(Observable.java:10307)
at rx.Observable.subscribe(Observable.java:10274)
at rx.observables.BlockingObservable.blockForSingle(BlockingObservable.java:445)
at rx.observables.BlockingObservable.single(BlockingObservable.java:342)
at com.netflix.client.AbstractLoadBalancerAwareClient.executeWithLoadBalancer(AbstractLoadBalancerAwareClient.java:117)
at org.springframework.cloud.netflix.feign.ribbon.LoadBalancerFeignClient.execute(LoadBalancerFeignClient.java:63)
at feign.SynchronousMethodHandler.executeAndDecode(SynchronousMethodHandler.java:97)
... 117 common frames omitted
feign在调用时,会有不稳定的情况出现,时而出现接口调不通。解决方案如下,复写FeignRibbonClientAutoConfiguration中的HttpClient的配置。
解决办法:
创建FeignRibbonHttpClientPoolConfig配置类。
import org.apache.http.config.Registry;
import org.apache.http.config.RegistryBuilder;
import org.apache.http.conn.socket.ConnectionSocketFactory;
import org.apache.http.conn.socket.PlainConnectionSocketFactory;
import org.apache.http.conn.ssl.NoopHostnameVerifier;
import org.apache.http.conn.ssl.SSLConnectionSocketFactory;
import org.apache.http.impl.NoConnectionReuseStrategy;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.impl.conn.PoolingHttpClientConnectionManager;
import org.apache.http.ssl.SSLContexts;
import org.springframework.context.annotation.Bean;
import org.springframework.stereotype.Component; import javax.net.ssl.SSLContext;
import javax.net.ssl.TrustManager;
import javax.net.ssl.X509TrustManager;
import java.security.KeyManagementException;
import java.security.cert.CertificateException;
import java.security.cert.X509Certificate; @Component
public class FeignRibbonHttpClientPoolConfig { private static final int POOL_MAX_TOTAL = 3000;
private static final int DEFAULT_MAX_PER_ROUTE = 500; //validateAfterInactivity 空闲永久连接检查间隔,这个牵扯的还比较多
//官方推荐使用这个来检查永久链接的可用性,而不推荐每次请求的时候才去检查
private static final int VALIDATE_AFTER_INACTIVITY = 1000; @Bean(name = "httpClient", destroyMethod = "close")
CloseableHttpClient httpClient() throws KeyManagementException {
return buildCloseableHttpClient();
} /**
* 构建HttpClient连接池
*
* @return
* @throws KeyManagementException
*/
public CloseableHttpClient buildCloseableHttpClient() throws KeyManagementException {
SSLContext sslcontext = SSLContexts.createDefault();
sslcontext.init(null, new TrustManager[]{new TrustAnyManager()}, null); //设置https客户端信任万能证书
SSLConnectionSocketFactory ssf = new SSLConnectionSocketFactory(sslcontext, NoopHostnameVerifier.INSTANCE);
//注册请求方式,根据URL自动请求
Registry<ConnectionSocketFactory> socketFactoryRegistry = RegistryBuilder.<ConnectionSocketFactory>create()
.register("http", PlainConnectionSocketFactory.INSTANCE)
.register("https", ssf)
.build();
//创建Http连接池,单位时间内释放已使用过连接池中的连接
PoolingHttpClientConnectionManager connectionManager = new PoolingHttpClientConnectionManager(socketFactoryRegistry);
connectionManager.setMaxTotal(POOL_MAX_TOTAL);
connectionManager.setDefaultMaxPerRoute(DEFAULT_MAX_PER_ROUTE);
connectionManager.setValidateAfterInactivity(VALIDATE_AFTER_INACTIVITY); return HttpClients.custom()
.setConnectionManager(connectionManager)
.setConnectionReuseStrategy(NoConnectionReuseStrategy.INSTANCE)
.build();
} class TrustAnyManager implements X509TrustManager {
public void checkClientTrusted(X509Certificate[] chain,
String authType) throws CertificateException {
} public void checkServerTrusted(X509Certificate[] chain,
String authType) throws CertificateException {
} public X509Certificate[] getAcceptedIssuers() {
return new X509Certificate[]{};
}
}
}
2. Feign调用出现 connect time out 超时问题.
因为feign已经集成robbon,hystrix,调用在规定时间内达不到就会报上述错误,并且这个规定时间会很短
解决办法:配置文件加入如下配置: 指的是在调用的一方
#连接超时和读超时(以properties文件形式为例): feign.client.config.springApplicationName.connectTimeout=60000. feign.client.config.springApplicationName.readTimeout=60000.
#禁用Hystrix熔断检测(熔断检测默认是1秒)
feign.hystrix.enabled= false #spring.ApplicationName是服务提供方注册到eureka上的应用名称.
配置完成后,还需要配置Hystrix的熔断时间,如果Hystrix时长小于feign的超时时长,那feign的超时设置就不会生效
#配置hystrix的熔断时间
hystrix.command.default.execution.isolation.thread.timeoutInMilliseconds = 60000
或者
设置全局配置
#设置连接超时
ribbon.ConnectTimeout= 1000
#设置读取超时
ribbon.ReadTimeout= 1000
指定服务配置
#设置针对my-plan服务的连接超时
my-plan.ribbon.ConnectTimeout= 10000
#设置针对my-plan服务的读取超时
my-plan.ribbon.ReadTimeout= 10000
#设置针对my-plan服务所有操作请求都进行重试
my-plan.ribbon.OkToRetryOnAllOperations= true
#设置针对my-plan服务切换实例的重试次数
my-plan.ribbon.MaxAutoRetriesNextServer= 2
#设置针对my-plan服务的当前实例的重试次数
my-plan.ribbon.MaxAutoRetries= 1
文章转载至:https://blog.csdn.net/flysun3344/article/details/81117403
SpringCloud:Feign调用接口不稳定问题以及如何设置超时的更多相关文章
- feign调用接口session丢失解决方案
微服务使用feign相互之间调用时,因为feign默认不传输Header,存在session丢失的问题.例如,使用Feign调用某个远程API,这个远程API需要传递一个鉴权信息,我们可以把cooki ...
- spring cloud feign 调用接口报错"No message available
There was an unexpected error (type=Internal Server Error, status=500). status 404 reading HelloServ ...
- springcloud feign 调用
client端调用feigh, 要在启动类上添加@EnableFeignClients注解,并添加扫描: 不然会调用失败,找不到调用方法
- SpringBoot之Feign调用方式比较
一:事发原因 两个东家都使用SpringCloud,巴拉巴拉用上了Spring全家桶,从eureka到ribbon,从ribbon到feign,从feign到hystrix,然后在使用feign的时候 ...
- SpringCloud使用Feign调用其他客户端带参数的接口,传入参数为null或报错status 405 reading IndexService#del(Integer);
SpringCloud使用Feign调用其他客户端带参数的接口,传入参数为null或报错status 405 reading IndexService#del(Integer); 第一种方法: 如果你 ...
- 跟我学SpringCloud | 第三篇:服务的提供与Feign调用
跟我学SpringCloud | 第三篇:服务的提供与Feign调用 上一篇,我们介绍了注册中心的搭建,包括集群环境吓注册中心的搭建,这篇文章介绍一下如何使用注册中心,创建一个服务的提供者,使用一个简 ...
- Spring Cloud Feign 在调用接口类上,配置熔断 fallback后,输出异常
Spring Cloud Feign 在调用接口类上,配置熔断 fallback后,出现请求异常时,会进入熔断处理,但是不会抛出异常信息. 经过以下配置,可以抛出异常: 将原有ErrorEncoder ...
- SpringCloud项目,接口调用返回http 500 - Internal Server Error的错误
今天上班的时候,自己正在参与的Spring Cloud项目出现了问题,原本上周五还正常的项目突然所有接口调用都是返回http 500的错误. 项目的状态是在Eureka上可以看到对应微服务是在线状态, ...
- springcloud(七): 使用Feign调用Eureka Server客户端服务
当我们通过RestTemplate调用其它服务的API时,所需要的参数须在请求的URL中进行拼接,如果参数少的话或许我们还可以忍受,一旦有多个参数的话,这时拼接请求字符串就会效率低下,并且显得好傻. ...
随机推荐
- Linux服务之nginx服务篇二(搭建)
一.简易搭建安装步骤 0.检查环境 1.配置yum源 使用yum list nginx 检查yum源中是否有nginx安装包 #官方网络源需要安装epel-* #或使用251的adv源(老师的yum源 ...
- python基础之列表、元组
一.列表 列表(list)是我们最以后最常用的数据类型之一,通过列表可以对数据实现最方便的存储.修改等操作 作用:多个装备,多个爱好,多门课程,多个女朋友等 定义:[ ]内可以有多个任意类型的值,逗号 ...
- ubuntu中软件的升级管理-(转自Josh_)
给Ubuntu软件升级命令 sudo apt-get update --更新软件源 sudo apt-get upgrade -更新已经安装的软件 以非root用户更新系统 sudo: sudo是l ...
- STM32的ADC精度提高方法
1.精度稳定低一点参考电压VREF稳定: 2.通过设置不同的ADC时钟 和 采样周期 来确定出最适合自己系统的参数: 3.测试思路: 在同样SMPx下,设定不同的时钟得到不同采样时间值: 在同样时钟下 ...
- opencv——PCA(主要成分分析)数学原理推导
引言: 最近一直在学习主成分分析(PCA),所以想把最近学的一点知识整理一下,如果有不对的还请大家帮忙指正,共同学习. 首先我们知道当数据维度太大时,我们通常需要进行降维处理,降维处理的方式有很多种, ...
- 某SPI设备驱动引起的开关机压力测试死机问题一例
环境 硬件平台:某ARM SoC 软件平台:Linux 问题现象:产品做开关机压力测试,发生死机. 分析 用crash工具解析两次死机dump信息,得到死机前的log如下.两次死机的backtrace ...
- 重新整理 .net core 实践篇—————配置系统之简单配置中心[十一]
前言 市面上已经有很多配置中心集成工具了,故此不会去实践某个框架. 下面链接是apollo 官网的教程,实在太详细了,本文介绍一下扩展数据源,和简单翻翻阅一下apollo 关键部分. apollo 服 ...
- Docker学习(6) 获取和推送镜像
查找镜像 拉取镜像 推送镜像 总结
- mybatis-plus 分页查询+ dao层抽象
1.配置文件添加paginationInterceptor @Configuration @MapperScan("fama.cost.*.mapper") public clas ...
- 3D点云几何拟合
3D点云几何拟合 Supervised Fitting of Geometric Primitives to 3D Point Clouds 论文地址: http://openaccess.thecv ...