启动nacos报错

启动nacos服务,出现如下异常:

2020-07-14 15:21:16.167 ERROR 5748 --- [           main] c.l.StartingSpringApplicationRunListener : read cluster conf fail

java.io.FileNotFoundException: C:\Users\Administrator\nacos\conf\cluster.conf (系统找不到指定的路径。)
	at java.io.FileInputStream.open0(Native Method)
	at java.io.FileInputStream.open(FileInputStream.java:195)
	at java.io.FileInputStream.(FileInputStream.java:138)
	at com.alibaba.nacos.core.utils.SystemUtils.readClusterConf(SystemUtils.java:124)
	at com.alibaba.nacos.core.listener.StartingSpringApplicationRunListener.logClusterConf(StartingSpringApplicationRunListener.java:142)
	at com.alibaba.nacos.core.listener.StartingSpringApplicationRunListener.contextPrepared(StartingSpringApplicationRunListener.java:92)
	at org.springframework.boot.SpringApplicationRunListeners.contextPrepared(SpringApplicationRunListeners.java:60)
	at org.springframework.boot.SpringApplication.prepareContext(SpringApplication.java:374)
	at org.springframework.boot.SpringApplication.run(SpringApplication.java:314)
	at org.springframework.boot.SpringApplication.run(SpringApplication.java:1260)
	at org.springframework.boot.SpringApplication.run(SpringApplication.java:1248)
	at com.alibaba.nacos.Nacos.main(Nacos.java:35)

2020-07-14 15:21:17.173  INFO 5748 --- [ nacos-starting] c.l.StartingSpringApplicationRunListener : Nacos is starting...

根据异常日志打断点
启动nacos报错_第1张图片
debug
启动nacos报错_第2张图片
可见STANDALONE_MODE返回false,也就是非单机模式。但是目前是想以单机模式启动的。
查看相关配置

/**
   * Standalone mode or not
   */
  public static final boolean STANDALONE_MODE = Boolean.getBoolean(STANDALONE_MODE_PROPERTY_NAME);

/**
 * The System property name of  Standalone mode
 */
String STANDALONE_MODE_PROPERTY_NAME = "nacos.standalone";

按道理来说,如果不配置则默认应该为false.

public static boolean getBoolean(String name) {
    boolean result = false;
    try {
        result = parseBoolean(System.getProperty(name));
    } catch (IllegalArgumentException | NullPointerException e) {
    }
    return result;
}

这个读取的是系统属性,在Spring的application.properties中配置是没有效果的。
启动nacos报错_第3张图片
在VM options中添加如下内容

-Dnacos.standalone=true

再次debug
启动nacos报错_第4张图片
单机模式启动成功
启动nacos报错_第5张图片
如果要以集群模式启动,需要怎么办呢?

  1. 在项目中查找cluster.conf文件,会发现在nacos-distribution模块中存在一个cluster.conf.example文件
  2. 将该文件拷贝到C:\Users\Administrator\nacos\conf\目录下,并复制一份为cluster.conf
  3. 修改cluster.conf中的ip为需要加入集群的主机的ip,然后启动服务

启动nacos报错_第6张图片
执行结果如下

