我的基于 Spring Boot 的服务在 Apache Http Client 时偶尔会遇到 javax.net.ssl.SSLProtocolException: Connection timed out (Read failed) > 在不受我控制的其他服务器上调用资源。每次异常过程都是这样:

  • 应用程序向服务器发出获取/发布请求,如 2019-12-19 09:58:16.253
  • 等了将近 15 分钟
  • 请求线程因 javax.net.ssl.SSLProtocolException 连接超时(读取失败) 在 2019-12-19 10:14:31.309 爆发

堆栈跟踪:

javax.net.ssl.SSLProtocolException: Connection timed out (Read failed) 
    at java.base/sun.security.ssl.Alert.createSSLException(Alert.java:126) 
    at java.base/sun.security.ssl.TransportContext.fatal(TransportContext.java:321) 
    at java.base/sun.security.ssl.TransportContext.fatal(TransportContext.java:264) 
    at java.base/sun.security.ssl.TransportContext.fatal(TransportContext.java:259) 
    at java.base/sun.security.ssl.SSLSocketImpl.handleException(SSLSocketImpl.java:1314) 
    at java.base/sun.security.ssl.SSLSocketImpl$AppInputStream.read(SSLSocketImpl.java:839) 
    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:138) 
    at org.apache.http.impl.conn.DefaultHttpResponseParser.parseHead(DefaultHttpResponseParser.java:56) 
    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:165) 
    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:272) 
    at org.apache.http.impl.execchain.ProtocolExec.execute(ProtocolExec.java:185) 
    at org.apache.http.impl.execchain.RedirectExec.execute(RedirectExec.java:110) 
    at org.apache.http.impl.client.InternalHttpClient.doExecute(InternalHttpClient.java:185) 
    at org.apache.http.impl.client.CloseableHttpClient.execute(CloseableHttpClient.java:83) 
    at org.apache.http.impl.client.CloseableHttpClient.execute(CloseableHttpClient.java:108) 
    at com.xx 
    at com.xx 
    at com.xx 
    at jdk.internal.reflect.GeneratedMethodAccessor255.invoke(Unknown Source) 
    at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) 
    at java.base/java.lang.reflect.Method.invoke(Method.java:566) 
    at org.springframework.web.method.support.InvocableHandlerMethod.doInvoke(InvocableHandlerMethod.java:189) 
    at org.springframework.web.method.support.InvocableHandlerMethod.invokeForRequest(InvocableHandlerMethod.java:138) 
    at org.springframework.web.servlet.mvc.method.annotation.ServletInvocableHandlerMethod.invokeAndHandle(ServletInvocableHandlerMethod.java:102) 
    at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.invokeHandlerMethod(RequestMappingHandlerAdapter.java:895) 
    at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.handleInternal(RequestMappingHandlerAdapter.java:800) 
    at org.springframework.web.servlet.mvc.method.AbstractHandlerMethodAdapter.handle(AbstractHandlerMethodAdapter.java:87) 
    at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:1038) 
    at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:942) 
    at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:1005) 
    at org.springframework.web.servlet.FrameworkServlet.doPost(FrameworkServlet.java:908) 
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:660) 
    at org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:882) 
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:741) 
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:231) 
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166) 
    at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:53) 
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193) 
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166) 
    at com.tehang.resource.train.infrastructure.fitlers.ResponseContainerFilter.doFilter(ResponseContainerFilter.java:74) 
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193) 
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166) 
    at org.springframework.boot.actuate.web.trace.servlet.HttpTraceFilter.doFilterInternal(HttpTraceFilter.java:90) 
    at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107) 
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193) 
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166) 
    at org.springframework.web.filter.RequestContextFilter.doFilterInternal(RequestContextFilter.java:99) 
    at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107) 
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193) 
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166) 
    at org.springframework.web.filter.FormContentFilter.doFilterInternal(FormContentFilter.java:92) 
    at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107) 
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193) 
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166) 
    at org.springframework.web.filter.HiddenHttpMethodFilter.doFilterInternal(HiddenHttpMethodFilter.java:93) 
    at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107) 
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193) 
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166) 
    at org.springframework.cloud.sleuth.instrument.web.ExceptionLoggingFilter.doFilter(ExceptionLoggingFilter.java:50) 
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193) 
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166) 
    at brave.servlet.TracingFilter.doFilter(TracingFilter.java:86) 
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193) 
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166) 
    at org.springframework.boot.actuate.metrics.web.servlet.WebMvcMetricsFilter.filterAndRecordMetrics(WebMvcMetricsFilter.java:117) 
    at org.springframework.boot.actuate.metrics.web.servlet.WebMvcMetricsFilter.doFilterInternal(WebMvcMetricsFilter.java:106) 
    at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107) 
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193) 
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166) 
    at org.springframework.web.filter.CharacterEncodingFilter.doFilterInternal(CharacterEncodingFilter.java:200) 
    at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107) 
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193) 
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166) 
    at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:199) 
    at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:96) 
    at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:490) 
    at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:139) 
    at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:92) 
    at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:74) 
    at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:343) 
    at org.apache.coyote.http11.Http11Processor.service(Http11Processor.java:408) 
    at org.apache.coyote.AbstractProcessorLight.process(AbstractProcessorLight.java:66) 
    at org.apache.coyote.AbstractProtocol$ConnectionHandler.process(AbstractProtocol.java:834) 
    at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1417) 
    at org.apache.tomcat.util.net.SocketProcessorBase.run(SocketProcessorBase.java:49) 
    at java.base/java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1128) 
    at java.base/java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:628) 
    at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61) 
    at java.base/java.lang.Thread.run(Thread.java:834) 
