阿里云HttpClient跨天之后解析不了域名

也许这是一个少见的情况,我使用HttpClient写了一个调用第三方服务的请求,在本机测试和腾讯云上测试都没有问题,但是放到阿里云之后,刚启动的时候是没有问题的,但是每次过零点之后,就会报异常:

java.net.UnknownHostException: www.xxxx.com: System error,异常栈如下:

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进程就好了。

在网上查找,说是jvm dns缓存导致的,在启动参数中添加下面两个参数:

设置解析成功的域名记录JVM中缓存的有效时间,这里修改为1秒钟有效,0表示禁止缓存,-1表示永远有效

-Dsun.net.inetaddr.ttl=1 

设置解析失败的域名记录JVM中缓存的有效时间,这里修改为1秒钟有效,0表示禁止缓存,-1表示永远有效

-Dsun.net.inetaddr.negative.ttl=1

但是我设置了,貌似没有什么效果,有条件的同学可以试试。

最终没办法 ,使用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; } }

原文地址:https://www.cnblogs.com/wgslucky/p/10050455.html