当前位置:网站首页>Nacos configuration management - unified configuration management
Nacos configuration management - unified configuration management
2022-07-20 13:03:00 【Liu Chu, Ge Nian】
List of articles
0. Question introduction
Previous microservice architecture :
But as the business grows , We may need to deploy dozens or even hundreds of microservices , But then there is a problem , If you want to add a business or modify a business now , For this business, you need to modify the configuration files of multiple microservices , It's ok if there are several configuration files to modify , But if there are many , This undoubtedly increases our workload . And after modifying the configuration , We also need to restart various micro Services , Hot updates that do not allow configuration changes …
At this time, a configuration management service is used to manage the configuration or core configuration of these micro services that are often changed , When the configuration needs to be modified , It is good to directly change the configuration of the configuration center to the micro service to read and combine its own configuration , The overall structure is shown in the figure below :
It's reflected here Nacos Is powerful , He is in configuration management : On the one hand, the configuration can be managed centrally , The other party can change the configuration , Timely notify the microservice , Hot update of configuration .
1. Nacos Configuration Management
Nacos It can not only be used as a registration center 、 It can also be used as a configuration center .
The structure diagram is as follows :
So how do we get there nacos Add configuration files in ? Now let's explore :
1.1. Unified configuration management
When there are more and more instances of microservice deployment , Up to dozens 、 Hundreds of hours , Modifying the microservice configuration one by one will drive people crazy , And it's easy to make mistakes . We need a unified configuration management solution , You can centrally manage the configuration of all instances .
Nacos On the one hand, the configuration can be managed centrally , The other party can change the configuration , Timely notify the microservice , Hot update of configuration .
The structure flow chart is as follows :
1.1.1. stay nacos Add profile to
Click on the configuration list , Click the plus sign on the right
Then add the configuration page to set the information of the configuration items , for example :
Click Add , At this point, we have one more configuration :
Be careful : The core configuration of the project , Configurations that require hot updates are available nacos The need for management . It is better to save some configurations that will not be changed locally in the microservice .
1.1.2. Pull the configuration from the microservice
The micro service needs to pull nacos Configuration managed in , And with local application.yml Configuration merge , To complete the project launch .
But there is another problem : If not already read application.yml, How do you know nacos The address? ?
therefore spring A new configuration file is introduced :bootstrap.yaml file
, Will be in application.yml Previously read , The process is as follows :
1.1.3. Pull the configuration steps from the microservice :
1) introduce nacos-config rely on
First , stay user-service In service , introduce nacos-config Client dependency for :
<!--nacos Configuration management dependency -->
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-nacos-config</artifactId>
</dependency>
2) add to bootstrap.yaml
then , stay user-service Add a bootstrap.yaml file , The contents are as follows :
spring:
application:
name: userservice # The service name
profiles:
active: dev # development environment , Here is dev
cloud:
nacos:
server-addr: localhost:8848 # Nacos Address
config:
file-extension: yaml # File suffix
It will be based on spring.cloud.nacos.server-addr obtain nacos Address , According to
${spring.application.name}-${spring.profiles.active}.${spring.cloud.nacos.config.file-extension}
As a document id, To read the configuration .
The file name here is :userservice-dev.yaml
, That is the configuration we just added in the configuration center
3) Read nacos To configure
stay user-service Medium UserController Add business logic , Read pattern.dateformat To configure :
Complete code :
package com.yyl.user.web;
import com.yyl.user.pojo.User;
import com.yyl.user.service.UserService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
@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(){
// Get the current time and format
return LocalDateTime.now().format(DateTimeFormatter.ofPattern(dateformat));
}
}
Request access , You can see the effect :
perfect !
边栏推荐
- Browser debugging
- File path of QGIS development plug-in
- How to use swiftui to create drawing application based on coredata pencil Kit
- Druid 配置和監控
- Sending the email to the following server failed. IOException while sending message problem solving
- 一道关于defer理解的go面试题
- 【数据库】索引
- Web3创业具备创新爆炸式增长的所有要素
- Feign入门之快速实战
- Part 15: SPI serial communication protocol of stm32
猜你喜欢
Solution: the configuration and path are completely correct, but the gateway cannot be accessed
File path of QGIS development plug-in
Three application functions of digital sand table popular science
Web3将如何改变人来的未来?
[sklearn error reporting solution] undefined metricwarning: precision is ill defined and being set to 0.0
PyG教程(8):计算更高效的稀疏矩阵形式
scratch判断闰年 电子学会图形化编程scratch等级考试四级真题和答案解析2022年6月
Find - b+ tree
LNMP environment, website cannot be accessed
Nacos集群搭建(转载)
随机推荐
Notepad++软件安装教程
idea 删除代码的注释
Common construction methods and example appreciation of STL pair
SLT 定义vetctor对象,vector构造函数
线段树杂谈·普通线段数|乘法线段数|主席树
这些年我开源的几个小项目
C language remote connection to MySQL succeeded, unable to insert data
Part 14: can bus communication of stm32
scratch判断闰年 电子学会图形化编程scratch等级考试四级真题和答案解析2022年6月
Error when idea connects H2 database
ORACLE从小机迁移到X86架构,如何评估硬件资源(CPU的数量,内存大小),是否有衡量的指标或者公司?
同花顺可靠么?请问手机开户股票开户安全吗?
Docker installs common software redis
UE GIS virtualization learning
LNMP environment, website cannot be accessed
Is flush reliable? Is it safe to open a stock account by mobile phone?
一汽红旗“王炸”上市,安全、舒适一个不落
Qt播放音视频文件报错DirectShowPlayerService::doRender: Unresolved error code 0x80040266
Docker安装常用软件-Mysql
Configuration et surveillance des fluides