Skip to content

Commit

Permalink
Fixes alibaba#272
Browse files Browse the repository at this point in the history
  • Loading branch information
hxy1991 committed Nov 14, 2018
1 parent 746035c commit de4a523
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -144,17 +144,33 @@ private static String getNacosHome() {
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 {
Expand Down
33 changes: 33 additions & 0 deletions common/src/test/java/com/alibaba/nacos/common/SystemUtilsTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;

/**
Expand Down Expand Up @@ -71,4 +75,33 @@ public void testPreferHostModeConstants() {
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");
}

}

0 comments on commit de4a523

Please sign in to comment.