Service Discovery Design Patterns for Microservice Architecture

When our service uses the REST API to call the service, we need to know the network location (IP address and port) of the service instance.

APIs for service instances in traditional applications running on servers are usually static. In today's cloud-based microservice applications, APIs are often not that simple to set up. Service instances change dynamically due to autoscaling, failures, and upgrades. Therefore, we have to use service discovery in client code.

1. Service Discovery

The IP address of the service cannot be statically configured on the client. Need to use dynamic service discovery. Conceptually, service discovery is very simple, its main component is a service registry, which contains a list of network locations of application service instances.

The service registry is updated when service instances start and stop. The service discovery mechanism works by querying the service registry for a list of available service instances and routing a request to one of them when a client invokes a service.

2. Application-level service discovery mode

An application's services and their clients can interact with a service registry for service discovery. Each service instance registers its network location with the service registry, from which a list of service instances is requested before invoking the service. The client then sends a request to one of the instances.

This approach is a combination of two patterns.

  • Self-registration mode  : During startup, the service instance calls the registry's registration API to register its network location, and the registry requires the service instance to periodically call the heartbeat API to prevent its registration from expiring. When a service instance shuts down, it unregisters itself from the service registry.
  • Client discovery mode  : In order to invoke a service, the service client queries the service registry for a list of service instances. The client uses a load balancing algorithm to select a service instance, and the client requests the selected instance.

Netflix and Pivotal have popularized enterprise-grade service discovery. For example, Netflix's Eureka is a highly available service registry. Netflix components can be easily used with Spring Cloud, a Spring-based framework developed by Pivotal. Spring Cloud-based services register with Eureka, and Spring Cloud-based clients use Eureka to discover services.

3. Disadvantages of application layer service discovery

  • Requires a language-specific service discovery library.
  • Operation and maintenance personnel are required to maintain settings and manage the service registry.
  • The possibility of missing logouts from the service registry arises when a service instance is running but not processing requests.

4. The service discovery mode provided by the platform

Many deployment platforms, such as Docker and Kubernetes, have built-in service registries and service discovery mechanisms. Each service is assigned a DNS name, a virtual IP (VIP) address, and a DNS name that resolves to the VIP address.

A service client requests a DNS name/VIP, and the deployment platform automatically routes the request to an available service instance. In this way, service registration, service discovery, request routing, etc. are all handled by the deployment platform. The service registry keeps track of the IP addresses of deployed services in the deployment platform.

This approach is a combination of two patterns:

  • Third-party registration pattern : Instead of a service registering itself with a service registry, a third-party registrar (usually part of the deployment platform) does the registration. The registrar registers service instances with the service registry at startup. The registrar unregisters the service instance from the service registry when the instance shuts down.
  • Server-side discovery mode  : instead of a client querying a service registry, it makes a request to a DNS name that resolves to a request router that queries the registry and load balances the requests. The AWS Elastic Load Balancer (ELB) is an example of a server-side discovery router. Clients send HTTP/TCP requests to the ELB, which load balances the traffic across a set of EC2 instances. ELB also acts as a service registry. Instances are explicitly registered with ELB through an API call, or automatically registered as part of an auto-scaling group.

5. Benefits of platform-based service discovery

  • The deployment platform handles all the work of service discovery.
  • Neither the service nor the client contains any code for service discovery.
  • Service discovery is available to all services and clients, no matter what language they are written in.

6. Disadvantages of platform-based service discovery

  • Only services that have been deployed using the platform can be discovered.

Guess you like

Origin blog.csdn.net/stone1290/article/details/126326810