Build a Clean Microservice Config Center with Nacos in One Step
This article walks through using Nacos as a centralized configuration center for Spring Cloud microservices, showing how to create configuration data, set up a Maven client, enable dynamic refresh with @RefreshScope, and manage multi‑environment and multi‑file configurations.
1. Background
In a previous article we introduced Nacos as a service registry. This piece expands the discussion to Nacos’s role as a configuration center, which allows unified maintenance of configuration resources for all microservices and eliminates manual configuration updates.
2. Practical Implementation
2.1 Create a Configuration
After starting the Nacos server, open the management console, click the "+" button on the "Configuration List" page, and add a new configuration. In the example we create a configuration with
dataId nacos-config-client.properties,
group DEFAULT_GROUP, and content blog.name=张三.
2.2 Create the Client Application
Create a Maven project named nacos-config-client and add the following dependencies to pom.xml:
<properties>
<spring-boot.version>2.2.5.RELEASE</spring-boot.version>
<spring-cloud.version>Hoxton.SR3</spring-cloud.version>
<spring-cloud-alibaba.version>2.2.1.RELEASE</spring-cloud-alibaba.version>
</properties>
<dependencies>
<!-- SpringBoot web -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!-- Nacos Config Center -->
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-nacos-config</artifactId>
</dependency>
</dependencies>
<dependencyManagement>
<dependencies>
<!-- SpringBoot version -->
<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 version -->
<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 version -->
<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>Create the main class:
@SpringBootApplication
public class Application {
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
}Add a REST controller to test the configuration value:
@RestController
@RefreshScope
public class HelloController {
@Value("${blog.name}")
private String name;
@RequestMapping("/hello")
public String hello() {
return name;
}
}Configure the Nacos address in application.properties:
spring.application.name=nacos-config-client
server.port=9012
# Nacos address
spring.cloud.nacos.config.server-addr=127.0.0.1:8848Start the client and access http://127.0.0.1:9012/hello. The response shows the value "张三", confirming that Spring Boot successfully fetched the configuration from Nacos.
Modify the configuration blog.name in the Nacos console, then request the endpoint again. The new value is returned, demonstrating dynamic refresh powered by the @RefreshScope annotation.
3. Configuration Rules
Nacos Spring Cloud defines five core properties: spring.cloud.nacos.config.server-addr: Nacos server address. spring.cloud.nacos.config.prefix: Prefix for dataId, defaults to spring.application.name. spring.cloud.nacos.config.file-extension: File suffix (default properties). spring.cloud.nacos.config.group: Group name (default DEFAULT_GROUP). spring.cloud.nacos.config.namespace: Namespace for environment isolation.
The full dataId format is:
${spring.application.name}-${spring.profiles.active}.${spring.cloud.nacos.config.file-extension}If you need a custom prefix, set spring.cloud.nacos.config.prefix. Custom parameters must be placed in bootstrap.properties or bootstrap.yaml to take effect.
4. Advanced Features
4.1 Multi‑Environment Isolation
Nacos supports three hierarchical isolation dimensions: Namespace, Group, and Data ID. Each can be used to separate configurations for development, testing, and production.
Namespace: isolates environments (e.g., dev vs prod). Group: groups configurations. Data ID: the actual configuration file.
Examples of isolation:
Profiles isolation : set spring.profiles.active to generate different dataId values.
Group isolation : create a group in the Nacos console and set spring.cloud.nacos.config.group in the client.
Namespace isolation : create a namespace in the console, note its ID, and set spring.cloud.nacos.config.namespace to that ID.
4.2 Multiple File Loading
To load several configuration files (e.g., redis.properties and rabbitmq.properties) create them in Nacos and reference them in bootstrap.properties using spring.cloud.nacos.config.extension-configs:
# Load multiple files with dynamic refresh
spring.cloud.nacos.config.extension-configs[0].data-id=redis.properties
spring.cloud.nacos.config.extension-configs[0].group=DEFAULT_GROUP
spring.cloud.nacos.config.extension-configs[0].refresh=true
spring.cloud.nacos.config.extension-configs[1].data-id=rabbitmq.properties
spring.cloud.nacos.config.extension-configs[1].group=DEFAULT_GROUP
spring.cloud.nacos.config.extension-configs[1].refresh=trueAlternatively, use shared-configs for shared configuration across services.
# Shared configuration equivalent to the above
spring.cloud.nacos.config.shared-configs[0].data-id=redis.properties
spring.cloud.nacos.config.shared-configs[0].group=DEFAULT_GROUP
spring.cloud.nacos.config.shared-configs[0].refresh=true
spring.cloud.nacos.config.shared-configs[1].data-id=rabbitmq.properties
spring.cloud.nacos.config.shared-configs[1].group=DEFAULT_GROUP
spring.cloud.nacos.config.shared-configs[1].refresh=trueWhen the same key appears in multiple sources, the priority order is A < B < C, where:
A – shared data IDs defined by spring.cloud.nacos.config.shared-dataids.
B – extension configs defined by spring.cloud.nacos.config.ext-config[n].data-id.
C – automatically generated configs based on application name and profile.
5. Summary
Nacos is a core component of the Spring Cloud Alibaba ecosystem, serving both as a service registry and a configuration center. Mastering its configuration‑center features—basic setup, dynamic refresh, multi‑environment isolation, and multi‑file loading—greatly simplifies microservice configuration management.
6. References
https://sca.aliyun.com/docs/2.2.x/user-guide/nacos/overview/
https://spring.didispace.com/spring-cloud/spring-cloud-alibaba-1.html
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.
Pan Zhi's Tech Notes
Sharing frontline internet R&D technology, dedicated to premium original content.
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.
