当前位置:网站首页>Nacos hand to hand teaching [i] dynamic configuration of Nacos
Nacos hand to hand teaching [i] dynamic configuration of Nacos
2022-07-25 00:58:00 【LoneWalker、】
1、 Preface
When we use a technology, we must first understand why we use it , What problems can it help us solve .
In the single architecture, we write the configuration in the configuration file , But one disadvantage is that every time you modify the configuration, you need to restart the service to take effect . It can also be maintained when there are few application instances , If it is a micro service architecture, there are dozens of hundreds of instances , Restart all instances every time you modify the configuration , It not only increases the instability of the system , It also greatly increases the maintenance cost .
nacos Configuration center , It can standardize the configuration 、 Unified format , When the configuration information changes , Changes take effect in real time , There is no need to restart the server , Can automatically perceive the corresponding changes , And send the new changes to the corresponding procedures , Respond quickly to change .
This article is based on Cloud Alibaba 2021.0.1.0 + Springboot 2.6.3

2、 Practice
nacos I won't talk about the download and installation of .nacos-server The default is to start in cluster mode , Here we use stand-alone mode .
startup.cmd -m standalone
After new project Add dependency first
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-nacos-config</artifactId>
<version>2021.0.1.0</version>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-bootstrap</artifactId>
<version>3.1.2</version>
</dependency>Here we need to pay attention to , This version is no longer loaded by default bootstrap file , If you need to load bootstrap Files need to be manually added with dependencies .
Then we go to nacos Create a new namespace on the console

So get a namespace id, Then go to the configuration file in the project to fill .
spring:
cloud:
nacos:
config:
server-addr: service ip:8848
file-extension: yaml
namespace: 434e7801-0b5d-4fd2-b96a-acbac849fd84
application:
name: nacos-demo
profiles:
active: devGo again nacos Console , In the configuration list, select the namespace we just created , Point rightmost + Number

Write a test interface , Start project , So let's test that out
@RefreshScope
@RestController
public class ConfigController {
@Value("${user.name}")
private String name;
@GetMapping("/test")
public String test(){
return name;
}
}First of all, we found that the project is based on 8082 Port boot

After calling the interface

We noticed that there is a @RefreshScope This annotation can help realize configuration hot loading .

After we modify the configuration file , Do not restart the project , Call the interface again

3、 analysis
There must be many questions after practice , At this point, let's analyze :
Data Id: Its value is Prefix - Environmental Science - Extension
${spring.cloud.nacos.config.prefix}-${spring.profiles.active}.${spring.cloud.nacos.config.file-extension}
- prefix: Prefix , The default is spring.application.name Value . The priority of value taking is first spring.cloud.nacos.config.prefix, Retake spring.cloud.nacos.config.name, Finally take spring.application.name, Just get a value .
- active: Configure the operating environment , That is, the current environment corresponds to profile. When spring.profiles.active It's empty time , Corresponding connector ”-“ There will be no ,dataId The splicing format of becomes ${prefix}.${file-extension}
- file-exetension: The type of configuration file , The default is properties, You can also configure items through spring.cloud.nacos.config.file-extension To configure the , At present, the supported types are TEXT、JSON、XML、YAML、HTML、Properties
NameSpace: In fact, we can also see from the examples , Nacos Introduce namespace Namespace To manage and isolate multiple environment configurations and services . for example , You may have a local development environment dev、 Test environment test、 Production environment prod Three different environments , Then you can create three different Namespace Distinguish between different environments .
Group: Compare fine-grained isolation , For example, user microservices are grouped separately USER_GROUP

边栏推荐
- Where is the most formal account opening for futures trading? Capital security?
- [icore4 dual core core _arm] routine 22: LwIP_ UDP experiment Ethernet data transmission
- 360 interview summary 2013 campus recruitment 2012-4-4
- record
- jquer $(‘div li‘) $(‘div,li‘) $(‘div>li‘) $(‘div‘,‘li‘)
- R language plot visualization: plot to visualize the residual analysis diagram of the regression model, the scatter diagram of the predicted value and residual corresponding to the training set and th
- Netease game Flink SQL platform practice
- MySQL的最左前缀原则
- What does it operation and maintenance management mean? How to establish an effective IT operation and maintenance management system?
- ROS机械臂 Movelt 学习笔记3 | kinect360相机(v1)相关配置
猜你喜欢

Daily question 1 · 1260. Two dimensional network migration · simulation

Improve static loading dynamic loading

What is the function of transdata operator and whether it can optimize performance

Heavy forecast! Analysys, together with Microsoft and the Central University of Finance and economics, talks about the digital economy

Latest information of 2022 cloud computing skills competition

Install scoop and lux (formerly Annie)

js && ||

Unity image control and rawimage

Netease game Flink SQL platform practice

Automated test series selenium three kinds of waiting for detailed explanation
随机推荐
Big talk · book sharing | Haas Internet of things device cloud integrated development framework
BisinessCardGen
mysql初次安装的root密码是什么
基于ABP实现DDD--领域逻辑和应用逻辑
Tencent low code platform is officially open source! You can drag and drop and generate mobile phone projects and PC projects! Get private benefits
The IPO of Tuba rabbit was terminated: the annual profit fell by 33%, and Jingwei Sequoia was the shareholder
BGP machine room and BGP
Summary of MATLAB basic grammar
record
How to better use the touchpad of notebook
[mindspore] [xception model] script statement is suspected to be wrong
Yolov7:oserror: [winerror 1455] the page file is too small to complete the final solution of the operation
Several states of the process
If real-time intersection with line segments in online CAD drawings is realized
Pads copper laying
Grafana connection tdengine reports an error 535
Install and configure php5-7 version under centos7.4
阿里 Seata 新版本终于解决了 TCC 模式的幂等、悬挂和空回滚问题
分页的相关知识
Redis pipeline technology / partition