Spring cloud Alibaba micro service component Nacos

Time:2022-5-24

1、 What is Nacos

Official: a dynamic platform that is easier to build cloud native applicationsService discovery(Nacos Discovery)、Service configuration(Nacos config) and service management platform.
Set registration center + configuration center + service management platform

Nacos features include:
-Service discovery and service health monitoring
-Dynamic configuration service
-Dynamic DNS Service
-Service and metadata management

2、 Nacos registry

Manage all microservices and solve the problems of complex calling relationship and difficult maintenance between microservices

2.1. Evolution of registration center and its design idea

Call other services directly through resttemplate, but when there are many services, the IP is difficult to maintain. Each time the IP changes, the service must be modified and redeployed.

Spring cloud Alibaba micro service component Nacos

Manually maintain a service registry, which can be saved to the database.
However, how to balance the load after the horizontal expansion of the service? How to deal with the problems such as one of the services is down and cannot be invoked next time

Spring cloud Alibaba micro service component Nacos

Configure all configurations in nginx In conf, the problem is that the configuration is very cumbersome, and how to deal with the downtime of one of the services.

Spring cloud Alibaba micro service component Nacos

Using the registry can solve the above problems, but how to deal with service downtime in multiple services.

Spring cloud Alibaba micro service component Nacos

The registration center introduces a heartbeat mechanism. Each service maintains a scheduled task locally and sends a heartbeat to the registration center every other period of time. If the registration center does not receive the heartbeat of the service within a period of time, it considers that the service has been down and modifies the status of the service. If it does not receive the heartbeat for a longer time, it will directly eliminate the service. If the service is stopped, the service will call the logoff interface, which will also eliminate the service. The service calling interface will also have a heartbeat mechanism to pull the service state of the calling interface in real time and save the service in the form of cache. No longer use the load balancing of nginx server, but use the load balancing of client, that is, the load balancing of micro server.

Spring cloud Alibaba micro service component Nacos

Nacos official description

2.2 Nacos registry architecture

Spring cloud Alibaba micro service component Nacos

2.3 core functions

Nacos discovery

Service registration: the Nacos client will register its own service with the Nacos server by sending a rest request and provide its own metadata, such as IP address, port and other information. After Nacos server receives the registration request, it will store these metadata information in a double-layer memory map.

Service heartbeat: after the service is registered, the Nacos client will maintain a regular heartbeat to continuously notify the Nacos server that the service is always available to prevent it from being rejected. The default is to send a heartbeat every 5S.

Service synchronization: Nacos server clusters will synchronize service instances with each other to ensure the consistency of service information. leader raft

Service discovery: when calling the service of the service provider, the service consumer (Nacos client) will send a rest poem to the Nacos server to obtain the service list registered above, cache it locally in the Nacos client, and open it locally in the Nacos client
Start a scheduled task to regularly pull the latest registry information of the server and update it to the local cache

Service health check: Nacos server will start a scheduled task to check the health of registered service instances. For instances that do not receive client heartbeat for more than 15s, its health attribute will be set to false (not found when client service is found). If an instance does not receive heartbeat for more than 30 seconds, it will be directly rejected (if the rejected instance resumes sending heartbeat, it will be re registered)

Differences between mainstream registries

Nacos supports CP + AP, and AP is used by default

Spring cloud Alibaba micro service component Nacos

3、 Nacos server deployment

Nacos download address

The version correspondence is as follows

Spring cloud Alibaba micro service component Nacos

$ tar -zxvf nacos-server-1.4.2.tar.gz -C /opt/
$ cd /opt/nacos
$ vim /nacos/bin/startup.sh

Spring cloud Alibaba micro service component Nacos

#Start Nacos on a single machine and execute the command
$ bin/startup.sh -m standalone
#Or change the cluster to standalone and start it on a single machine
#Global configuration file properties
You can modify the data source, port, login user name, password and other information.
After startup, the log path is Nacos / logs

Spring cloud Alibaba micro service component Nacos

Access the management side of Nacos:http://127.0.0.1:8848/nacosThe default username and password is Nacos / Nacos

4、 Nacos client service setup

Project module preparation

Copy two services, modify the artifact ID of the service, and associate them in the parent POM file
<modules>
 <module>order-nacos</module>
 <module>stock-nacos</module>
</modules>
If it is found that the service is gray, find ignored files in the setting and uncheck it

Spring cloud Alibaba micro service component Nacos

Spring cloud Alibaba micro service component Nacos

4.1. Introducing dependency

Spring cloud & spring cloud Alibaba is supported in the parent POM, and dependency is introduced

<dependencyManagement>
    <dependencies>
        <dependency>
            <groupId>com.alibaba.cloud</groupId>
            <artifactId>spring-cloud-alibaba-dependencies</artifactId>
            <version>${spring.cloud.alibaba.version}</version>
            <type>pom</type>
            <scope>import</scope>
        </dependency>

        <!--  Version manager for spring cloud -- >
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-dependencies</artifactId>
            <version>${spring.cloud.version}</version>
            <type>pom</type>
            <scope>import</scope>
        </dependency>
    </dependencies>
</dependencyManagement>

Project introduction dependency

<!--  Nacos service registration and discovery -- >
<dependency>
    <groupId>com.alibaba.cloud</groupId>
    <artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
</dependency>

Modify application YML file

server:
  port: 8020
#Application name (Nacos will automatically treat this name as the service name)
spring:
  application:
    name: order-server
  cloud:
    nacos:
      server-addr: 127.0.0.1:8848
      discovery:
        username: nacos
        password: nacos
        #Named environments are used to isolate services
        namespace: public


server:
  port: 8021
#Application name (Nacos will automatically treat this name as the service name)
spring:
  application:
    name: stock-server
  cloud:
    nacos:
      server-addr: 127.0.0.1:8848
      discovery:
        username: nacos
        password: nacos
        #Named environments are used to isolate services
        namespace: public

Start the service and you can see the log printed on the console. The service has been registered with Nacos. Open the page and you can see that the service has been registered with Nacos.

Spring cloud Alibaba micro service component Nacos

Spring cloud Alibaba micro service component Nacos

Now, if you want to call the interfaces of other services, you don't need to specify the IP, just specify the service name directly.

Spring cloud Alibaba micro service component Nacos

But at this time http://127.0.0.1:8020/order/add Interface. An error is found and no instance is found, but the service has indeed registered Nacos. This is because the load balancer has to be configured. At this time, the interface is called successfully!

Spring cloud Alibaba micro service component Nacos

Spring cloud Alibaba micro service component Nacos

Load balancing test

The default load balancing is similar to polling. A stock server service is configured with port 8022, and IP printing is added where it is called.

Spring cloud Alibaba micro service component Nacos

Spring cloud Alibaba micro service component Nacos

Spring cloud Alibaba micro service component Nacos

Spring cloud Alibaba micro service component Nacos

Access interface test

Nacos uses ribbon's load balancing mechanism by default.

Spring cloud Alibaba micro service component Nacos

Spring cloud Alibaba micro service component Nacos

Spring cloud Alibaba micro service component Nacos

expand

Of course, Nacos can not only be used in micro services, but also provides a restful interface for developersOpen API Guide

This work adoptsCC agreement, reprint must indicate the author and the link to this article