阿里云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;
} }

阿里云HttpClient跨天之后解析不了域名的更多相关文章
- 解决阿里云OSS跨域问题
解决阿里云OSS跨域问题 现象 本人项目中对阿里云图片请求进行了两次,第一次通过img标签进行,第二次通过异步加载获取.第一次请求到图片,浏览器会进行缓存,随后再进行异步请求,保存跨域失效. 错误信息 ...
- 一个阿里云apache服务器配置两个或多个域名forLinux
一个阿里云apache服务器配置两个或多个域名for Linux: 默认已经配置好了阿里云提供的一键web安装,可以参考:http://www.42iot.com/?id=8 修改/alidata/s ...
- 《阿里云天池大赛赛题解析》——O2O优惠卷预测
赛事链接:https://tianchi.aliyun.com/competition/entrance/231593/introduction?spm=5176.12281925.0.0.7e157 ...
- 阿里云首次安装和部署nginx
1.执行yum命令安装依赖 yum -y install pcre* yum -y install openssl* 2.下载nginx //如果没有安装wget,下载已编译版本 yum instal ...
- 干货满满!解密阿里云RPA (机器人流程自动化)的产品架构和商业化发展
阿里云RPA,作为阿里云自研8年的技术,在资本的热捧下,逐渐从幕后来到台前,成为企业服务市场的黑马.本文将从产品上全面剖析,阿里云RPA这款产品的现阶段情况,同时简单谈谈阿里云RPA的商业化进展. 阿 ...
- 阿里云+wordpress搭建个人博客网站【小白专用的图文教程】
[声明] 欢迎转载,但请保留文章原始出处→_→ 生命壹号:http://www.cnblogs.com/smyhvae/ 文章来源:http://www.cnblogs.com/smyhvae/p/4 ...
- 阿里云+wordpress搭建个人博客网站
[正文] 在阿里云上搭建使用个人博客主要分为以下几个步骤: 1.购买阿里云ECS主机 2.购买域名 3.申请备案 4.环境配置 5.安装wordpress 6.域名解析 声明一下,本人对服务器端的知识 ...
- 阿里云+wordpress
阿里云+wordpress搭建个人博客网站[小白专用的图文教程] [声明] 欢迎转载,但请保留文章原始出处→_→ 生命壹号:http://www.cnblogs.com/smyhvae/ 文章来源 ...
- 开通阿里云 CDN
CDN,内容分发网络,主要功能是在不同的地点缓存内容,通过负载均衡技术,将用户的请求定向到最合适的缓存服务器上去获取内容,从而加快文件加载速度. 阿里云提供了按量计费的CDN,开启十分方便,于是我在自 ...
随机推荐
- zepto.js移动端城市选择插件
http://sc.chinaz.com/jiaoben/170327301850.htm
- Spring 使用 feign时设置header信息
最近使用 SpringBoot 项目,把一些 http 请求转为 使用 feign方式.但是遇到一个问题:个别请求是要设置header的. 于是,查看官方文档和博客,大致推荐两种方式.也可能是我没看明 ...
- Solve Error: node postinstall sh: node: command not found
When install the yeoman using the following command: npm install -g yo You might have the following ...
- css 控制文字显示两行,多余用省略号 手机端
p { width:100px; position:relative; line-height:20px; /*行高为高度的一半,这样就是两行*/ height:40px; overflow:hidd ...
- 19.3.25 sql查询语句
1.单表查询:select * from 表名 where id = 111 2.查询表内数据并以id排序:select * from 表名 order by id (降序:desc/升序:asc) ...
- Python全栈-magedu-2018-笔记10
第三章 - Python 内置数据结构 集set 约定 set 翻译为集合 collection 翻译为集合类型,是一个大概念 set 可变的.无序的.不重复的元素的集合 set定义 初始化 set( ...
- java_基础_接口和抽象类
1.接口 java中接口的存在意义是:让多个继承该接口的类实现多态,让多个类有相同的特征 示例代码: interface MyInterface{ void myMethod(); } class T ...
- jsplumb 使用总结
1 删除连线问题 funcion clearDrawGraph { if (this.graphInstance !== null) { const connections = this.graphI ...
- go/wiki/MutexOrChannel Golang并发:选channel还是选锁?
https://mp.weixin.qq.com/s/JcED2qgJEj8LaBckVZBhDA https://github.com/golang/go/wiki/MutexOrChannel M ...
- [math] 我对对数的最新理解
前言 作为资深学渣,每次遇到对数就极度恐慌.恐慌不是因为要考试---.而是因为不理解,只能靠死记硬背运算规则.不能进行有效的推理,这让我极度不爽,因为会忘记.故惶恐. 所以总是耿耿于怀,想要试图理解对 ...