alibaba/spring-cloud-alibaba

基于bootstrap.yml的配置方式和基于application.yml的配置方式,Nacos配置项user.name的行为差异

Closed this issue · 2 comments

Describe the bug
基于bootstrap.yml的配置方式和基于application.yml的配置方式,Nacos配置项user.name的行为差异

public static void main(String[] args) throws Exception {
      ConfigurableApplicationContext run = SpringApplication.run(DemoApplication.class, args);
      while (true) {
          String name = run.getEnvironment().getProperty("user.name");
          String age = run.getEnvironment().getProperty("user.age");
          log.info("user name: {} ", name);
          log.info("user age: {}", age);
          Thread.sleep(1000);
      }
  }

bootstrap.yml:

spring:
  cloud:
    nacos:
      serverAddr: 127.0.0.1:8848
      config:
        name: main
        namespace: 0ca68ca8-33be-4990-aaf3-d17b04e72cc6
        group: dev
        file-extension: yaml
        refresh-enabled: true

application.yml:

spring:
  cloud:
    nacos:
      config:
        server-addr: 127.0.0.1:8848
        namespace: 0ca68ca8-33be-4990-aaf3-d17b04e72cc6
        name: main
        file-extension: yaml
        group: dev
        refresh-enabled: true

  config:
    import:
      - nacos:main?refreshEnabled=true&group=dev
<dependencyManagement>
      <dependencies>
          <dependency>
              <groupId>org.springframework.boot</groupId>
              <artifactId>spring-boot-dependencies</artifactId>
              <version>2.6.13</version>
              <type>pom</type>
              <scope>import</scope>
          </dependency>
          <dependency>
              <groupId>org.springframework.cloud</groupId>
              <artifactId>spring-cloud-dependencies</artifactId>
              <version>2021.0.5</version>
              <type>pom</type>
              <scope>import</scope>
          </dependency>
          <dependency>
              <groupId>com.alibaba.cloud</groupId>
              <artifactId>spring-cloud-alibaba-dependencies</artifactId>
              <version>2021.0.6.0</version>
              <type>pom</type>
              <scope>import</scope>
          </dependency>
      </dependencies>
  </dependencyManagement>

image
实测发现:
在main中配置

user:
  name: 测试
  age: 20

在基于bootstrap.yml的配置方式下生效,并且能正确输出,在修改相应值后也会实时输出修改后的值
在基于application.yml的配置下,只会输出本机电脑用户名,并且修改后,日志中会输出检测到值更新了,但是实际输出的值依旧是本机电脑的用户名,而user.age能正确输出且能响应修改

目前仅发现user.name这个配置项有这个问题

为什么基于application.yml加载的配置文件,user.name这个配置项在更新后,输出没有更新?

Desktop (please complete the following information):

  • OS: win11[23H2]
  • Version [nacos-server 2.3.2, nacos-client 2.2.0]

仔细看看官网文档介绍

究其原因还是因为bootstrap配置的属性,优先级高于系统属性以及application配置的属性,如果在nacos中配置的属性,系统已经自带了,那么基于bootstrap的配置才能覆盖并且动态刷新