2020-07-14 15:51:25.415  INFO 10888 --- [           main] c.l.StartingSpringApplicationRunListener : The server IP list of Nacos is [192.168.6.71]
2020-07-14 15:51:26.422  INFO 10888 --- [ nacos-starting] c.l.StartingSpringApplicationRunListener : Nacos is starting...
2020-07-14 15:51:27.434  INFO 10888 --- [ nacos-starting] c.l.StartingSpringApplicationRunListener : Nacos is starting...
2020-07-14 15:51:28.436  INFO 10888 --- [ nacos-starting] c.l.StartingSpringApplicationRunListener : Nacos is starting...
2020-07-14 15:51:29.441  INFO 10888 --- [ nacos-starting] c.l.StartingSpringApplicationRunListener : Nacos is starting...
2020-07-14 15:51:30.443  INFO 10888 --- [ nacos-starting] c.l.StartingSpringApplicationRunListener : Nacos is starting...
2020-07-14 15:51:31.444  INFO 10888 --- [ nacos-starting] c.l.StartingSpringApplicationRunListener : Nacos is starting...
2020-07-14 15:51:32.446  INFO 10888 --- [ nacos-starting] c.l.StartingSpringApplicationRunListener : Nacos is starting...
2020-07-14 15:51:33.447  INFO 10888 --- [ nacos-starting] c.l.StartingSpringApplicationRunListener : Nacos is starting...
2020-07-14 15:51:34.034  INFO 10888 --- [           main] c.l.StartingSpringApplicationRunListener : Nacos logs files: C:\Users\Administrator\nacos\logs\
2020-07-14 15:51:34.034  INFO 10888 --- [           main] c.l.StartingSpringApplicationRunListener : Nacos conf files: C:\Users\Administrator\nacos\conf\
2020-07-14 15:51:34.034  INFO 10888 --- [           main] c.l.StartingSpringApplicationRunListener : Nacos data files: C:\Users\Administrator\nacos\data\
2020-07-14 15:51:34.035  INFO 10888 --- [           main] c.l.StartingSpringApplicationRunListener : Nacos started successfully in cluster mode.

从日志也不难看出,nacos就是通过读取这个文件来获取The server IP list of Nacos的。

如果不希望将配置文件存放在以上目录,也可以通过修改配置来实现。查看源码:
com.alibaba.nacos.core.listener.StartingSpringApplicationRunListener

private void logClusterConf() {
    if (!STANDALONE_MODE) {
        try {
            List<String> clusterConf = readClusterConf();
            LOGGER.info("The server IP list of Nacos is {}", clusterConf);
        } catch (IOException e) {
            LOGGER.error("read cluster conf fail", e);
        }
    }
}

主要的实现在readClusterConf方法中。

public static List<String> readClusterConf() throws IOException {
    List<String> instanceList = new ArrayList<String>();
    // 通过try-resource打开文件
    try(Reader reader = new InputStreamReader(new FileInputStream(new File(CLUSTER_CONF_FILE_PATH)),
    StandardCharsets.UTF_8)) {
        List<String> lines = IoUtils.readLines(reader);
        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();
            }
            int multiIndex = instance.indexOf(Constants.COMMA_DIVISION);
            if (multiIndex > 0) {
                // support the format: ip1:port,ip2:port  # multi inline
                instanceList.addAll(Arrays.asList(instance.split(Constants.COMMA_DIVISION)));
            } else {
                //support the format: 192.168.71.52:8848
                instanceList.add(instance);
            }
        }
        return instanceList;
    }
}

以上代码说明:

  1. 读取文件的路径在常量CLUSTER_CONF_FILE_PATH中定义的
  2. 读取文件时是按行来读取的,以#开头的是注释不会读取,如果不是#开头但是包含#的截取#前面的字符作为ip,可以一行配置多个ip,重点以,(常量COMMA_DIVISION定义)分割即可。遍历每一行,最后将所有的值都加入到list中,作为instanceList。支持格式:192.168.71.52:8848和192.168.71.52(默认端口8848)。

继续查看常量CLUSTER_CONF_FILE_PATH定义

/**
 * The file path of cluster conf.
 */
public static final String CLUSTER_CONF_FILE_PATH = getClusterConfFilePath();

private static String getClusterConfFilePath() {
    return NACOS_HOME + File.separator + "conf" + File.separator + "cluster.conf";
}

/**
 * The home of nacos.
 */
public static final String NACOS_HOME = getNacosHome();

private static String getNacosHome() {
    String nacosHome = System.getProperty(NACOS_HOME_KEY);
    if (StringUtils.isBlank(nacosHome)) {
        nacosHome = System.getProperty("user.home") + File.separator + "nacos";
    }
    return nacosHome;
}

/**
 * The key of nacos home.
 */
public static final String NACOS_HOME_KEY = "nacos.home";

不难看出,如果想修改配置文件的路径,有两种方式:

  1. 配置系统参数nacos.home
  2. 配置系统参数user.home,以前者优先

你可能感兴趣的:(nacos,java)