Nacos configuration management
In addition to being a registration center, Nacos can also be used for configuration management.
1. Unified configuration management
multiple servicessame configurationor needDynamically adjusted configuration, can be handed over to a unified configuration center for management, and the service pulls the configuration from the configuration center,
If you need to modify the configuration, you only need to modify the configuration in the configuration center to implement batch and dynamic configuration updates of services.
Add configuration files in nacos
The microservice needs to pull the configuration managed in nacos and merge it with the local application.yml configuration to complete the project startup. If the application.yml has not obtained the attributes, the project startup will report an error and the attribute parsing exception
Spring introduces a new configuration file: bootstrap.yaml file, which will be read before application.yml, the process is as follows
read configuration
Introduce nacos-config dependency
In user-service service, import nacos-config client dependency:
<!--nacos配置管理依赖-->
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-nacos-config</artifactId>
</dependency>
Add bootstrap.yaml (service name, environment, configuration center address, file extension)
to read the configuration in the code
Then, add a bootstrap.yaml file in user-service, the content is as follows:
spring:
application:
# 服务名
name: user-service
profiles:
# 环境
active: dev
cloud:
nacos:
# nacos 服务端地址
server-addr: localhost:8848
config:
# 文件扩展名
file-extension: yaml
Access test
Add business logic to UserController in user-service, read jdbc.username
configuration:
@Value("${jdbc.username}")
private String username;
@GetMapping("/config")
public String config(){
return username;
}
2. Configure automatic refresh
Requirement: After modifying the configuration in nacos, the microservice can make the configuration take effect without restarting, that is, configuration hot update .
To achieve configuration hot update, two methods can be used:
Method 1: @Value
Where to 类上
add@RefreshScope
Method 2: Use @ConfigurationProperties
annotations to read configuration
way 1
Add the annotation @RefreshScope to the class where the variable injected by @Value is located:
@RestController
@RequestMapping("/user")
@RefreshScope //刷新配置
public class UserController {
way 2
Read configuration using @ConfigurationProperties
annotations
In the user-service service, add a class to read the jdbc.username property:
import lombok.Data;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;
@Data
@Configuration
@ConfigurationProperties(prefix = "jdbc")
public class JdbcProperties {
private String username;
private String password;
}
Use this class in UserController:
@Autowired
JdbcProperties jdbcProperties;
@GetMapping("/config")
public String config(){
return jdbcProperties.toString();
}
3. Multi-environment configuration
During the development process, the same set of configurations may have different configuration values in different environments (dev, test, prod...), how to switch configurations in multiple environments?
demo
1), nacos configuration center adds the configuration of the test environment
2), modify the current environment
Remember to change dev to test
3), test
4. Configuration sharing
When different environments have the same configurations, these configurations are configured in the configuration file of each environment, and duplicate configurations appear at this time
How to configure it only once, but also share the configuration in multiple environments?
In fact, when the microservice starts, it will go to nacos to read two configuration files, for example:
- Environment configuration:
[spring.application.name]-[spring.profiles.active].yaml
, for example: user-service-dev.yaml - Shared configuration:
[spring.application.name].yaml
, for example: user-service.yaml
1) Add shared configuration
Add shared configuration: add one in nacosuser-service.yamlFile:
At this time, there are 3 configuration files for user services in nacos:
2) Read shared configuration
In the user-service service, modify the UserController class to read the newly added properties:
//读取公共配置
@Value("${user.auth}")
private Boolean auth;
@Autowired
JdbcProperties jdbcProperties;
@GetMapping("/config")
public String config(){
//return username;
return jdbcProperties.getUsername()+" - "+auth;
}
After that it can be tested.
When nacos and service local have the same attribute at the same time, the priority is divided into high and low: the lowest is the local configuration
5. Configuration isolation
Step 1: Create a new namespace
- The default namespace has only one public
- Create two new namespaces dev and test
Step 2: Add configuration
- Add user-service.yaml
Step 3: Add configuration to the development environment
Add user-service.yaml
Step 4: Read configuration
User service reads test environment configuration
- The configuration file bootstrap.yml reads the specified namespace and group
spring:
application:
# 服务名
name: user-service
#profiles:
#active: dev # 不需要指定,namespace指定的就是对应环境
cloud:
nacos:
# nacos 服务端地址
server-addr: localhost:8848
config:
file-extension: yaml # 文件扩展名
namespace: 42b87aee-ed82-4b07-ad0d-6138cf4c0c24 # test 环境的namespace
group: USER_GROUP # 组名 区分大小写
Step Five: Test
…
Shared configuration under namespace
Add shared configuration to the test environment in nacos
Add configuration in bootstrap.yml
spring:
cloud:
nacos:
config:
server-addr: 127.0.0.1:8848
file-extension: yaml
namespace: c05434f9-5d25-49ba-be6e-862b21380d25
group: USER_GROUP
# 主要是下面这个配置
# 当前namespace 下各个service 都共享的配置
shared-configs:
- data-id: common.yaml
group: COMMON_GROUP
refresh: true #是否支持应用中可动态刷新, 默认是不支持的。
Test afterwards.