当前位置:网站首页>Basic use of Nacos (2) -- Nacos configuration center
Basic use of Nacos (2) -- Nacos configuration center
2022-07-24 08:40:00 【Xiao Li, who loves traveling】
List of articles
Preface
Pre knowledge : Nacos Basic use of (1)—— introduction
This note is combined with Xiaobai SpringCloud+Vue Online education project . If necessary , Welcome to .
SpringCloud+Vue Online education project ( The whole stack )
One 、 Configuration center introduction
1、Spring Cloud Config
Spring Cloud Config It provides the support scheme of server and client for the external configuration of distributed system . On the configured server side, you can manage the central location of external properties for applications in all environments . Client and server conceptual Spring Environment and PropertySource Keep abstractions in sync , They are perfect for Spring Applications , But it can be used with applications running in any language . When an application is in the deployment pipeline, from development to testing to production , You can manage the configuration between these environments , And ensure that applications have everything they need to run when migrating . The server storage backend is used by default implementation git, Therefore, it is easy to support the configuration environment of marked version , And can be accessed by various tools for managing content . It's easy to add alternative implementations , And use Spring Configure them to insert .
Spring Cloud Config Contains Client and Server Two parts ,server Provide storage of configuration files 、 Provide the contents of the configuration file in the form of an interface ,client Get data through the interface 、 And based on this data to initialize their own applications .Spring cloud Use git or svn Store the configuration file , By default git.
2、Nacos Replace Config
Nacos It can be done with Spring, Spring Boot, Spring Cloud Integrate , And can replace Spring Cloud Eureka, Spring Cloud Config. adopt Nacos Server and spring-cloud-starter-alibaba-nacos-config Realize the dynamic change of configuration .
(1) Application scenarios
In the process of system development , Developers usually change some parameters that need to be changed 、 Variables are separated from the code and managed independently , In the form of a separate configuration file . The goal is to have static system artifacts or deliverables ( Such as WAR,JAR Bag, etc ) Better adapt to the actual physical operating environment . Configuration management is generally included in the process of system deployment , It is completed by system administrator or operation and maintenance personnel . Configuration change is an effective way to adjust the behavior of the system at runtime .
(2) Problem solved
If the unified configuration center is not used in the microservice Architecture , The problem is :
- Configuration files are scattered across projects , Inconvenient to maintain
- Configure content security and permissions
- After updating the configuration , The project needs to be restarted
(3)nacos Configuration center function :
Centralized management of system configuration ( edit 、 Storage 、 distribution )、 Dynamic update does not restart 、 Rollback configuration ( Change management 、 Historical version management 、 Change audit ) And all configuration related activities .
Two 、 Read Nacos Configuration center configuration file
1、 stay Nacos Create a unified profile
(1) Click the Create button

(2) Enter configuration information
Data ID The complete rule format is as follows
${prefix}-${spring.profiles.active}.${file-extension}
prefixThe default is the project configurationspring.application.nameValue ( namely :nacos-provider), You can also configure items throughspring.cloud.nacos.config.prefixTo configure the .spring.profiles.active=devThat is, the current environment corresponds to profile. Be careful : Whenspring.profiles.activeIt's empty time , Corresponding connector - There will be no ,dataId The splicing format of becomes${prefix}.${file-extension}file-exetensionTo configure the data format of the content , You can use the configuration itemspring.cloud.nacos.config.file-extensionTo configure the . Currently only supported properties and yaml type .
(3) Ensure that the configuration information is correct , Click on the confirmation

(4) Click the configuration list to see the configuration file

