当前位置:网站首页>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
边栏推荐
- Spark performance optimization guide
- Nacos集群搭建和mysql持久化配置
- Ucosiii transplantation and use, reference punctual atom
- 关于unity文件读取的操作(一)
- ESP32 LVGL8. 1 - arc (arc 19)
- Use stm32cube MX / stm32cube ide to generate FatFs code and operate SPI flash
- Ionic instruction set order from creation to packaging
- 硬核解析Promise对象(这七个必会的常用API和七个关键问题你都了解吗?)
- Daily CISSP certification common mistakes (April 11, 2022)
- 根据快递单号查询物流查询更新量
猜你喜欢
教你用简单几个步骤快速重命名文件夹名
kettle庖丁解牛第17篇之文本文件输出
多功能工具箱微信小程序源码
Summary of actual business optimization scheme - main directory - continuous update
Introduction to quantexa CDI syneo platform
Use bitnami / PostgreSQL repmgr image to quickly set up PostgreSQL ha
Multifunctional toolbox wechat applet source code
[popular science] CRC verification (I) what is CRC verification?
硬核解析Promise对象(这七个必会的常用API和七个关键问题你都了解吗?)
Setting up keil environment of GD single chip microcomputer
随机推荐
Tangle
Promote QT default control to custom control
C language simulates entering and leaving the stack, first in first out, first in first out, shared memory
Ctfshow - web362 (ssti)
使用 bitnami/postgresql-repmgr 镜像快速设置 PostgreSQL HA
Jeecg boot microservice architecture
Daily network security certification test questions (April 12, 2022)
函数递归以及趣味问题的解决
iptables初探
Seata处理分布式事务
os_ authent_ Prefix
Daily CISSP certification common mistakes (April 14, 2022)
SQL中函数 decode()与 replace()的用法
【数学建模】—— 层次分析法(AHP)
机器学习实战 -朴素贝叶斯
Creation and use of QT dynamic link library
玻璃体中的硫酸软骨素
解决:cnpm : 无法加载文件 ...\cnpm.ps1,因为在此系统上禁止运行脚本
Resolution: cnpm: unable to load file \cnpm. PS1, because running scripts is prohibited on this system
机器学习理论基础篇--关于机器学习的一些术语