首页 文章

我使用Spring引导RestTemplate对公共网络IP发出json请求,但它总是显示断开连接并在一分钟后重新连接

提问于
浏览
0

我使用Spring引导RestTemplate对公共网络IP发出json请求,但它总是显示断开连接并在一分钟后重新连接

记录如下:

[] | [20181210 09:11:58.092] | [调试] | [person-certify-7c46cfc9d-t4h7m] | [http-nio-8184-exec-10] | [org.apache.http.wire] | - > http-outgoing-15 <<“end of stream”| [person-certify-7c46cfc9d-t4h7m] [person-certify] - | [] | [20181210 09:11:58.092] | [调试] | [person-certify-7c46cfc9d-t4h7m] | [http-nio-8184-exec-10] | [o.a.h.i.c.DefaultManagedHttpClientConnection] | - > http-outgoing-15:关闭连接| [person-certify-7c46cfc9d-t4h7m] [person-certify] - | [] | [20181210 09:12:58.092] | [调试] | [person-certify-7c46cfc9d-t4h7m] | [http-nio-8184-exec-10] | [o.a.h.i.c.PoolingHttpClientConnectionManager] | - >连接租用:[id:17] [route:{} - > http://118.25.31.127:80] [总保持活动:1;分配路线:1000个中的1个;总分配:1000中的2] |

1 回答

  • 0

    看起来您的目标服务器只接受TLSv1.2连接,您必须确保您的代码仅执行与您的代码似乎没有执行的协议的连接 . 你能检查你的日志(调试模式),看看版本是什么?你正在使用什么样的 spring 版?

    在调试模式下握手期间应该显示日志...

    .....SSLConnectionSocketFactory - Enabled protocols: [TLSv1]
    

    如何配置TLSv2已在此主题中得到解答How to enforce TLS1.2 to Rest client using Rest Template

    简而言之,你可以尝试

    import javax.net.ssl.SSLContext;
    import org.apache.http.impl.client.CloseableHttpClient;
    import org.apache.http.impl.client.HttpClientBuilder;
    import org.springframework.http.client.HttpComponentsClientHttpRequestFactory;
    import org.springframework.web.client.RestTemplate;
    
    SSLContext context = SSLContext.getInstance("TLSv1.2");
    context.init(null, null, null);
    
    CloseableHttpClient httpClient = HttpClientBuilder.create().setSSLContext(context)
        .build();
    HttpComponentsClientHttpRequestFactory factory = new HttpComponentsClientHttpRequestFactory(httpClient);
    RestTemplate restTemplate = new RestTemplate(factory);
    

相关问题