Learning Nacos? Let's start with services and practical tutorials~


I have written many articles related to Nacos, such as< Spring Cloud integration Nacos service discovery source code analysis? Turned three sets of source code, quality and freshness! >At present, it is also planned to write a column on technical analysis of Spring Cloud, one technical framework and one technical framework to disassemble and analyze the principle and Implementation for you.

Since we start with Nacos, this article will supplement the deployment of Nacos Server and the call of Nacos Client to intuitively understand what functions Nacos contains. This is the basis for using Nacos and the basis for subsequent in-depth analysis. It is strongly recommended to study together.

Deployment of Nacos Server

The deployment of Nacos Server has been described in detail in the official manual, corresponding to the link address( https://nacos.io/zh-cn/docs/deployment.html ). Let's not talk about other deployment methods for the time being. We focus on building and deploying in the form of source code, which is also the best way to learn.

Basic environment requirements for Nacos deployment: JDK 1.8 +, Maven 3.2 X +, ready.

Download the source code from Github:

// Download source code
git clone https://github.com/alibaba/nacos.git
// Enter the source directory
cd nacos/
// Perform compilation and packaging operations
mvn -Prelease-nacos -Dmaven.test.skip=true clean install -U
// View the generated jar package
ls -al distribution/target/

// Enter the packaged file directory, and the subsequent startup can be executed
cd distribution/target/nacos-server-$version/nacos/bin

After entering the bin directory through the above command, there are usually startup scripts in different environments:

shutdown.cmd	shutdown.sh	startup.cmd	startup.sh

Execute the script of the corresponding environment to start it. The parameter standalone represents stand-alone mode operation:

// Linux/Unix/Mac
sh startup.sh -m standalone
// ubuntu
bash startup.sh -m standalone
// Windows
startup.cmd -m standalone

The above operations are applicable to packaging and deployment, as well as local startup services. However, if you are learning the source code, you can directly execute the main method (Nacos class) in the console (Nacos console).

Execute the main method to start. The default mode is cluster mode. You can specify the single machine startup through JVM parameters:


For convenience, you can also directly add this parameter to the source code of the startup class:

@SpringBootApplication(scanBasePackages = "com.alibaba.nacos")
public class Nacos {

    public static void main(String[] args) {
        // Set stand-alone startup in the form of environment variables
        System.setProperty(Constants.STANDALONE_MODE_PROPERTY_NAME, "true");

        SpringApplication.run(Nacos.class, args);

After the above steps, we can start a Nacos Server. Let's see how to use it later.

Nacos management background

When starting the Nacos Server, the console will print the following log information:

   ,--,:  : |                                           Nacos 
,`--.'`|  ' :                       ,---.               Running in stand alone mode, All function modules
|   :  :  | |                      '   ,'\   .--.--.    Port: 8848
:   |   \ | :  ,--.--.     ,---.  /   /   | /  /    '   Pid: 47395
|   : '  '; | /       \   /     \.   ; ,. :|  :  /`./   Console:
'   ' ;.    ;.--.  .-. | /    / ''   | |: :|  :  ;_
|   | | \   | \__\/: . ..    ' / '   | .; : \  \    `.      https://nacos.io
'   : |  ; .' ," .--.; |'   ; :__|   :    |  `----.   \
|   | '`--'  /  /  ,.  |'   | '.'|\   \  /  /  /`--'  /
'   : |     ;  :   .'   \   :    : `----'  '--'.     /
;   |.'     |  ,     .-./\   \  /            `--'---'
'---'        `--`---'     `----'

From the above log, we can see that the startup mode is "stand alone mode", the port is 8848, and the management background is: .

Here we directly access the local service: .

By default, the user and password are both nacos. After successful login, you can click freely, including configuration management, service management, permission management, namespace and cluster management.

You can see that the default namespace is public and the default user is nacos.

At this time, execute a curl command to register the simulation service:

curl -X POST ''

After execution, check the management background here and you will find that a record has been added to the service list.

Click the details of this record to see more information.

Since the above service is registered through a command and does not exist, Nacos Server will regularly check the health status of the service. You will find that after a while, the service will disappear. This means that the Nacos Server discovery service is "hung up" and removed.

For other similar operations, you can try to use curl command or client tools, and cooperate with the management background to see the corresponding data.

Service discovery command:

curl -X GET ''

Issue configuration command:

curl -X POST ""

Get configuration command:

curl -X GET ""

Spring Cloud integrates Nacos

Finally, let's take a simple example to integrate Nacos into the Spring Cloud project to see whether the service can be registered successfully. We will specifically explain the service invocation between Spring Cloud and cloud later.

First, create a new Spring Boot project, introduce the dependency of Spring Cloud and Spring Cloud Alibaba, and complete POM The XML file is as follows:

<?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">
        <relativePath/> <!-- lookup parent from repository -->
    <description>springcloud alibaba nacos integrate</description>



Dependency management defines the dependent version information of Spring Cloud and Spring Cloud Alibaba. It should be noted here that there are restrictions between Spring Cloud and Spring Boot versions. This can be in https://spring.io/projects/spring-cloud View in.

Then, configure the nacos registry server address in yml:

    name: nacos-spring-cloud-learn

At this time, start the service and check the console of Nacos Server. You will find that the service has been successfully registered in Nacos, just like the effect of directly executing curl command.


By explaining how to deploy Nacos services and how to integrate them into spring cloud, this article is ready for further study of spring cloud. It also involves some practical experience and pits.

Nacos series

New horizon of procedure
Official account " "New horizons of program", a platform that enables you to improve your soft power and hard technology simultaneously, and provides a large amount of data

Keywords: Spring Boot Spring Cloud Microservices Nacos

Added by bough on Thu, 10 Feb 2022 10:55:53 +0200