Master Nacos Config Management in Spring Cloud: A Step‑by‑Step Guide
This tutorial walks you through using Nacos as a centralized configuration center in a Spring Cloud application, covering the benefits of Nacos config management, creating configuration entries, setting up a Spring Boot project with the required dependencies, implementing a refreshable controller, and verifying dynamic updates at runtime.
Introduction
Nacos not only provides service registration and discovery but also integrates a configuration center, allowing all configuration of a micro‑service architecture to be stored centrally. The advantages include isolated multi‑environment configurations with finer permission control, higher security, and the ability to package an application once and run it in multiple places, aligning with the "12 factors" of cloud‑native applications.
Quick Start
1. Create a Configuration in Nacos
Open the Nacos console, go to the configuration list, click the "+" button, and fill in the following fields:
DataId : alibaba-nacos-config-client.properties Group : DEFAULT_GROUP (default)
Configuration Format : Properties Configuration Content :
didispace.title=spring-cloud-alibaba-learning2. Create the Spring Boot Application
Step 1 : Create a new Spring Boot project named alibaba-nacos-config-client.
Step 2 : Add the following dependencies to pom.xml (only the relevant parts are shown):
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.0.5.RELEASE</version>
</parent>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>Finchley.SR1</version>
<type>pom</type>
<scope>import</scope>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-alibaba-dependencies</artifactId>
<version>0.2.1.RELEASE</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-nacos-config</artifactId>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.18.2</version>
<optional>true</optional>
</dependency>
</dependencies>3. Implement the Application Main Class and Controller
@SpringBootApplication
public class TestApplication {
public static void main(String[] args) {
SpringApplication.run(TestApplication.class, args);
}
@Slf4j
@RestController
@RefreshScope
static class TestController {
@Value("${didispace.title:}")
private String title;
@GetMapping("/test")
public String hello() {
return title;
}
}
}4. Create bootstrap.properties
Configure the application name and Nacos address:
spring.application.name=alibaba-nacos-config-client
server.port=8001
spring.cloud.nacos.config.server-addr=127.0.0.1:8848Note : The bootstrap.properties file must be used, and the spring.application.name value must match the DataId created in Nacos (excluding the .properties suffix).
5. Run the Application
Start the Spring Boot application. The startup logs will show the DataId and Group that Nacos will load, e.g.:
INFO NacosPropertySourceBuilder: Loading nacos data, dataId:'alibaba-nacos-config-client.properties', group:'DEFAULT_GROUP'6. Verify Configuration Retrieval and Dynamic Refresh
Call the HTTP endpoint http://localhost:8001/test (using curl, Postman, etc.). The response will be the value of didispace.title from Nacos. Then modify the same key in the Nacos console and publish the change. The next request returns the updated value without restarting the application, thanks to the @RefreshScope annotation.
INFO RefreshEventListener: Refresh keys changed:[didispace.title]This demonstrates that the configuration is refreshed dynamically in the running client.
Reference
Nacos official documentation: https://nacos.io/zh-cn/docs/what-is-nacos.html
Code Repository
GitHub: https://github.com/dyc87112/SpringCloud-Learning/
Gitee: https://gitee.com/didispace/SpringCloud-Learning/
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.
Programmer DD
A tinkering programmer and author of "Spring Cloud Microservices in Action"
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.
