Service registration and discovery-Nacos

(1 Introduction

The service consumer needs to call a cluster composed of multiple service providers. First, the service consumer needs to maintain the requested address of each node of the service provider cluster in the local configuration file. Second, if a node in the service provider cluster goes offline or goes down, the request address of this node needs to be deleted synchronously in the local configuration of the service consumer to prevent the request from being sent to the down node and causing the request to fail. In order to solve this kind of problem, it is necessary to introduce a service registration center. It mainly has the following functions:
1. Service address management.
2. Service registration.
3. Service dynamic perception.

(2) Basic introduction to Alibaba Nacos

Nacos is committed to solving the problems of unified configuration, service registration and discovery in microservices. It provides a set of simple and easy-to-use feature sets to help developers quickly realize dynamic service discovery, service configuration, service metadata and traffic management.Insert picture description here

(3) Basic use

Nacos supports three deployment modes, namely stand-alone, cluster and multi-cluster. It should be noted that Nacos relies on the Java environment and requires the use of JDK 1.8 or above.

Nacos service registration discovery related API description
Nacos provides SDK and Open API methods to complete service registration and discovery operations. Since the server only provides a REST interface, the SDK is essentially an encapsulation of HTTP requests.
Detailed documentation reference:
https://nacos.io/zh-cn/docs/sdk.html

(4) High-availability deployment of Nacos

In a distributed architecture, any middleware or application does not allow a single point to exist, so open source components generally support high-availability cluster solutions by themselves. As shown in the figure, Nacos provides a cluster architecture similar to ZooKeeper, including a Leader node and multiple Follower nodes. Unlike ZooKeeper, its data consistency algorithm uses Raft, and the middleware that also uses this algorithm includes Redis Sentinel's Leader election, Etcd, etc.
Insert picture description here

(5) Analysis of the realization principle of Nacos

Insert picture description here

Provider APP: Service provider.
Consumer APP: Service consumers.
Name Server: Realize the service routing of Nacos high-availability cluster through VIP (Vritual IP) or DNS.
Nacos Server: Nacos service provider, the Open API contained in it is the function access entry, Config Service Naming Service is the configuration service and name service module provided by Nacos. Consistency Protocol is a consensus protocol used to realize data synchronization of Nacos cluster nodes. Here, the Raft algorithm is used (the middleware using similar algorithms also includes Etcd and Redis sentinel elections).
Nacos Console: Nacos console.

On the whole, the service provider accesses the Nacos Server high-availability cluster through VIP (Virtual IP), and completes service registration and service query based on Open API. Nacos Server itself can support the master-slave mode, so the bottom layer will use the data consistency algorithm to complete the data synchronization of the slave nodes. The same is true for service consumers, querying the service list from Nacos Server based on Open API.

(6) The principle of the registration center

Core steps:
1. The service instance is registered to the service registry when it is started, and it is deregistered when it is closed.
2. Service consumers query the service registry to obtain available examples.
3. The service registry needs to call the health check API of the service instance to verify whether it can handle the request.

The realization principle of Nacos service registration and discovery is shown in the figure.

Insert picture description here

1. The service provider uses the Open API to initiate service registration
2. The service provider establishes a heartbeat mechanism with Nacos Server to detect the service status
3. The service consumer queries the list of service provider registration instances in Nacos Server
4. The service consumer constructs timed tasks , Pull data every 10s
5. If Nacos Server detects an abnormal service provider in the form of subscription publishing, it will push updates to consumers based on the UDP protocol

Guess you like

Origin blog.csdn.net/Octopus21/article/details/114548271