How to Quickly Set Up Nacos for Service Discovery and Configuration
This guide walks you through installing Nacos via source compilation or Docker, starting it in standalone mode, and using its core OpenAPI endpoints to register, heartbeat, query, and deregister service instances, complete with code snippets and visual screenshots.
1. Environment Setup
There are two ways to set up Nacos: manual compilation and startup, or using a Docker container.
1.1 Manual Compilation and Startup
Two steps are required: compile Nacos and then start it.
Step 1 – Compile Nacos
You can either compile from source or download the official binary.
Method 1 – Source Compilation
Clone the Nacos project from GitHub and build it with Maven. git clone https://github.com/alibaba/nacos.git After downloading the source, navigate to the directory and run:
mvn -Prelease-nacos -Dmaven.test.skip=true clean install -UThe first compilation may take a long time because it downloads many dependencies.
When finished, the compiled files are located at:
distribution/target/nacos-server-$version/nacosTip: If GitHub download is slow, consider using Gitee (a Chinese mirror) for faster access.
Method 2 – Download Official Binary
The official pre‑compiled package can be downloaded directly from the GitHub Releases page.
On the Nacos project homepage, click the Releases section on the right and download the zip file.
Step 2 – Startup
Run the following command in the compiled Nacos directory: sh bin/startup.sh -m standalone The -m standalone flag starts Nacos in single‑node mode, which is the simplest way for practice.
On Windows, you can execute startup.cmd instead.
1.2 Docker Container
The official Nacos Docker image can be used directly.
# Clone the Nacos Docker project
git clone https://github.com/nacos-group/nacos-docker.git
# Start in standalone mode
cd nacos-docker
docker-compose -f example/standalone-derby.yaml upThis launches three containers:
prometheus – collects Nacos metrics for monitoring
grafana – visualizes the collected metrics
nacos-standalone – the Nacos server itself
The Nacos container exposes port 8848; access the console at http://localhost:8848/nacos.
Default username and password are both nacos.
2. Experience Nacos API
The following four core APIs are demonstrated: register instance, heartbeat, query service list, and deregister instance.
2.1 Register Instance
Send a POST request:
curl -X POST 'http://127.0.0.1:8848/nacos/v1/ns/instance?serviceName=service-a&ip=192.168.1.1&port=8080'The service appears in the console, even though the service name does not exist yet; Nacos treats any registered instance as alive as long as it continues sending heartbeats.
2.2 Instance Heartbeat
Send a PUT request:
curl -X PUT 'http://127.0.0.1:8848/nacos/v1/ns/instance/beat?serviceName=service-a&beat=%7B%22ip%22:%22192.168.31.1%22,%22port%22:8080%7D'The beat parameter is a JSON string that must be URL‑encoded.
2.3 Query Service Instance List
Send a GET request:
curl -X GET 'http://127.0.0.1:8848/nacos/v1/ns/instance/list?serviceName=service-a'The response returns a JSON object containing the registered instance details.
{
"metadata":{},
"dom":"service-a",
"cacheMillis":3000,
"useSpecifiedURL":false,
"hosts":[
{
"valid":true,
"marked":false,
"metadata":null,
"instanceId":"192.168.31.1#8080#DEFAULT#DEFAULT_GROUP@@service-a",
"port":8080,
"healthy":true,
"ip":"192.168.31.1",
"clusterName":"DEFAULT",
"weight":0,
"ephemeral":true,
"serviceName":"service-a",
"enabled":true
}
],
"name":"DEFAULT_GROUP@@service-a",
"checksum":"2be56e35d79c2cf9982e0b5e19b2bd29",
"lastRefTime":1604222416258,
"env":"",
"clusters":""
}2.4 Deregister Instance
Send a DELETE request:
curl -X DELETE 'http://127.0.0.1:8848/nacos/v1/ns/instance?serviceName=service-a&ip=192.168.31.1&port=8080'After deregistration, the console shows an empty service list.
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.
Java High-Performance Architecture
Sharing Java development articles and resources, including SSM architecture and the Spring ecosystem (Spring Boot, Spring Cloud, MyBatis, Dubbo, Docker), Zookeeper, Redis, architecture design, microservices, message queues, Git, etc.
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.
