SpringCloud:Feign调⽤接⼝不稳定问题以及如何设置超时
1. Feign调⽤接⼝不稳定报错
Caused by: java.SocketException: Software caused connection abort: recv failed
at java.SocketInputStream.socketRead0(Native Method)
at java.SocketInputStream.socketRead(SocketInputStream.java:116)
at ad(SocketInputStream.java:170)
at ad(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.adLine(SessionInputBufferImpl.java:282)
at org.apache.DefaultHttpResponseParser.parseHead(DefaultHttpResponseParser.java:140)
at org.apache.DefaultHttpResponseParser.parseHead(DefaultHttpResponseParser.java:57)
at org.apache.http.impl.io.AbstractMessageParser.parse(AbstractMessageParser.java:259)
at org.apache.http.iveResponseHeader(DefaultBHttpClientConnection.java:163)
at org.apache.iveResponseHeader(CPoolProxy.java:167)
at org.apache.http.protocol.HttpRequestExecutor.doReceiveResponse(HttpRequestExecutor.java:273)
at org.apache.http.ute(HttpRequestExecutor.java:125)
at org.apache.ute(MainClientExec.java:271)
at org.apache.ute(ProtocolExec.java:184)
at org.apache.ute(RetryExec.java:88)
at org.apache.ute(RedirectExec.java:110)
at org.apache.http.impl.client.InternalHttpClient.doExecute(InternalHttpClient.java:184)
at org.apache.http.impl.ute(CloseableHttpClient.java:82)
at org.apache.http.impl.ute(CloseableHttpClient.java:107)
at org.apache.http.impl.ute(CloseableHttpClient.java:55)
at feign.ute(ApacheHttpClient.java:87)
at org.springframework.cloudflix.feign.ribbon.RetryableFeignLoadBalancer$1.doWithRetry(RetryableFeignLoadBalancer.java:92) at org.springframework.cloudflix.feign.ribbon.RetryableFeignLoadBalancer$1.doWithRetry(RetryableFeignLoadBalancer.java:77) at support.RetryTemplate.doExecute(RetryTemplate.java:286)
at ute(RetryTemplate.java:163)
at org.springframework.cloudflix.feign.ute(RetryableFeignLoadBalancer.java:77)
at org.springframework.cloudflix.feign.ute(RetryableFeignLoadBalancer.java:48)
at comflix.client.AbstractLoadBalancerAwareClient$1.call(AbstractLoadBalancerAwareClient.java:109)
at active.LoadBalancerCommand$3$1.call(LoadBalancerCommand.java:303)
at active.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.Next(OnSubscribeConcatMap.java:144)
at active.LoadBalancerCommand$1.call(LoadBalancerCommand.java:185)
at active.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.queue(TrampolineScheduler.java:73)
at rx.internal.schedulers.TrampolineScheduler$InnerCurrentThreadScheduler.schedule(TrampolineScheduler.java:52)
at rx.internal.operators.Next(OperatorRetryWithPredicate.java:79)
at rx.internal.operators.Next(OperatorRetryWithPredicate.java:45)
at rx.internal.util.quest(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)
error parse newat 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 comflix.uteWithLoadBalancer(AbstractLoadBalancerAwareClient.java:117)
at org.springframework.cloudflix.feign.ute(LoadBalancerFeignClient.java:63)
at uteAndDecode(SynchronousMethodHandler.java:97)
... 117 common frames omitted
feign在调⽤时,会有不稳定的情况出现,时⽽出现接⼝调不通。解决⽅案如下,复写FeignRibbonClientAutoConfiguration中的HttpClient的配置。
解决办法:
创建FeignRibbonHttpClientPoolConfig配置类。
import org.fig.Registry;
import org.fig.RegistryBuilder;
import org.socket.ConnectionSocketFactory;
import org.socket.PlainConnectionSocketFactory;
import org.ssl.NoopHostnameVerifier;
import org.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.PoolingHttpClientConnectionManager;
import org.apache.http.ssl.SSLContexts;
import t.annotation.Bean;
import org.springframework.stereotype.Component;
import javax.ssl.SSLContext;
import javax.ssl.TrustManager;
import javax.ssl.X509TrustManager;
import java.security.KeyManagementException;
import CertificateException;
import 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 = ateDefault();
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⽂件形式为例):
tTimeout=60000.
adTimeout=60000.
#禁⽤Hystrix熔断检测(熔断检测默认是1秒)
abled= false
#spring.ApplicationName是服务提供⽅注册到eureka上的应⽤名称.
配置完成后,还需要配置Hystrix的熔断时间,如果Hystrix时长⼩于feign的超时时长,那feign的超时设置就不会⽣效#配置hystrix的熔断时间
ution.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
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论