当前位置:网站首页>Actual combat of Nacos as service configuration center
Actual combat of Nacos as service configuration center
2022-04-23 18:39:00 【Hua Weiyun】
@toc
1、Nacos As configuration center - Basic configuration
1.1 newly build cloudalibaba-config-nacos-client3377 modular

1.2 pom.xml
<dependencies> <!--nacos-config--> <dependency> <groupId>com.alibaba.cloud</groupId> <artifactId>spring-cloud-starter-alibaba-nacos-config</artifactId> </dependency> <!--nacos-discovery--> <dependency> <groupId>com.alibaba.cloud</groupId> <artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId> </dependency> <!--web + actuator--> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-actuator</artifactId> </dependency> <!-- General basic configuration --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-devtools</artifactId> <scope>runtime</scope> <optional>true</optional> </dependency> <dependency> <groupId>org.projectlombok</groupId> <artifactId>lombok</artifactId> <optional>true</optional> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> </dependencies>
1.2 YML file
1.2.1 Why configure two yml file
Nacos Same as springcloud-config equally , At project initialization , Make sure to pull the configuration from the configuration center first ,
After pulling the configuration , To ensure the normal start of the project .
springboot There is a priority order for the loading of configuration files in ,bootstrap Priority over application.
1.2.2 bootstrap.yml
# nacos To configure server: port: 3377spring: application: name: nacos-config-client cloud: nacos: discovery: server-addr: localhost:8848 #Nacos Address of service registration center config: server-addr: localhost:8848 #Nacos As the configuration center address file-extension: yaml # Appoint yaml Format configuration # ${spring.application.name}-${spring.profile.active}.${spring.cloud.nacos.config.file-extension}
1.2.3 application.yml
spring: profiles: active: dev # Represents the development environment
1.3 Main startup class
@EnableDiscoveryClient@SpringBootApplicationpublic class NacosConfigClientMain3377{ public static void main(String[] args) { SpringApplication.run(NacosConfigClientMain3377.class, args); }}
1.4 Business class
ConfigClientController
@RestController@RefreshScope // Add... In the controller class @RefreshScope Annotation enables the configuration under the current class to support Nacos Dynamic refresh of .public class ConfigClientController{ @Value("${config.info}") private String configInfo; @GetMapping("/config/info") public String getConfigInfo() { return configInfo; }}
By using Spring Cloud Primary annotation @RefreshScope Implement automatic configuration update .
1.5 stay Nacos Add configuration information to
Nacos Match rule document :https://nacos.io/zh-cn/docs/quick-start-spring-cloud.html

The formula :${spring.application.name}-${spring.profiles.active}.${spring.cloud.nacos.config.file-extension}
We added a new configuration :


The correspondence between the configuration in our current program and the configuration center file is as follows :

1.6 test
Before starting, you need to be in nacos client - Configuration Management - There is a corresponding... Under the configuration management column yaml The configuration file
function cloud-config-nacos-client3377 The main startup class of
Call the interface to view the configuration information :http://localhost:3377/config/info

It does use the information in the remote configuration center .
Test dynamic refresh , Let's modify the configuration center first version=4

visit :http://localhost:3377/config/info, Found that the configuration has been updated , Than before Spring Cloud Congfig+eureka_Spring Cloud Bus The configuration of is much more convenient .

2、Nacos As configuration center - Classification configuration
2.1 What's wrong with the above configuration ?
problem 1:
In development , Usually a system will prepare dev development environment ,test Test environment ,prod Production environment . How to ensure that the service can correctly read Nacos The configuration file of the corresponding environment ?
problem 2:
A large-scale distributed microservice system will have many microservice subprojects , Each microservice project will have a corresponding development environment 、 Test environment 、 Pretest environment 、 Formal environment … How to manage these micro service configurations ?
2.2 Nacos Graphical management interface of
Configuration management interface :

Namespace interface :

2.3 Namespace+Group+Data ID The relationship between the three ? Why is it designed this way? ?
2.3.1 What is it? ?
similar Java Inside package And class name
The outermost namespace Can be used to differentiate deployment environments ,Group and DataID Logically distinguish between two target objects .
2.3.2 The relationship between the three

