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

preface

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:

-Dnacos.standalone=true

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

@SpringBootApplication(scanBasePackages = "com.alibaba.nacos")
@ServletComponentScan
@EnableScheduling
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: http://192.168.1.190:8848/nacos/index.html
'   ' ;.    ;.--.  .-. | /    / ''   | |: :|  :  ;_
|   | | \   | \__\/: . ..    ' / '   | .; : \  \    `.      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: http://192.168.1.190:8848/nacos/index.html .

Here we directly access the local service: http://127.0.0.1:8848/nacos/index.html .

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 'http://127.0.0.1:8848/nacos/v1/ns/instance?serviceName=nacos.naming.serviceName&ip=20.18.7.10&port=8080'

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 'http://127.0.0.1:8848/nacos/v1/ns/instance/list?serviceName=nacos.naming.serviceName'

Issue configuration command:

curl -X POST "http://127.0.0.1:8848/nacos/v1/cs/configs?dataId=nacos.cfg.dataId&group=test&content=HelloWorld"

Get configuration command:

curl -X GET "http://127.0.0.1:8848/nacos/v1/cs/configs?dataId=nacos.cfg.dataId&group=test"

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">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.4.2</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.springcloud</groupId>
    <artifactId>spring-cloud-alibaba-nacos</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>spring-cloud-alibaba-nacos</name>
    <description>springcloud alibaba nacos integrate</description>
    <properties>
        <java.version>1.8</java.version>
        <spring-boot.version>2.4.2</spring-boot.version>
        <spring-cloud.version>2020.0.0</spring-cloud.version>
        <cloud-alibaba.version>2021.1</cloud-alibaba.version>
    </properties>
    <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-dependencies</artifactId>
                <version>${spring-cloud.version}</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
            <dependency>
                <groupId>com.alibaba.cloud</groupId>
                <artifactId>spring-cloud-alibaba-dependencies</artifactId>
                <version>${cloud-alibaba.version}</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
        </dependencies>
    </dependencyManagement>
    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-actuator</artifactId>
        </dependency>
        <dependency>
            <groupId>com.alibaba.cloud</groupId>
            <artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
            <exclusions>
                <exclusion>
                    <groupId>org.junit.vintage</groupId>
                    <artifactId>junit-vintage-engine</artifactId>
                </exclusion>
            </exclusions>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>

</project>

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:

spring:
  application:
    name: nacos-spring-cloud-learn
  cloud:
    nacos:
      discovery:
        server-addr: 127.0.0.1:8848

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.

Summary

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