Sometimes some parameters need to be modified in the configuration center. At this time, how to achieve real-time effect without restarting?

Add the dependent

<dependencies>
    ...
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-actuator</artifactId>
    </dependency>   
    ...
</dependencies>
Copy the code

Spring-boot-starter -actuator: The /refresh(POST requests) endpoints of this module can refresh configurations. See the Spring Boot series for more information.

Effective premise

Add the @refreshScope annotation to the Bean that needs to be refreshed.

@RefreshScope @RestController public class TestController { @Value("${username}") private String username; .Copy the code

When the configuration changes, the Bean marked with @refreshScope gets special processing to take effect.

Extension problem

If the project is under-configured, you can manually refresh the configuration via /refresh. If the project is more complex, this is definitely not possible. The Spring Cloud Bus message Bus can handle the real dynamic refresh of configuration changes.

Recommended reading

Dry goods: 2TB architect four-stage video tutorial

Interview: the most complete Java multithreaded interview questions and answers

Interview: the most comprehensive ali advanced Java interview questions in history

Interview: The most complete Spring interview questions in history

Tutorial: The most complete Spring Boot complete video tutorial

Books: 15 must-read books for advanced Java architects

Tools: Recommended an online creation flow chart, mind mapping software

Share Java dry goods, high concurrency programming, hot technology tutorials, microservices and distributed technology, architecture design, blockchain technology, artificial intelligence, big data, Java interview questions, and cutting-edge hot news.