==Namespace=public,Group=DEFAULT_GROUP, Default Cluster yes DEFAULT==
Nacos The default namespace is public,Namespace It is mainly used to realize isolation .
For example, we now have three environments : Development 、 test 、 Production environment , We can create three Namespace, Different Namespace It's isolated .
Group The default is DEFAULT_GROUP,Group Different micro services can be divided into the same group
Service Micro services ; One Service It can contain more than one Cluster( colony ),Nacos Default Cluster yes DEFAULT,Cluster Is a virtual partition of a specified microservice .
For example, for disaster recovery , take Service Microservices are deployed in Hangzhou computer room and Guangzhou computer room respectively , At this time, you can give it to Hangzhou computer room Service Microservice has a cluster name (HZ), For Guangzhou machine room Service Microservice has a cluster name (GZ), We can also make micro services in the same computer room call each other as much as possible , To improve performance .
And finally Instance, This is an example of microservice .
2.4 Three schemes load configuration
2.4.1 DataID programme
Appoint spring.profile.active And configuration files DataID To read different configurations in different environments
Default space + Default group + newly build dev and test Two DataID.
newly build dev To configure DataID

newly build test To configure DataID


test
First use dev Environmental testing

visit :http://localhost:3377/config/info

Reuse test Environmental testing

visit :http://localhost:3377/config/info

2.4.2 Group programme
adopt Group Achieve environmental differentiation , newly build Group

stay nacos Create a new configuration file on the GUI console DataID

bootstrap.yml
stay config Add one more group Can be configured . It can be configured as DEV_GROUP or TEST_GROUP

application.yml

Access test :http://localhost:3377/config/info

2.4.3 Namespace programme
newly build dev/test Of Namespace, Note the following namespace ID:

Back to service management - Service list view

stay dev Namespace 3 Configuration items

bootstrap.yml, Here it is namespace Property to configure the namespace ID

application.yml

The above configuration represents that what we want to access here is dev Under the namespace ( The namespace configured above ID yes dev Of ),TEST_GROUP In group nacos-config.client-dev.yaml The configuration file .
visit :http://localhost:3377/config/info

You can see , It is consistent with our analysis .
Here we are ,Nacos As the configuration center, that's all , Another article will be written after cluster construction , In the middle of the night 2 O 'clock , The liver doesn't move .
版权声明
本文为[Hua Weiyun]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/04/202204231836021833.html
边栏推荐
- Use Chenxi bookkeeping book to analyze the balance of revenue and expenditure of each account in a certain period of time
- 【ACM】70. climb stairs
- 昇腾 AI 开发者创享日全国巡回首站在西安成功举行
- Use bitnami / PostgreSQL repmgr image to quickly set up PostgreSQL ha
- Query the logistics update quantity according to the express order number
- Daily network security certification test questions (April 12, 2022)
- QT notes on qmap container freeing memory
- 机器学习理论基础篇--关于机器学习的一些术语
- Setting up keil environment of GD single chip microcomputer
- listener. log
猜你喜欢

Imx6 debugging LVDS screen technical notes
![[popular science] CRC verification (I) what is CRC verification?](/img/80/a1fa10ce6781aebf1b53d91fba52f4.png)
[popular science] CRC verification (I) what is CRC verification?

ctfshow-web361(SSTI)

Use stm32cube MX / stm32cube ide to generate FatFs code and operate SPI flash

On iptables

昇腾 AI 开发者创享日全国巡回首站在西安成功举行

机器学习实战 -朴素贝叶斯

使用晨曦记账本,分析某个时间段每个账户收支结余

WiFi ap6212 driver transplantation and debugging analysis technical notes

CANopen STM32 transplantation
随机推荐
配置iptables
ESP32 LVGL8. 1 - BTN button (BTN 15)
【科普】CRC校验(一)什么是CRC校验?
Keil RVMDK compiled data type
Creation and use of QT dynamic link library
ctfshow-web362(SSTI)
Daily CISSP certification common mistakes (April 11, 2022)
机器学习实战 -朴素贝叶斯
Deeply understand what new and make in golang are and what are the differences?
Log4j2 cross thread print traceid
Nacos集群搭建和mysql持久化配置
14个py小游戏源代码分享第二弹
ctfshow-web362(SSTI)
机器学习理论之(8):模型集成 Ensemble Learning
CISSP certified daily knowledge points (April 13, 2022)
In win10 system, all programs run as administrator by default
Serial port debugging tools cutecom and minicom
How to restore MySQL database after win10 system is reinstalled (mysql-8.0.26-winx64. Zip)
Usage of functions decode() and replace() in SQL
Gson fastjason Jackson of object to JSON difference modifies the field name