前言
首先放上官网 官网, 一切以官方为准。
对于springboot多环境配置文件。可以在nacos中用命名空间 namespace
的概念区分。不同的业务隔离,用 group
分组。然后每个业务用 dataId
标识。
配置中心应用
项目参考 SpringBoot多环境打包 中的配置文件。
pom.xml配置文件
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 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142
| <?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 https://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>cn.idea360</groupId> <artifactId>idea360-kafka-spring-boot-demo</artifactId> <version>0.0.1</version> <name>dea360-kafka-spring-boot-demo</name> <description>Demo project for Spring Boot</description>
<properties> <java.version>1.8</java.version> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding> <spring-boot.version>2.3.7.RELEASE</spring-boot.version> </properties>
<dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency>
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> <exclusions> <exclusion> <groupId>org.junit.vintage</groupId> <artifactId>junit-vintage-engine</artifactId> </exclusion> </exclusions> </dependency> <dependency> <groupId>com.alibaba.cloud</groupId> <artifactId>spring-cloud-starter-alibaba-nacos-config</artifactId> <version>2.2.3.RELEASE</version> </dependency>
</dependencies>
<dependencyManagement> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-dependencies</artifactId> <version>${spring-boot.version}</version> <type>pom</type> <scope>import</scope> </dependency> </dependencies> </dependencyManagement>
<profiles> <profile> <id>dev</id> <activation> <activeByDefault>true</activeByDefault> </activation> <properties> <profileActive>dev</profileActive> </properties> </profile> <profile> <id>test</id> <properties> <profileActive>test</profileActive> </properties> </profile> <profile> <id>prod</id> <properties> <profileActive>prod</profileActive> </properties> </profile> </profiles>
<build> <finalName>${project.artifactId}</finalName> <resources> <resource> <directory>src/main/resources</directory> <filtering>true</filtering> <includes> <include>bootstrap.yml</include> <include>application.yml</include> <include>application-${profileActive}.yml</include> <include>logback-spring.xml</include> </includes> </resource> </resources> <plugins> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-compiler-plugin</artifactId> <version>3.8.1</version> <configuration> <source>1.8</source> <target>1.8</target> <encoding>UTF-8</encoding> </configuration> </plugin> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> <version>2.3.7.RELEASE</version> <configuration> <mainClass>cn.idea360.springdemo.SpringDemoApplication</mainClass> </configuration> <executions> <execution> <id>repackage</id> <goals> <goal>repackage</goal> </goals> </execution> </executions> </plugin> <plugin> <artifactId>maven-assembly-plugin</artifactId> <version>3.1.0</version> <configuration> <descriptors> <descriptor>src/main/assembly/assembly.xml</descriptor> </descriptors> </configuration> <executions> <execution> <id>make-assembly</id> <phase>package</phase> <goals> <goal>single</goal> </goals> </execution> </executions> </plugin> </plugins> </build>
</project>
|
nacos后台配置
- 登录 http://localhost:8848/nacos/ , 创建dev命名空间
- 创建配置文件
在 Nacos Spring Cloud 中,dataId 的完整格式如下:
1
| ${prefix}-${spring.profiles.active}.${file-extension}
|
- prefix 默认为 spring.application.name 的值,也可以通过配置项 spring.cloud.nacos.config.prefix来配置。
- spring.profiles.active 即为当前环境对应的 profile。 注意:当 spring.profiles.active 为空时,对应的连接符 - 也将不存在,dataId 的拼接格式变成 prefix.{file-extension}
- file-exetension 为配置内容的数据格式,可以通过配置项 spring.cloud.nacos.config.file-extension 来配置。目前只支持 properties 和 yaml 类型。
- 编辑配置文件
类型选择yaml, 编辑配置信息
spring配置文件
bootstrap.yml
namespace和server-addr需要在bootstrap.yml中配置,如果多环境可以在pom.xml中的
中配置
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
| <profiles> <profile> <id>dev</id> <activation> <activeByDefault>true</activeByDefault> </activation> <properties> <profileActive>dev</profileActive> <nacos.servers>127.0.0.1:8848</nacos.servers> <nacos.namespace>2448da93-0835-420b-ab0f-c2af5f21af03</nacos.namespace> </properties> </profile> <profile> <id>test</id> <properties> <profileActive>test</profileActive> <nacos.servers>127.0.0.1:8848</nacos.servers> <nacos.namespace>493bd17c-ffb4-4d28-bc62-6d2ce583ecfe</nacos.namespace> </properties> </profile> <profile> <id>prod</id> <properties> <profileActive>prod</profileActive> <nacos.servers>127.0.0.1:8848</nacos.servers> <nacos.namespace>c0d5d90e-d69f-4ac4-a6b0-ba6e72e03082</nacos.namespace> </properties> </profile> </profiles>
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
| spring: application: name: idea360-kafka-spring-boot-demo profiles: active: @profileActive@ cloud: nacos: config: server-addr: 127.0.0.1:8848 namespace: 2448da93-0835-420b-ab0f-c2af5f21af03 group: DEFAULT_GROUP file-extension: yaml logging: level: root: debug
|
application-dev.yml
代码中读取配置文件
1 2 3 4
| @Autowired private Environment environment;
String property = environment.getProperty("spring.profiles.active");
|
最后
本文到此结束,感谢阅读。如果您觉得不错,请关注公众号【当我遇上你】,您的支持是我写作的最大动力。