Skip to content

Commit a340658

Browse files
committed
update doc, add multi ip demo code; add multi ip test case.
1 parent 2e31a02 commit a340658

File tree

2 files changed

+28
-7
lines changed

2 files changed

+28
-7
lines changed

library/README.md

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -55,13 +55,25 @@ Java Dns Cache Manipulator(DCM) Library
5555
DnsCacheManipulator.setDnsCache("www.hello.com", "192.168.1.1");
5656
DnsCacheManipulator.setDnsCache("www.world.com", "1234:5678:0:0:0:0:0:200e"); // 支持IPv6地址
5757

58-
// 之后Java代码中使用到域名都会解析成上面指定的IP
59-
// 下面是一个简单获取域名对应的IP,演示一下
58+
// 之后Java代码中的域名解析都会是上面设定的IP
59+
// 下面用一个简单获取域名对应的IP,来演示一下
6060

6161
String ip = InetAddress.getByName("www.hello.com").getHostAddress();
6262
// ip = "192.168.1.1"
6363
String ipv6 = InetAddress.getByName("www.world.com").getHostAddress();
6464
// ipv6 = "1234:5678:0:0:0:0:0:200e"
65+
66+
67+
// 可以设置多个IP
68+
DnsCacheManipulator.setDnsCache("www.hello-world.com", "192.168.2.1", "192.168.2.2");
69+
70+
String ipHw = InetAddress.getByName("www.hello-world.com").getHostAddress();
71+
// ipHw = 192.168.2.1 ,读到第一个IP
72+
InetAddress[] allIps = InetAddress.getAllByName("www.hello-world.com");
73+
// 上面读到设置的多个IP
74+
75+
// 设置过期时间,单元毫秒
76+
DnsCacheManipulator.setDnsCache(3600 * 1000, "www.hello-hell.com", "192.168.1.1", "192.168.1.2");
6577
```
6678

6779
通过`dns-cache.properties`文件批量配置

library/src/test/java/com/alibaba/dcm/DnsCacheManipulatorTest.java

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package com.alibaba.dcm;
22

3+
import org.junit.Assert;
34
import org.junit.Before;
45
import org.junit.Test;
56

@@ -13,11 +14,7 @@
1314

1415
import static java.lang.System.currentTimeMillis;
1516
import static java.lang.Thread.sleep;
16-
import static org.junit.Assert.assertEquals;
17-
import static org.junit.Assert.assertNotSame;
18-
import static org.junit.Assert.assertNull;
19-
import static org.junit.Assert.assertTrue;
20-
import static org.junit.Assert.fail;
17+
import static org.junit.Assert.*;
2118

2219
/**
2320
* @author Jerry Lee (oldratlee at gmail dot com)
@@ -52,6 +49,18 @@ public void test_loadDnsCacheConfig_fromMyConfig() throws Exception {
5249
assertEquals(IP2, ip);
5350
}
5451

52+
@Test
53+
public void test_setMultiIp() throws Exception {
54+
DnsCacheManipulator.setDnsCache("multi.ip.com", "1.1.1.1", "2.2.2.2");
55+
String ip = InetAddress.getByName("multi.ip.com").getHostAddress();
56+
assertEquals("1.1.1.1", ip);
57+
58+
InetAddress[] all = InetAddress.getAllByName("multi.ip.com");
59+
assertEquals(2, all.length);
60+
String[] ips = {all[0].getHostAddress(), all[1].getHostAddress()};
61+
assertArrayEquals(new String[]{"1.1.1.1", "2.2.2.2"}, ips);
62+
}
63+
5564
@Test
5665
public void test_configNotFound() throws Exception {
5766
try {

0 commit comments

Comments
 (0)