十年網(wǎng)站開發(fā)經(jīng)驗 + 多家企業(yè)客戶 + 靠譜的建站團(tuán)隊
量身定制 + 運(yùn)營維護(hù)+專業(yè)推廣+無憂售后,網(wǎng)站問題一站解決
這篇文章給大家介紹如何解決NoHttpResponse failed to respond問題,內(nèi)容非常詳細(xì),感興趣的小伙伴們可以參考借鑒,希望對大家能有所幫助。
環(huán)翠網(wǎng)站制作公司哪家好,找創(chuàng)新互聯(lián)!從網(wǎng)頁設(shè)計、網(wǎng)站建設(shè)、微信開發(fā)、APP開發(fā)、響應(yīng)式網(wǎng)站開發(fā)等網(wǎng)站項目制作,到程序開發(fā),運(yùn)營維護(hù)。創(chuàng)新互聯(lián)自2013年起到現(xiàn)在10年的時間,我們擁有了豐富的建站經(jīng)驗和運(yùn)維經(jīng)驗,來保證我們的工作的順利進(jìn)行。專注于網(wǎng)站建設(shè)就選創(chuàng)新互聯(lián)。
項目運(yùn)行中遇到接口報錯org.apache.http.NoHttpResponseException: xxxx.com:80 failed to respond 報錯,報錯無規(guī)律出現(xiàn),使用用postMan 等工具測試接口都正常,項目調(diào)試中無法重現(xiàn)報錯,但生產(chǎn)環(huán)境中會有該錯誤產(chǎn)生。
服務(wù)端是springBoot 項目,客戶端是SpringMvc
分析:使用postMan或者手寫Test調(diào)用接口無異常,那么和項目中存在的差異在哪呢?從引入的包開始分析,發(fā)現(xiàn)使用httpClient版本均一致,忽然想到,項目中為了性能,是啟用了連接池的,會不會是服務(wù)端主動關(guān)閉了連接,客戶端不知道,仍然使用這個鏈接
驗證:客戶端建立連接->服務(wù)端手動關(guān)閉連接->客戶端調(diào)用接口 賓狗 報錯出來了
結(jié)論:服務(wù)端和客戶端的keepAliveTimeOut 不一致,導(dǎo)致服務(wù)端先于客戶端關(guān)閉了鏈接,而客戶端仍然使用該連接,導(dǎo)致報錯
解決:
方案1. 服務(wù)端設(shè)置keepAliveTimeOut 時間與客戶端一致
方案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問題就分享到這里了,希望以上內(nèi)容可以對大家有一定的幫助,可以學(xué)到更多知識。如果覺得文章不錯,可以把它分享出去讓更多的人看到。