当前位置:网站首页>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
边栏推荐
- 解决:cnpm : 无法加载文件 ...\cnpm.ps1,因为在此系统上禁止运行脚本
- 实战业务优化方案总结---主目录---持续更新
- Daily CISSP certification common mistakes (April 18, 2022)
- Hard core parsing promise object (do you know these seven common APIs and seven key questions?)
- STM32: LCD display
- ctfshow-web362(SSTI)
- listener.log
- 教你用简单几个步骤快速重命名文件夹名
- Daily CISSP certification common mistakes (April 14, 2022)
- Machine learning theory (7): kernel function kernels -- a way to help SVM realize nonlinear decision boundary
猜你喜欢
纠结
Quantexa CDI(场景决策智能)Syneo平台介绍
Jeecg boot microservice architecture
os_ authent_ Prefix
使用晨曦记账本,分析某个时间段每个账户收支结余
Setting up keil environment of GD single chip microcomputer
ESP32 LVGL8. 1 - arc (arc 19)
ESP32 LVGL8. 1 - bar progress bar (bar 21)
Chondroitin sulfate in vitreous
Introduction to quantexa CDI syneo platform
随机推荐
STM32学习记录0008——GPIO那些事1
22 year flying Book manpower Kit
Test questions of daily safety network (February 2024)
Daily network security certification test questions (April 15, 2022)
Interpretation and compilation of JVM
Résolution: cnpm: impossible de charger le fichier... Cnpm. PS1 parce que l'exécution de scripts est désactivée sur ce système
iptables -L执行缓慢
CISSP certified daily knowledge points (April 15, 2022)
Nacos作为服务注册中心
Custom prompt box MessageBox in QT
Implementation of TCP UDP communication with golang language
os_authent_prefix
kettle庖丁解牛第17篇之文本文件输出
使用 bitnami/postgresql-repmgr 镜像快速设置 PostgreSQL HA
ctfshow-web362(SSTI)
CANopen usage method and main parameters of object dictionary
数据库上机实验四(数据完整性与存储过程)
Teach you to quickly rename folder names in a few simple steps
串口调试工具cutecom和minicom
ESP32 LVGL8. 1 - img picture (IMG 20)