启动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.<init>(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...

根据异常日志打断点
在这里插入图片描述
debug
在这里插入图片描述
可见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中配置是没有效果的。
在这里插入图片描述
在VM options中添加如下内容

-Dnacos.standalone=true

再次debug
在这里插入图片描述
单机模式启动成功
在这里插入图片描述
如果要以集群模式启动,需要怎么办呢?

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

在这里插入图片描述
执行结果如下

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,以前者优先
  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
在使用Nacos配置文件时,可能会遇到配置文件读不到的问题。解决这个问题的步骤如下: 1. 检查项目中的配置文件优先级。在之前的Cloud版本中,通常使用`application.yml`作为配置文件。但是在使用Nacos时,需要使用`bootstrap.yml`作为配置文件,并且它的优先级要高于`application.yml`。确保配置文件的命名和路径正确,并且`bootstrap.yml`中的配置正确设置了Nacos服务器地址等信息。 2. 检查相关版本号是否对应。在项目启动时,如果出现`@Value(${})`值不到的错误,可能是由于依赖的版本不对应导致的。检查nacos、Spring Cloud、Spring Cloud Alibaba等相关版本号是否匹配。可以参考官方文档的版本说明进行确认,并确保项目中的依赖已正确添加。 3. 检查占位符的值是否正确解析。如果配置文件中使用了占位符,例如`${spring.profiles.active}`,它会被解析为项目指定的环境,比如`dev`。如果没有配置`spring.profiles.active`,则该占位符的值为空。另外,可以通过`${file-extension}`指定文件类型。例如,如果没有指定`spring.profiles.active`,那么最终拼接的文件名为`nacos-config-demo.yaml`;如果指定了`spring.profiles.active=dev`,则最终拼接的文件名为`nacos-config-demo-dev.yaml`。确保占位符的值正确解析并拼接正确的文件名。 通过以上步骤,应该能够解决Nacos配置文件读不到的问题。如果问题还存在,请检查配置文件的命名、路径和内容是否正确,并确保Nacos服务器可正常访问。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* *2* *3* [SpringCloud无法获取Nacos中的配置文件信息](https://blog.csdn.net/weixin_43032413/article/details/126601920)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 100%"] [ .reference_list ]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

lang20150928

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值