简单添加host配置到jvm默认的dns查询缓存中
来源:互联网 发布:用流量看淘宝费流量吗? 编辑:程序博客网 时间:2024/06/06 07:46
编写了一个操作,可以自己将定义dns指向添加到jvm的dns查询缓存中,适用于不想将dns配置直接写入系统 /etc/hosts的情况。
/** * 从hosts文件中读取dns配置,并加入本地jvm系统缓存中 * @throws NoSuchFieldException * @throws IllegalAccessException * @throws NoSuchMethodException * @throws IOException * @throws InvocationTargetException * @return 此次设置dns配置的数量 */ public static int load() throws NoSuchFieldException, IllegalAccessException, NoSuchMethodException, IOException, InvocationTargetException { List<Pair<String,String>> hosts=hosts();//读取项目中的host文件,获得项目中定义的dns配置 int totalLoaded=0; if(hosts.size()>0){ //获取jvm的dns缓存设置方法 Field f =InetAddress.class.getDeclaredField("addressCache"); f.setAccessible(true); Object addressCache=f.get(null); Method put=addressCache.getClass().getDeclaredMethod("put",String.class,InetAddress[].class); put.setAccessible(true); //获取原先的缓存策略 int policy=InetAddressCachePolicy.get(); //将本地dns设置的缓存策略设置为永远有效 Field cachePolicy=InetAddressCachePolicy.class.getDeclaredField("cachePolicy"); cachePolicy.setAccessible(true); cachePolicy.set(null,InetAddressCachePolicy.FOREVER); //解析host配置,加载入jvm缓存 for(Pair<String,String> host : hosts){ String ip=host.getValue(); String[] hostNames=host.getKey().split(","); for(String hostName :hostNames){ if(hostName.length()==0) continue; put.invoke(addressCache,hostName,new InetAddress[]{InetAddress.getByAddress(hostName,address(ip))}); totalLoaded++; log.debug("load InetAddress set "+ip+" "+hostName); } } //将缓存策略替换为原先的默认值 cachePolicy.set(null,policy); } return totalLoaded; } /** * 将字符串形式的ip地址转换成字节形式 * @param ip * @return */ private static byte[] address(String ip){ boolean isIp=true; for(char c : ip.toCharArray()){ if((c>'9'||c<'0')&&c!='.'){ isIp=false; break; } } if(!isIp) return null; String[] ipArr=ip.split("\\."); byte[] bip=new byte[ipArr.length]; for(int i=0;i<ipArr.length;++i){ bip[i]=(byte)(Integer.parseInt(ipArr[i],10)&0xff); } return bip; }
阅读全文
1 0
- 简单添加host配置到jvm默认的dns查询缓存中
- 清除chrome的host DNS缓存
- DNS的正、反解查询命令:host、nslookup、dig
- Dns服务的简单配置
- DNS配置:设置Host文件或DNS
- 使用反射机制修改JVM的DNS缓存
- Linux中主dns和辅dns服务器的简单配置
- linux中添加dns缓存提高解析速度
- 将查询的数据写到redis缓存中
- CF中dns服务器简单配置
- DNS查询指令:host 、nslookup 、dig 详解
- DNS查询指令:host 、nslookup 、dig 详解
- DNS查询指令:host 、nslookup 、dig 详解
- DNS查询指令:host 、nslookup 、dig 详解
- Linux中DNS的配置
- 如何将项添加到缓存中
- 向数据表中添加查询到的数据
- Phoenix默认jvm配置
- Qt调用jrtplib实现单播、多播和广播
- 开发的应用程序启动不了
- javascript数字插入
- PL/SQL(五):存储过程
- Java基础——多态
- 简单添加host配置到jvm默认的dns查询缓存中
- 代理及动态代理
- ExtJs学习笔记(四)——动态修改store属性
- hdoj 1337
- onvif协议规范
- 消息中间件介绍
- python列表
- C#各个版本中的新增特性详解
- vue学习:解决vue变量未渲染前代码显示问题