2. Take any module as an example
(1) stay service Of pom.xml Introduce dependency in
<!-- Configuration center -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-nacos-config</artifactId>
</dependency>
(2) establish bootstrap.properties The configuration file
# Configure center address
spring.cloud.nacos.config.server-addr=127.0.0.1:8848
#spring.profiles.active=dev
# This configuration affects... In the unified configuration center dataId
spring.application.name=service-statistics-dev
(3) Before the project application.properties Content notes , Start the project to see the effect
The console outputs relevant information
2020-06-12 11:11:01.875 INFO 8676 --- [-127.0.0.1_8848] trationDelegate$BeanPostProcessorChecker : Bean 'org.springframework.cloud.autoconfigure.ConfigurationPropertiesRebinderAutoConfiguration' of type [org.springframework.cloud.autoconfigure.ConfigurationPropertiesRebinderAutoConfiguration] is not eligible for getting processed by all BeanPostProcessors (for example: not eligible for auto-proxying)
2020-06-12 11:11:02.464 INFO 8676 --- [-127.0.0.1_8848] o.s.c.a.n.c.NacosPropertySourceBuilder : Loading nacos data, dataId: 'service-statistics-dev.properties', group: 'DEFAULT_GROUP'
2020-06-12 11:11:02.468 INFO 8676 --- [-127.0.0.1_8848] b.c.PropertySourceBootstrapConfiguration : Located property source: CompositePropertySource {
name='NACOS', propertySources=[NacosPropertySource {
name='service-statistics-dev-dev.properties'}, NacosPropertySource {
name='service-statistics-dev.properties'}]}
2020-06-12 11:11:02.469 INFO 8676 --- [-127.0.0.1_8848] o.s.boot.SpringApplication : The following profiles are active: dev
3、 Add :springboot Profile load order
Actually yml and properties Files are the same principle , And on a project, either yml perhaps properties, The existence of one of two . Recommended yml, More concise .
bootstrap And application
(1) Loading order
bootstrap.properties/yml --> application.properties/yml --> application-dev.properties/yml
(2) Configuration difference
bootstrap.yml and application.yml Can be used to configure parameters .
bootstrap.yml It can be understood as some parameter configuration at the system level , These parameters will not change .
application.yml Can be used to define application level .
3、 ... and 、 Namespace
In actual development , There are usually many different environments ( Only by default public), At this time, you can create different namespce, for example , Development 、 Three different environments for testing and production , Then use a set of nacos There are three different clusters namespace. In order to achieve multi environment isolation .
(1) Switch namespace
1 Click on namespace

2 New namespace

3 Click clone

4 The target space is specified as dev

5 stay bootstrap.properties Add configuration in
The value is a namespace id value
spring.cloud.nacos.config.namespace=c2074590-581c-4a5c-9d43-ea48953f5bf2
Four 、 Load multiple profiles
1 Separate application.properties, Comment out the port configuration
2 newly build port.properties
server.port=8008
3 stay bootstrap.properties Add configuration in
# Load profile
spring.cloud.nacos.config.ext-config[0].data-id=port.properties
# Enable dynamic refresh configuration , Otherwise, the configuration file will be modified , Engineering cannot perceive
spring.cloud.nacos.config.ext-config[0].refresh=true
4 Just restart the project

8 Hours Python Zero basics easy to get started
边栏推荐
- Crypto giants all in metauniverse, and platofarm may break through
- Hack the box - Web requests module detailed Chinese tutorial
- Web3 traffic aggregation platform starfish OS interprets the "p2e" ecosystem of real business
- Go: Gin basicauth Middleware
- [emotion] what is "excellent"
- 4、 Midway integrates swagger and supports JWT bearers
- You can't access this shared folder because your organization's security policies prevent unauthenticated guests from accessing it. These policies can help protect your computer from unsafe or malicio
- Beandefinition three ways to load beans
- MySQL date formatting
- js获取当前浏览器的默认语言
猜你喜欢

On express framework

国产“火箭心”人工心脏上市 不同人工心脏有什么区别?

Musk responded that the brain has been uploaded to the cloud: already did it!

2、 Encapsulation and tool classes for adding, deleting, modifying and checking in midway

Treap

Look at the most influential infrastructure m-dao of Web3 through the current situation of Dao
![[Shangshui Shuo series together] June summary +no anxiety +july plan + how to test + how to improve](/img/75/e8f5a1dfe7594b75b6c7facb49ccfa.png)
[Shangshui Shuo series together] June summary +no anxiety +july plan + how to test + how to improve
![[wechat applet development] (II) wechat native bottom tabbar configuration](/img/74/5f5da7ea47f95a25011ba52959f480.png)
[wechat applet development] (II) wechat native bottom tabbar configuration

What is the difference between domestic "rocket heart" artificial heart and different artificial heart?

MySQL date formatting
随机推荐
Enterprises love hybrid app development, and applet container technology can improve efficiency by 100%
Realize page return to parent directory based on cookies
The solution of [an error occurred while trying to create a file in the destination directory: access denied] is prompted when installing the software
JQ native write bullet frame mask layer
Is yuancosmos hype? Or the future
[Shangshui Shuo series] final rad New Literacies
Golang implements sanggi diagram and analyzes user behavior trajectory
"Explanation" change exchange
[technical interview] how to introduce yourself
积分管理系统项目小结
[wechat applet development (III)] realize the stacking and sliding of cards
Confusing defer, return, named return value, anonymous return value in golang
Relationship between fork and pipeline
How difficult is it to build a digital collection platform?
Precautions for using kettle excel input
Clear up some disk space and try again
js获取当前浏览器的默认语言
2022.7.11全盘题解
In 2022, how to choose cross end technology solutions?
Learn the rxjs operator