Using Nacos as Service Registry and Configuration Center in Spring Cloud
This article provides a step‑by‑step tutorial on integrating Nacos as a service discovery registry and dynamic configuration center for Spring Cloud microservices, covering dependency setup, server installation, service registration, configuration management, namespaces, groups, and multi‑config sets with live refresh capabilities.
Hello, I am Wukong. In the previous article we explained the architecture of OpenFeign; this time we dive into the Nacos ecosystem. Nacos, a mature service registry and configuration center from Alibaba, is widely used as a cloud‑native standard.
1. Nacos as a Registration Center
1.1 Add Nacos discovery dependency
In the passjava-common module's pom.xml add the following dependency:
com.alibaba.cloud
spring-cloud-starter-alibaba-nacos-discovery1.2 Download and start Nacos Server
Download the Nacos Server zip from GitHub . After extracting, run the startup script according to your OS:
Linux/Unix/Mac: sh startup.sh -m standalone
Windows: cmd startup.cmd
If Windows shows Please set the JAVA_HOME variable in your environment, We need java(x64)! jdk8 or later is better! , edit startup.cmd and replace %JAVA_HOME% with the actual JDK path, e.g. C:\Program Files\Java\jdk1.8.0_131 . After correction the server starts successfully.
1.3 Configure each microservice to point to Nacos
Add the server address to the application.yml of each service (passjava‑question, passjava‑channel, passjava‑content, passjava‑member, passjava‑study):
spring:
cloud:
nacos:
discovery:
server-addr: 127.0.0.1:88481.4 Add @EnableDiscoveryClient annotation
@EnableDiscoveryClient
@MapperScan("com.jackson0714.passjava.question.dao")
@SpringBootApplication
public class PassjavaQuestionApplication {
public static void main(String[] args) {
SpringApplication.run(PassjavaQuestionApplication.class, args);
}
}1.5 Set service name
spring:
application:
name: passjava-question1.6 Access Nacos UI
Open http://localhost:8848/nacos/index.html#/login and log in with username nacos and password nacos . The UI lists all registered services such as passjava-channel , passjava-member , etc.
2. Nacos as a Configuration Center
2.1 Traditional local configuration
Define properties in application.properties :
member.nickname = "悟空聊架构"
member.age = "18"Inject them with @Value in a controller and expose an endpoint to return the values.
2.2 Add Nacos Config dependency
com.alibaba.cloud
spring-cloud-starter-alibaba-nacos-config2.3 Bootstrap configuration
Create bootstrap.properties (higher priority than other config files) for the member service:
spring.application.name=passjava-member
spring.cloud.nacos.config.server-addr=127.0.0.1:88482.4 Add configuration in Nacos UI
Create a Data ID passjava-member.properties in the DEFAULT_GROUP with content:
member.nick="悟空"
member.age=102.5 Enable dynamic refresh
Annotate a controller with @RefreshScope so that changes in Nacos are pushed without restarting the service:
@RefreshScope
@RestController
@RequestMapping("member/sample")
public class SampleController {}The console logs show refresh events such as:
Refresh keys changed: [member.age]
... dataId=passjava-member.properties, group=DEFAULT_GROUP ...2.6 Test the result
Calling http://localhost:10000/member/sample/test-local-config returns the nickname and age defined in Nacos, confirming that the configuration is live.
2.7 Namespaces
To isolate configurations per microservice, create a namespace for each (e.g., passjava-member ) and set the namespace in bootstrap.properties :
spring.cloud.nacos.config.namespace=passjava-memberAfter adding configuration under that namespace, the service retrieves the correct values.
2.8 Groups (environments)
Use groups to separate dev, test, prod configurations. Create a group prod in Nacos and set it in bootstrap.properties :
spring.cloud.nacos.config.group=prodRequests now return the values from the prod group.
2.9 Multiple configuration sets
Complex configurations (datasource, mybatis‑plus, etc.) can be split into separate YAML files and imported 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
... (similar entries for mybatis.yml and more.yml)After adding these files in Nacos and enabling refresh, the service can read them at runtime.
Conclusion
Using Nacos you can:
Introduce the Nacos dependency.
Configure the Nacos data source.
Manage configuration data sets (DataId).
Enable dynamic refresh with @RefreshScope .
Inject values via @Value .
Prefer configuration‑center values over local files.
Separate configurations by namespace.
Separate environments by group.
Use multiple configuration sets via extension-configs .
The underlying principles of Nacos are continuously evolving; happy new year and keep learning!
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.