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

边栏推荐
- MIT6.s081-2020 Lab2 System Calls
- LeetCode 732. 我的日程安排表 III
- ESP32 ESP-IDF看门狗TWDT
- Title 1093: character reverse order
- H3C S5820V2_ Upgrade method after stacking IRF2 of 5830v2 switch
- How to use the container reflection method encapsulated by thinkphp5.1 in business code
- [leetcode] day96 - the first unique character & ransom letter & letter ectopic word
- 【C语言】字符串左旋
- 【Postman】动态变量(也称Mock函数)
- ContentType的作用
猜你喜欢

Basic knowledge of error

【Tera Term】黑猫带你学TTL脚本——嵌入式开发中串口自动化神技能

About PHP startup, mongodb cannot find the specified module

Sqlmap tutorial (III) practical skills II

【Postman】Collections-运行配置之导入数据文件

Fault, error, failure of functional safety

二维码的前世今生 与 六大测试点梳理

进程和线程的理解

曼哈顿距离和-打印菱形

Expose the serial fraudster Liu Qing in the currency circle, and default hundreds of millions of Cheng Laolai
随机推荐
Huawei BFD configuration specification
曼哈顿距离与曼哈顿矩形-打印回字型矩阵
Function of contenttype
Reading notes of effective managers
Thoughts on data security (Reprint)
Overview of three core areas of Mathematics: algebra
Buuctf-[gxyctf2019] no dolls (xiaoyute detailed explanation)
Fault, error, failure of functional safety
Title 1093: character reverse order
ContentType的作用
10m25dcf484c8g (FPGA) amy-6m-0002 BGA GPS module
nodejs实现微博第三方登录
HCIA review
Sqlmap tutorial (III) practical skills II
C language bubble sort
[postman] the monitors monitoring API can run periodically
Buuctf-[bjdctf2020]zjctf, but so (xiaoyute detailed explanation)
多线程应用的测试与调试
Usage of test macro of GTEST
Analysis report on development trends and investment planning of China's methanol industry from 2022 to 2028