休息模板 org.apache.http.NoHttpResponseException [英] Rest Template org.apache.http.NoHttpResponseException

查看:196
本文介绍了休息模板 org.apache.http.NoHttpResponseException的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有两个 Spring Boot 服务 A 和 B.还有一个外部服务 C.这是请求路径:

I have two Spring Boot Services A and B. Also an external service C. That's the request path:

Web 浏览器 <-> 服务 A <-> 服务 B <-> 外部服务 C

Web Browser <-> service A <-> Service B <-> External Service C

外部服务正在返回一个返回前端的资源.对于 A、B 和 C 之间的通信,我使用了 Rest 模板.进入 Web 应用程序时一切正常,但是一旦我运行并行运行的 BDD 测试(9 个线程),当调用外部服务 C 时,我在服务 B 中收到 NoHttpResponseException.

The external service is returning a resource which goes back to frontend. For communication between A, B and C I'm using Rest Template. Everything goes fine when entering the Web App but as soon as I'm running BDD tests which are running in parallel (9 threads) I'm getting NoHttpResponseException in service B when calling External Service C.

org.apache.http.NoHttpResponseException Service_C failed to respond
at org.apache.http.impl.conn.DefaultHttpResponseParser.parseHead(DefaultHttpResponseParser.java:141)
at org.apache.http.impl.conn.DefaultHttpResponseParser.parseHead(DefaultHttpResponseParser.java:56)
at org.apache.http.impl.io.AbstractMessageParser.parse(AbstractMessageParser.java:259)

这是我的 Rest 模板配置:

Here is my Rest Template configuration:

    @Bean
public RestTemplate restTemplateExternal() throws KeyStoreException, NoSuchAlgorithmException, KeyManagementException {
    HttpComponentsClientHttpRequestFactory requestFactory = getRequestFactoryWithDisabledSSLValidation();
    RestTemplate restTemplate = new RestTemplate(requestFactory);

    return restTemplate;
}

private HttpComponentsClientHttpRequestFactory getRequestFactoryWithDisabledSSLValidation() throws KeyStoreException, NoSuchAlgorithmException, KeyManagementException {
    TrustStrategy acceptingTrustStrategy = (X509Certificate[] chain, String authType) -> true;

    SSLContext sslContext = SSLContexts.custom()
            .loadTrustMaterial(null, acceptingTrustStrategy)
            .build();

    SSLConnectionSocketFactory csf = new SSLConnectionSocketFactory(sslContext);

    PoolingHttpClientConnectionManager connectionManager = new PoolingHttpClientConnectionManager();

    CloseableHttpClient httpClient = HttpClients.custom()
            .setConnectionManager(connectionManager)
            .setSSLSocketFactory(csf)
            .build();

    HttpComponentsClientHttpRequestFactory requestFactory = new HttpComponentsClientHttpRequestFactory();

    requestFactory.setHttpClient(httpClient);

    return requestFactory;
}

我已经尝试调用 connectionManager.setValidateAfterInactivity(0); 但它没有帮助.

I've already tried to call connectionManager.setValidateAfterInactivity(0); but it doesn't help.

让我补充一点,从服务 B 到外部服务 C 的所有请求都指向同一个端点.只有参数 (x) 发生变化:/resource?param={x}

Let me add that all the request from service B to external service C are to the same endpoint. Only parameter (x) is changing:/resource?param={x}

老实说,我不是 100% 确定是否会为每个服务请求创建 HttpClient(RestTemplate bean 是 Singleton),还是每个服务只有一个实例?

To be honest I'm not 100% sure if HttpClient is gonna be created with each request to service (RestTemplate bean is Singleton) or is that only one instance per service?

也许我需要在连接管理器中setDefaultMaxPerRoute"?如果是,那么我如何区分正确的数字是多少?我真的很感激在这种情况下如何正确配置 RestTemplate 的简要说明.

Maybe i need to 'setDefaultMaxPerRoute' in Connection Manager? If yes then how do i distinguish what is the proper number? I'd really appreciate a brief description of how to properly configure RestTemplate in such case.

推荐答案

看起来这里有类似的问题:获取 NoHttpResponseException 进行负载测试

Looks like that's a similar issue here: get NoHttpResponseException for load testing

尝试了 clientBuilder.setRetryHandler(new DefaultHttpRequestRetryHandler(3, true)); 并且成功了.如果有人能更深入地解释这个问题,我仍然会很感激.

Tried with clientBuilder.setRetryHandler(new DefaultHttpRequestRetryHandler(3, true)); and it made the trick. I'd still appreciate if someone could explain the problem more deeply.

这篇关于休息模板 org.apache.http.NoHttpResponseException的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