幾年以來我一直在使用Apache HttpClient 4.5.13 ,直到今天我還沒有遇到任何問題。
在 Springboot 應用程式中,當它呼叫特定服務器時,第一次呼叫(或在某些不活動后的第一次呼叫)需要大約 8 秒(!!!)然后所有后續呼叫(到同一臺服務器)都非常快(少于第二)。(與 curl 相同的呼叫總是需要不到一秒鐘)如果我等待一段時間(例如,一個小時)然后重試,第一次呼叫又需要 8 秒,接下來的呼叫非常快。
第一次通話緩慢的原因可能是什么?我使用這個客戶端多年,我從來沒有遇到過這個問題。一點代碼:
RequestConfig requestConfig = RequestConfig.custom()
.setConnectTimeout(Integer.parseInt(env.getProperty("httpclient.connection.timeout")))
.setConnectionRequestTimeout(Integer.parseInt(env.getProperty("httpclient.connection.request.timeout")))
.setSocketTimeout(Integer.parseInt(env.getProperty("httpclient.socket.timeout")))
.setCookieSpec("easy")
.build();
result = HttpClients.custom().setSSLContext(sslContext)
.setSSLHostnameVerifier(getHostnameVerifier())
.setConnectionManager(poolingConnManager)
.setDefaultRequestConfig(requestConfig)
.setDefaultSocketConfig(socketConfig)
.setDefaultCookieSpecRegistry(r)
.addInterceptorFirst(customHttpRequestInterceptor)
.addInterceptorLast(customHttpResponseInterceptor)
.build();
我還啟用了 Http 除錯,但我沒有看到任何奇怪的東西,服務器回應在 8 秒后到達:
[2022-01-22 17:11:40][][][][][pool-6-thread-3][DEBUG]org.apache.http.wire - http-outgoing-3 >> "POST /service/url1 HTTP/1.1[\r][\n]"
[2022-01-22 17:11:40][][][][][pool-6-thread-3][DEBUG]org.apache.http.wire - http-outgoing-3 >> "Authorization: Bearer eyJjdHkiOiJKV1QiLCJyZWFsb[...][\r][\n]"
[2022-01-22 17:11:40][][][][][pool-6-thread-3][DEBUG]org.apache.http.wire - http-outgoing-3 >> "X-Tax-Code: AAAAAAAAAAAAAAA01[\r][\n]"
[2022-01-22 17:11:40][][][][][pool-6-thread-3][DEBUG]org.apache.http.wire - http-outgoing-3 >> "Content-type: application/json[\r][\n]"
[2022-01-22 17:11:40][][][][][pool-6-thread-3][DEBUG]org.apache.http.wire - http-outgoing-3 >> "accept: application/json[\r][\n]"
[2022-01-22 17:11:40][][][][][pool-6-thread-3][DEBUG]org.apache.http.wire - http-outgoing-3 >> "Content-Length: 25[\r][\n]"
[2022-01-22 17:11:40][][][][][pool-6-thread-3][DEBUG]org.apache.http.wire - http-outgoing-3 >> "Host: serverhost[\r][\n]"
[2022-01-22 17:11:40][][][][][pool-6-thread-3][DEBUG]org.apache.http.wire - http-outgoing-3 >> "Connection: Keep-Alive[\r][\n]"
[2022-01-22 17:11:40][][][][][pool-6-thread-3][DEBUG]org.apache.http.wire - http-outgoing-3 >> "User-Agent: Apache-HttpClient/4.5.13 (Java/1.8.0_252)[\r][\n]"
[2022-01-22 17:11:40][][][][][pool-6-thread-3][DEBUG]org.apache.http.wire - http-outgoing-3 >> "Accept-Encoding: gzip,deflate[\r][\n]"
[2022-01-22 17:11:40][][][][][pool-6-thread-3][DEBUG]org.apache.http.wire - http-outgoing-3 >> "[\r][\n]"
[2022-01-22 17:11:40][][][][][pool-6-thread-3][DEBUG]org.apache.http.wire - http-outgoing-3 >> "{"product_name":"XXXXXXX"}"
**Server response arrives after 8 seconds here**
[2022-01-22 17:11:48][][][][][pool-6-thread-3][DEBUG]org.apache.http.wire - http-outgoing-3 << "HTTP/1.1 200 [\r][\n]"
更新:我使用 OkHttpClient 重寫客戶端并且我得到了同樣的慢呼叫,所以我認為它不依賴于客戶端。
uj5u.com熱心網友回復:
幾天后,我終于得到了與 curl 相同的緩慢回應。
所以這是一個服務器端的問題。
轉載請註明出處,本文鏈接:https://www.uj5u.com/ruanti/420783.html
標籤:
下一篇:AMD架構的GPU計算平臺