Caused by: java.net.SocketException: Connection timed out (Read failed) 
    at java.base/java.net.SocketInputStream.socketRead0(Native Method) 
    at java.base/java.net.SocketInputStream.socketRead(SocketInputStream.java:115) 
    at java.base/java.net.SocketInputStream.read(SocketInputStream.java:168) 
    at java.base/java.net.SocketInputStream.read(SocketInputStream.java:140) 
    at java.base/sun.security.ssl.SSLSocketInputRecord.read(SSLSocketInputRecord.java:448) 
    at java.base/sun.security.ssl.SSLSocketInputRecord.bytesInCompletePacket(SSLSocketInputRecord.java:68) 
    at java.base/sun.security.ssl.SSLSocketImpl.readApplicationRecord(SSLSocketImpl.java:1104) 
    at java.base/sun.security.ssl.SSLSocketImpl$AppInputStream.read(SSLSocketImpl.java:823) 
    ... 90 common frames omitted 

http 工具

  compile('org.apache.httpcomponents:httpclient:4.5.6') 

异常是如何发生的?是spring boot应用的问题还是服务器端的问题?

请您参考如下方法:

一般来说,这个异常Caused by: java.net.SocketException: Connection timed out是因为你的应用程序正在等待从服务器获取响应,并且无法在指定时间内获取

您可以更改/设置连接超时

Spring Boot 1.4 及更高版本

从 Spring Boot 1.4 开始,您可以使用属性 server.connection-timeout。请参阅 Spring Boot 的 common application properties .

Spring Boot 1.3 及更早版本

提供自定义的 EmbeddedServletContainerFactory bean:

@Bean 
public EmbeddedServletContainerFactory servletContainerFactory() { 
    TomcatEmbeddedServletContainerFactory factory = new TomcatEmbeddedServletContainerFactory(); 
 
    factory.addConnectorCustomizers(connector ->  
            ((AbstractProtocol) connector.getProtocolHandler()).setConnectionTimeout(10000)); 
 
    // configure some more properties 
 
    return factory; 
} 


评论关闭
IT干货网

微信公众号号:IT虾米 (左侧二维码扫一扫)欢迎添加!