当前位置:网站首页>使用Nacos管理配置
使用Nacos管理配置
2022-07-06 06:08:00 【雪峰.贵】
文章目录
一、加依赖
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-nacos-config</artifactId>
</dependency>
二、约定
箭头连接的两端要一致,微服务里是一个bootstrap.yml,对应Nacos上多个指定环境的配置。微服务启动时指定active,就会去Nacos上找相应环境的配置。
三、bootstrap.yml
spring:
cloud:
nacos:
config:
server-addr: 127.0.0.1:8848
file-extension: yaml
application:
name: content-center
profiles:
active: dev
四、加配置
此时这个配置就已经配置到了开发环境
五、业务代码取配置
@Value("${your.config}")
private String config;
六、@RefreshScope
在获取配置的类上加上注解,就能动态的获取Nacos配置.
七、回滚配置

八、一个微服务通用配置
1. 所有环境的通用配置写在:{微服务名}.yaml中
2.yaml的优先级:
指定profile的yaml > {微服务名}.yaml
九、不同微服务的配置共享
ext-config方式
spring:
cloud:
nacos:
config:
server-addr: 127.0.0.1:8848
file-extension: yaml
ext-config:
# 需共享的DataId,yaml后缀不能少,只支持yaml/properties
# 越靠后,优先级越高 优先级common2.yaml > common1.yaml
- data-id: common1.yaml
# common1.yaml所在的group
group: DEFAULT_GROUP
# 是否允许刷新,默认false
refresh: true
- data-id: common2.yaml
group: DEFAULT_GROUP
refresh: true
application:
name: content-center
profiles:
active: dev
shared-dataids方式
spring:
cloud:
nacos:
config:
# 共享配置的DataId,多个使用,分隔
# 越靠后,优先级越高;common2.yml > common1.yaml
# .yaml后缀不能少,只支持yaml/properties
shared-dataids: common1.yaml,common2.yaml
# 哪些共享配置支持动态刷新,多个使用,分隔
refreshable-dataids: common1.yaml
server-addr: 127.0.0.1:8848
file-extension: yaml
application:
name: content-center
profiles:
active: dev
只需将两种方式的代码复制到bootstrap里,并在Nacos上相应的组里配置common1.yaml,common2.yaml即可。
十、远程配置与本地配置优先级
默认Nacos上的配置>application.yml & bootstrap.yml
也可在Nacos上的某个配置文件修改优先级

边栏推荐
- Expose the serial fraudster Liu Qing in the currency circle, and default hundreds of millions of Cheng Laolai
- LeetCode 1200. 最小绝对差
- 【Postman】动态变量(也称Mock函数)
- HCIA复习
- Luogu p1460 [usaco2.1] healthy Holstein cows
- Hongliao Technology: Liu qiangdong's "heavy hand"
- 曼哈顿距离与曼哈顿矩形-打印回字型矩阵
- Buuctf-[bjdctf2020]zjctf, but so (xiaoyute detailed explanation)
- 黑猫带你学eMMC协议第10篇:eMMC读写操作详解(read & write)
- 【无标题】
猜你喜欢

Investment strategy discussion and market scale prediction report of China's solid state high power amplifier industry from 2022 to 2028

【Postman】Monitors 监测API可定时周期运行

Hongliao Technology: Liu qiangdong's "heavy hand"
![[postman] test script writing and assertion details](/img/65/6520fe78bb2b3ff99f16d09ea8c0d1.png)
[postman] test script writing and assertion details

【Postman】Collections配置运行过程

单元测试的意义

曼哈顿距离与曼哈顿矩形-打印回字型矩阵

ESP32 ESP-IDF看门狗TWDT

黑猫带你学UFS协议第4篇:UFS协议栈详解

【论文代码】SML部分代码阅读
随机推荐
[untitled]
Application du Groupe Li dans gtsam
异常检测方法总结
【Postman】Monitors 监测API可定时周期运行
Réflexions sur la sécurité des données (réimpression)
[happy Spring Festival] if you feel happy, dance
Eigen稀疏矩阵操作
[postman] dynamic variable (also known as mock function)
黑猫带你学eMMC协议第10篇:eMMC读写操作详解(read & write)
误差的基本知识
SQLMAP使用教程(三)实战技巧二
H3C V7 switch configuration IRF
【微信小程序】搭建开发工具环境
Redis6 cluster setup
Significance of unit testing
Analysis report on development trends and investment planning of China's methanol industry from 2022 to 2028
黑猫带你学UFS协议第8篇:UFS初始化详解(Boot Operation)
Testing and debugging of multithreaded applications
【Postman】测试(Tests)脚本编写和断言详解
Sqlmap tutorial (III) practical skills II