读取配置文件顺序
SpringBoot 读取配置文件顺序:bootstrap.yml > bootstrap.properties > nacos 配置 > application.yml > application.properties
- 加载顺序:若 application.yaml 和 bootstrap.yaml 在同一目录下,bootstrap.yaml 先加载,application.yaml 后加载。bootstrap.yaml 用于应用程序上下文的引导阶段,application.yaml 由父 Spring ApplicationContext 加载
- 配置区别:bootstrap.yaml 和 application.yaml 都可以用来配置参数。bootstrap.yaml 用来程序引导时执行,应用于更加早期配置信息读取,可以理解为系统级别的一些参数配置,这些参数配置是不会变动的,一旦 bootstrap.yaml 被加载,则内容不会被覆盖
- 属性覆盖问题:启动上下文时,Spring Cloud 会创建一个 Bootstrap Context 作为 Spring 应用的 Application Context 的父上下文,初始化的时候,Bootstrap Context 负责从外部源加载配置属性并解析配置,这两个上下文共享一个从外部获取的 Environment。Bootstrap 属性有高优先级,默认情况下,它们不会被本地配置覆盖,即如果加载的 application.yaml 的内容标签与 bootstrap 的标签一致,application 也不会覆盖 bootstrap,而 application.yaml 里面的内容可以动态替换
环境隔离
- 命名空间(namespace)



- 组(group)


- 唯一标识(dataId)

动态感知
在进行修改 yaml 或 properties 文件,每次都要重新启动项目,可以在 nacos 配置中心进行文件配置,使得项目可以动态感知,在 resources 创建的文件名必须是 bootstrap。在 nacos 中创建配置文件,配置文件名称要和项目中 bootstrap.properties 中配置的名称一致,注意:配置文件的名称建议为项目服务名且要带上具体环境,例如:configclient-prod.properties
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30
| <?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <parent> <artifactId>springcloud</artifactId> <groupId>com.example</groupId> <version>0.0.1-SNAPSHOT</version> </parent> <modelVersion>4.0.0</modelVersion>
<artifactId>config-nacos</artifactId>
<properties> <maven.compiler.source>8</maven.compiler.source> <maven.compiler.target>8</maven.compiler.target> </properties>
<dependencies> <dependency> <groupId>com.alibaba.cloud</groupId> <artifactId>spring-cloud-starter-alibaba-nacos-config</artifactId> </dependency>
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> </dependencies> </project>
|
- application.yaml
1 2 3 4 5 6 7 8
| server: port: 8050 spring: profiles: active: dev
|
- bootstrap.yaml
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38
| spring: application: name: config-nacos cloud: nacos: server-addr: localhost:8848 username: nacos password: nacos config: file-extension: yaml
namespace: dev group: test
shared-configs: - data-id: com.common.test1 refresh: true - data-id: com.common.test2 refresh: true extension-configs[0]: data-id: com.common.test3 refresh: true
|
1 2 3 4 5 6 7 8 9 10 11 12 13
| @SpringBootApplication public class ConfigApplication {
public static void main(String[] args) throws InterruptedException { ConfigurableApplicationContext applicationContext = SpringApplication.run(ConfigApplication.class, args); while (true){ String userName = applicationContext.getEnvironment().getProperty("user.name"); String userAge = applicationContext.getEnvironment().getProperty("user.age"); System.err.println(userName+","+userAge); TimeUnit.SECONDS.sleep(1); } } }
|
1 2 3 4 5 6 7 8 9 10 11 12 13
| @RestController @RequestMapping("/config") @RefreshScope public class ConfigController { @Value("${user.name}") public String username; @RequestMapping("/show") public String show(){ return username; } }
|