当前位置:网站首页>Nacos configuration management
Nacos configuration management
2022-07-19 02:07:00 【Hua Weiyun】
1. Unified configuration management
Configuration change hot update


stay Nacos Add configuration information to :


The steps of configuration acquisition are as follows :

- introduce Nacos The configuration management client depends on :
<!--nacos Configuration management dependency --><dependency> <groupId>com.alibaba.cloud</groupId> <artifactId>spring-cloud-starter-alibaba-nacos-config</artifactId></dependency>- stay userservice Medium resource Directory add a
bootstrap.ymlfile , This file is the boot file , Priority overapplication.yml
spring: application: name: userservice # The service name profiles: active: dev # development environment cloud: nacos: server-addr: 47.107.53.146:8848 # nacos Address config: file-extension: yaml # File suffix - stay
user-serviceLieutenant generalpattern.dateformatThis attribute is injected intoUserControllerTest in :
@Slf4j@RestController@RequestMapping("/user")public class UserController { @Autowired private UserService userService; @Value("${pattern.dateformat}") private String dateformat; /** * route : /user/110 * * @param id user id * @return user */ @GetMapping("/{id}") public User queryById(@PathVariable("id") Long id) { return userService.queryById(id); } @GetMapping("/now") public String now() { return LocalDateTime.now().format(DateTimeFormatter.ofPattern(dateformat)); }}

You can see 8081 and 8082 Both microservices can access the formatted time , Instructions can be from nacos Read the information of the configuration file in the console
Section :
- 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 Address 、 The current environment 、 The service name 、 File suffix . These determine when the program starts nacos Which file to read
2. Configure auto refresh
Nacos After the configuration file in is changed , Microservices can sense... Without restarting . However, it needs to be implemented through the following two configurations :
change userservice-dev.yaml The configuration file :

- Mode one : stay
@ValueAdd an annotation on the class where the injected variable is located@RefreshScope
@Component@Data@ConfigurationProperties(prefix = "pattern")public class PatternProperties { private String dateformat;}- You can see that without restarting the service , change nacos The configuration file on can also achieve a consistent effect

- Mode two : Use
@ConfigurationPropertiesNote in place of@Valueannotation .
The configuration changes are as follows :

stay user-service In service , Add a class , Read
patterrn.dateformatattribute :
@Component@Data@ConfigurationProperties(prefix = "pattern")public class PatternProperties { private String dateformat;}- UserController add to :
@Autowiredprivate PatternProperties properties;@GetMapping("/now")public String now() { return LocalDateTime.now().format(DateTimeFormatter.ofPattern(properties.getDateformat()));}You can see , Modify without restarting the service nacos The configuration file on can also achieve the same effect

Section :
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
3. Multi environment configuration sharing
The microservice starts from nacos Read multiple configuration files :
- [spring.application.name]-[spring.profiles.active].yaml, for example :
userservice-dev.yaml - [spring.application.name].yaml, for example :
userservice.yaml
No matter what profile How to change ,[spring.application.name].yaml This file must be loaded , Therefore, the multi environment sharing configuration can be written to this file 
newly build
userservice.yamlThe configuration file is as follows :
verification userservice.yaml Whether the configured must be read
@Component@Data@ConfigurationProperties(prefix = "pattern")public class PatternProperties { private String dateformat; private String envShareValue;}- bootstrap.yml as follows , Express userservice.yaml and userservice-dev.yaml The file must be read

@Autowiredprivate PatternProperties properties;@GetMapping("prop")public PatternProperties properties() { return properties;}start-up userservice 8082 Environmental Science , It is amended as follows test Configuration file environment

At this time, this service can only read userservice.yml The configuration file
userservice 8081 Read as follows

userservice 8082 Read as follows

At this point, these two services can get the shared attribute value , explain userservice.yml The profile properties in are shared

You can also see the information of the read configuration file on the console
If in nacos Configuration in progress ,userservice.yaml and userservice-dev.yaml The same attribute exists in both configuration files , Which configuration file shall prevail ? If in idea There are the same profile attributes in , Which one will prevail ?
Configure the same property both remotely and locally , Test the following :

application.yml
stay Local plus name Property value
stay Distal nacos Of userservice.yml add

Access property values , Discovery is based on nacos Upper userservice.yml Subject to

userservice-dev.yml add

test , Find the properties in the local configuration file and Distal userservice.yml 、userservice-dev.yml When both properties exist at the same time , With
userservice-dev.ymlSubject to
Priority of multiple configurations :

Section :
Microservices will start from nacos Read the configuration file :
①[ service name ]-[spring.profile.active].yaml, Environment configuration
②[ service name ].yaml, The default configuration , Multi environment sharingpriority :
①[ service name ]-[ Environmental Science ].yaml>[ service name ].yaml> Local configuration
边栏推荐
- 动手学深度学习--线性神经网络篇
- 如何理解Volatile以及如何使用它
- MATLAB :Warning: the font “Times” is not available
- HRNet
- 高斯分布的性质(含代码)
- PCA主成分分析(降维)过程推导
- Leveraging Semi-Supervised Learning for Fairness using Neural Networks
- Allegro design entry CIS and OrCAD capture CIS relationship
- 02基于ZigBee的智能家居系统设计
- gdb+vscode进行调试1——使用CMakelist文件进行编译和调试+附加进程调试
猜你喜欢

Frustratingly Simple Few-Shot Object Detection

Labelme 的简单用法和界面介绍

VIM 配置文件

散列表、布隆过滤器、分布式一致性hash

How to understand volatile and how to use it

SAE J1708/J1587 协议详解
![[vernacular analog 1] PN junction and diode](/img/a8/26d3dc49eb74b7c8c5970ddbd31b65.png)
[vernacular analog 1] PN junction and diode

06基于STM32的智能电子药盒设计

Prohibit smart Safari from playing automatically when opening a web page

gdb+vscode进行调试2——gdb断点相关
随机推荐
CAN协议通信
03 design of urban road dedusting and cooling system based on ZigBee
Leveraging Semi-Supervised Learning for Fairness using Neural Networks
gdb+vscode进行调试6——gdb调试多线程命令札记
Array definition format
Neutralizing Self-Selection Bias in Sampling for Sortition
How to understand volatile and how to use it
Determine whether two arrays are exactly equal
Factory method mode notes
A causal linear model to quantify edge unfairness for unfair edge prioritization
二階邊緣檢測 - Laplacian of Guassian 高斯拉普拉斯算子
Oozie 集成 Shell
Original code, inverse code, complement code
01 design of intelligent warehouse management system based on RFID
Hands on deep learning -- multi layer perceptron (MLP)
Build map reduce development environment
On the properties and methods of list < t >
动手学深度学习--线性神经网络篇
工厂方法模式随记
VGG (Visual Geometry Group)





















