当前位置:网站首页>Nacos configuration management
Nacos configuration management
2022-06-10 17:32:00 【itmkyuan】
Unified configuration management
● Configuration change hot update 
The steps of configuration acquisition are as follows :
1. introduce Nacos The configuration management client depends on :
<!--nacos The configuration management of depends on -->
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-nacos-config</artifactId>
</dependency>
2. stay userservice in resource Directory add a bootstrap.yml file , This file is the boot file , Priority over application.yml:
spring:
application:
name: userservice
profiles:
active: dev # Environmental Science
cloud:
nacos:
server-addr: 8001 # Address
config:
file-extension: yaml # File suffix
We are servervice Lieutenant general pattern.dateformat This attribute is injected into UserController Test in
@Slf4j
@RestController
@RequestMapping("/user")
public class UserController {
@Value("${pattern.dateformat}")
private String dateformat;
@GetMapping("now")
public String now(){
return LocalDateTime.now().format(
DateTimeFormatter.ofPattern(dateformat, Locale.CHINA)
);
}
}
Give the configuration to Nacos Management steps :
① stay Nacos Add profile to
② Introduce... Into microservices nacos Of config rely on
③ Add... To microservices bootstrap.yml, To configure nacos The address of 、 The current environment 、 The service name 、 File suffix . These determine when the program starts nacos Which file to read
Configure auto update
Nacos After the configuration file of is changed , Microservices can sense... Without restarting . However, it needs to be implemented through the following two configurations :
- Mode one : stay @Value Add annotation on the class of the injected variable @RefreshScope
@RestController
@RequestMapping("/user")
@RefreshScope
public class UserController {
@Value("${pattern.dateformat}")
private String dateformat;
@GetMapping("now")
public String now(){
return LocalDateTime.now().format(
DateTimeFormatter.ofPattern(dateformat, Locale.CHINA)
);
}
}
- Mode two : Use @ConfigurationProperties annotation
@Data
@Component
@ConfigurationProperties(prefix = "pattern")
public class PatternProperties {
private String dateformat;
}
Nacos After configuration changes , Microservices can achieve hot updates , The way :
① adopt @Value Annotation injection , combination @RefreshScope To refresh
② adopt @ConfigurationProperties Inject , Automatically refresh
matters needing attention
- Not all configurations fit into the configuration center , Maintenance is more troublesome
- It is suggested that some key parameters , Parameters that need to be adjusted at run time nacos Configuration center , It is generally custom configuration .
Multi environment configuration sharing
The microservice starts from nacos Read multiple configuration files :
- [spring.application.name]-[spring.profiles.active].yml, for example :userservice-dev.yml
- [spring.application.name].yml, for example :userservice.yml
No matter what profie How to change ,[spring.application.name].yml This file must be loaded , Therefore, the environment sharing configuration can be written to this file .
[ Failed to transfer the external chain picture , The origin station may have anti-theft chain mechanism , It is suggested to save the pictures and upload them directly (img-EYhmcoB3-1654525516151)(https://files.mdnice.com/user/24442/2d9821e2-7016-4cf3-ada4-ecb5124fa0cd.png)]
Priority of multiple configurations :
- service name -profile.yaml> The service name .yaml> Local configuration

Microservices will start from Nacos Read the configuration file :
① [ service name ]-[profile.active].yaml, Environment configuration
② [ service name ].yaml, The default configuration , Multi environment sharing
priority :
① [ service name ]-[ Environmental Science ].yaml>[ service name ].yaml> The local environment
Thank you for your participation :
Video address :https://www.bilibili.com/video/BV1LQ4y127n4?p=25
边栏推荐
- Swift 3pThread tool Promise Pipeline Master/Slave Serial Thread confinement Serial queue
- 二十多年了,安全套市场还只有杜蕾斯、冈本、杰士邦
- What is the highest compound interest insurance product? How much does it cost a year?
- AHK common functions
- Xinsi technology performed well in the Gartner application security test key capability report 2022 and won the highest score among the five common use cases
- Web3 is the most complete money making secret script. Just read this one
- Example code of PHP for uploading multiple pictures
- AIChE | ab initio drug design framework integrating mathematical programming method and deep learning model
- 几个对程序员的误解,害人不浅!
- Under the "plastic ban order", does the Hong Kong stock exchange pay for the deep excavation of degradable plastics by Zhongbao new materials?
猜你喜欢
随机推荐
期货账户资金安全吗?
When V-IF and V-for need to be used at the same time
Redis operation set, Zset, hash data types and use of visualization tools
Hidden Markov model and its training (1)
Example analysis of SQL injection error reporting
AIChE | 集成数学规划方法和深度学习模型的从头药物设计框架
品牌难立,IPO难行,中国茶企困于“传统”?
亟需丰富智能家居产品线,扫地机器人赛道上挤得下萤石吗?
[the second revolution of report tools] optimize report structure and improve report operation performance based on SPL language
Under the "plastic ban order", does the Hong Kong stock exchange pay for the deep excavation of degradable plastics by Zhongbao new materials?
线上交流丨技能网络:解决多任务多模态问题的稀疏模型(青源Talk第19期 唐都钰)
Fabric.js 缩放画布
5年后,你将如何融入20万亿美元的「项目经济」
Set up proxy for chocolate
重庆第一个科创板IPO,来了
域名备案和icp备案有哪些区别?
企鹅电竞停步,虎牙也难行
IPO治不了威马的杂症?
Swift 3pThread tool Promise Pipeline Master/Slave Serial Thread confinement Serial queue
单片机底层通信协议① —— 同步和异步、并行和串行、全双工和半双工以及单工、电平信号和差分信号








