Using Nacos as a Configuration Center for Multi‑Environment Management in Spring Cloud
The article explains how to leverage Nacos as a configuration center in Spring Cloud to manage multi‑environment settings using Data ID naming, configuration groups, and namespaces, providing code examples, command‑line usage, and a recommendation to prefer namespaces for clear separation and permission control.
This article discusses how to use Nacos as a configuration center to solve configuration management problems across different environments (development, testing, gray release, production) in a Spring Boot project.
Similar to Maven’s groupId , artifactId , and version coordinates, Nacos provides three identifiers—Namespace, Data ID, and Group—to uniquely locate a configuration set.
Three ways to separate environments are presented:
Data ID naming : include the environment name in the Data ID. Example format: ${prefix}-${spring.profiles.active}.${file-extension} When spring.profiles.active is empty, the hyphen is omitted, resulting in ${prefix}.${file-extension} .
Group separation : keep the same Data ID but assign different groups (e.g., DEV_GROUP , TEST_GROUP ) to each environment. The group can be set via the JVM argument -Dspring.cloud.nacos.config.group=DEV_GROUP .
Namespace separation : create separate namespaces for each environment (e.g., ns_dev , ns_test ) and specify the target namespace with -Dspring.cloud.nacos.config.namespace=ns_dev . This method also supports combining with profile and group arguments.
Sample project files are provided to illustrate the setup.
pom.xml (relevant dependencies): <?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.3.6.RELEASE</version> </parent> <groupId>com.example</groupId> <artifactId>example</artifactId> <version>0.0.1-SNAPSHOT</version> <properties> <java.version>1.8</java.version> <spring-cloud-alibaba.version>2.2.3.RELEASE</spring-cloud-alibaba.version> </properties> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>com.alibaba.cloud</groupId> <artifactId>spring-cloud-starter-alibaba-nacos-config</artifactId> </dependency> </dependencies> <dependencyManagement> <dependencies> <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> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> </build> </project>
bootstrap.yml (Nacos connection configuration): spring: application: name: example cloud: nacos: config: server-addr: 192.168.100.10:8848 file-extension: yaml
HelloController.java (demonstrates @RefreshScope and @Value): package com.example.controller; import org.springframework.beans.factory.annotation.Value; import org.springframework.cloud.context.config.annotation.RefreshScope; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; @RestController @RequestMapping("/hello") @RefreshScope public class HelloController { @Value("${greet.hello}") private String greet; @GetMapping("/sayHi") public String sayHi() { return greet; } }
Command‑line examples for launching the application with different environment settings:
Data ID naming (profile test): java -Dspring.profiles.active=test -jar example-0.0.1-SNAPSHOT.jar
Group separation: java -Dspring.cloud.nacos.config.group=DEV_GROUP -jar example-0.0.1-SNAPSHOT.jar
Namespace separation (dev): java -Dspring.cloud.nacos.config.namespace=ns_dev -jar example-0.0.1-SNAPSHOT.jar
Combined usage (test profile, test group, test namespace): java -Dspring.profiles.active=test -Dspring.cloud.nacos.config.group=TEST_GROUP -Dspring.cloud.nacos.config.namespace=ns_test -jar example-0.0.1-SNAPSHOT.jar
In the final summary, the author notes that while Data ID and Group methods work, they lead to many configuration files and potential permission‑management issues. Using separate namespaces provides a clear, scalable way to isolate environments and manage access control.
Reference: Nacos Documentation
Top Architect
Top Architect focuses on sharing practical architecture knowledge, covering enterprise, system, website, large‑scale distributed, and high‑availability architectures, plus architecture adjustments using internet technologies. We welcome idea‑driven, sharing‑oriented architects to exchange and learn together.
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.