javax.net.ssl.SSLException:根据总数中的少数请求重置连接

t0ybt7op  于 2022-12-02  发布在  Java
关注(0)|答案(2)|浏览(477)

我们有一个服务每秒发送一次POST请求到我们的一个微服务。每次发送请求都是到同一个URL。
发送请求的代码类TrackTask使用org.apache.http.impl.client.ClosebleHttpClient,并使用org.apache.http.client.methods.HttpPost创建POST请求。
在一小时内发送的3600个请求中,很少有请求会出现SSLException,但这是非常随机的。3600个请求中可能有2个出现SSLException,也可能有8个。
我们得到的例外:

javax.net.ssl.SSLException: Connection reset
        at sun.security.ssl.Alert.createSSLException(Alert.java:127) ~[na:1.8.0_312]
        at sun.security.ssl.TransportContext.fatal(TransportContext.java:324) ~[na:1.8.0_312]
        at sun.security.ssl.TransportContext.fatal(TransportContext.java:267) ~[na:1.8.0_312]
        at sun.security.ssl.TransportContext.fatal(TransportContext.java:262) ~[na:1.8.0_312]
        at sun.security.ssl.SSLSocketImpl.handleException(SSLSocketImpl.java:1563) ~[na:1.8.0_312]
        at sun.security.ssl.SSLSocketImpl.access$400(SSLSocketImpl.java:73) ~[na:1.8.0_312]
        at sun.security.ssl.SSLSocketImpl$AppInputStream.read(SSLSocketImpl.java:973) ~[na:1.8.0_312]
        at org.apache.http.impl.io.SessionInputBufferImpl.streamRead(SessionInputBufferImpl.java:137) ~[da-qa-1.0-SNAPSHOT.jar:na]
        at org.apache.http.impl.io.SessionInputBufferImpl.fillBuffer(SessionInputBufferImpl.java:153) ~[da-qa-1.0-SNAPSHOT.jar:na]
        at org.apache.http.impl.io.SessionInputBufferImpl.readLine(SessionInputBufferImpl.java:280) ~[da-qa-1.0-SNAPSHOT.jar:na]
        at org.apache.http.impl.conn.DefaultHttpResponseParser.parseHead(DefaultHttpResponseParser.java:138) ~[da-qa-1.0-SNAPSHOT.jar:na]
        at org.apache.http.impl.conn.DefaultHttpResponseParser.parseHead(DefaultHttpResponseParser.java:56) ~[da-qa-1.0-SNAPSHOT.jar:na]
        at org.apache.http.impl.io.AbstractMessageParser.parse(AbstractMessageParser.java:259) ~[da-qa-1.0-SNAPSHOT.jar:na]
        at org.apache.http.impl.DefaultBHttpClientConnection.receiveResponseHeader(DefaultBHttpClientConnection.java:163) ~[da-qa-1.0-SNAPSHOT.jar:na]
        at org.apache.http.impl.conn.CPoolProxy.receiveResponseHeader(CPoolProxy.java:157) ~[da-qa-1.0-SNAPSHOT.jar:na]
        at org.apache.http.protocol.HttpRequestExecutor.doReceiveResponse(HttpRequestExecutor.java:273) ~[da-qa-1.0-SNAPSHOT.jar:na]
        at org.apache.http.protocol.HttpRequestExecutor.execute(HttpRequestExecutor.java:125) ~[da-qa-1.0-SNAPSHOT.jar:na]
        at org.apache.http.impl.execchain.MainClientExec.execute(MainClientExec.java:272) ~[da-qa-1.0-SNAPSHOT.jar:na]
        at org.apache.http.impl.execchain.ProtocolExec.execute(ProtocolExec.java:186) ~[da-qa-1.0-SNAPSHOT.jar:na]
        at org.apache.http.impl.execchain.RetryExec.execute(RetryExec.java:89) ~[da-qa-1.0-SNAPSHOT.jar:na]
        at org.apache.http.impl.execchain.RedirectExec.execute(RedirectExec.java:110) ~[da-qa-1.0-SNAPSHOT.jar:na]
        at org.apache.http.impl.client.InternalHttpClient.doExecute(InternalHttpClient.java:185) ~[da-qa-1.0-SNAPSHOT.jar:na]
        at org.apache.http.impl.client.CloseableHttpClient.execute(CloseableHttpClient.java:83) ~[da-qa-1.0-SNAPSHOT.jar:na]
        at org.apache.http.impl.client.CloseableHttpClient.execute(CloseableHttpClient.java:108) ~[da-qa-1.0-SNAPSHOT.jar:na]
        at com.cooladata.da.TrackTask.sendTrack(TrackTask.java:62) [da-qa-1.0-SNAPSHOT.jar:na]
        at com.cooladata.da.TrackTask.startListening(TrackTask.java:108) [da-qa-1.0-SNAPSHOT.jar:na]
        at com.cooladata.da.TrackTask.run(TrackTask.java:120) [da-qa-1.0-SNAPSHOT.jar:na]
        at java.lang.Thread.run(Thread.java:748) [na:1.8.0_312]
        Suppressed: java.net.SocketException: Broken pipe (Write failed)
                at java.net.SocketOutputStream.socketWrite0(Native Method) ~[na:1.8.0_312]
                at java.net.SocketOutputStream.socketWrite(SocketOutputStream.java:111) ~[na:1.8.0_312]
                at java.net.SocketOutputStream.write(SocketOutputStream.java:155) ~[na:1.8.0_312]
                at sun.security.ssl.SSLSocketOutputRecord.encodeAlert(SSLSocketOutputRecord.java:81) ~[na:1.8.0_312]
                at sun.security.ssl.TransportContext.fatal(TransportContext.java:355) ~[na:1.8.0_312]
                ... 26 common frames omitted
Caused by: java.net.SocketException: Connection reset
        at java.net.SocketInputStream.read(SocketInputStream.java:210) ~[na:1.8.0_312]
        at java.net.SocketInputStream.read(SocketInputStream.java:141) ~[na:1.8.0_312]
        at sun.security.ssl.SSLSocketInputRecord.read(SSLSocketInputRecord.java:464) ~[na:1.8.0_312]
        at sun.security.ssl.SSLSocketInputRecord.bytesInCompletePacket(SSLSocketInputRecord.java:68) ~[na:1.8.0_312]
        at sun.security.ssl.SSLSocketImpl.readApplicationRecord(SSLSocketImpl.java:1341) ~[na:1.8.0_312]
        at sun.security.ssl.SSLSocketImpl.access$300(SSLSocketImpl.java:73) ~[na:1.8.0_312]
        at sun.security.ssl.SSLSocketImpl$AppInputStream.read(SSLSocketImpl.java:957) ~[na:1.8.0_312]
        ... 21 common frames omitted

我们过去没有或几乎没有这样的例外。
有谁能帮助我们理解这种随机异常的原因以及我们如何解决它?

u91tlkcl

u91tlkcl1#

由于Spring Boot random "SSLException: Connection reset" in Kubernetes with JDK11的帖子而修复
一旦我将httpclient配置为NO REUSE连接,它就可以工作了,我们不再得到SSLExceptions:

httpClient = HttpClients.custom().setDefaultRequestConfig(config)
        .setConnectionReuseStrategy(NoConnectionReuseStrategy.INSTANCE)
        .setConnectionManager(poolingConnManager)
        .build();
ovfsdjhp

ovfsdjhp2#

我在使用java.net.URLConnection的getInputStream()方法时看到了此错误。如果您的URL有空格,java可能会抛出此错误

相关问题