A method for implementing a service registration center based on a consistent hash algorithm
By adopting a consistent hash algorithm and gossip protocol in the service registration center, each node is only responsible for part of the service registration and subscription, solving the problem of data overload of a single node in a large-scale service cluster, realizing sharded storage of service data and stable and reliable operation of the system.
Patent Information
- Application Number
- CN202310188733.1
- Authority / Receiving Office
- CN · China
- Patent Type
- Patents(China)
- Current Assignee / Owner
- Filing Date
- 2023-02-17
- Publication Date
- 2025-04-18
- Estimated Expiration
- 2043-02-17
AI Technical Summary
Under large-scale service clusters, existing service registries such as zookeeper and eureka can cause data overload in a single node.
The method based on a consistent hash algorithm is adopted to realize the cluster deployment of the service registration center. Each node is only responsible for part of the service registration and subscription, and synchronizes metadata information through the gossip protocol to avoid data overloading of a single node.
It realizes shard storage of service data, avoids data overload of a single node, and ensures the stable and reliable long-term continuous operation of the system.
Smart Images

Figure CN116155925B_ABST
Abstract
Description
Technical Field
[0001] The present invention relates to the field of distributed service management, and in particular to a method for realizing a service registration center based on a consistent hash algorithm. Background Art
[0002] Well-known products used in the industry for service registration centers include Zookeeper and Eureka. These two products can achieve high availability through cluster deployment. The nodes in the cluster synchronize service data with each other to ultimately achieve data consistency. Therefore, each node in the registration center ultimately stores the full service data.
[0003] However, in a large-scale service cluster, the service data will be too large, and Zookeeper and Eureka will cause data overload problems on a single node.
[0004] Therefore, the prior art has defects and needs to be improved. Summary of the invention
[0005] The technical problem to be solved by the present invention is to provide a method for realizing a service registration center based on a consistent hash algorithm to avoid data overload of a single node.
[0006] The technical solution of the present invention is as follows: a method for implementing a service registration center based on a consistent hash algorithm is provided, comprising the following steps.
[0007] S1: The registration center cluster deploys N nodes. Each node in the cluster synchronizes the metadata information of each other with other nodes in the cluster through the gossip protocol. The metadata information of the node includes the node name NODENAME, node IP, node communication port, node hash slot, node version number and node status. Each node name should be different, and the calculation method of the hash slot is hash (NODENAME) % 16384. The metadata information of each node together constitutes the cluster metadata information. Each node maintains a copy of the cluster metadata information from its own perspective. Each node in the cluster detects each other's status in real time and synchronizes their own metadata information in real time, so that the cluster metadata information of each node is consistent.
[0008] S2: When the service provider registers with the registry cluster, it randomly accesses any node in the registry cluster to obtain the metadata information of the registry cluster. The service provider calculates the hash slot based on its own service name APPLICATION. The calculation method is hash(APPLICATION)%16384, and the target registry node is calculated using a consistent hash algorithm based on the metadata information of the registry cluster. The service provider registers its own service information with the target registry node through a TCP long connection. The service information may include service IP, port, protocol, service version number, service hash slot, etc., and the service provider sends a heartbeat to the target registry node every 1-30s for renewal of the service.
[0009] S3: When the service consumer subscribes to the registry cluster, it randomly accesses any node in the registry cluster to obtain the metadata information of the registry cluster. The service consumer calculates the hash slot based on the service name APPLICATION to be subscribed. The calculation method is hash(APPLICATION)%16384. The target registry node is calculated using a consistent hash algorithm based on the metadata information of the registry cluster. The target service is subscribed to the target registry node through a TCP long connection. The service consumer sends a heartbeat to the target registry node every 1-30 seconds to renew the subscription.
[0010] S4: When accepting service registration and subscription, the registration center node calculates the service hash slot using the calculation method of the hash slot in step S2 and / or step S3, and verifies whether it belongs to the hash slot range that the current node is responsible for. If it does not (caused by outdated registration center cluster metadata information obtained by service subscribers and consumers), the service provider is notified to re-execute step S2 and the service consumer to re-execute step S3. If it does, the service provider and subscriber information is cached locally.
[0011] S5: The service consumer obtains the subscribed service information from the registration center node every 1-30 seconds and updates the cache locally, thereby completing the remote RPC service call on demand.
[0012] The method for implementing a service registration center based on a consistent hash algorithm also includes: when a service provider goes offline, the relevant registration center node can sense it and notify the corresponding service consumer.
[0013] The method for implementing a service registration center based on a consistent hash algorithm also includes: if a new registration center node is added when the registration center cluster is expanded, the gossip protocol is applied between the nodes to propagate the new online node information and status and update the cluster metadata information. When each node in the cluster detects a change in the cluster metadata information, it determines whether the registered service belongs to itself, and notifies the affected service providers to re-execute step S2 and the service consumers to re-execute step S3.
[0014] The method for implementing a service registration center based on a consistent hash algorithm also includes: if any node in the registration center cluster detects that other registration center nodes are offline, the offline node information is broadcast in the cluster and each node in the registration center cluster updates the cluster metadata information in a timely manner; at the same time, the service provider registered and subscribed to the offline node re-executes step S2, and the service consumer re-executes step S3.
[0015] By adopting the above scheme, the present invention provides a method for implementing a service registration center based on a consistent hash algorithm. The service registration center nodes are deployed in a cluster, and a consistent hash algorithm is used to implement each node being responsible for only part of the service registration and subscription, thereby realizing sharded storage of service data, avoiding data overload of a single node, and ensuring stable and reliable long-term continuous operation of the system. BRIEF DESCRIPTION OF THE DRAWINGS
[0016] Figure 1 The figure is a flow chart of a method according to an embodiment of the present invention. DETAILED DESCRIPTION
[0017] The present invention is described in detail below in conjunction with the accompanying drawings and specific embodiments.
[0018] See also Figure 1 This embodiment provides a method for implementing a service registration center based on a consistent hash algorithm, including the following steps.
[0019] S1: The registration center cluster deploys N nodes. Each node in the cluster synchronizes the metadata information of each other with other nodes in the cluster through the gossip protocol. The metadata information of the node includes the node name NODENAME, node IP, node communication port, node hash slot, node version number and node status. Each node name should be different, and the calculation method of the hash slot is hash (NODENAME) % 16384. The metadata information of each node together constitutes the cluster metadata information. Each node maintains a copy of the cluster metadata information from its own perspective. Each node in the cluster detects each other's status in real time and synchronizes their own metadata information in real time, so that the cluster metadata information of each node is consistent.
[0020] S2: When the service provider registers with the registry cluster, it randomly accesses any node in the registry cluster to obtain the metadata information of the registry cluster. The service provider calculates the hash slot based on its own service name APPLICATION. The calculation method is hash(APPLICATION)%16384, and the target registry node is calculated using a consistent hash algorithm based on the metadata information of the registry cluster. The service provider registers its own service information with the target registry node through a TCP long connection. The service information may include service IP, port, protocol, service version number, service hash slot, etc., and the service provider sends a heartbeat to the target registry node every 1-30s for renewal of the service.
[0021] S3: When the service consumer subscribes to the registry cluster, it randomly accesses any node in the registry cluster to obtain the metadata information of the registry cluster. The service consumer calculates the hash slot based on the service name APPLICATION to be subscribed. The calculation method is hash(APPLICATION)%16384. The target registry node is calculated using a consistent hash algorithm based on the metadata information of the registry cluster. The target service is subscribed to the target registry node through a TCP long connection. The service consumer sends a heartbeat to the target registry node every 1-30 seconds to renew the subscription.
[0022] S4: When accepting service registration and subscription, the registration center node calculates the service hash slot using the calculation method of the hash slot in step S2 and / or step S3, and verifies whether it belongs to the hash slot range that the current node is responsible for. If it does not (caused by outdated registration center cluster metadata information obtained by service subscribers and consumers), the service provider is notified to re-execute step S2 and the service consumer to re-execute step S3. If it does, the service provider and subscriber information is cached locally.
[0023] S5: The service consumer obtains the subscribed service information from the registration center node every 1-30 seconds and updates the cache locally, thereby completing the remote RPC service call on demand.
[0024] The method for implementing a service registration center based on a consistent hash algorithm also includes: when a service provider goes offline, the relevant registration center node can sense it and notify the corresponding service consumer.
[0025] The method for implementing a service registration center based on a consistent hash algorithm also includes: if a new registration center node is added when the registration center cluster is expanded, the gossip protocol is applied between the nodes to propagate the new online node information and status and update the cluster metadata information. When each node in the cluster detects a change in the cluster metadata information, it determines whether the registered service belongs to itself, and notifies the affected service providers to re-execute step S2 and the service consumers to re-execute step S3.
[0026] The method for implementing a service registration center based on a consistent hash algorithm also includes: if any node in the registration center cluster detects that other registration center nodes are offline, the offline node information is broadcast in the cluster and each node in the registration center cluster updates the cluster metadata information in a timely manner; at the same time, the service provider registered and subscribed to the offline node re-executes step S2, and the service consumer re-executes step S3.
[0027] In summary, the present invention provides a method for implementing a service registration center based on a consistent hash algorithm. The service registration center nodes are deployed in a cluster, and a consistent hash algorithm is used to implement each node being responsible for only part of the service registration and subscription, thereby realizing sharded storage of service data, avoiding data overload of a single node, and ensuring stable and reliable long-term continuous operation of the system.
[0028] The above are only preferred embodiments of the present invention and are not intended to limit the present invention. Any modifications, equivalent substitutions and improvements made within the spirit and principles of the present invention should be included in the protection scope of the present invention.
Claims
1. A method for implementing a service registration center based on a consistent hash algorithm, characterized in that: The following steps are involved: S1: The registration center cluster deploys N nodes, and each node in the cluster synchronizes metadata information with other nodes in the cluster through the gossip protocol; the node metadata includes the node name NODENAME; The metadata information of each node together constitutes the cluster metadata information. Each node maintains a copy of the cluster metadata information from its own perspective. Each node in the cluster detects each other's status in real time and synchronizes their own metadata information in real time, so that the cluster metadata information of each node is consistent. S2: When a service provider registers with the registry cluster, it randomly accesses any node in the registry cluster to obtain metadata information of the registry cluster. The service provider calculates the hash slot based on its own service name APPLICATION. The target registry node is calculated using a consistent hash algorithm based on the registry cluster metadata information, and its own service information is registered with the target registry node through a TCP persistent connection. The service provider sends a heartbeat to the target registry node every 1-30 seconds for service renewal. S3: When the service consumer subscribes to the registry cluster, it randomly accesses any node in the registry cluster to obtain the metadata information of the registry cluster. The service consumer calculates the hash slot according to the service name APPLICATION to be subscribed, and calculates the target registry node using the consistent hash algorithm based on the metadata information of the registry cluster. The target service is subscribed to the target registry node through a TCP long connection, and the service consumer sends a heartbeat to the target registry node every 1-30 seconds for subscription renewal; S4: When accepting service registration and subscription, the registration center node calculates the service hash slot using the hash slot calculation method in step S2 and / or step S3, and verifies whether it belongs to the hash slot range that the current node is responsible for. If not, the service provider is notified to re-execute step S2 and the service consumer to re-execute step S3. If yes, the service provider and subscriber information are cached locally. S5: The service consumer obtains the subscribed service information from the registration center node every 1-30 seconds and updates the cache locally, thereby completing the remote RPC service call on demand.
2. According to claim 1, a method for implementing a service registration center based on a consistent hash algorithm is characterized in that: Also includes: When a service provider goes offline, the relevant registration center node can sense it and notify the corresponding service consumer.
3. According to claim 1, a method for implementing a service registration center based on a consistent hash algorithm is characterized in that: Also includes: If the registration center cluster is expanded and a new registration center node is added, the gossip protocol is used to propagate the information and status of the newly online node between the nodes and update the cluster metadata information. When each cluster node detects the change in cluster metadata information, it determines whether the registered service belongs to itself, and notifies the affected service providers to re-execute step S2 and the service consumers to re-execute step S3.
4. According to claim 1, a method for implementing a service registration center based on a consistent hash algorithm is characterized in that: Also includes: If any node in the registration center cluster detects that other registration center nodes are offline, the offline node information is broadcast in the cluster and each node in the registration center cluster updates the cluster metadata information in a timely manner; at the same time, the service provider registered and subscribed to the offline node re-executes step S2, and the service consumer re-executes step S3.
5. According to claim 1, a method for implementing a service registration center based on a consistent hash algorithm is characterized in that: The metadata information of the node also includes: node IP, node communication port, node hash slot, node version number and node status information.
6. According to claim 1, a method for implementing a service registration center based on a consistent hash algorithm is characterized in that: Each node has a different name.
7. According to claim 1, a method for implementing a service registration center based on a consistent hash algorithm is characterized in that: In step S1, the calculation method of the hash slot is hash(NODENAME)%16384.
8. According to claim 1, a method for implementing a service registration center based on a consistent hash algorithm is characterized in that: In step S2, the calculation method of the hash slot is hash(APPLICATION)%16384.
9. The method for implementing a service registration center based on a consistent hash algorithm according to claim 1, characterized in that: In step S3, the calculation method of the hash slot is hash(APPLICATION)%16384.
10. The method for implementing a service registration center based on a consistent hash algorithm according to claim 1, characterized in that: Service information includes: service IP, port, protocol, service version number, and service hash slot.
Citation Information
Patent Citations
Method for improving application service concurrency and application speed, terminal and storage medium
CN109951560A
Message subscription method and device, server and computer readable storage medium
CN111163159A