자바로 만든 모니터링 시스템에서, 새로운 서버가 DNS 서버에 추가될 때, 사용할 수 있을 것이다..
은근히 많이.. 모르는 사람들이 많았다..

java옵션에 sun.net.inetaddr.ttl을 주어 dns 캐싱 시간을 줄 수있다.

-Dsun.net.inetaddr.ttl=30

Tomcat에 넣어주면 좋다. restart할 필요가 없다. 구현된 jvm의 c코드를 보면 도메인별로 캐쉬하고 있기 때문에 한번 실패하면, 그 도메인이 실패하면 다시 요청하지 않도록 default 코드로 되어 있다.

jdk 1.4 이상이면, jre/lib/security/java.security 파일에 다음의 내용을 추가하면된다.

networkaddress.cache.ttl=30

또는 코딩상에서 쓸수 있다.
java.security.Security.setProperty("networkaddress.cache.ttl" , "30");


테스트하는 코드가 있다. 참조 (http://www.sdn.sap.com/irj/scn/weblogs?blog=/pub/wlg/1887
)

package com.bouvet.java;

import java.net.InetAddress;
/**
 * @author dagfinn.parnas
 * Class which test DNS chaching setting
 */
public class DNSTest implements Runnable {
 public static void main(String args[]){
  //this allows you to set the setting directly without changing the java.security file
  //java.security.Security.setProperty("networkaddress.cache.ttl" , "5");
  //Get the host name from in-parameters if they exist
  String hostName;
  if(args==null || args.length==0){
   hostName="www.bouvet.no";
  }else {
   hostName=args[0];
  }
  
  DNSTest testRunnable = new DNSTest(hostName);
  Thread thread = new Thread(testRunnable);
  thread.run();
 }
 
 private String dnsName;
 
 public DNSTest(String dnsName){
  this.dnsName=dnsName;
 }
 
 /**
  * Run method
  */
 public void run() {
  int i = 1;
  while(true){
   try {
    //do the dns lookup
    InetAddress addresse = InetAddress.getByName(dnsName);
    System.out.println(i++ +" " + addresse.getHostName()+ "="+ addresse.getHostAddress());
   } catch(Exception e){
    e.printStackTrace();
   }
   //wait a second
   sleep(1000);
  }
 }
 private void sleep(long ms){
  try {
   Thread.sleep(ms);
  }catch(InterruptedException e){
   e.printStackTrace();
  }
  
 }
}



Posted by '김용환'
,