alibaba / nacos

an easy-to-use dynamic service discovery, configuration and service management platform for building cloud native applications.
https://nacos.io
Apache License 2.0
30.06k stars 12.8k forks source link

The spring boot config client doesn't startup when the value of property dataId defined in annotation @NacosPropertySource contains a dot #3331

Closed rainyhao closed 4 years ago

rainyhao commented 4 years ago

Issue Description

Type: bug report

Describe what happened

使用SpringBoot的配置中心客户端0.2.7 api的时候发现两个问题. Two issues I found in spring-boot client api when use the feature of Config Center

  1. 当在SpringBoot启动类上标@NacosPropertySource注解, 指定dataId的名字中包含点的时候,启动报错 An exception occured when the value of property "dataId" defined in the annotation @NacosPropertySource contains a dot - "." while the bootstrap main class is annotated with this annotation.

dataId = NacosUtils.readFromEnvironment(dataId, environment); groupId = NacosUtils.readFromEnvironment(groupId, environment); // 这个是173行 // this is line 173 type = StringUtils.isEmpty(NacosUtils.readTypeFromDataId(dataId)) ? type : NacosUtils.readTypeFromDataId(dataId);


- 问题还在173行中调用的 NacosUtils.readTypeFromDataId 这个方法, 代码内容如下
The code in at line 173 calls another method NacosUtils.readTypeFromDataId, the method body is
```java
// 把dataId的值 study.nacos.boot 代入到这个方法的时候, 方法返回字符串 boot
// Pass the value "study.nacos.boot" of dataId to this method, a string value "boot" is returned.
// 方法执行完后返回到 AbstractNacosPropertySourceBuilder 类的 173行, type值就由 properties 变成了 boot
// After the method invocation, code go back to line 173 of class AbstractNacosPropertySourceBuilder, the value of variable type is changed to "boot"
public static String readTypeFromDataId(String dataId) {
    if (StringUtils.isEmpty(dataId)) {
        return dataId;
    }
    int index = dataId.lastIndexOf(".");
    return index != -1 ? dataId.substring(index + 1) : "";
}

第一个问题现象就描述完了, 本来想说点猜测的想法, 不过想了想, 还是不在各位大神面前得瑟了 ^_^ That's the first issue I found.

这个问题在使用spring boot配置文件里直接配, 不会出现 But it doesn't appears when the project is configured with spring boot configuration file: application.properties

  1. 还是在使用 @NacosPropertySource 标在启动类上的时候, 在Nacos控制台里修改完配置项, 工程中不会实时更新, autoRefresh已经指定过true了 The auto refresh is not fired when the project is configured with @NacosPropertySource.

同样这个问题在使用spring boot配置文件里直接配的时候, 也不会出现 But it does fire when configured with spring boot configuration file.

Describe what you expected to happen

第一个问题的期望, 当然是正常启动不报错 第二个问题的期望, 当在nacos里修改过dataId的内容时, 工程里可以实时更新

How to reproduce it (as minimally and precisely as possible)

issue 1:

  1. 配置的dataId名字中包含点 . 比如 study.nacos.boot
  2. 在启动类上 标 @NacosPropertySource(dataId="study.nacos.boot")
  3. 运行工程

issue 2: 直接测试就成

I don't want to translate the reproducing part any more, sorry to foreign guys.

Tell us your environment

OS: MacOS 10.13.4 JAR Library Version: com.alibaba.boot:nacos-config-spring-boot-starter:0.2.7 Nacos Server Version: 1.2.1

题外话 Other things I'd like to say

我个人感觉现现在汉语也热了, 咱们应该鼓励老外学汉语. The project owner encourages we using English, but I encourage you guys study Chinese.

Anything else we need to know?

Nothing else, thank you.

rainyhao commented 4 years ago

不好意思提错地方了, 刚才没有找到nacos spring, 我在那边提了