preface

I’ve already written a lot of articles about NACOS, such as “Spring Cloud Integration with NACOS Service Discovery Source Analysis?” At present, I also plan to write a technical analysis column of Spring Cloud, a technical framework and a technical framework for everyone to break down the analysis principle and implementation.

With Nacos as a starting point, this article will complement the deployment of the Nacos Server and the invocation of the Nacos Client to get a sense of what Nacos contains. This is the basis for using NaCos and will be the basis for further analysis. It is highly recommended to study it together.

Deployment of Nacos Server

The deployment of Nacos Server is described in detail in the official manual (https://nacos.io/zh-cn/docs/d…). . Leaving aside other ways to deploy, we’ll focus on building and deploying through source code, which is also the best way to learn.

Basic environment requirements for Nacos deployment: JDK 1.8+, Maven 3.2.x+, ready to go.

Download the source code from GitHub:

/ git clone / / https://github.com/alibaba/nacos.git/download source code into the source directory CD nacos / / / compiling packaging MVN Prelease -- nacos -dmaven.test. skip=true clean install -u // Check out the generated jar ls-al distribution/target/ // Subsequent executable boot CD distribution/target/nacos - server - $version/nacos/bin

After the above command into the bin directory, usually have a different environment startup script:

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

Execute the script for the corresponding environment to start, and the standalone parameter means run in stand-alone mode:

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

This works for packaging and deployment, as well as for starting the service locally, but if you’re learning the source code, you can simply execute the main method (the Nacos class) in Console (nacos-console).

Execute the main method startup, which by default is also in cluster mode. You can specify stand-alone startup using JVM parameters:

-Dnacos.standalone=true

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

@SpringBootApplication(scanBasePackages = "com.alibaba.nacos") @ServletComponentScan @EnableScheduling public class Nacos {public static void main(String[] args) {public static void main(String[] args) {public static void main(String[] args) System.setProperty(Constants.STANDALONE_MODE_PROPERTY_NAME, "true"); SpringApplication.run(Nacos.class, args); }}

After the above steps, we are ready to start a Nacos Server, we will see how to use it.

NACOS manages the backend

When Nacos Server is started, the console prints the following log message:

,--. ,--.'| ,--,: : | 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 ' : | ; . ', ". -. | '; : __ | | ` -. \ | | '` -' / /,. | '|', '| \ \ / / / ` --'/' : |. :.' \ : : '----' '. /; . | |,. -. / \ \ / ` - '-' - '` - ` -' ` -- '

Through the above log, we can see that startup mode to “stand u.s mode”, port 8848, management background is: http://192.168.1.190:8848/nac… .

We have direct access to the machine service here: http://127.0.0.1:8848/nacos/i… .

By default, both the user and password are nacos. After successful login, you can click casually, which includes configuration management, service management, permission management, namespace, cluster management several sections.

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

Execute a curl command to register the mock service:

The 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, if you look at the administration backend here, you will see that a record has been added to the list of services.

Click on the details of this record to see more information.

Since we registered a service with a command above, the service does not exist, and Nacos Server periodically checks the health status of the service. You’ll notice that the service disappears after a while. This is the case where the Nacos Server discovery service is “down”, so remove it.

If you want to do something like this, you can use the curl command or the client tool to do it. You can use the curl command to look at the data in the background.

Service discovery command:

The curl -x GET http://127.0.0.1:8848/nacos/v1/ns/instance/list? serviceName=nacos.naming.serviceName'

Issue the configuration command:

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

Get the configuration command:

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

Spring Cloud integration with NACOS

Finally, let’s take a simple example of integrating Nacos into a Spring Cloud project to see if we can successfully register the service. The invocation of services between the Spring Cloud will be covered in a later article.

First, create a new Spring Boot project and introduce Spring Cloud and Spring Cloud Alibaba dependencies. The complete pom.xml file is as follows:

<? The 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 > < the parent > < groupId > org. Springframework. Boot < / groupId > The < artifactId > spring - the boot - starter - parent < / artifactId > < version > 2.4.2 < / version > < relativePath / > <! -- lookup parent from repository --> </parent> <groupId>com.springcloud</groupId> < artifactId > spring - the cloud - alibaba - nacos < / artifactId > < version > 0.0.1 - the SNAPSHOT < / version > <name>spring-cloud-alibaba-nacos</name> <description>springcloud Alibaba nacos</ description> <properties> < Java version > 1.8 < / Java version > < spring - the boot. Version > 2.4.2 < / spring - the 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>

DependencyManagement defines the version information of Spring Cloud and Spring Cloud Alibaba. The important thing to note here is that there is a limitation between the Spring Cloud and Spring Boot versions. This can be in https://spring.io/projects/sp…

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 point, you can start the service and look at the console of the Nacos Server. You can see that you registered the service with Nacos just as you did with the curl command above.

summary

This article prepares you for further learning about the SpringCloud by showing you how to deploy Nacos services and how to integrate them into the SpringCloud. It also involves some practical experience and pit.

Nacos series

  • Spring Cloud integration with Nacos service discovery source code analysis?
  • “To learn the service discovery of micro-services? Let’s first understand some popular science knowledge”
  • The Soul Ferry of Microservice — NaCos: A Complete Guide to the Principle
  • You are also interested in reading the source code. Tell me how I read the Nacos source code.
  • Nacos already has an Optional use case, so it’s time to take this syntax seriously.
  • Why is the String.intern method used in the NACOS source code?
  • “Learning Nacos? Let’s get the service up first, a practical tutorial”