当前位置:网站首页>使用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上的某个配置文件修改优先级

边栏推荐
- Testing and debugging of multithreaded applications
- Wib3.0 leapfrogging, in leapfrogging (ง • ̀_•́) ง
- 2022 software testing workflow to know
- GTSAM中李群的運用
- Overview of three core areas of Mathematics: algebra
- Nodejs realizes the third-party login of Weibo
- 假设检验学习笔记
- 数学三大核心领域概述:几何
- Cognitive introspection
- [course notes] Compilation Principle
猜你喜欢
随机推荐
【论文代码】SML部分代码阅读
ContentType的作用
Function of activation function
leaflet 地图
黑猫带你学eMMC协议第10篇:eMMC读写操作详解(read & write)
Hongliao Technology: Liu qiangdong's "heavy hand"
[postman] test script writing and assertion details
GTSAM中李群的運用
OSPF configuration command of Huawei equipment
HCIA review
Thoughts on data security (Reprint)
[wechat applet] build a development tool environment
MPLS test report
The latest 2022 review of "graph classification research"
曼哈顿距离和-打印菱形
黑猫带你学UFS协议第8篇:UFS初始化详解(Boot Operation)
LeetCode 732. 我的日程安排表 III
H3C V7 switch configuration IRF
Testing and debugging of multithreaded applications
How to recover Huawei router's forgotten password
![[Thesis code] SML part code reading](/img/3c/0deccf499d9b1cbe30a302cb115d73.png)








