我目前正在编写一个API,它需要从服务器A到自身建立HTTPS连接。使用Jmeter进行测试,我发现即使同时启动10个线程,线程也很容易挂起。但是,如果连接到不同的服务器,则问题不存在。我只用了几行代码来测试它。它提出了同样的问题。
try {
URL url = new URL("some URL that connects to the server itself");
HttpURLConnection con = (HttpURLConnection) url.openConnection();
con.setConnectTimeout(20000);
con.setReadTimeout(30000);
con.setRequestMethod("GET");
con.setDoInput(true);
BufferedReader reader = new BufferedReader(new InputStreamReader(con.getInputStream()));
String line = reader.readLine();
int count = 0;
while (line != null) {
count += line.length();
line = reader.readLine();
}
//con.disconnect();
}catch(Exception e){
e.printStackTrace();
}
在10个线程中,只有2个线程按预期返回结果,所有其他线程似乎都超时了堆栈跟踪
[err] java.net.SocketTimeoutException: Read timed out
[err] at java.net.SocketInputStream.socketRead0(Native Method)
[err] at java.net.SocketInputStream.read(SocketInputStream.java:150)
[err] at java.net.SocketInputStream.read(SocketInputStream.java:121)
[err] at sun.security.ssl.InputRecord.readFully(InputRecord.java:465)
[err] at sun.security.ssl.InputRecord.read(InputRecord.java:503)
[err] at sun.security.ssl.SSLSocketImpl.readRecord(SSLSocketImpl.java:954)
[err] at sun.security.ssl.SSLSocketImpl.performInitialHandshake(SSLSocketImpl.java:1343)
[err] at sun.security.ssl.SSLSocketImpl.startHandshake(SSLSocketImpl.java:1371)
[err] at sun.security.ssl.SSLSocketImpl.startHandshake(SSLSocketImpl.java:1355)
[err] at sun.net.www.protocol.https.HttpsClient.afterConnect(HttpsClient.java:563)
[err] at sun.net.www.protocol.https.AbstractDelegateHttpsURLConnection.connect(AbstractDelegateHttpsURLConnection.java:185)
[err] at sun.net.www.protocol.http.HttpURLConnection.getInputStream0(HttpURLConnection.java:1511)
[err] at sun.net.www.protocol.http.HttpURLConnection.getInputStream(HttpURLConnection.java:1439)
[err] at com.sun.net.ssl.internal.www.protocol.https.HttpsURLConnectionOldImpl.getInputStream(HttpsURLConnectionOldImpl.java:224)
我使用-Djavax.net.debug = all启用调试选项,这里有一些显示在控制台上的痕迹
Default Executor-thread-15, called closeInbound()
Default Executor-thread-15, fatal error: 80: Inbound closed before receiving peer's close_notify: possible truncation attack?
javax.net.ssl.SSLException: Inbound closed before receiving peer's close_notify: possible truncation attack?
%% Invalidated: [Session-19, TLS_RSA_WITH_AES_128_CBC_SHA256]
Default Executor-thread-15
, SEND TLSv1.2 ALERT:
fatal,
description = internal_error
Default Executor-thread-15, Exception sending alert: java.io.IOException: writer side was already closed.
我的问题是为什么在与服务器本身建立连接时会出现问题?为什么每次只有两个连接可以恢复?我该怎么解决?
我尝试使用http.maxConnections
设置系统属性
System.setProperty("http.maxConnections", String.valueOf(300));
但它没有帮助。