当前位置:网站首页>微服务 - 配置中心 - Nacos
微服务 - 配置中心 - Nacos
2022-07-25 05:19:00 【从零开始的JAVA世界】
Nacos除了可以做注册中心,同样可以做配置管理来使用。
1. 统一配置管理
当微服务部署的实例越来越多,达到数十、数百时,逐个修改微服务配置就会让人抓狂,而且很容易出错。我们需要一种统一配置管理方案,可以集中管理所有实例的配置。

Nacos一方面可以将配置集中管理,另一方可以在配置变更时,及时通知微服务,实现配置的热更新。
1. 在nacos中添加配置文件


注意:项目的核心配置,需要热更新的配置才有放到nacos管理的必要。基本不会变更的一些配置还是保存在微服务本地比较好。
2. 微服务拉取配置
微服务要拉取nacos中管理的配置,并且与本地的application.yml配置合并,才能完成项目启动。
但如果尚未读取application.yml,又如何得知nacos地址呢?
因此spring引入了一种新的配置文件:bootstrap.yaml文件,会在application.yml之前被读取,流程如下:
![[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-DdCE5Af7-1654156812944)(assets/L0iFYNF.png)]](/img/58/3a99aed50f574c1c8696b4e7f071d1.png)
1)引入nacos-config依赖
首先,在user-service服务中,引入nacos-config的客户端依赖:
<!--nacos配置管理依赖-->
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-nacos-config</artifactId>
</dependency>
2)添加bootstrap.yaml
然后,在user-service中添加一个bootstrap.yaml文件,内容如下:
spring:
application:
name: userservice # 服务名称
profiles:
active: dev #开发环境,这里是dev
cloud:
nacos:
server-addr: localhost:8848 # Nacos地址
config:
file-extension: yaml # 文件后缀名
这里会根据spring.cloud.nacos.server-addr获取nacos地址,再根据
${spring.application.name}-${spring.profiles.active}.${spring.cloud.nacos.config.file-extension}作为文件id,来读取配置。
本例中,就是去读取userservice-dev.yaml:
![[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-bzaLoFXa-1654156812944)(assets/image-20210714170845901.png)]](/img/a8/12224969068ba1fafb18e89ce16642.png)
2. 配置热更新
修改nacos中的配置后,微服务中无需重启即可让配置生效,就是配置热更新。
要实现配置热更新,可以使用两种方式:
1. 方式一
在@Value注入的变量所在类上添加注解@RefreshScope:
这样nacos中的配置文件改变,@Value注入的变量也会从nacos配置实时更新。
2. 方式二
使用@ConfigurationProperties注解 读读配置。
新建一个类,读取配置文件属性: 

3. 配置共享
其实微服务启动时,会去nacos读取多个配置文件,例如:
[spring.application.name]-[spring.profiles.active].yaml,例如:userservice-dev.yaml[spring.application.name].yaml,例如:userservice.yaml
而[spring.application.name].yaml不包含环境,因此可以被多个环境共享。
添加一个环境共享配置
4. 配置的优先级
![[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-wyIhtKxQ-1654156812948)(assets/image-20210714174623557.png)]](/img/f7/d9558c81d00c119ed3793f34ad588c.png)
nacos中 带环境的配置 覆盖 共享配置 覆盖本地配置。
5. 多服务共享配置
方式一:
spring:
application:
name: userservice #服务id
profiles:
active: dev #环境
cloud:
nacos:
server-addr: localhost:8848 # Nacos地址
#配置中心
config:
file-extension: yaml # 文件后缀名
shared-configs: #多服务间共享的配置文件
-dateId: common.yaml
方式二:
spring:
application:
name: userservice #服务id
profiles:
active: dev #环境
cloud:
nacos:
server-addr: localhost:8848 # Nacos地址
#配置中心
config:
file-extension: yaml # 文件后缀名
extension-configs: #多服务间共享的配置文件
-dateId: common.yaml
优先级
边栏推荐
- 搭建私有CA服务器
- Docker builds MySQL master-slave replication
- What is virtual DOM? How to implement a virtual DOM
- 微服务 - 网关Gateway组件
- What about reinstalling win11 system?
- Xiaohongshu joins hands with HMS core to enjoy HD vision and grow grass for a better life
- How to judge whether it is attacked by DDoS
- Go language function
- rhce第一天
- 05. Libavformat Library of ffmpeg
猜你喜欢

Implement is by yourself_ base_ of

Thesis reading | which is the best multilingual pre training technology for machine translation? See the latest progress!
![[untitled]](/img/6c/df2ebb3e39d1e47b8dd74cfdddbb06.gif)
[untitled]

Li Kou 731. My schedule II

一篇文章带你读懂Redis的哨兵模式
![[globally unique ID] how to handle the ID primary key after dividing the database and table?](/img/a1/800ee868b74ce5d98956d4a0e2c075.png)
[globally unique ID] how to handle the ID primary key after dividing the database and table?

Preliminary understanding of Panda3D particle system

Solution of win11 blue screen code 0x0000001a

单点登录(一处登录,处处可用)

Browser cache HTTP cache CDN cache localstorage / sessionstorage / cookies
随机推荐
When we talk about immutable infrastructure, what are we talking about
Seven suggestions for Server Protection
Special analysis of data security construction in banking industry
Teach you how to locate unreasonable SQL? And optimize it
STL notes (VI): container vector
微服务 - 网关Gateway组件
ping命令
deep报错
Logu p3398 hamsters find sugar solution
typora+PicGo+阿里云OSS 搭建以及报错解决【转载】
Summary and Prospect of aut, the transport layer protocol of sound network -- dev for dev column
LCP插件创建对等物理接口
2022-07-24: what is the output of the following go language code? A:[]int{}; B:[]int(nil); C:panic; D: Compilation error. package main import ( “fmt“ ) f
教你三招从让性能从20s优化到500ms
基环树入门
rhcsa暑假第三天
Tips for downloading videos in batches
基于云原生的私有化 PaaS 平台交付实践
FinClip实现微信授权登录的三种方案
[untitled]