Location>code7788 >text

Distributed Systems Architecture 2: Service Discovery

Popularity:73 ℃/2024-12-16 22:57:53

1. Concepts

Service discovery refers to a distributed system where service instances dynamically register their information to a registry, and other services can discover the location of these instances, enabling inter-service communication.

Why do you need service discovery?

For distributed applications, service discovery is not optional, but mandatory. The main purpose is to allow service instances to recognize and communicate with each other to ensure that the system remains operational during dynamic scaling up, scaling down, and failure recovery.

2. Composition

Consists of enrollment centers and consumers

  • Registry Service Registry: Service instances register their metadata (IP, port, health status, etc.) with the registry. Maintains a service registry that records information about all available services
  • ConsumerConsumer: obtains the location information (list of service instances) of the target service from the registry, and selects an instance for communication through load balancing.

3. Two modes of discovery

3.1 Client Discovery Model

Concept: The client is responsible for determining the network location of available services and requests are responsible for equalization.

Process: The client queries the registry to get a list of instances, then the client uses a load balancing algorithm to select an available service instance to issue a request. As shown in the figure:

在这里插入图片描述

Netflix OSS A good example of a client discovery pattern is provided.Netflix Eureka is a service registry that provides a set of REST APIs for managing service instance registration and querying for available instances.Netflix Ribbon is an IPC client that can be used with Eureka to load-balance requests between available service instances.

Pros: Lightweight

Disadvantages: the client needs to implement the service discovery logic, the code is complex.

3.2 Server-side discovery model

Process: the client sends a request to the service through a load balancer (e.g., Nginx or API Gateway), the load balancer queries the registry for instance information and then forwards the request to the appropriate service instance

在这里插入图片描述

Case in point:AWS Elastic Load Balancer(ELB) is an example of a server-side discovery route

Benefits: the client no longer cares about the details of service discovery, but only needs to send the request to the load balancer

Cons: Need to deploy a load balancer to guarantee high availability

4. Registration Center

The registry acts as a database containing the network locations of service instances. It is itself required to havehigh availability, data consistency and reliability are usually ensured through distributed consistency protocols (e.g., Raft, Paxos).

For example: Netflix Eureka, which provides a REST API for registering and querying service instances.

The service instance registers its network location using a POST request. It uses PUT requests to refresh its registration information every 30 seconds. Registration information is removed by using an HTTP DELETE request or instance registration timeout.

A client can use an HTTP GET request to retrieve a registered service instance.

Other registries: etcd, Consul, Zookeeper and these I'll write about later in the article after I've learned about them

5. Ways of registering services

5.1 Self-Registration

Process: After the service instance starts, it actively registers its information to the service registry. And prevent the registration information from expiring by sending a heartbeat request.

Applicable scenarios: microservice architecture, service instances interact directly with the registry

在这里插入图片描述

Example:

  • Eureka
    • In Spring Cloud, the service is passed through the@EnableEurekaClient Annotation implements active registration.
    • The service registers its own metadata information (IP, port, health status, etc.) with Eureka Server when it starts.

Pros: Simple to implement, with the registration logic in the hands of the service itself;

Cons: The service code is coupled with the registration center logic, which increases the complexity of the server side, and the failure of the registration center may affect the service startup.

5.2 Passive Registration (Third-Party Registration)

Also known as third-party registration, the principle: the service instance itself is not responsible for the registration operation, but theThe status of the service is detected through an external agent or monitoring component, when a new instance of an available service is detected, it is then registered with the service registry.

在这里插入图片描述

Example:

Kubernetes

  • utilizationkubeletRegularly monitor the status of the Pod and register the Pod's network information with the Kubernetes Service.

Benefits: decoupled service instance and registration logic, no intrusion of service code. Applicable to existing services

Cons: reliance on third-party components, less real-time registration than active registration

6. Summarize the core process of service discovery

  • Service Registration: The service registers its own metadata with the registry when it starts.
  • Heartbeat Detection: The service continuously sends health status to the registry to ensure availability.
  • service discovery: The consumer obtains service instance information from the registry.
  • service call: The consumer chooses the appropriate instance to invoke (client-side load balancing or server-side load balancing).
  • Service cancellation: Log out of your information from the registration center when the service is closed.

7. Frame Comparison

The interview process may also document the difference between the service discovery framework, and the basis for selection, you can prepare some of the framework in advance, here is a brief list:

Service Registration and Discovery Framework Language Support consistency health checkup Configuration complexity performances Data persistence feature richness
Eureka Java (following a decimal or fraction) slightly less than be in favor of simpler your (honorific) unsupported moderate
Zookeeper polyglot unyielding unsupported intricate center be in favor of moderate
Consul polyglot (following a decimal or fraction) slightly less than be in favor of simpler your (honorific) be in favor of your (honorific)
Etcd polyglot vigorous be in favor of intricate lower (one's head) be in favor of moderate
Nacos polyglot (following a decimal or fraction) slightly less than be in favor of simpler your (honorific) be in favor of your (honorific)
DNS(K8S) polyglot your (honorific) be in favor of moderate your (honorific) be your (honorific)