“This is the fifth day of my participation in the First Challenge 2022. For details: First Challenge 2022.”

background

The first few articles introduced the capability mechanism of Nacos configuration center service. Next, we will introduce another very important feature of Nacos, namely service registration and discovery. When it comes to service registration and discovery, I believe everyone should be familiar with it. Service in Nacos is called his first-class citizen. Nacos supports the discovery, configuration, and management of almost all major types of “services.”

Services (Service)

A service is a software function or set of software functions (such as the retrieval of specific information or the execution of a set of operations) that can be reused by different clients for different purposes (such as through cross-process network calls). Nacos support mainstream ecological Service, such as Kubernetes Service, gRPC | Dubbo RPC Service or Spring Cloud RESTful Service.

The architecture diagram of NACOS is shown

(Dubbo) RPC microservices infrastructure

The meanings of the six steps in the figure are explained as follows:

  1. The service container is responsible for starting, loading, and running the service provider.
  2. At startup, service providers register their services with the registry.
  3. At startup, service consumers subscribe to the registry for the services they need.
  4. The registry returns a list of service provider addresses to the consumer, and if there are changes, the registry pushes the change data to the consumer based on the long connection.
  5. The service consumer, from the provider address list, selects one provider to call based on the soft load balancing algorithm. If the call fails, selects another one to call.
  6. Service consumers and providers accumulate calls and call times in memory and regularly send statistics to the monitoring center every minute.

Service Registry

In the figure above, Registry is the Registry responsible for the registration and discovery of services. Dubbo service system used Zookeeper or its own Registry implementation, while Nacos is another Registry implementation.

A service registry, which is a database of services, instances, and metadata (Dubbo3 has isolated the source data center and configuration services). Service instances are registered in the service registry at startup and deregistered at shutdown.

Clients of the service and router query the service registry for available instances of the service, and the service registry may invoke the health check API of the service instance to verify that it can handle requests.

Service Metadata

Service metadata refers to data describing services, including endpoints, service labels, service version numbers, service instance weights, routing rules, and security policies.

Logical architecture and its components

  • Service management: service CRUD, domain name CRUD, service health status check, service weight management and other functions
  • Configuration management: realize configuration tube CRUD, version management, gray management, monitoring management, push track, aggregation data and other functions
  • Metadata management: Provides metadata CURD and marking capabilities
  • Plug-in mechanism: three modules can be divided and combined to achieve the extension point SPI mechanism
  • Event mechanism: realize asynchronous event notification, SDK data change asynchronous notification and other logic
  • Log module: manages log classification, log level, log portability (especially to avoid conflicts), log format, exception code + help documents
  • Callback mechanism: THE SDK notifies the data and calls the user back through a unified mode for processing. Interfaces and data structures need to be extensible
  • Addressing mode: address IP, domain name, Nameserver, broadcast and other addressing modes, need to be extensible
  • Push channel: Solve the performance problems of push between server and storage, between servers, and between server and SDK
  • Capacity management: Manages the capacity of each tenant and each group to prevent storage write failures and affect service availability
  • Traffic management: Controls the request frequency, number of long links, packet size, and request flow control according to tenants and groups
  • Cache mechanism: Dr Directory, local cache, server cache mechanism. Tools are required for using Dr Directories
  • Startup mode: Start different programs +UI in single-machine mode, configuration mode, service mode, DNS mode, or all mode
  • Consistency protocol: different consistency mechanisms for different data and different consistency requirements
  • Storage module: to solve the data persistent, non-persistent storage, solve the problem of data fragmentation
  • Nameserver: Routes the namespace to clusterID and maps the user environment to the nacOS physical environment
  • CMDB: solve metadata storage, docking problems with the three-party CMDB system, and solve the relationship between applications, people and resources
  • Metrics: Expose the standard Metrics data, which is convenient to get through with the three-party monitoring system
  • Trace: exposes the standard Trace, which facilitates communication with SLA system, log whiteness, track push and other capabilities, and can be connected with metering and billing system
  • Access management: it is equivalent to the process of aliyun opening services and assigning identities, capacities and permissions
  • User management: solve user management, login, SSO and other problems
  • Authority management: solve identity identification, access control, role management and other problems
  • Audit system: Extended interface to facilitate access to audit systems of different companies
  • Notification system: core data change, or operation, convenient through SMS system, notification to the corresponding person data change
  • OpenAPI: Expose standard Rest style HTTP interface, easy to use, convenient integration of multiple languages
  • Console: Users can use the Console to manage services and configurations
  • SDK: Multilingual SDK
  • Agent: the DNS -f mode is similar, or it can be integrated with solutions such as mesh
  • CLI: Command line lightweight product management, as easy to use as Git

Service registration and discovery for Nacos

Service Provider

An application that provides reusable and callable services.

Mock service Registration

The most important aspect of service registration is where the service is registered. On the registry server side, there must be a container for managing the service, which holds all instances of the service. You don’t need to know the exact implementation details of the container, just the concept.

  • Register two instances of the same service with Nacos:

  • Enter dual registration mode injection

  • Keep the service online

The demo code is as follows:

The service can be registered through the registerInstance method of the NamingService interface. This method has many overloaded methods, so we choose a simple one to call. After registration is complete, get all available instances immediately by calling the getAllInstances method, and then let the main thread wait, printing the following:

Naming client successfully acquired two instances.

Service Consumer

After a service is registered with the registry, the consumer of the service can go through the process of service discovery. The consumer can send a request directly to the registry to obtain a service instance. In this case, the registry will return all available service instances to the consumer, but this is not recommended. Another approach is for a consumer of a service to subscribe to a service in the registry and submit a listener that is notified when a service in the registry changes and the consumer updates the local list of service instances to ensure that all services are available.

Nacos consumes service mechanisms

The application that initiates a call to a service. After service registration, service consumers can subscribe to the registry for the service they need, the registry will be all instances of the service “push” to consumers, in fact, to obtain services is the client active polling, with the client to obtain the configuration of the configuration center of the same principle.

Now I create a service consumer and subscribe to a service from the registry. Upon receiving the list of services returned from the registry, I perform five select service instances, which is equivalent to a mock service request, as shown in the following figure:

The printInstances method mainly prints all service instances. After the ServiceConsumer class is started, the following logs are printed:

Nacos mechanism load balancing

There are many ways to implement load balancing, including polling method, random method, and mode-taking after hashing the request IP address, etc. From the perspective of load dimensions, load balancing can be divided into server load balancing and client load balancing. After the client of Nacos obtains the complete list of instances of the service, it performs load balancing algorithm on the client to obtain an available instance, and the mode uses random acquisition.

The complete process of registering and subscribing Nacos services

  • Nacos client service registration consists of two parts, one is to register service information to the server, the other is to send heartbeat packets to the server, both of these two operations through NamingProxy and server data interaction.

  • The Nacos client also has two parts for service subscription: one is the scheduled task that continuously queries the available service instances from the server side, and the other is the scheduled task that continuously removes the service from the changed service queue and notifies the EventListener holder to update the service subscription list.

The resources

Nacos. IO/useful – cn/docs /…