[ISSUE #4208] Fix bug when configuring multiple ips. (#5891)

* [ISSUE #4208] Fix bug when configuring multiple ips.

* [ISSUE #4208] Fix bug when configuring multiple ips.

* Format the code.

* Add unit test.
This commit is contained in:
mask 2021-05-31 09:58:05 +08:00 committed by GitHub
parent 09c7293085
commit e2909b0796
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 30 additions and 2 deletions

View File

@ -43,6 +43,7 @@ import java.util.Iterator;
import java.util.List;
import java.util.Properties;
import java.util.Random;
import java.util.StringTokenizer;
import java.util.concurrent.Callable;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.ScheduledThreadPoolExecutor;
@ -154,8 +155,9 @@ public class ServerListManager implements Closeable {
if (StringUtils.isNotEmpty(serverAddrsStr)) {
this.isFixed = true;
List<String> serverAddrs = new ArrayList<String>();
String[] serverAddrsArr = this.serverAddrsStr.split(",");
for (String serverAddr : serverAddrsArr) {
StringTokenizer serverAddrsTokens = new StringTokenizer(this.serverAddrsStr, ",;");
while (serverAddrsTokens.hasMoreTokens()) {
String serverAddr = serverAddrsTokens.nextToken().trim();
if (serverAddr.startsWith(HTTPS) || serverAddr.startsWith(HTTP)) {
serverAddrs.add(serverAddr);
} else {

View File

@ -73,6 +73,32 @@ public class ServerListManagerTest {
Assert.assertTrue(mgr3.contain("http://1.1.1.1:8848"));
Assert.assertEquals("ServerManager-fixed-1.1.1.1_8848-[http://1.1.1.1:8848]", mgr3.toString());
}
{
Properties properties3 = new Properties();
properties3.put(PropertyKeyConst.CONTEXT_PATH, "aaa");
properties3.put(PropertyKeyConst.SERVER_ADDR, "1.1.1.1:8848,2.2.2.2:8848");
final ServerListManager mgr4 = new ServerListManager(properties3);
Assert.assertEquals(2, mgr4.getServerUrls().size());
Assert.assertEquals("http://1.1.1.1:8848", mgr4.getServerUrls().get(0));
Assert.assertEquals("http://2.2.2.2:8848", mgr4.getServerUrls().get(1));
Assert.assertTrue(mgr4.contain("http://1.1.1.1:8848"));
Assert.assertEquals("ServerManager-fixed-1.1.1.1_8848-2.2.2.2_8848-[http://1.1.1.1:8848, http://2.2.2.2:8848]", mgr4.toString());
}
{
Properties properties4 = new Properties();
properties4.put(PropertyKeyConst.CONTEXT_PATH, "aaa");
properties4.put(PropertyKeyConst.SERVER_ADDR, "1.1.1.1:8848;2.2.2.2:8848");
final ServerListManager mgr5 = new ServerListManager(properties4);
Assert.assertEquals(2, mgr5.getServerUrls().size());
Assert.assertEquals("http://1.1.1.1:8848", mgr5.getServerUrls().get(0));
Assert.assertEquals("http://2.2.2.2:8848", mgr5.getServerUrls().get(1));
Assert.assertTrue(mgr5.contain("http://1.1.1.1:8848"));
Assert.assertEquals("ServerManager-fixed-1.1.1.1_8848-2.2.2.2_8848-[http://1.1.1.1:8848, http://2.2.2.2:8848]", mgr5.toString());
}
}