Unified configuration management
Function: Unified management of configuration files for microservices with the same configuration files.
Unified configuration management is a solution to the scenario: Under normal circumstances, if there are multiple microservice instances with the same function, if the configuration is changed, they must be changed one by one and then restarted.
The core configuration is placed in the configuration management service . When starting, the microservice reads the core configuration in the configuration management service and combines it with its own configuration to start.
After the configuration changes in the configuration management service, the required microservices will be automatically notified, and the hot update will be automatically completed after the microservices read it .
Add in Configuration Management->Configuration List:
Code configuration process
Project startup->bootstrap.yml->read nacos configuration file->read local application.yml->create spring container->load beans.
1. Introduce Maven dependencies
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-nacos-config</artifactId>
</dependency>
2. Create bootstrap.yml
Service name + development environment + file suffix name form the data id, that is: ${spring.application.name}-${spring.profiles.active}.${spring.cloud.nacos.config.file-extension}
spring:
application:
name: userservice #eureka的服务名称
profiles:
active: dev #开发环境
cloud:
nacos:
server-addr: localhost:8848 # nocos服务器地址
config:
file-extension: yaml #文件后缀名
When using:
@Value("${pattern.dateformat}")
private String dateFormat;
Configure hot update
The update will take effect in about five seconds.
Method 1: Use @RefreshScope
Hot updates can be achieved by using the @RefreshScope annotation on classes that use @Value.
@Slf4j
@RestController
@RequestMapping("/user")
@RefreshScope
public class UserController {
@Value("${pattern.dateformat}")
private String dateFormat;
@GetMapping("date")
public String now() {
return LocalDateTime.now().format(DateTimeFormatter.ofPattern(dateFormat));
}
}
Method 2: Use @ConfigurationProperties (recommended)
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-configuration-processor</artifactId>
<optional>true</optional>
</dependency>
@Data
@Component
@ConfigurationProperties(prefix = "pattern")
public class PatternProperties {
private String dateformat;
}
@Slf4j
@RestController
@RequestMapping("/user")
@RefreshScope
public class UserController {
@Autowired
private PatternProperties properties;
@GetMapping("date")
public String now() {
return LocalDateTime.now().format(DateTimeFormatter.ofPattern(properties.getDateformat()));
}
}
Multi-environment configuration sharing
Some values remain unchanged during development, testing, and production, so they are suitable for multi-environment configuration sharing.
Multiple configuration priorities: service name-profile.yaml > service name.yaml > local configuration .