在 Spring Cloud 中,微服务启动会加载 “服务名-profile.yaml”、“服务名.yaml” 配置文件,其中:
(1)“服务名-profile.yaml” 配置文件用于特定环境,如:开发环境 dev
(2)“服务名.yaml” 配置文件为默认配置,可以多环境共享
那么它们之间的优先级是怎么样的呢?
“服务名-profile.yaml” -> “服务名.yaml” -> 本地配置
“服务名-profile.yaml” 优先级最高,本地配置优先级最低,如下图:
这里创建三个配置文件,一个公共配置,一个开发环境配置和一个生产环境配置,如下:
(1)nacos_spring_cloud_demo-prod.yaml 生产环境配置
demo: title1: title1-prod
(2)nacos_spring_cloud_demo.yaml 公共配置
demo: title1: title1 title2: title2
(3)application.yaml 本地配置,在应用的 resources 目录下创建 application.yaml 文件
demo: title1: title1-local title2: title2-local title3: title3-local
如下图:
server: port: 8080 spring: application: # 应用名称,用来匹配 Nacos 中的配置集 # 默认将根据下面指定的 spring.cloud.config.namespace + spring.cloud.config.group + spring.application.name + file-extension # 进行匹配,例如:当前应用将匹配 # 8719efd1-94a6-49f7-9846-2debd66f6c0f 命名空间中 DEFAULT_GROUP 分组下 # 名为 nacos_spring_cloud 且格式为 yaml 的配置集 name: nacos_spring_cloud_demo cloud: nacos: config: # Nacos 服务地址 server-addr: 127.0.0.1:8848 # Nacos 配置文件扩展名 file-extension: yaml # Nacos 命名空间ID namespace: 8719efd1-94a6-49f7-9846-2debd66f6c0f # Nacos 分组 group: DEFAULT_GROUP # Nacos 登录账号 username: nacos # Nacos 登录密码 password: nacos
<?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"> <modelVersion>4.0.0</modelVersion> <parent> <groupId>com.hxstrive.nacos</groupId> <artifactId>springcloud_alibaba_nacos</artifactId> <version>1.0-SNAPSHOT</version> </parent> <artifactId>nacos_spring_cloud_demo</artifactId> <name>nacos_spring_cloud_demo</name> <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <maven.compiler.source>1.8</maven.compiler.source> <maven.compiler.target>1.8</maven.compiler.target> </properties> <dependencyManagement> <dependencies> <dependency> <groupId>com.alibaba.cloud</groupId> <artifactId>spring-cloud-alibaba-dependencies</artifactId> <version>2.1.2.RELEASE</version> <type>pom</type> <scope>import</scope> </dependency> <!-- https://mvnrepository.com/artifact/org.springframework.cloud/spring-cloud-dependencies --> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-dependencies</artifactId> <version>Greenwich.RELEASE</version> <type>pom</type> <scope>import</scope> </dependency> <!-- https://mvnrepository.com/artifact/org.springframework.boot/spring-boot-dependencies --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-dependencies</artifactId> <version>2.1.3.RELEASE</version> <type>pom</type> <scope>import</scope> </dependency> </dependencies> </dependencyManagement> <dependencies> <!-- Spring Boot Begin --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-actuator</artifactId> </dependency> <!-- @ConfigurationProperties 需要 --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-configuration-processor</artifactId> <optional>true</optional> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> <!-- Spring Boot End --> <!-- Spring Cloud Begin --> <dependency> <groupId>com.alibaba.cloud</groupId> <artifactId>spring-cloud-starter-alibaba-nacos-config</artifactId> </dependency> <!-- Spring Cloud End --> </dependencies> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> </build> </project>
package com.hxstrive.nacos; import org.springframework.beans.factory.annotation.Value; import org.springframework.cloud.context.config.annotation.RefreshScope; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.RestController; import java.util.HashMap; import java.util.Map; /** * 配置优先级 * @author hxstrive.com */ @RestController @RefreshScope public class Demo4 { @Value("${demo.title1}") private String title1; @Value("${demo.title2}") private String title2; @Value("${demo.title3}") private String title3; @GetMapping("/demo4") public Map<String,Object> demo4() { return new HashMap<String,Object>(){ { put("title1", title1); put("title2", title2); put("title3", title3); } }; } }
启动应用,访问 http://localhost:8080/demo4 地址,输出如下图:
由上图可知:
(1)title1 配置同时存在于 nacos_spring_cloud_demo-prod.yaml、nacos_spring_cloud_demo.yaml 和本地配置中,而上图中 title1 的值为 “title1-prod”,因此 nacos_spring_cloud_demo-prod.yaml 优先级最高,它覆盖了nacos_spring_cloud_demo.yaml 和本地配置。
(2)title2 配置同时存在于 nacos_spring_cloud_demo.yaml 和本地配置中,而上图中 title2 的值为 “title2”,因此 nacos_spring_cloud_demo.yaml 优先了本地配置。
(3)title3 配置仅存在于本地配置中。