Dynamic Configuration of Spring Cloud Microservices with Spring Cloud Alibaba Nacos
This tutorial demonstrates how to replace static Spring Boot property files with Spring Cloud Alibaba Nacos for real‑time, centralized configuration management across multiple microservices, covering dependency integration, bootstrap setup, namespace and group usage, multi‑config sets, and dynamic refresh with code examples.
1. Traditional Configuration Method
Define configuration items directly in application.properties and inject them using @Value in a controller, then expose an endpoint to return the values. This approach requires restarting services when configuration changes.
member.nickname = "悟空聊架构"
member.age = "18" @Value("${member.nickname}")
private String nickname;
@Value("$member.age")
private Integer age; @RequestMapping("/test-local-config")
public R testLocalConfig() {
return R.ok().put("nickname", nickname).put("age", age);
}Result: values are read from the local file, but any change requires a service restart.
2. Introduce Nacos Dependency
Add the Spring Cloud Alibaba Nacos Config starter to the pom.xml of the common module.
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-nacos-config</artifactId>
</dependency>3. Configure Nacos Metadata
Create bootstrap.properties with higher priority than other configuration files.
spring.application.name=passjava-member
spring.cloud.nacos.config.server-addr=127.0.0.1:88484. Add Configuration in Nacos Console
Data ID: passjava-member.properties Group: DEFAULT_GROUP Content:
member.nick="悟空"
member.age=105. Enable Dynamic Refresh
Annotate the controller with @RefreshScope so that changes in Nacos are automatically reloaded without restarting the service.
@RefreshScope
@RestController
@RequestMapping("member/sample")
public class SampleController { }Log output shows the refreshed key member.age and the listener com.alibaba.cloud.nacos.refresh.NacosContextRefresher.
6. Namespace Management
Create separate namespaces for each microservice (e.g., passjava-member, passjava-channel, etc.) and configure the namespace in bootstrap.properties:
spring.cloud.nacos.config.namespace=passjava-memberAfter modifying the configuration in the corresponding namespace and restarting the service, the endpoint returns the updated values.
7. Group (Environment) Segmentation
Use groups to distinguish environments such as dev, test, and prod. Set the active group in bootstrap.properties: spring.cloud.nacos.config.group=prod Fetching the endpoint now returns the configuration from the prod group.
8. Multiple Configuration Sets
Split large configuration files (e.g., datasource, MyBatis‑Plus) into separate Data IDs and load them via extension-configs:
spring.cloud.nacos.config.extension-configs[0].data-id=datasource.yml
spring.cloud.nacos.config.extension-configs[0].group=dev
spring.cloud.nacos.config.extension-configs[0].refresh=true
spring.cloud.nacos.config.extension-configs[1].data-id=mybatis.yml
spring.cloud.nacos.config.extension-configs[1].group=dev
spring.cloud.nacos.config.extension-configs[1].refresh=true
spring.cloud.nacos.config.extension-configs[2].data-id=more.yml
spring.cloud.nacos.config.extension-configs[2].group=dev
spring.cloud.nacos.config.extension-configs[2].refresh=trueTesting shows that both the simple properties and the external YAML configurations are correctly applied.
9. Summary of Nacos Usage
Introduce Nacos dependency.
Configure Nacos server address and namespace.
Create Data IDs and groups for different environments.
Enable dynamic refresh with @RefreshScope.
Inject values using @Value.
Namespace isolates configuration per microservice.
Group separates configurations per environment.
Extension configs allow multiple configuration files.
10. Code Repository
All source code is available at https://github.com/Jackson0714/PassJava-Platform .
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
Wukong Talks Architecture
Explaining distributed systems and architecture through stories. Author of the "JVM Performance Tuning in Practice" column, open-source author of "Spring Cloud in Practice PassJava", and independently developed a PMP practice quiz mini-program.
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.
