当前位置:网站首页>Nacos configuration management
Nacos configuration management
2022-07-19 05:19:00 【Charming】
List of articles
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 :
@Autowired
private 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

@Autowired
private 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
边栏推荐
猜你喜欢

uniapp 使用uview实现折叠面板

ArcMap 创建常量栅格并镶嵌至新栅格

C语言初学者之初识代码专项练习

uniapp 表单(input、radio、picker)提交获取参数值

Applet cloud development upload pictures to cloud storage

Use echars to realize water drop, ring, segmentation, stacking, organization chart, map outline and other charts

ECS deployment web project

【C语言—零基础第六课】输入输出语句格式与复合语句

模拟库函数

小程序云开发表单提交并在页面中获取数据
随机推荐
Interface parameters return encapsulated class result
Continue from the previous issue: the remaining two methods of the rotation chart
Log4j的使用
【C语言—零基础第六课】输入输出语句格式与复合语句
STL container - basic operation of vector
Cesium 绑定鼠标事件和移除鼠标事件
Two methods of rotation chart and automatic rotation
Two JS methods of rolling wheel loading and modal box dragging
computed和watch的区别
【C语言—零基础第十一课】旋转大转盘之指针
STL容器——set集合的应用
Use (offset, page) in JS to achieve login effect
Email (including attachments, Netease, QQ)
【C语言—零基础_学习_复习_第四课】数据类型及其运算
交换机用户模式、特权模式、全局模式、端口模式
The principle and local storage of the throttle valve of the rotation chart are summarized
【Es6】详细解说Set ,Array的常用对象及其他方法(完整版)
微信小程序云开发使用方法-1
Applet cloud development form submission and data acquisition in the page
微信小程序5-基础加强(没写完)