用了 3 年 Apollo,最后我选择了 Nacos,原因不多说了
GitHub笔记 文章源自JAVA秀-https://www.javaxiu.com/10108.html
作者:废物大师兄
cnblogs.com/cjsblog/p/14007311.html文章源自JAVA秀-https://www.javaxiu.com/10108.html
本文探讨Nacos作为配置中心,如何实现不同环境(开发、测试、灰度、正式)的配置管理问题文章源自JAVA秀-https://www.javaxiu.com/10108.html
就像Maven用groupId、artifactId、version三者来定位jar包在仓库中的位置一样,Nacos也提供了 Namespace (命名空间) 、Data ID (配置集ID)、 Group (组) 来确定一个配置文件(或者叫配置集)文章源自JAVA秀-https://www.javaxiu.com/10108.html
由此,实现多环境配置的方案也有三种:文章源自JAVA秀-https://www.javaxiu.com/10108.html
1、用命名空间(namespace)来区分不同的环境,一个命名空间对应一个环境;文章源自JAVA秀-https://www.javaxiu.com/10108.html
2、用配置组(group)来区分不同的环境,命名空间用默认的public即可,一个组对应一种环境;文章源自JAVA秀-https://www.javaxiu.com/10108.html
3、用配置集ID(Data ID)名称来区分不同的环境,命名空间和组用默认的即可,通过文件命名来区分;文章源自JAVA秀-https://www.javaxiu.com/10108.html
接下来,逐个来看文章源自JAVA秀-https://www.javaxiu.com/10108.html
http://{host}:{port}/nacos文章源自JAVA秀-https://www.javaxiu.com/10108.html
http://{host}:{port}/nacos/index.html文章源自JAVA秀-https://www.javaxiu.com/10108.html
默认用户名密码都是nacos文章源自JAVA秀-https://www.javaxiu.com/10108.html
文章源自JAVA秀-https://www.javaxiu.com/10108.html
为了方便演示,这里建了一个名为example的Spring Boot项目文章源自JAVA秀-https://www.javaxiu.com/10108.html
文章源自JAVA秀-https://www.javaxiu.com/10108.html
pom.xml文章源自JAVA秀-https://www.javaxiu.com/10108.html
<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.0modelVersion> <parent> <groupId>org.springframework.bootgroupId> <artifactId>spring-boot-starter-parentartifactId> <version>2.3.6.RELEASEversion> <relativePath/> parent> <groupId>com.examplegroupId> <artifactId>exampleartifactId> <version>0.0.1-SNAPSHOTversion> <name>examplename> <properties> <java.version>1.8java.version> <spring-cloud-alibaba.version>2.2.3.RELEASEspring-cloud-alibaba.version> properties> <dependencies> <dependency> <groupId>org.springframework.bootgroupId> <artifactId>spring-boot-starter-webartifactId> dependency> <dependency> <groupId>com.alibaba.cloudgroupId> <artifactId>spring-cloud-starter-alibaba-nacos-configartifactId> dependency> dependencies> <dependencyManagement> <dependencies> <dependency> <groupId>com.alibaba.cloudgroupId> <artifactId>spring-cloud-alibaba-dependenciesartifactId> <version>${spring-cloud-alibaba.version}version> <type>pomtype> <scope>importscope> dependency> dependencies> dependencyManagement> <build> <plugins> <plugin> <groupId>org.springframework.bootgroupId> <artifactId>spring-boot-maven-pluginartifactId> plugin> plugins> build>project>
bootstrap.yml文章源自JAVA秀-https://www.javaxiu.com/10108.html
spring: application: name: example cloud: nacos: config: server-addr: 192.168.100.10:8848 file-extension: yaml
HelloController.java文章源自JAVA秀-https://www.javaxiu.com/10108.html
package com.example.controller;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.RequestMapping;import org.springframework.web.bind.annotation.RestController;/** * @author ChengJianSheng */@RestController@RequestMapping("/hello")@RefreshScopepublic class HelloController { @Value("${greet.hello}") private String greet; @GetMapping("/sayHi") public String sayHi() { return greet; }}
1. 利用 Data ID 命名 来区分环境
利用Data ID命名来区分环境,命名空间和组默认即可文章源自JAVA秀-https://www.javaxiu.com/10108.html
在 Nacos Spring Cloud 中,dataId 的完整格式如下:文章源自JAVA秀-https://www.javaxiu.com/10108.html
${prefix}-${spring.profiles.active}.${file-extension}
prefix 默认为 spring.application.name 的值,也可以通过配置项 spring.cloud.nacos.config.prefix来配置文章源自JAVA秀-https://www.javaxiu.com/10108.html
spring.profiles.active 即为当前环境对应的 profile,详情可以参考 Spring Boot文档。注意:当 spring.profiles.active 为空时,对应的连接符 - 也将不存在,dataId 的拼接格式变成
${prefix}.${file-extension}
文章源自JAVA秀-https://www.javaxiu.com/10108.htmlfile-exetension 为配置内容的数据格式,可以通过配置项 spring.cloud.nacos.config.file-extension 来配置。目前只支持 properties 和 yaml 类型。文章源自JAVA秀-https://www.javaxiu.com/10108.html
文章源自JAVA秀-https://www.javaxiu.com/10108.html
文章源自JAVA秀-https://www.javaxiu.com/10108.html
文章源自JAVA秀-https://www.javaxiu.com/10108.html
文章源自JAVA秀-https://www.javaxiu.com/10108.html
文章源自JAVA秀-https://www.javaxiu.com/10108.html
用命令行启动也是一样的文章源自JAVA秀-https://www.javaxiu.com/10108.html
文章源自JAVA秀-https://www.javaxiu.com/10108.html
例如:文章源自JAVA秀-https://www.javaxiu.com/10108.html
java -Dspring.profiles.active=test -jar example-0.0.1-SNAPSHOT.jar
文章源自JAVA秀-https://www.javaxiu.com/10108.html
2. 利用 Group 来区分环境
项目不变,我们把spring.application.name改成example2文章源自JAVA秀-https://www.javaxiu.com/10108.html
命名空间用默认的public文章源自JAVA秀-https://www.javaxiu.com/10108.html
文章源自JAVA秀-https://www.javaxiu.com/10108.html
文章源自JAVA秀-https://www.javaxiu.com/10108.html
文章源自JAVA秀-https://www.javaxiu.com/10108.html
java -Dspring.cloud.nacos.config.group=DEV_GROUP -jar example-0.0.1-SNAPSHOT.jar
文章源自JAVA秀-https://www.javaxiu.com/10108.html
java -Dspring.cloud.nacos.config.group=TEST_GROUP -jar example-0.0.1-SNAPSHOT.jar
文章源自JAVA秀-https://www.javaxiu.com/10108.html
java -Dspring.profiles.active=test -Dspring.cloud.nacos.config.group=TEST_GROUP -jar example-0.0.1-SNAPSHOT.jar
如果是这样的话,这个时候,Data ID 命名就应该是 example2-test.yaml文章源自JAVA秀-https://www.javaxiu.com/10108.html
3. 利用 Namespace 区分环境
文章源自JAVA秀-https://www.javaxiu.com/10108.html
创建命名空间的时候,如果不指定ID,则自动生成的id就是这样的uuid字符串,我们还是自己指定一个有意义的ID吧文章源自JAVA秀-https://www.javaxiu.com/10108.html
文章源自JAVA秀-https://www.javaxiu.com/10108.html
文章源自JAVA秀-https://www.javaxiu.com/10108.html
文章源自JAVA秀-https://www.javaxiu.com/10108.html
文章源自JAVA秀-https://www.javaxiu.com/10108.html
文章源自JAVA秀-https://www.javaxiu.com/10108.html
文章源自JAVA秀-https://www.javaxiu.com/10108.html
文章源自JAVA秀-https://www.javaxiu.com/10108.html
java -Dspring.cloud.nacos.config.namespace=ns_dev -jar example-0.0.1-SNAPSHOT.jar
文章源自JAVA秀-https://www.javaxiu.com/10108.html
java -Dspring.profiles.active=dev -Dspring.cloud.nacos.config.namespace=ns_dev -jar example-0.0.1-SNAPSHOT.jar
文章源自JAVA秀-https://www.javaxiu.com/10108.html
java -Dspring.cloud.nacos.config.namespace=ns_test -jar example-0.0.1-SNAPSHOT.jar
文章源自JAVA秀-https://www.javaxiu.com/10108.html
java -Dspring.profiles.active=test \ -Dspring.cloud.nacos.config.namespace=ns\_test \ -Dspring.cloud.nacos.config.group=TEST\_GROUP \ -jar example-0.0.1-SNAPSHOT.jar
文章源自JAVA秀-https://www.javaxiu.com/10108.html
4. 小结
第一种,用 Data ID 区分环境,虽然简单,但是每个项目要创建4个配置文件,随着项目的增多,都在一个命名空间下回显得很混乱,查找起来也不是很方便,而且不利于做权限控制文章源自JAVA秀-https://www.javaxiu.com/10108.html
第二种,用Group区分,问题也是一样的文章源自JAVA秀-https://www.javaxiu.com/10108.html
综上,最好的是用Namespace区分环境,清晰明了,而且有利于做权限控制文章源自JAVA秀-https://www.javaxiu.com/10108.html
https://nacos.io/zh-cn/docs/concepts.html文章源自JAVA秀-https://www.javaxiu.com/10108.html

评论