当前位置:网站首页>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
边栏推荐
- Serial port debugging tools cutecom and minicom
- ESP32 LVGL8. 1 - bar progress bar (bar 21)
- Ucosiii transplantation and use, reference punctual atom
- 使用 bitnami/postgresql-repmgr 镜像快速设置 PostgreSQL HA
- CISSP certified daily knowledge points (April 14, 2022)
- K210串口通信
- ESP32 LVGL8. 1 - input devices (input devices 18)
- WIN1 remote "this may be due to credssp encryption Oracle correction" solution
- Daily CISSP certification common mistakes (April 14, 2022)
- Nacos作为服务注册中心
猜你喜欢
logstash 7. There is a time problem in X. the difference between @ timestamp and local time is 8 hours
Tangle
On iptables
ESP32 LVGL8. 1 - bar progress bar (bar 21)
Druid SQL和Security在美团点评的实践
Cygwin64 right click to add menu, and open cygwin64 here
ESP32 LVGL8. 1 - img picture (IMG 20)
Halo open source project learning (VII): caching mechanism
ESP32 LVGL8. 1 - arc (arc 19)
【ACM】70. climb stairs
随机推荐
Interpretation and compilation of JVM
Sentinel服务熔断实战(sentinel整合ribbon+openFeign+fallback)
CISSP certified daily knowledge points (April 11, 2022)
Function recursion and solving interesting problems
【ACM】70. climb stairs
STM32: LCD显示
Introduction to QT programming
ctfshow-web362(SSTI)
Keil RVMDK compiled data type
On iptables
迁移学习进阶
Implementation of TCP UDP communication with golang language
Spark performance optimization guide
ctfshow-web361(SSTI)
Software test summary
使用晨曦记账本,分析某个时间段每个账户收支结余
Machine learning theory (7): kernel function kernels -- a way to help SVM realize nonlinear decision boundary
特征选择feature_selection--SelectKBest
logstash 7. There is a time problem in X. the difference between @ timestamp and local time is 8 hours
Daily network security certification test questions (April 12, 2022)