Comprehensive Guide to Using Nacos for Service Registration, Configuration Management, and Cluster Deployment
This article provides a detailed tutorial on Nacos, covering its advantages over Eureka, step‑by‑step setup for service registration and discovery, configuration management with dynamic refresh, shared configurations, persistence to MySQL, cluster deployment, and the trade‑offs between CP and AP modes in a cloud‑native microservices environment.
Preface
Nacos, an open‑source service registry and configuration center from Alibaba, aims to offer developers a convenient and easy‑to‑use framework.
The following diagram shows how Nacos replaces three major Spring Cloud components: Eureka , Config , and Bus .
Why is Nacos so popular?
Nacos helps you more agilely build, deliver, and manage a microservice platform. It is the service infrastructure for building service‑centric modern applications such as microservice and cloud‑native architectures.
Compared with Eureka and Config, Nacos eliminates the need to set up separate services, provides bilingual UI, and avoids the risk of component abandonment.
How to learn Nacos by yourself?
Start with the official documentation:
https://nacos.io/zh-cn/docs/what-is-nacos.html (Chinese/English)
https://spring-cloud-alibaba-group.github.io/github-pages/hoxton/en-us/index.html (English)
https://github.com/alibaba/nacos (GitHub repository)
Video tutorials are also available via the public account 码猿技术专栏 (keyword 9527 ).
Project version information
JDK 1.8+
Spring Boot 2.2.2.RELEASE
Spring Cloud Hoxton.SR3
Spring Cloud Alibaba 2.2.1.RELEASE
Starting Nacos service
Download the matching Nacos version (e.g., 1.2.1 ) from GitHub, unzip, and run startup.cmd (Windows) or startup.sh (Linux). Access the login page at http://localhost:8848/nacos (username: nacos, password: nacos).
Service registration and discovery
Create two modules: nacos-provider (service provider) and nacos-consumer (service consumer).
nacos‑provider
Add Maven dependency spring-cloud-starter-alibaba-nacos-discovery .
Configure application.yml with server port, application name, and Nacos address.
Enable discovery with @EnableDiscoveryClient .
Implement a simple REST controller.
Run the module; the service appears in Nacos service list.
nacos‑consumer
Add the same Maven dependency.
Configure application.yml similarly.
Enable discovery with @EnableDiscoveryClient .
Use a @LoadBalanced RestTemplate to call the provider via its service name.
Run the module; both services are visible in Nacos and can communicate.
Configuration management
Nacos can store configuration centrally and refresh it dynamically.
Adding configuration
Add a new module nacos-config and include the dependency spring-cloud-starter-alibaba-nacos-config . In bootstrap.yml set spring.application.name , spring.profiles.active , Nacos address, and file extension (properties or yaml).
Define a DataId (e.g., config.version ) in the Nacos console.
Reading configuration
@Component
@Data
public class DynamicConfigEntity {
@Value("${config.version}")
private String version;
}Expose it via a controller and verify the value at http://localhost:9003/nacos/test/1 .
Dynamic refresh
Add @RefreshScope to the component; after changing the value in Nacos and restarting, the new value is reflected without restarting the client.
Multi‑environment isolation (Namespace)
Create separate namespaces (e.g., dev, test, prod) in Nacos and specify the namespace ID in bootstrap.yml to isolate configurations per environment.
Business‑level isolation (Group)
Use groups (e.g., ORDER_GROUP , USER_GROUP ) to separate configurations for different services.
Shared configuration
Define shared config files in shared-configs list of bootstrap.yml and inject values with @Value . Example shared files: share-config1.properties (database URL) and share-config2.properties (database user).
Persistence
By default Nacos uses an embedded Derby database. To persist to MySQL, create a database, run nacos-mysql.sql , and configure the datasource in conf/application.properties .
Cluster deployment
Download the tar.gz package, modify conf/application.properties to set different ports (e.g., 8848, 8849, 8850), and create conf/cluster.conf with the list of node addresses.
Start each node with bash -f ./startup.sh . Verify the cluster in the Nacos console under "Cluster Management → Node List".
Nginx load balancing
upstream nacos {
server 172.16.1.84:8848;
server 172.16.1.84:8849;
server 172.16.1.84:8850;
}
server {
listen 80;
location / {
proxy_pass http://nacos;
}
}Clients can connect directly to the Nacos nodes or via the Nginx address.
CAP characteristics of Nacos
Nacos supports both CP and AP modes; the default is AP. In CP mode services are registered as permanent instances, while in AP mode they are temporary.
Conclusion
The article demonstrates how to quickly get started with Nacos for service registration, configuration management, dynamic refresh, multi‑environment isolation, shared configuration, persistence, and cluster deployment, emphasizing its cloud‑native design.
Code Ape Tech Column
Former Ant Group P8 engineer, pure technologist, sharing full‑stack Java, job interview and career advice through a column. Site: java-family.cn
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.