HttpClient的使用和各种报错

项目中用到了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

参考

https://hc.apache.org/httpcomponents-client-4.5.x/httpclient/apidocs/org/apache/http/client/config/RequestConfig.html

复习:

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值