This is an EXCEPTION caused by HTTP and is an accidental exception.
Abnormal log
org.apache.http.NoHttpResponseException: The target server failed to respond
Copy the code
The solution
Server solution
The client can detect that the service has expired and shut it down.
- Keep-alive: timeout=xx (HttpHeader); keep-alive: timeout=xx (HttpHeader);
- The second parameter in the Nginx configuration is this, so as long as the server keeps alive_timeout 30s; Keepalive_timeout 30s 29s; Ok, less 1s here to prevent concurrency problems caused by network delay.
Client solution
If the server is not convenient to modify, then the second solution is to modify the client side, more troublesome.
- First, make sure you use the latest ones
httpClient
. - Secondly, there are three ways:
- for
jest
This type has timingcloseIdleConnection
, the scheduling time can be adjusted askeep alive
Less than half the time to ensure recycling before timeout - According to the
keep Alive
Time, adjustmentValidateAfterInactivity less than
KeepAlive Time ‘, but this method still does not avoid simultaneous closing. - Compensation scheme, use
Retry
Retry sending.HttpClient
Auto retry is enabled by default, do not calldisableAutomaticRetries
Can be
- for
Recommended scheme
- The client uses a short connection, such as a
UrlConnection
; - will
HttpClient
The connection keepalive time is set to less than 15 seconds or lessnginx
的keepalive_timeout
Tune more than 30s; - Don’t have to
PoolingHttpClientConnectionManager
; - Retry after error,
HttpClient
的RetryHandler
; - JDK11 has been brought
HttpClinet
Isn’t that attractive?