多环境配置共享指在多种环境下(如:开发环境、测试环境、生产环境)下共用一个配置,这样可以减少配置量,如:应用名称、端口等等信息。
在项目开发中,微服务启动时会从 nacos 读取多个配置文件,分别如下:
(1)[spring.application.name]-[spring.profiles.active].yaml,例如:userservice-dev.yaml
(2)[spring.application.name].yaml,例如:userservice.yaml
注意:无论应用的 profile 如何变化,[spring.application.name].yaml 配置文件一定会被加载,因此多环境共享配置可以写入这个配置文件。
这里创建三个配置文件,一个公共配置,一个开发环境配置和一个生产环境配置,如下:
(1)nacos_spring_cloud_demo.yaml 公共配置
demo: title: nacos_spring_cloud_demo
(2)nacos_spring_cloud_demo-prod.yaml 生产环境配置
demo: multiEnValue: nacos_spring_cloud_demo-prod
(3)nacos_spring_cloud_demo-dev.yaml 开发环境配置
demo: multiEnValue: nacos_spring_cloud_demo-dev
如下图:
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 Demo3 { @Value("${demo.title}") private String title; @Value("${demo.multiEnValue}") private String multiEnValue; @GetMapping("/demo3") public Map<String,Object> demo3() { return new HashMap<String,Object>(){ { put("title", title); put("multiEnValue", multiEnValue); } }; } public void setTitle(String title) { this.title = title; } }
将 spring.profiles.active 设置为 dev,如下图:
启动应用,访问服务,输出如下图:
从上图输出可知,成功获取了共享配置,并且获取了 dev 环境的特有配置。
进而,再将 spring.profiles.active 设置为 prod,如下图:
启动应用,访问服务,输出如下图: