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

边栏推荐
- 【Tera Term】黑猫带你学TTL脚本——嵌入式开发中串口自动化神技能
- 【LeetCode】Day96-第一个唯一字符&赎金信&字母异位词
- J'ai un chaton.
- 【论文阅读】NFlowJS:基于鲁棒学习的合成负数据密集异常检测
- 假设检验学习笔记
- 单元测试的意义
- Significance of unit testing
- How to recover Huawei router's forgotten password
- 曼哈顿距离与曼哈顿矩形-打印回字型矩阵
- VINS-Mono: A Robust and Versatile Monocular Visual-Inertial State Estimator
猜你喜欢
随机推荐
公司视频加速播放
(中)苹果有开源,但又怎样呢?
Title 1093: character reverse order
如何在业务代码中使用 ThinkPHP5.1 封装的容器内反射方法
进程和线程的理解
The ECU of 21 Audi q5l 45tfsi brushes is upgraded to master special adjustment, and the horsepower is safely and stably increased to 305 horsepower
HCIA复习
Basic knowledge of error
The ECU of 21 Audi q5l 45tfsi brushes is upgraded to master special adjustment, and the horsepower is safely and stably increased to 305 horsepower
properties文件
Nodejs realizes the third-party login of Weibo
Linux regularly backs up MySQL database
【论文阅读】NFlowJS:基于鲁棒学习的合成负数据密集异常检测
A complete collection of necessary learning websites for office programmers
黑猫带你学UFS协议第8篇:UFS初始化详解(Boot Operation)
Buuctf-[bjdctf2020]zjctf, but so (xiaoyute detailed explanation)
Leaflet map
Baidu online AI competition - image processing challenge: the 8th program of handwriting erasure
Web service connector: Servlet
公司視頻加速播放



![[paper reading] nflowjs: synthetic negative data intensive anomaly detection based on robust learning](/img/9c/2753f68ecec3555aaca23800dada1e.png)





![[ram IP] introduction and experiment of ram IP core](/img/34/1c988456e32a8e9840d1d073caefbf.jpg)