解决feign调用接口不稳定的问题

更新时间:2023-05-16 18:34:56 阅读: 评论:0

解决feign调⽤接⼝不稳定的问题
我就废话不多说了,⼤家还是直接看代码吧~
Caud by: SocketException: Software caud connection abort: recv failed
SocketInputStream.socketRead0(Native Method)
SocketInputStream.socketRead(SocketInputStream.java:116)
ad(SocketInputStream.java:170)
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.DefaultHttpResponParr.parHead(DefaultHttpResponParr.java:140)
at org.apache.DefaultHttpResponParr.parHead(DefaultHttpResponParr.java:57)
at org.apache.http.impl.io.AbstractMessageParr.par(AbstractMessageParr.java:259)
at org.apache.http.iveResponHeader(DefaultBHttpClientConnection.java:163)
at org.apache.iveResponHeader(CPoolProxy.java:167)
at org.apache.http.protocol.HttpRequestExecutor.doReceiveRespon(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(CloableHttpClient.java:82)
at org.apache.http.impl.ute(CloableHttpClient.java:107)
at org.apache.http.impl.ute(CloableHttpClient.java:55)
at feign.ute(ApacheHttpClient.java:87)
at org.flix.feign.ribbon.RetryableFeignLoadBalancer$1.doWithRetry(RetryableFeignLoadBalancer.java:92)
at org.flix.feign.ribbon.RetryableFeignLoadBalancer$1.doWithRetry(RetryableFeignLoadBalancer.java:77)
at support.RetryTemplate.doExecute(RetryTemplate.java:286)
at ute(RetryTemplate.java:163)
at org.flix.feign.ute(RetryableFeignLoadBalancer.java:77)
at org.flix.feign.ute(RetryableFeignLoadBalancer.java:48)
脚为什么臭
flix.client.AbstractLoadBalancerAwareClient$1.call(AbstractLoadBalancerAwareClient.java:109)
active.LoadBalancerCommand$3$1.call(LoadBalancerCommand.java:303)
active.LoadBalancerCommand$3$1.call(LoadBalancerCommand.java:287)
at rx.internal.util.ScalarSynchronousObrvable$3.call(ScalarSynchronousObrvable.java:231)
产假多久at rx.internal.util.ScalarSynchronousObrvable$3.call(ScalarSynchronousObrvable.java:228)
at rx.Obrvable.unsafeSubscribe(Obrvable.java:10211)
at rx.internal.operators.OnSubscribeConcatMap$ConcatMapSubscriber.drain(OnSubscribeConcatMap.java:286)
at rx.internal.operators.Next(OnSubscribeConcatMap.java:144)
active.LoadBalancerCommand$1.call(LoadBalancerCommand.java:185)
active.LoadBalancerCommand$1.call(LoadBalancerCommand.java:180)
at rx.Obrvable.unsafeSubscribe(Obrvable.java:10211)
at rx.internal.operators.OnSubscribeConcatMap.call(OnSubscribeConcatMap.java:94)
at rx.internal.operators.OnSubscribeConcatMap.call(OnSubscribeConcatMap.java:42)
at rx.Obrvable.unsafeSubscribe(Obrvable.java:10211)
dnf打孔
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(ScalarSynchronousObrvable.java:276)
at rx.Subscriber.tProducer(Subscriber.java:209)
at rx.internal.util.ScalarSynchronousObrvable$JustOnSubscribe.call(ScalarSynchronousObrvable.java:138)
at rx.internal.util.ScalarSynchronousObrvable$JustOnSubscribe.call(ScalarSynchronousObrvable.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.Obrvable.subscribe(Obrvable.java:10307)
at rx.Obrvable.subscribe(Obrvable.java:10274)
at rx.obrvables.BlockingObrvable.blockForSingle(BlockingObrvable.java:445)
at rx.obrvables.BlockingObrvable.single(BlockingObrvable.java:342)
爱国的例子flix.uteWithLoadBalancer(AbstractLoadBalancerAwareClient.java:117)
at org.flix.feign.ute(LoadBalancerFeignClient.java:63)
at uteAndDecode(SynchronousMethodHandler.java:97)
... 117 common frames omitted
feign在调⽤时,会有不稳定的情况出现,时⽽出现接⼝调不通。解决⽅案如下,复写FeignRibbonClientAutoConfiguration中的HttpClient的配置。代码如下:
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.NoConnectionReuStrategy;
import org.apache.http.impl.client.CloableHttpClient;
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;
ssl.SSLContext;
ssl.TrustManager;
ssl.X509TrustManager;
import java.curity.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 = "clo")
CloableHttpClient httpClient() throws KeyManagementException {
return buildCloableHttpClient();
}
/**
* 构建HttpClient连接池
*
* @return
* @throws KeyManagementException
*/
眼线public CloableHttpClient buildCloableHttpClient() 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.tMaxTotal(POOL_MAX_TOTAL);
connectionManager.tDefaultMaxPerRoute(DEFAULT_MAX_PER_ROUTE);
connectionManager.tValidateAfterInactivity(VALIDATE_AFTER_INACTIVITY);
消防画大全
return HttpClients.custom()
.tConnectionManager(connectionManager)
.tConnectionReuStrategy(NoConnectionReuStrategy.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[]{};
}
}
}
补充知识:springcloud之FeignClient访问微服务接⼝缓慢
昨天开发FeignClient,想调⽤微服务。逻辑是A服务调⽤B服务。AB在同⼀个局域⽹内。
经过反复测试,有⼀个访问缓慢的现象,具体表现为:
程序启动第⼀次访问初始化1.2秒左右,还可以理解。
但后⾯访问还是要1.1秒左右(格式化到SSS毫秒打印⽇志监控的)。
但如果连续访问⼏次,后⾯⼏次⼜是⼏⼗毫秒。过⼀会再访问,或者换浏览器换post⼯具请求,⼜会1.2秒左右。
当时就有点懵逼,这么成熟的⼯具不可能会这么慢吧,都是⼀个局域⽹。
排查了eureka注册中⼼,发现B服务多注册了⼀个,IP地址是192开头,经过询问,是⼀个同事的笔记本连接wifi,wifi⾃动分配了192开头的ip,笔记本是可以访问注册中⼼和其他服务的。但其他服务是访问不了这台笔记本的。
也就是说,B服务的⼀个注册的⽹络和其他服务是不通的。
但不知道为什么eureka却认为192ip注册是⼀个正确的微服务,⽽且⼀直是UP状态。注册中⼼的ip肯定是访问不了笔记本192ip的。
⼿⼯访问了⼀下192ip,不会直接提⽰404或⽹络错误,⽽是会加载⼀会。
这也许导致了FeignClient认为192ip是⼀台可⽤的机器。所以第⼀次请求的时候就去请求192ip,但没返回,到了超时时间,再换B服务的其他地址,就导致了耗时。
让同事把服务停了,再次调⽤服务,速度就很快了。
总结:个别机器IP不通,会导致FeignClient调⽤微服务缓慢。⽽且在eureka中⼼中是UP状态,没有错误提⽰。
需要注意⽹络互通情况。
以上这篇解决feign调⽤接⼝不稳定的问题就是⼩编分享给⼤家的全部内容了,希望能给⼤家⼀个参考,也希望⼤家多多⽀持。

本文发布于:2023-05-16 18:34:56,感谢您对本站的认可!

本文链接:https://www.wtabcd.cn/fanwen/fan/82/657403.html

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

标签:服务   访问   注册
相关文章
留言与评论(共有 0 条评论)
   
验证码:
推荐文章
排行榜
Copyright ©2019-2022 Comsenz Inc.Powered by © 专利检索| 网站地图