Brief analysis of registration and discovery of microservice registry

Registries are used to centrally manage microservices and implement functions such as registration, discovery and inspection of services. At present, there are many mature registry components, such as Consul, Eureka, Zookeeper, ETCD, Nacos. There are gaps in performance, concurrency and high availability between different components. But the basic functionality is transparent to the user. In fact, if we develop a set of registry can also meet the basic functions.

  1. IP port registration support: The registry provides an interface to add information about service publishers.

  2. Providing other information, such as the service name, and registering only the IP address can only achieve basic functions. If encryption or load balancing is required, for example, setting different weights for different nodes in the same service to allocate traffic, more detailed rules and parameters are required.

  3. Gracefully closed, service offline function is necessary, since the registration must be removed in time when the node fails, if not offline in time, will cause a large number of requests are still wrong access. The registry can provide a shutdown interface, and the application should also call the interface when shutdown occurs to perform a proper offline operation.

  4. Health check function, the above elegant closing is only a normal situation, when the user uses the kill -9 rude stop mode, or network failure occurs, the registry needs to check the occurrence of exceptions in time. There are different kinds of health checks.

    1. Client heartbeat connection: The client sends a heartbeat message to the server to indicate that the service status is normal. The heartbeat message can be in the form of TCP or HTTP.
    2. Maintain a long connection: A client heartbeat can be realized by maintaining a long connection between the client and server on a socket.
    3. Creating sessions: ZooKeeper does not actively send heartbeats. Instead, it relies on the temporary node feature provided by the component itself to maintain temporary nodes through sessions connected to ZooKeeper.
    4. Registry active probing: Invoking an HTTP interface of a service publisher to perform a health check, such as consul.
  5. Connect to the registry: The simple method is to fix the registry IP address in the configuration file, but this scalability is poor and cannot support horizontal multi-machine deployment, or write the addresses of multiple servers, like Kafka, from which metadata information is obtained, and then make a secondary connection.

  6. Service discovery: To view locally published and subscribed services, the registry needs to provide rich interfaces to support multi-level combined query based on application name, IP address, subscribed service name, and published service name.

  7. Service Subscription (optional) : When a node quits or a new node joins the service, how can subscribers receive timely notification? This is the problem of pull and push. Push is typical, such as The implementation of Notify based on Socket long connection by ZooKeeper, and the implementation of long polling, both of which have certain technical difficulties. Timing polling by pull is easier, but you need to adjust the time interval for requests, and the higher the frequency, the greater the pressure on the registry.

When the service node number is more and more long, the performance of the registry will become a bottleneck, then you need to pass level expansion to improve the performance of a service registry cluster, for using the class Paxos strong consistency of the components of the agreement, since each write operation needs more than half the nodes confirmed that performance of horizontal expansion can only increase the cluster, and cannot ascend cluster write performance, Because all write operations need to be performed by the leader node, horizontal capacity expansion improves the write and read performance of a cluster for components that adopt final consistency, but does not guarantee real-time data consistency. In terms of security, authentication information must be added to each registration, publication, and heartbeat to prevent information leakage and service attacks caused by malicious registration.

Finally, based on the above, a rough outline of a registry can be implemented, while more advanced functions, such as high availability of services and disaster recovery, security issues need to be further improved.