Java's SpringCloud Alibaba [3] [Microservice Nacos-config Configuration Center]
-
- 1. Nacos-config Configuration Center
- 2. Quick configuration
-
- 1. Basic operation of Nacos-config
- 2. Use of Nacos Configuration Center
- 3. The Nacos client goes to the registration center every 10ms to judge according to MD5
- 4. Based on the file extension method with dataid as yaml
- 5. Support custom group configuration
- 6. Support custom extended Data id configuration
- 7. @RefreshScope [Set dynamic perception corresponding to configuration changes]
1. Nacos-config Configuration Center
1. Official documents
Nacos provides key/value storage for storing configuration and other metadata, and provides server-side and client-side support for externalized configuration in distributed systems. Using Spring Cloud Alibaba Nacos Confg, you can centrally manage
the external property configuration of your Spring Cloud application in Nacos Server.
1. Maintainability 2. Timeliness 3. Security
Springcloud config compares
three major advantages
- Most application scenarios of springcloud config are used in conjunction with git, and dynamic changes also need to rely on the Spring Cloud Bus message bus to pass all client changes
- springcloud config is used by GIT in most scenarios, and dynamic changes also require the SpringCloud Bus message bus to pass through all client changes
- nacos config uses long polling to update the configuration. Once the configuration changes, the process of notifying the Provider is very fast, killing the original config of springcloud by a few blocks in terms of speed.
2. Quick configuration
Visit: http://192.168.180.128:8847/nacos/index.html#/login
1. Basic operation of Nacos-config
New configuration
Click to publish
edit configuration
View modified history information
check the details
operations such as details and rollback
listen query
Create new namespace
Clone configuration files into other namespaces
User management & role management
Add users
Add roles
Rights management
We set up the logout system
2. Use of Nacos Configuration Center
Nacos server initialization
1. Start Nacos Server. The startup method can be found on the Nacos official website.
2. After starting Nacos, add the following configuration to Nacos
Data ID: nacos-config.properties
Group : DEFAULT_GROUP
Configuration format: Properties
Configuration content:
user.name = Xu Shu2
user.age = 11
Note Note that dataid is the extension of properties (the default file extension method)
How to use the client
If you want to use Nacos in your project to realize the external configuration of the application, use the starter whose group ID is com.alibaba.cloud and artfact ID is spring-cloud-starter-alibaba-nacos-config
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-nacos-config</artifactId>
</dependency>
Now you can create a standard SpringBoot application
@SpringBootApplication
public class ConfigApplication {
public static void main(String[] args) {
SpringApplication.run(ConfigApplication.class,args);
}
}
server:
port: 8050
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!--Nacos config 依赖-->
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-nacos-config</artifactId>
</dependency>
</dependencies>
@SpringBootApplication
public class ConfigApplication {
public static void main(String[] args) {
ConfigurableApplicationContext applicationContext = SpringApplication.run(ConfigApplication.class, args);
String userName = applicationContext.getEnvironment().getProperty("user.name");
String userAge = applicationContext.getEnvironment().getProperty("user.age");
System.out.println("user name :"+userName +"; age: "+userAge);
}
}
Before running this Example, you must use the bootstarp.properties configuration file to configure the Nacos Server address, for example
spring:
application:
name: nacos-config
cloud:
nacos:
server-addr: 192.168.180.128:8847
3. The Nacos client goes to the registration center every 10ms to judge according to MD5
4. Based on the file extension method with dataid as yaml
spring-cloud-starter-alibaba-nacos-config also perfectly supports the yaml format. At this time, you only need to complete the following two steps:
1. The declared dataid file extension displayed in the application's bootstrap.properties configuration file.
bootstrap.properties as shown below
spring.cloud.nacos.config.file-extension=yaml
user:
name: 徐庶-yaml
age: 11
We found that the configuration information could not be obtained. We
need to modify the corresponding configuration information and set the corresponding read file extension.
# Nacos 客户端 默认是Properties 的文件扩展名 file-extension进行设置
config:
file-extension: yaml
# Nacos 客户端 默认是Properties 的文件扩展名 file-extension进行设置
config:
file-extension: yaml
#refresh-enabled: false nacos客户端将无法感知配置的变化
Re-run the project
and set the application.yml configuration file
application-dev.yml for multiple environments
. Among the configuration information output above, the user name is the configuration information just now, but the age is still the old configuration information.
Set the namespace to dev
namespace: dev
We can see that no configuration information can be read, because we have not set the configuration information in the dev environment.
The clone is successful
5. Support custom group configuration
${spring.cloud.nacos.config.group}
The default is used when no configuration is explicitly specified DEFAULT_GROUP
. If you need to customize your own Group, you can do it through the following configuration:
spring.cloud.nacos.config.group=DEVELOP_GROUP
group: itbluebox
The corresponding information cannot be read.
Cloning and setting the Group
can continue to read the corresponding configuration information
6. Support custom extended Data id configuration
Spring Cloud Alibaba Nacos Config can support custom Data ld configuration from version 0.2.1. For the detailed design of this part, please refer to here. A complete configuration example is as follows:
String userConfig = applicationContext.getEnvironment().getProperty("user.config");
System.out.println("user name :"+userName +"; age: "+userAge + "; userConfig:"+userConfig);
group: itbluebox
shared-configs:
- data-id: com.itblueboxmall.common.properties
refresh: true
#group:默认是Default-group
- data-id: com.itblueboxmall.common2.properties
refresh: true
#group:默认是Default-group
extension-configs[0]:
data-id: com.itblueboxmall.common03.properties
refresh: true
7. @RefreshScope [Set dynamic perception corresponding to configuration changes]
The @Value annotation can get the value of the configuration center, but it cannot dynamically perceive the modified value. You need to use the @RefreshScope annotation
@RestController
@RequestMapping("/config")
public class ConfigController {
@Value("${user.name}")
public String username;
@RequestMapping("/show")
public String show(){
return this.username;
}
}
http://localhost:8050/config/show
The content output by the console has changed.
Visiting the corresponding interface again does not change
http://localhost:8050/config/show
setting dynamic perception corresponding configuration changes
@RefreshScope
Restart the running project
and modify the configuration information.
The console output content has changed.
Visit: http://localhost:8050/config/show