This is the 16th day of my participation in the November Gwen Challenge. Check out the event details: The last Gwen Challenge 2021

Antecedents to review

We have covered the installation and deployment of Nacos in previous articles, and have a preliminary understanding of Nacos. From this article, we will start to use Nacos in projects.

Let’s take a look at integrating Nacos components into the SpringCloud project.

SpringCloud integrates Nacos service registry functionality

We need to create a SpringCloud project and then extend Nacos components from that project.

This project was created through IDEA.

Create a new project

Create a New Maven Project, file-New Project.

Go to this page and click Next.

The project path as shown.

Introduce nacOS-related JAR packages

After creating the project, the next step is to import the corresponding JAR package.

Modify the POM file with the following code:

<dependency>
    <groupId>com.alibaba.cloud</groupId>
    <artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
    <version>Hoxton.SR9</version>
</dependency>
Copy the code

Modify the Application class to introduce the @enableDiscoveryClient annotation to enable the NACOS service governance function

@EnableDiscoveryClient @SpringBootApplication public class Application { public static void main(String[] args) { SpringApplication.run(Application.class); }}Copy the code

Configure parameters related to Nacos

Modify the application. Yml file and add parameters related to Nacos

127.0.0.1:8848; This service refers to the installed NACOS server.

Server: port: 8080 Spring: Application: name: test Cloud: nacos: Discovery: server-addr: 127.0.0.1:8848Copy the code

Start the project

Run the Application class.

Check whether the service is successfully registered

Let’s go to the NACOS service website to see if the service has been successfully registered.

On the following menu, you can check whether the corresponding service is successfully registered.

conclusion

Today we will learn the basics of Nacos service governance. Of course, there are some other configuration items of Nacos service governance, which you can check out the official website documentation.

It’s important to learn how to read official documents.