https://nacos.io/zh-cn/docs/concepts.html
在系统开发过程中通常会将一些需要变更的参数、变量等从代码中分离出来独立管理,以独立的配置文件的形式存在。目的是让静态的系统工件或者交付物(如 WAR,JAR 包等)更好地和实际的物理运行环境进行适配。配置管理一般包含在系统部署的过程中,由系统管理员或者运维人员完成这个步骤。配置变更是调整系统运行时的行为的有效手段之一。
在服务或者应用运行过程中,提供动态配置或者元数据以及配置管理的服务提供者。
在数据中心中,系统中所有配置的编辑、存储、分发、变更管理、历史版本管理、变更审计等所有与配置相关的活动统称为配置管理。
一个具体的可配置的参数与其值域,通常以 param-key=param-value 的形式存在。例如我们常配置系统的日志输出级别(logLevel=INFO|WARN|ERROR) 就是一个配置项。
一组相关或者不相关的配置项的集合称为配置集。在系统中,一个配置文件通常就是一个配置集,包含了系统各个方面的配置。例如,一个配置集可能包含了数据源、线程池、日志级别等配置项。
Nacos 中的某个配置集的 ID。配置集 ID 是组织划分配置的维度之一。Data ID 通常用于组织划分系统的配置集。一个系统或者应用可以包含多个配置集,每个配置集都可以被一个有意义的名称标识。Data ID 通常采用类 Java 包(如 com.taobao.tc.refund.log.level)的命名规则保证全局唯一性。此命名规则非强制。
Nacos 中的一组配置集,是组织配置的维度之一。通过一个有意义的字符串(如 Buy 或 Trade )对配置集进行分组,从而区分 Data ID 相同的配置集。当您在 Nacos 上创建一个配置时,如果未填写配置分组的名称,则配置分组的名称默认采用 DEFAULT_GROUP 。配置分组的常见场景:不同的应用或组件使用了相同的配置类型,如 database_url 配置和 MQ_topic 配置。
Nacos 的客户端 SDK 会在本地生成配置的快照。当客户端无法连接到 Nacos Server 时,可以使用配置快照显示系统的整体容灾能力。配置快照类似于 Git 中的本地 commit,也类似于缓存,会在适当的时机更新,但是并没有缓存过期(expiration)的概念。
我们看看上图,由几个问题要解决
我们建立一个工程 artisan-cloud-nacos-config ,假设这个工程 要从配置中心上拉取配置 。
(不要被这个工程的名字给迷惑了,其实就是我们普通的微服务,比如ORDER 、PRODUCT服务要接入配置中,就需要这么搞)
<dependency>
<groupId>com.alibaba.cloudgroupId>
<artifactId>spring-cloud-alibaba-nacos-discoveryartifactId>
dependency>
<dependency>
<groupId>com.alibaba.cloudgroupId>
<artifactId>spring-cloud-alibaba-nacos-configartifactId>
dependency>
编写配置文件,需要写一个bootstrap.yml配置文件
spring:
cloud:
nacos:
config: # 这里是 config , 服务注册的这个地方是 discovery
server-addr: 1.117.97.88:8848
file-extension: yml
application: # 表示当前微服务需要向配置中心索要artisan-config-center的配置
name: artisan-config-center
profiles: # 表示我需要向配置中心索要artisan-config-center的开发环境的配置
active: dev
server-addr: 1.117.97.88:8848
表示我微服务怎么去找我的配置中心spring.application.name=artisan-config-center
表示当前微服务需要向配置中心索要artisan-config-center的配置spring.profiles.active=dev
表示我需要向配置中心索要artisan-config-center的dev环境的配置索要文件的格式为
${application.name}- ${spring.profiles.active}.${file-extension}
同样的,我们这个服务也要注册到Nacos
新建配置
启动服务,观察日志
2022-02-04 12:37:50.945 INFO 14772 --- [ main] c.a.c.n.c.NacosPropertySourceBuilder : Loading nacos data, dataId: 'artisan-config-center-dev.yml', group: 'DEFAULT_GROUP', data: server:
port: 5678
2022-02-04 12:37:50.948 INFO 14772 --- [ main] b.c.PropertySourceBootstrapConfiguration : Located property source: CompositePropertySource {name='NACOS', propertySources=[NacosPropertySource {name='artisan-config-center-dev.yml'}, NacosPropertySource {name='artisan-config-center.yml'}, NacosPropertySource {name='artisan-config-center'}]}
我们有个新业务上线, 业务需要,保留原来老逻辑的代码,所有我们打算搞个一个开关变isNewPath
来控制,为了以防万一,如果新功能上了生产后,发现有bug,怎么做到修改isNewBusi的值不需要重启 ?
在上个工程的基础上,我们来增加这个功能
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
@RefreshScope
@Slf4j
public class OrderInfoController {
@Value("${isNewPath}")
private Boolean isNewPath;
@RequestMapping("/selectOrderInfoById/{orderNo}")
public Object selectOrderInfoById(@PathVariable("orderNo") String orderNo) {
log.info("是否业务走新逻辑:{}", isNewPath);
if (isNewPath) {
return "查询订单执行新逻辑->execute new logic : " + orderNo;
}
return "查询订单执行老逻辑->execute old logic : " + orderNo;
}
}
启动服务,观察拉取的配置中心的配置项
2022-02-04 13:30:27.453 INFO 21040 --- [ main] c.a.c.n.c.NacosPropertySourceBuilder : Loading nacos data, dataId: 'artisan-config-center-dev.yml', group: 'DEFAULT_GROUP', data: server:
port: 5678
# 是否走新业务逻辑的开关
isNewPath: true
我们看到是true , 访问下试试呢
Nacos Config上修改值为false ,观察日志,重新测试
看下应用的日志
2022-02-04 13:32:29.234 INFO 21040 --- [.117.97.88_8848] o.s.cloud.commons.util.InetUtils : Cannot determine local hostname
2022-02-04 13:32:29.273 INFO 21040 --- [.117.97.88_8848] trationDelegate$BeanPostProcessorChecker : Bean 'org.springframework.cloud.autoconfigure.ConfigurationPropertiesRebinderAutoConfiguration' of type [org.springframework.cloud.autoconfigure.ConfigurationPropertiesRebinderAutoConfiguration$$EnhancerBySpringCGLIB$$de53a9bf] is not eligible for getting processed by all BeanPostProcessors (for example: not eligible for auto-proxying)
2022-02-04 13:32:30.791 INFO 21040 --- [.117.97.88_8848] o.s.cloud.commons.util.InetUtils : Cannot determine local hostname
2022-02-04 13:32:30.884 WARN 21040 --- [.117.97.88_8848] c.a.c.n.c.NacosPropertySourceBuilder : Ignore the empty nacos configuration and get it based on dataId[artisan-config-center] & group[DEFAULT_GROUP]
2022-02-04 13:32:30.952 WARN 21040 --- [.117.97.88_8848] c.a.c.n.c.NacosPropertySourceBuilder : Ignore the empty nacos configuration and get it based on dataId[artisan-config-center.yml] & group[DEFAULT_GROUP]
2022-02-04 13:32:31.012 INFO 21040 --- [.117.97.88_8848] c.a.c.n.c.NacosPropertySourceBuilder : Loading nacos data, dataId: 'artisan-config-center-dev.yml', group: 'DEFAULT_GROUP', data: server:
port: 5678
# 是否走新业务逻辑的开关
isNewPath: false
2022-02-04 13:32:31.013 INFO 21040 --- [.117.97.88_8848] b.c.PropertySourceBootstrapConfiguration : Located property source: CompositePropertySource {name='NACOS', propertySources=[NacosPropertySource {name='artisan-config-center-dev.yml'}, NacosPropertySource {name='artisan-config-center.yml'}, NacosPropertySource {name='artisan-config-center'}]}
2022-02-04 13:32:31.014 INFO 21040 --- [.117.97.88_8848] o.s.boot.SpringApplication : The following profiles are active: dev
2022-02-04 13:32:31.023 INFO 21040 --- [.117.97.88_8848] o.s.boot.SpringApplication : Started application in 3.29 seconds (JVM running for 128.768)
2022-02-04 13:32:31.040 INFO 21040 --- [.117.97.88_8848] o.s.c.e.event.RefreshEventListener : Refresh keys changed: [isNewPath]
已经获取到了最新值 , isNewPath: false ,并且 Refresh keys changed: [isNewPath]
我们不重启应用,直接访问
咦,不停机走了老逻辑
扫码关注腾讯云开发者
领取腾讯云代金券
Copyright © 2013 - 2025 Tencent Cloud. All Rights Reserved. 腾讯云 版权所有
深圳市腾讯计算机系统有限公司 ICP备案/许可证号:粤B2-20090059 深公网安备号 44030502008569
腾讯云计算(北京)有限责任公司 京ICP证150476号 | 京ICP备11018762号 | 京公网安备号11010802020287
Copyright © 2013 - 2025 Tencent Cloud.
All Rights Reserved. 腾讯云 版权所有