Why Choose Nacos Over Eureka? A Hands‑On Guide to Service Discovery, Config, and Cluster Setup
This comprehensive tutorial explains Nacos' advantages over Eureka, its Raft‑based consistency, integration with Spring Cloud Alibaba, detailed architecture, installation steps, standalone and cluster modes, and practical demos for using Nacos as both a service registry and a configuration center in microservice applications.
1. Nacos Advantages
Nacos provides a new choice for service registration and discovery beyond Eureka and Consul, offering out‑of‑the‑box simplicity and ease of use.
1.1 Comparison with Eureka
1. Eureka 2.0 source code is closed. 2. Nacos registers more instances than Eureka according to the official site. 3. Nacos uses the Raft protocol, giving its cluster stronger consistency than Eureka.
Raft, a distributed consensus algorithm published in 2013, is praised for its simplicity and ease of implementation compared to other algorithms.
Raft Data Consistency Strategy
Raft relies on a Leader node to ensure cluster data consistency. Clients send data to the Leader, which marks it as uncommitted, replicates it to Followers, waits for a majority response, then marks it as committed and acknowledges the client.
1.2 Comparison with Spring Cloud Config
Three major advantages
Spring Cloud Config often requires Git and Spring Cloud Bus for dynamic updates.
Spring Cloud Config lacks a visual interface.
Nacos Config uses long‑living connections for rapid configuration updates, outperforming Spring Cloud Config.
2. Spring Cloud Alibaba Suite
The suite includes three main components:
Nacos – dynamic service discovery, configuration, and service management platform.
Sentinel – traffic‑based flow control, circuit breaking, and system load protection.
AliCloud OSS – scalable, secure, low‑cost object storage.
Component mapping with Spring Cloud Netflix:
Nacos = Eureka/Consul + Config + Admin
Sentinel = Hystrix + Dashboard + Turbine
Dubbo = Ribbon + Feign
RocketMQ = RabbitMQ
Schedulerx = Quartz
3. Nacos Architecture and Installation
3.1 Architecture
Nacos combines service registry, configuration center, and service management, similar to a fusion of Eureka/Consul, Config, and Admin.
3.2 Server Download and Installation
Installation tutorial: https://www.cnblogs.com/crazymakercircle/p/11992539.html
4. Running Nacos Server
4.1 Two Modes
standalone
cluster
4.2 Standalone Mode
Used for demos and testing; run without configuration changes: sh bin/startup.sh -m standalone On Windows: cmd bin/startup.cmd -m standalone Access the console at http://cdh1:8848/nacos/index.html (default username/password: nacos nacos).
4.3 Cluster Mode
Suitable for production; requires MySQL and two configuration files ( conf/cluster.conf and conf/application.properties).
conf/cluster.conf<br/>conf/application.propertiesExample cluster.conf:
# IP list<br/>10.10.109.214<br/>11.16.128.34<br/>11.16.128.36MySQL configuration in application.properties:
db.num=1<br/>db.url.0=jdbc:mysql://localhost:3306/nacos_config?characterEncoding=utf8&connectTimeout=1000&socketTimeout=3000&autoReconnect=true<br/>db.user=root<br/>db.password=rootCluster mode stores data in MySQL; standalone mode stores data in an embedded Derby database located at NACOS_PATH/data/derby-data.
Note: MySQL 8.0 is not supported.
5. Practical Demo 1 – Using Nacos as a Service Registry
5.1 Project Structure
Two modules: service-provider-demo and service-consumer-demo.
5.2 Adding Nacos Client Dependency
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
</dependency>5.3 Provider Service
Enable discovery with @EnableDiscoveryClient and expose a simple REST endpoint:
package com.crazymaker.cloud.nacos.demo.controller;
@RestController
@RequestMapping("/echo")
public class EchoController {
@RequestMapping(value="/{string}", method=RequestMethod.GET)
public String echo(@PathVariable String string) {
return "echo: " + string;
}
}Configuration (application.yml):
spring:
application:
name: service-provider-demo
cloud:
nacos:
discovery:
server-addr: ${NACOS_SERVER:cdh1:8848}
server:
port: 180805.4 Consumer Service
Use Feign client to call the provider:
@FeignClient(name="service-provider-demo")
public interface EchoClient {
@GetMapping("/echo/{string}")
String echo(@PathVariable("string") String string);
}Controller:
@RestController
@RequestMapping("/echo")
public class EchoConsumerController {
@Resource
EchoClient echoClient;
@GetMapping("/{string}")
public String echoRemoteEcho(@PathVariable String string) {
return "provider echo is:" + echoClient.echo(string);
}
}Configuration (application.yml):
spring:
application:
name: sevice-consumer-demo
cloud:
nacos:
discovery:
server-addr: 127.0.0.1:8848
server:
port: 18081Access via Swagger UI at http://localhost:18080/provider/swagger-ui.html and http://localhost:18081/consumer/swagger-ui.html.
6. Practical Demo 2 – Using Nacos as a Configuration Center
6.1 Core Concepts
Profile : environment‑specific configuration (dev, test, prod).
Data ID : each profile maps to a Data ID in Nacos (format: ${prefix}-${spring.profiles.active}.${file-extension}).
Group : default DEFAULT_GROUP, can be customized for organization.
6.2 Adding Config Dependency
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-nacos-config</artifactId>
</dependency>6.3 Bootstrap Configuration (bootstrap.yml)
spring:
application:
name: nacos-config-demo-provider
profiles:
active: dev
cloud:
nacos:
discovery:
server-addr: ${NACOS_SERVER:cdh1:8848}
config:
server-addr: ${NACOS_SERVER:cdh1:8848}
prefix: nacos-config-demo
group: DEFAULT_GROUP
file-extension: yaml
server:
port: 18083
servlet:
context-path: /config6.4 Sample Controller
@RestController
@RequestMapping("/config")
public class ConfigGetController {
@Value("${foo.bar:empty}")
private String bar;
@Value("${spring.datasource.username:empty}")
private String dbusername;
@GetMapping("/bar")
public String getBar() { return "bar is :" + bar; }
@GetMapping("/dbusername")
public String getDbusername() { return "db username is :" + dbusername; }
}Access via Swagger UI at http://localhost:18083/config/swagger-ui.html to view configuration values retrieved from Nacos.
7. Configuration Isolation
Isolation can be achieved via Nacos server address, namespace ID, and group name, allowing separation by environment, project, or module.
8. Nacos Cluster Deployment
IP Planning
Deploy three instances locally (nacos, nacos1, nacos2) with ports 8848, 8847, 8846 and configure MySQL as the data source in conf/application.properties.
spring.datasource.platform=mysql
db.num=1
db.url.0=jdbc:mysql://localhost:3306/nacos?characterEncoding=utf8&connectTimeout=1000&socketTimeout=3000&autoReconnect=true
db.user=root
db.password=123456Cluster Configuration
Create conf/cluster.conf with the three node addresses:
127.0.0.1:8848
127.0.0.1:8847
127.0.0.1:8846Start each instance with ./startup.sh; the default mode is cluster.
Reverse Proxy (Nginx) for High Availability
upstream nacos_server {
server 127.0.0.1:8848;
server 127.0.0.1:8847;
server 127.0.0.1:8846;
}
server {
listen 8648;
location / {
proxy_pass http://nacos_server;
}
}Access the clustered console via http://localhost:8648/nacos/#/clusterManagement.
Conclusion
Nacos offers a unified solution for service discovery and configuration management in cloud‑native microservice architectures, supporting both standalone and clustered deployments, with flexible isolation mechanisms and easy integration into Spring Cloud Alibaba projects.
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.
Open Source Linux
Focused on sharing Linux/Unix content, covering fundamentals, system development, network programming, automation/operations, cloud computing, and related professional knowledge.
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.
