SpringBoot dynamically reads nacos configuration (configuration hot update)

In a microservices architecture, dynamic updating of configurations is critical to application flexibility and maintainability. Spring Cloud provides a mechanism, the @RefreshScope annotation, to implement hot update of configuration. This article will introduce the principle of configuring hot update and the role of @RefreshScope.

Configure hot update principle

The principle of configuration hot update is based on Spring Cloud Config and Spring Cloud Bus.

  1. Spring Cloud Config: Spring Cloud Config allows application configurations to be centrally stored in remote version control repositories (such as Git, SVN), and provides configurations to applications through HTTP or message broker services (such as RabbitMQ, Kafka) information. When the application starts, it will obtain configuration information from the configuration server.

  2. Spring Cloud Bus: Spring Cloud Bus is an event bus used to propagate state changes in distributed systems. It is based on the message broker service. When the application configuration changes, messages can be broadcast through Spring Cloud Bus to notify all related microservice instances for configuration updates.

Combining Spring Cloud Config and Spring Cloud Bus, the process of implementing hot update of configuration is as follows:

  • Developers or operators update configuration files stored in the configuration server.
  • After the configuration server receives the configuration update, it sends a configuration change message to the message broker service through Spring Cloud Bus.
  • After receiving the message, the microservice instance subscribed to the message broker service re-obtains the latest configuration information from the configuration server and applies it to the application.

The role of @RefreshScope

The @RefreshScope annotation is used to mark a Spring Bean, indicating that the properties of the Bean need to be dynamically updated when the configuration changes. When the configuration is updated, the Spring container will re-create the beans with the @RefreshScope annotation, so that the new configuration will take effect.

The specific implementation steps are as follows:

  1. Bean creation phase: When the application starts, the Bean annotated with @RefreshScope is created and initialized. The property values ​​of these beans are populated with the values ​​configured when the application is started.

  2. Configuration update phase: When the configuration changes, Spring Cloud Bus broadcasts the configuration change message.

  3. Dynamic Update Phase: After the microservice instance subscribed to the message broker service receives the message, Spring Cloud Config will reload the configuration, and the beans with the @RefreshScope annotation will be marked as “dirty” ( dirty). When the bean is accessed, the Spring container will detect that the bean is “dirty”, re-create and initialize the bean, thereby updating the bean's property values ​​to the latest configuration values.

Example

The following is a simple example that demonstrates how to use @RefreshScope to implement hot update of configuration in Spring Boot:

import org.springframework.beans.factory.annotation.Value;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RefreshScope
public class ConfigController {

    @Value("${message:Hello World}")
    private String message;

    @GetMapping("/message")
    public String getMessage() {
        return message;
    }
} 

In this example, the message property is injected with the @Value annotation and annotated with the @RefreshScope. When the configuration changes, the value of the message attribute is dynamically updated.

Practical cases

In the program, I need to read the dynamically configured percentage ratio value but cannot query Mysql every time it is updated (causing IO waste)

You can’t update the project every time (a waste of time)

So choose to use @RefreshScope

import lombok.Data;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.stereotype.Component;

/**
 * @author zhangjiaxuan
 */
@Component
@ConfigurationProperties(prefix = "your.nacos.count.path")
@Data
@RefreshScope
public class YourClassName {

    // Here directly andnacosJust match the parameters in
    private Long day;

    private Long year;

}

Then introduce YourClassName.class into the program that needs to dynamically read parameters (define the name here)

@Resource
YourClassName yourClassName;

Then directly yourClassName.get

You can dynamically read the configuration on nacos

in conclusion

Through Spring Cloud Config and Spring Cloud Bus, as well as the @RefreshScope annotation, we can achieve hot update of configuration, thereby improving the flexibility and maintainability of applications under the microservice architecture. Configuration hot update enables applications to dynamically adapt to changing environments at runtime, providing basic support for continuous integration and continuous deployment.

Commonly used directions:

Program switch/Program hot update data/