也許這是一個少見的狀況,我使用HttpClient寫了一個調用第三方服務的請求,在本機測試和騰訊雲上測試都沒有問題,可是放到阿里雲以後,剛啓動的時候是沒有問題的,可是每次過零點以後,就會報異常:java
java.net.UnknownHostException: www.xxxx.com: System error,異常棧以下:apache
java.net.UnknownHostException: wwww.xxx.com: System error at java.base/java.net.Inet4AddressImpl.lookupAllHostAddr(Native Method) at java.base/java.net.InetAddress$PlatformNameService.lookupAllHostAddr(InetAddress.java:925) at java.base/java.net.InetAddress.getAddressesFromNameService(InetAddress.java:1505) at java.base/java.net.InetAddress$NameServiceAddresses.get(InetAddress.java:844) at java.base/java.net.InetAddress.getAllByName0(InetAddress.java:1495) at java.base/java.net.InetAddress.getAllByName(InetAddress.java:1354) at java.base/java.net.InetAddress.getAllByName(InetAddress.java:1288) at org.apache.http.impl.conn.SystemDefaultDnsResolver.resolve(SystemDefaultDnsResolver.java:45) at org.apache.http.impl.conn.DefaultHttpClientConnectionOperator.connect(DefaultHttpClientConnectionOperator.java:111) at org.apache.http.impl.conn.PoolingHttpClientConnectionManager.connect(PoolingHttpClientConnectionManager.java:353) at org.apache.http.impl.execchain.MainClientExec.establishRoute(MainClientExec.java:380) at org.apache.http.impl.execchain.MainClientExec.execute(MainClientExec.java:236) at org.apache.http.impl.execchain.ProtocolExec.execute(ProtocolExec.java:184) at org.apache.http.impl.execchain.RetryExec.execute(RetryExec.java:88) at org.apache.http.impl.execchain.RedirectExec.execute(RedirectExec.java:110) at org.apache.http.impl.client.InternalHttpClient.doExecute(InternalHttpClient.java:184) at org.apache.http.impl.client.CloseableHttpClient.execute(CloseableHttpClient.java:82) at org.apache.http.impl.client.CloseableHttpClient.execute(CloseableHttpClient.java:107)
只要重啓一下java進程就行了。json
在網上查找,說是jvm dns緩存致使的,在啓動參數中添加下面兩個參數:緩存
設置解析成功的域名記錄JVM中緩存的有效時間,這裏修改成1秒鐘有效,0表示禁止緩存,-1表示永遠有效服務器
-Dsun.net.inetaddr.ttl=1 app
設置解析失敗的域名記錄JVM中緩存的有效時間,這裏修改成1秒鐘有效,0表示禁止緩存,-1表示永遠有效jvm
-Dsun.net.inetaddr.negative.ttl=1post
可是我設置了,貌似沒有什麼效果,有條件的同窗能夠試試。測試
最終沒辦法 ,使用Java jdk 自帶的http服務從新寫了一個請求,觀察一段時間再也不報那個異常,具體緣由仍是清楚,若是有知道的,謝謝告知。阿里雲
package global.util; import java.io.BufferedReader; import java.io.DataOutputStream; import java.io.IOException; import java.io.InputStreamReader; import java.net.URL; import javax.net.ssl.HttpsURLConnection; import org.apache.http.Header; import global.common.log.DKLog; public class HttpUtil { //發送https post請求 public static String sendHttpsPostRequest(String data, String requestUri, Header... headers) { DataOutputStream wr = null; BufferedReader in = null;
HttpsUrlConnection con = null; try { URL obj = new URL(requestUri); //https請求 con = (HttpsURLConnection) obj.openConnection(); if(headers != null) { for(Header header : headers) { con.setRequestProperty(header.getName(), header.getValue()); } } con.setRequestProperty("Content-Type", " application/json"); con.setConnectTimeout(15000); // 添加請求頭 con.setRequestMethod("POST"); // 發送Post請求 con.setDoOutput(true); wr = new DataOutputStream(con.getOutputStream()); wr.writeBytes(data); wr.flush(); int responseCode = con.getResponseCode(); if (responseCode == 200) { in = new BufferedReader(new InputStreamReader(con.getInputStream())); String inputLine; StringBuffer response = new StringBuffer(); while ((inputLine = in.readLine()) != null) { response.append(inputLine); } return response.toString(); } else { DKLog.system.error("請求服務器異常,返回異常碼:{}", responseCode); } } catch (Exception e) { DKLog.system.error("Http請求失敗,{},data:{}", requestUri, data, e); } finally { if (wr != null) { try { wr.close(); } catch (IOException e) { e.printStackTrace(); } } if (in != null) { try { in.close(); } catch (IOException e) { e.printStackTrace(); } }
if(con != null){
con.disconnect();
} } return null; } }