SpringCloud-Nacos-Config use
1. Introduction to Nacos Configuration Center
When we use SpringBoot, we will provide an application.properties
or application.yml
file by default . We will write some global configurations or configurations that need to be dynamically maintained into the file, such as database connection information, current limit threshold, server address, and so on. In order to solve the difference in information such as service connection configuration in different environments, SpringBoot provides a spring.profiles.active={profile}
mechanism based on switching between different environments, but this mechanism has shortcomings under microservices:
- Dynamic configuration update problem : low efficiency, need to restart the application.
- Configuration centralized management problem : If the number of micro-service nodes is large, a configuration file is maintained in each node. Once a certain attribute in the configuration file needs to be modified, the workload is huge.
- Security and permission issues of configuration content : The configuration file is submitted to the code base (usually GitHub) along with the source code, which can easily cause data leakage of the configuration information of the production environment.
- Configuration management issues in different deployment environments : For the management of the profile mechanism, this method is more cumbersome for daily maintenance.
Therefore, unified configuration management is a solution to make up for the above shortcomings. That is, some configurations in each application system are placed on third-party middleware for unified maintenance, and changes to the data on the unified configuration center need to be pushed to the responding service nodes to achieve dynamic updates.
In addition to the service registration function, Nacos also has the function of the configuration center, which realizes the functions of CURD, version management, monitoring management, push and aggregation of the configuration.
2. Use of SpringCloud-Alibaba-NacosConfig
Case 1: Get the configuration on the configuration center
1. First is the pom file:
<dependencyManagement>
<dependencies>
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-alibaba-dependencies</artifactId>
<version>2.2.0.RELEASE</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-nacos-config</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
2. Add configuration on Nacos configuration center:
After successful creation:
3. Add configuration in bootstrap.properties file:
# 配置中心的地址
spring.cloud.nacos.config.server-addr=192.168.237.130:8848
spring.application.name=nacos-test
# 表示Nacos配置中心上DataId的前缀
spring.cloud.nacos.config.prefix=myNacos
4. Startup class:
@SpringBootApplication
public class DemoApplication {
public static void main(String[] args) {
ConfigurableApplicationContext context = SpringApplication.run(DemoApplication.class, args);
String info = context.getEnvironment().getProperty("info");
System.out.println(info);
}
}
Output result:
Case 2: Dynamically update configuration
Startup class:
@SpringBootApplication
public class DemoApplication {
public static void main(String[] args) throws InterruptedException {
ConfigurableApplicationContext context = SpringApplication.run(DemoApplication.class, args);
while (true){
String info = context.getEnvironment().getProperty("info");
System.out.println(info);
Thread.sleep(2000);
}
}
}
Cyclic printing can be found:
if you change the corresponding file content on the configuration center
at this time : look at the print station at this time and find that the configuration has been updated in real time:
Configure Yaml file extension based on DataID
When Nacos Config loads the configuration from Nacos Config Server, it will match its DataID, and in the sight of SpringCloudNacos, the default rules for DataID are: ${prefix}- ${spring.profile.active}- ${file-extension}
- By default, it would go on to load DataID Nacos server
${spring.application.name}. ${file-extension:properties}
-based configuration prefix. - If
spring.cloud.nacos.config.prefix=xxx
the attribute is explicitly specified ,DataID=xxx
the configuration will be loaded . - If in actual application, the configuration in Yaml format is used
boostrap.properties
, it should be declared in the configuration file:spring.cloud.nacos.config.file-extension=yaml
3. Several basic concepts of configuration center
In the previous, the configuration files we created all use the default Namespace: public and Group: DEFAULT_GROUP. The following figure is the data model provided by Nacos. His data model Key is uniquely determined by triples.
- Namespace is used to solve the problem of data isolation under multiple environments or multiple tenants.
- Group is a mechanism used in Nacos to implement DataID group management. It can realize the isolation of different service/DataID.
Generally speaking, we distinguish different environments through Namespace, and Group is a data grouping at the business level.
Case 3: Specify Namespace and Group to obtain configuration file information
1. View the unique ID of the namespace:
2. Create a configuration file in the dev environment The
configuration information is as follows:
3. Configure in the bootstrap.properties file:
spring.cloud.nacos.config.server-addr=192.168.237.130:8848
spring.application.name=nacos-test
spring.cloud.nacos.config.prefix=myTest
# 我这里是用了空间名为dev的ID
spring.cloud.nacos.config.namespace=0602f1cc-4ad3-4135-ba1d-6bc65e54dbe4
# 组名
spring.cloud.nacos.config.group=test-0124
4. Start printing:
5. If you change the name of the Group and print, it will print out null:
Among them, there is another way to write bootstrap.properties:
# 指定Nacos Config的DataID
spring.cloud.nacos.config.ext-config[0].data-id=myTest.properties
# 指定DataID所在组
spring.cloud.nacos.config.ext-config[0].group=test-0124
# 指定配置发生更新时,是否动态刷新
spring.cloud.nacos.config.ext-config[0].refresh=true
A few points need to be explained here:
- This method is obsolete (not recommended).
- For the data-id specification, the file format must be added, including: properties, yaml, json. Otherwise, an error will be reported.
- For the creation of files in Nacos Config, the choice of configuration format does not determine the end of the configuration file . What does it mean? See the following process:
1. Create a configuration file with a configuration format of properties:
2. After the creation is successful, it is found that the end of the file does not end with properties.
3. Therefore, if you want to use config[0].data-id=
this configuration method, you must explicitly add the corresponding file format when creating the file, such as:
4. In this way, the configuration file can be obtained correctly:
Finally, a few points need to be stated:
- The relevant configuration of SpringCloud must be configured in the bootstrap.properties file. Bootstrap is the parent context of the application, that is, its loading takes precedence over the application.
- Since the service address information of the Nacos Configuration Center needs to be read before loading the remote configuration, the Nacos service address and other property configurations need to be placed in the bootstrap.properties file.
Although this article is very simple and basic, the purpose of writing this blog is very simple: to let everyone know how to use the configuration center Config in Nacos, and it has a real-time update function. Understand the distinction between Namespace and Group ( The ID card of a configuration file is determined by three things: Namespace, Group, DataID ), which configurations are mainly written in the project.
The next article is going to explain it from the perspective of principle.