项目中用到了HttpClient, 开发运维过程中也遇到了各种报错,遂整理,以加深认识和方便复习巩固
java.net.ConnectException: Connection refused (Connection refused)
服务端没有启动,客户端尝试去连接请求, 客户端调用代码报错就会出现Connection refused
java.net.SocketTimeoutException: Read timed out
服务端处理太耗时了,然后客户端10 * 1000
。可以模拟一个慢处理的http服务接口,然后httpclient客户端调用的时候,设置一个socketTimeout
;客户端会出现读超时,而服务端还是接着处理的
org.apache.commons.httpclient.NoHttpResponseException
In some circumstances, usually when under heavy load, the web server may be able to receive requests but unable to process them. A lack of sufficient resources like worker threads is a good example. This may cause the server to drop the connection to the client without giving any response. HttpClient throws NoHttpResponseException when it encounters such a condition. In most cases it is safe to retry a method that failed with NoHttpResponseException.
服务端负载高,来不及处理客户端的请求,这种情况会抛出NoHttpResponseException
;建议客户端重试
java.net.SocketTimeoutException: connect timed out
建立连接的超时,客户端可以设置connectTimeout
Connection reset by peer (connect failed)
四月 04, 2020 11:35:47 下午 org.apache.http.impl.execchain.RetryExec execute
信息: I/O exception (java.net.SocketException) caught when processing request to {}->http://127.0.0.1:8088: Connection reset by peer (connect failed)
四月 04, 2020 11:35:49 下午 org.apache.http.impl.execchain.RetryExec execute
信息: Retrying request to {}->http://127.0.0.1:8088
四月 04, 2020 11:35:49 下午 org.apache.http.impl.execchain.RetryExec execute
信息: I/O exception (java.net.SocketException) caught when processing request to {}->http://127.0.0.1:8088: Connection reset
四月 04, 2020 11:35:49 下午 org.apache.http.impl.execchain.RetryExec execute
如果一端的Socket被关闭(或主动关闭,或因为异常退出而 引起的关闭),另一端仍发送数据,发送的第一个数据包引发该异常(Connect reset by peer)
比如:服务器的并发连接数超过了其承载量,服务器会将其中一些连接关闭;客户关掉了浏览器,而服务器还在给客户端发送数据;
java.io.IOException: Broken pipe
信息: I/O exception (java.net.SocketException) caught when processing request to {}->http://127.0.0.1:8088: Connection reset by peer (connect failed)
四月 04, 2020 11:35:49 下午 org.apache.http.impl.execchain.RetryExec execute
信息: Retrying request to {}->http://127.0.0.1:8088
四月 04, 2020 11:35:47 下午 org.apache.http.impl.execchain.RetryExec execute
信息: I/O exception (java.net.SocketException) caught when processing request to {}->http://127.0.0.1:8088: Connection reset
四月 04, 2020 11:35:49 下午 org.apache.http.impl.execchain.RetryExec execute
信息: Retrying request to {}->http://127.0.0.1:8088
四月 04, 2020 11:35:47 下午 org.apache.http.impl.execchain.RetryExec execute
信息: I/O exception (java.net.SocketException) caught when processing request to {}->http://127.0.0.1:8088: Broken pipe (Write failed)
四月 04, 2020 11:35:47 下午 org.apache.http.impl.execchain.RetryExec execute
抛出SocketExcepton:Connect reset by peer后,如果再继续写数据则抛出Broken pipe (Write failed)
。
SpringBoot 服务端的一些配置
有一些配置以及默认值
/**
* Maximum number of worker threads.
*/
private int maxThreads = 0;
/**
* Maximum number of connections that the server accepts and processes at any
* given time. Once the limit has been reached, the operating system may still
* accept connections based on the "acceptCount" property.
*/
private int maxConnections = 0;
/**
* Maximum queue length for incoming connection requests when all possible request
* processing threads are in use.
*/
private int acceptCount = 0;
server.tomcat.max-threads=200
server.tomcat.max-connections=10000
参考
复习: