This commit is contained in:
hxy1991 2018-11-14 12:02:19 +08:00
parent 746035c11a
commit de4a523318
2 changed files with 54 additions and 5 deletions

View File

@ -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<String> 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<String> instanceList = new ArrayList<String>();
List<String> 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 {

View File

@ -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<String> instanceList = SystemUtils.readClusterConf();
Assert.assertEquals(instanceList.get(0), "192.168.1.2:8848");
Assert.assertEquals(instanceList.get(1), "192.168.1.3");
}
}