Master Dynamic Configuration Refresh in Spring Cloud Without Nacos – Step‑by‑Step Guide
This article walks you through configuring Spring Cloud for dynamic property updates, covering dependency setup, enabling bootstrap, defining configuration properties, exposing a refresh endpoint, and listening to EnvironmentChangeEvent, all illustrated with complete code snippets and screenshots.
1. Introduction
In a Spring Cloud micro‑service architecture, configuration management is essential. As services grow, configuration often needs to be adjusted dynamically so that applications can respond in real time. Spring Cloud offers several mechanisms to achieve dynamic updates.
2. Practical Example
2.1 Dependency Management
<code><spring-cloud.version>2021.0.7</spring-cloud.version>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>${spring-cloud.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement></code>The project also needs spring-cloud-context ; you can add spring-cloud-starter as shown below.
<code><dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter</artifactId>
</dependency></code>2.2 Enabling Bootstrap Configuration
There are two ways to activate the default bootstrap.yml configuration:
First method – add the starter dependency:
<code><dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-bootstrap</artifactId>
</dependency></code>Second method – launch the application with the JVM argument:
<code>-Dspring.cloud.bootstrap.enabled=true</code>The key listener is BootstrapApplicationListener .
2.3 Initial Configuration
Example bootstrap.yml :
<code>pack:
name: ooxx</code>Corresponding properties class:
<code>@Component("pp")
@ConfigurationProperties(prefix = "pack")
public class PackProperties {
private String name;
public String getName() { return name; }
public void setName(String name) { this.name = name; }
}</code>Test controller:
<code>@RestController
@RequestMapping("/pack")
public class PackController {
@Resource(name = "pp")
private PackProperties props;
@GetMapping("")
public PackProperties p() { return props; }
}</code>When the configuration file changes, the updated values are displayed as shown in the screenshot below.
2.4 Triggering Refresh
Inject ContextRefresher and expose a refresh endpoint:
<code>@Resource
private ContextRefresher contextRefresher;
@GetMapping("/refresh")
public Object refresh() { return this.contextRefresher.refresh(); }</code>Calling this endpoint re‑initializes beans in the refresh scope and re‑binds @ConfigurationProperties classes, publishing an EnvironmentChangeEvent .
After modifying bootstrap.yml to:
<code>pack:
name: ooxx</code>and invoking /refresh , the new configuration is applied without restarting the service, as demonstrated in the following image.
2.5 Listening to Configuration Changes
Implement an ApplicationListener<EnvironmentChangeEvent> to react to updates:
<code>@Component
public class PackApplicationEventListener implements ApplicationListener<EnvironmentChangeEvent> {
@Resource
private Environment env;
@Override
public void onApplicationEvent(EnvironmentChangeEvent event) {
System.out.println(event.getKeys());
System.out.println(env.getProperty("pack.name"));
}
}</code>When the configuration changes, the listener prints the changed keys and the new property value, e.g.:
<code>[pack.name]
xxxooo</code>Spring Full-Stack Practical Cases
Full-stack Java development with Vue 2/3 front-end suite; hands-on examples and source code analysis for Spring, Spring Boot 2/3, and Spring Cloud.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.