diff --git a/common/src/main/java/com/alibaba/nacos/common/util/SystemUtils.java b/common/src/main/java/com/alibaba/nacos/common/util/SystemUtils.java index 6fab17c19..c271cb697 100644 --- a/common/src/main/java/com/alibaba/nacos/common/util/SystemUtils.java +++ b/common/src/main/java/com/alibaba/nacos/common/util/SystemUtils.java @@ -144,17 +144,33 @@ public class SystemUtils { return nacosHome; } + public static String getConfFilePath() { + return NACOS_HOME + File.separator + "conf" + File.separator; + } + private static String getClusterConfFilePath() { return NACOS_HOME + File.separator + "conf" + File.separator + "cluster.conf"; } public static List readClusterConf() throws IOException { - try { - return IoUtils.readLines( - new InputStreamReader(new FileInputStream(new File(CLUSTER_CONF_FILE_PATH)), UTF_8)); - } catch (IOException e){ - throw e; + List instanceList = new ArrayList(); + List lines = IoUtils.readLines( + new InputStreamReader(new FileInputStream(new File(CLUSTER_CONF_FILE_PATH)), UTF_8)); + String comment = "#"; + for (String line : lines) { + String instance = line.trim(); + if (instance.startsWith(comment)) { + // # it is ip + continue; + } + if (instance.contains(comment)) { + // 192.168.71.52:8848 # Instance A + instance = instance.substring(0, instance.indexOf(comment)); + instance = instance.trim(); + } + instanceList.add(instance); } + return instanceList; } public static void writeClusterConf(String content) throws IOException { diff --git a/common/src/test/java/com/alibaba/nacos/common/SystemUtilsTest.java b/common/src/test/java/com/alibaba/nacos/common/SystemUtilsTest.java index b7e1f394a..50223641f 100644 --- a/common/src/test/java/com/alibaba/nacos/common/SystemUtilsTest.java +++ b/common/src/test/java/com/alibaba/nacos/common/SystemUtilsTest.java @@ -17,10 +17,14 @@ package com.alibaba.nacos.common; import com.alibaba.nacos.common.util.SystemUtils; +import org.apache.commons.io.FileUtils; import org.junit.Assert; import org.junit.BeforeClass; import org.junit.Test; +import java.io.File; +import java.io.IOException; +import java.util.List; import java.util.Random; /** @@ -71,4 +75,33 @@ public class SystemUtilsTest { Assert.assertEquals(preferHostMode, SystemUtils.PREFER_HOSTNAME_OVER_IP); } + + @Test + public void testReadClusterConf() throws IOException { + FileUtils.forceMkdir(new File(SystemUtils.getConfFilePath())); + + String lineSeparator = System.getProperty("line.separator"); + + /* + * #it is ip + * #example + * 192.168.1.1:8848 + */ + SystemUtils.writeClusterConf("#it is ip" + lineSeparator + "#example" + lineSeparator + "192.168.1.1:8848"); + Assert.assertEquals(SystemUtils.readClusterConf().get(0), "192.168.1.1:8848"); + + /* + * #it is ip + * #example + * # 192.168.1.1:8848 + * 192.168.1.2:8848 # Instance A + */ + SystemUtils.writeClusterConf( + "#it is ip" + lineSeparator + " #example" + lineSeparator + " # 192.168.1.1:8848" + lineSeparator + + " 192.168.1.2:8848 # Instance A " + lineSeparator + "192.168.1.3#:8848"); + List instanceList = SystemUtils.readClusterConf(); + Assert.assertEquals(instanceList.get(0), "192.168.1.2:8848"); + Assert.assertEquals(instanceList.get(1), "192.168.1.3"); + } + }