您好,登錄后才能下訂單哦!
這篇文章給大家介紹如何解決NoHttpResponse failed to respond問(wèn)題,內(nèi)容非常詳細(xì),感興趣的小伙伴們可以參考借鑒,希望對(duì)大家能有所幫助。
項(xiàng)目運(yùn)行中遇到接口報(bào)錯(cuò)org.apache.http.NoHttpResponseException: xxxx.com:80 failed to respond 報(bào)錯(cuò),報(bào)錯(cuò)無(wú)規(guī)律出現(xiàn),使用用postMan 等工具測(cè)試接口都正常,項(xiàng)目調(diào)試中無(wú)法重現(xiàn)報(bào)錯(cuò),但生產(chǎn)環(huán)境中會(huì)有該錯(cuò)誤產(chǎn)生。
服務(wù)端是springBoot 項(xiàng)目,客戶端是SpringMvc
分析:使用postMan或者手寫(xiě)Test調(diào)用接口無(wú)異常,那么和項(xiàng)目中存在的差異在哪呢?從引入的包開(kāi)始分析,發(fā)現(xiàn)使用httpClient版本均一致,忽然想到,項(xiàng)目中為了性能,是啟用了連接池的,會(huì)不會(huì)是服務(wù)端主動(dòng)關(guān)閉了連接,客戶端不知道,仍然使用這個(gè)鏈接
驗(yàn)證:客戶端建立連接->服務(wù)端手動(dòng)關(guān)閉連接->客戶端調(diào)用接口 賓狗 報(bào)錯(cuò)出來(lái)了
結(jié)論:服務(wù)端和客戶端的keepAliveTimeOut 不一致,導(dǎo)致服務(wù)端先于客戶端關(guān)閉了鏈接,而客戶端仍然使用該連接,導(dǎo)致報(bào)錯(cuò)
解決:
方案1. 服務(wù)端設(shè)置keepAliveTimeOut 時(shí)間與客戶端一致
方案2. 客戶端配置ConnectionKeepAliveStrategy 代碼如下:
ConnectionKeepAliveStrategy strategy = new ConnectionKeepAliveStrategy() {@Override public long getKeepAliveDuration(HttpResponse response, HttpContext context) { Args.notNull(response, "HTTP response"); final HeaderElementIterator it = new BasicHeaderElementIterator( response.headerIterator(HTTP.CONN_KEEP_ALIVE)); while (it.hasNext()) {final HeaderElement he = it.nextElement(); final String param = he.getName(); final String value = he.getValue(); if (value != null && param.equalsIgnoreCase("timeout")) {try {return Long.parseLong(value) * 1000; } catch (final NumberFormatException ignore) { } } }return 1; } };
HttpClients.custom().setConnectionManager(poolingHttpClientConnectionManager) .setConnectionManagerShared(true) .setKeepAliveStrategy(myStrategy) .build()
關(guān)于如何解決NoHttpResponse failed to respond問(wèn)題就分享到這里了,希望以上內(nèi)容可以對(duì)大家有一定的幫助,可以學(xué)到更多知識(shí)。如果覺(jué)得文章不錯(cuò),可以把它分享出去讓更多的人看到。
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。