Using Nacos as a Configuration Center in Spring Boot Applications
This article explains why a configuration center is essential for microservices, introduces Nacos as a centralized solution, and provides step‑by‑step instructions with code examples for integrating Nacos into Spring Boot, managing Data IDs, namespaces, groups, and dynamic configuration refresh.
We introduce the basic usage of Nacos as a configuration center, starting with why a configuration center is needed.
1. Why a configuration center is needed
Before a configuration center, traditional static configuration suffers from lack of real‑time updates, risk of production accidents, scattered formats, and missing audit, version control, and permission features.
A configuration center centralizes configuration files, providing standardization, real‑time updates without restarting services, and audit capabilities.
2. Using Nacos configuration center
Three mainstream solutions exist for microservice configuration: Nacos, Apollo, Config+Bus. This article focuses on Nacos.
2.1 Spring Boot integration with Nacos
(1) Declare project version information:
<properties>
<spring-boot.version>2.3.2.RELEASE</spring-boot.version>
<spring-cloud.version>Hoxton.SR9</spring-cloud.version>
<spring-cloud-alibaba.version>2.2.6.RELEASE</spring-cloud-alibaba.version>
</properties>
<!-- only declare dependencies, do not import them -->
<dependencyManagement>
<dependencies>
<!-- Spring Boot BOM -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-dependencies</artifactId>
<version>${spring-boot.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
<!-- Spring Cloud BOM -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>${spring-cloud.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
<!-- Spring Cloud Alibaba BOM -->
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-alibaba-dependencies</artifactId>
<version>${spring-cloud-alibaba.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>(2) Add Nacos configuration dependency:
<!-- Spring Cloud Alibaba Nacos Config -->
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-nacos-config</artifactId>
</dependency>(3) Add Nacos settings to application.properties :
spring.profiles.active=dev
spring.application.name=cloud-producer-server
server.port=8080
# Nacos address
spring.cloud.nacos.config.server-addr=localhost:8848
# file type
spring.cloud.nacos.config.file-extension=yaml(4) Create a Data ID cloud-producer-server-dev.yaml in the Nacos console.
(5) Write a test controller that injects configuration values with @Value and enables dynamic refresh with @RefreshScope :
// configuration publishing, dynamic refresh
@RefreshScope
@RestController
@RequestMapping("provider")
public class ProviderController {
// use native @Value() to import config
@Value("${user.id}")
private String id;
@Value("${user.name}")
private String name;
@Value("${user.age}")
private String age;
@GetMapping("getNacosConfig")
public String providerTest() {
return "I am provider, successfully fetched Nacos config (id:" + id + ",name:" + name + ",age:" + age + ")";
}
}(6) Run the service and access http://localhost:8080/provider/getNacosConfig to see the configuration values.
(7) Modify a configuration item (e.g., user.name ) in Nacos; the change is reflected instantly without restarting because of @RefreshScope .
2.2 Core concepts of Nacos
Data ID
Data ID uniquely identifies a configuration set and follows the pattern ${prefix}-${profile}.${extension} , where prefix defaults to spring.application.name , profile is the active Spring profile, and extension is the file type (properties, yaml, etc.).
Namespace (environment isolation)
Namespaces separate configurations for different environments (dev, test, prod). If not specified, the default is the public namespace.
Group (business isolation)
Groups allow grouping of configurations within the same environment, defaulting to DEFAULT_GROUP . Different services can use distinct groups such as ORDER_GROUP or USER_GROUP to avoid conflicts.
3. Summary
Add spring-cloud-starter-alibaba-nacos-config dependency.
Inject configuration with @Value() .
Enable dynamic refresh with @RefreshScope .
Use Namespace and Group to isolate configurations per environment or business scenario.
4. Shared configuration
When many microservices share common settings (e.g., datasource or logging), they can be extracted into shared Data IDs such as db.yaml and log.yaml . These can be loaded via extension-configs or shared-configs . The priority order is A < B < C, where:
A: spring.cloud.nacos.config.shared-configs[n].data-id (multiple shared Data IDs).
B: spring.cloud.nacos.config.extension-configs[n].data-id (multiple extension Data IDs).
C: automatically generated Data IDs based on spring.cloud.nacos.config.prefix , spring.cloud.nacos.config.file-extension , and spring.cloud.nacos.config.group .
Selected Java Interview Questions
A professional Java tech channel sharing common knowledge to help developers fill gaps. Follow us!
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.