A service memory cache calling method and device
Through the service memory cache calling method, the hash ring and consistent hash algorithm are used to determine the target service node, which solves the problems of cache penetration and system instability under high concurrent requests, and achieves efficient request processing and system stability.
Patent Information
- Application Number
- CN202011283433.4
- Authority / Receiving Office
- CN · China
- Patent Type
- Patents(China)
- Current Assignee / Owner
- Filing Date
- 2020-11-17
- Publication Date
- 2024-10-18
- Estimated Expiration
- 2040-11-17
AI Technical Summary
Existing caching technology can easily lead to cache penetration and system instability in high concurrent request scenarios. The traditional monolithic architecture has poor communication, management, and scalability, resulting in high maintenance costs and long delivery cycles.
Through the service memory cache calling method, the hash ring and consistent hash algorithm are used to determine the target service node, achieve efficient hits and memory cache of service requests, reduce server performance loss, and ensure system stability.
It improves the requested TPS, reduces server performance loss, enhances system stability, and avoids cache penetration and system instability.
Smart Images

Figure CN112486672B_ABST
Abstract
Description
Technical Field
[0001] One or more embodiments of the present specification relate to the field of computer technology, and in particular, to a method and device for calling a service memory cache. Background Art
[0002] In the past decade, with the rapid development of informatization, Internet+ and the insurance industry, software development has been constantly innovating, and the corresponding framework has also developed towards ecology. Today's software application development has gradually shown characteristics such as microservices, personalization, short cycles, and high quality. The new fast-reading and iterative personalized needs are not available in traditional application development, so splitting aggregate projects into individual microservice projects according to specific functions or needs has become a current research hotspot.
[0003] System architecture design is a critical part of the system construction process. It determines a series of issues such as the stability, robustness, and scalability of the system. It also defines how to divide the system into different components based on a series of factors such as technology, business, organization, scalability, maintainability, and flexibility, and make these components work together to provide users with certain specific services. However, with the continuous development of business and the continuous increase of functions, the communication, management, and coordination costs corresponding to the traditional monolithic architecture are getting higher and higher, and a series of problems such as increased maintenance costs, long delivery cycles, high technology selection costs, and poor horizontal and vertical scalability have emerged. In order to solve the problems faced by traditional system architecture, microservices have emerged with the development of technologies and practices such as domain-driven design, continuous delivery technology, virtualization technology, small autonomous team formation, intelligent automation of infrastructure, and large-scale cluster system design. As a distributed system solution, microservices focus on promoting the use of fine-grained services. Different microservices work together, and each service has its own life cycle. Since microservices mainly build models around business domains and integrate new concepts and technologies over the past decade, they can avoid many problems and traps caused by traditional layered architectures, and have great research value and practical significance.
[0004] At present, the existing cache technologies include in-memory cache jetcache or distributed cache redis (Redis: an open source log-type, key-value database written in ANSI C language, supporting the network, memory-based and persistent, and providing APIs in multiple languages). Using jetcache to cache data will cause cache penetration because there is no lock function when loading data. When using it, it is necessary to implement distributed locks for cooperation. In addition, if all data is cached in the application service node alone, the memory overhead of the application server will be very huge, which will also have a huge impact on the stability of the system. When using distributed cache redis, although the same cache function can be achieved, the redis server and the application server will have a large coupling. When the redis service goes down, it will cause a fatal blow to the application server.
[0005] As for the cache of non-consistent server hash, most of them will perform the same cache on each server to ensure the hit rate of the cache for concurrent requests from different users. However, if the cached content is large or large, the memory overhead will be very high. At the same time, a lot of hardware costs will be increased, resulting in the loss of caching non-transaction data. In most cases, it will cause cache penetration or other system instability. Summary of the invention
[0006] In view of this, the purpose of one or more embodiments of the present specification is to propose a service memory cache calling method to solve the problem of large call volume and high concurrency requests between microservices, which affects the stability of the system. By hitting the service request to the target service node, non-transaction return data can be cached in memory according to the service node, which can greatly improve the TPS of the request, reduce server performance loss, and ensure the stability of the system.
[0007] Based on the above purpose, one or more embodiments of this specification provide a service memory cache calling method, the method comprising:
[0008] Get the service request, the current service node list and the service node information of the hash ring;
[0009] Determine whether the service node information corresponding to the current service node list is the same as the service node information of the hash ring;
[0010] If the service node information in the current service node list is the same as the service node information in the hash ring, determining a target service node corresponding to the service request in the hash ring according to the service request;
[0011] If the service node information in the current service node list is different from the service node information in the hash ring, based on the consistent hashing algorithm, determine the hash ring corresponding to the current service node list according to the service nodes in the current service node list, and, according to the service request, determine the target service node corresponding to the service request in the hash ring corresponding to the current service node list;
[0012] Based on the target service node corresponding to the service request, the service memory cache corresponding to the service request is called.
[0013] Optionally, obtaining the current service node list includes:
[0014] Determine whether the current service node list is stored in the memory cache;
[0015] If the current service node list is stored in the memory cache, obtaining the current service node list from the memory cache;
[0016] If the current service node list is not stored in the memory cache, the current service node list is obtained from a registration center.
[0017] Optionally, after obtaining the service request, the current service node list and the service node information of the hash ring, the method further includes:
[0018] The user information in the service request is input into the interceptor for thread isolation, and the user information is converted into a preset format to obtain the converted user information, and the converted user information is used as the user information corresponding to the service request.
[0019] Optionally, if the service node information in the current service node list is the same as the service node information in the hash ring, determining, according to the service request, a target service node corresponding to the service request in the hash ring includes:
[0020] If the service node information in the current service node list is the same as the service node information in the hash ring;
[0021] Based on a consistent hashing algorithm, mapping the user information corresponding to the service request to the hash ring, and obtaining a hash value of the user information on the hash ring;
[0022] According to the hash value of the user information on the hash ring, a target service node corresponding to the service request is determined in the hash ring.
[0023] Optionally, if the service node information in the current service node list is different from the service node information in the hash ring, determining the hash ring corresponding to the current service node list based on the consistent hashing algorithm according to the service nodes in the current service node list, and determining, according to the service request, the target service node corresponding to the service request in the hash ring corresponding to the current service node list, including:
[0024] If the number of service nodes in the current service node list is different from the number of service nodes in the hash ring, then based on the consistent hashing algorithm, the hash ring corresponding to the current service node list is determined according to the service nodes in the current service node list, and, according to the service request, the target service node corresponding to the service request is determined in the hash ring corresponding to the current service node list.
[0025] Optionally, the determining, based on the consistent hashing algorithm and according to the service nodes in the current service node list, a hash ring corresponding to the current service node list includes:
[0026] For each service node in the current service node list, a consistent hashing algorithm is translated a preset number of times according to the node information of the service node to obtain a hash value corresponding to the service node and hash values of several virtual nodes corresponding to the service node; the hash value corresponding to the service node and the hash values of several virtual nodes corresponding to the service node are mapped to a hash ring; wherein the node information of the service node includes at least one of the following information: service name, port number, random number and timestamp.
[0027] Optionally, determining, according to the service request, a target service node corresponding to the service request in a hash ring corresponding to the current service node list includes:
[0028] According to the user information of the service request, a consistent hash algorithm is performed to translate to obtain a hash value corresponding to the user information;
[0029] According to the hash value corresponding to the user information, a target service node corresponding to the service request is determined in the hash ring corresponding to the current service node list.
[0030] One or more embodiments of the present specification provide a service memory cache calling device, the device comprising:
[0031] An acquisition unit, used to acquire service requests, a current service node list and service node information of a hash ring;
[0032] A judging unit, configured to judge whether the service node information corresponding to the current service node list is the same as the service node information of the hash ring;
[0033] A first determining unit, configured to determine, according to the service request, a target service node corresponding to the service request in the hash ring if the service node information in the current service node list is the same as the service node information in the hash ring;
[0034] A second determining unit is configured to determine, based on a consistent hashing algorithm, a hash ring corresponding to the current service node list according to the service nodes in the current service node list if the service node information in the current service node list is different from the service node information in the hash ring, and, based on the service request, determine a target service node corresponding to the service request in the hash ring corresponding to the current service node list;
[0035] A calling unit, configured to call the service request based on a target service node corresponding to the service request. Optionally, obtaining a current service node list includes:
[0036] Determine whether the current service node list is stored in the memory cache;
[0037] If the current service node list is stored in the memory cache, obtaining the current service node list from the memory cache;
[0038] If the current service node list is not stored in the memory cache, the current service node list is obtained from a registration center.
[0039] Optionally, the device further comprises: an interception unit, configured to:
[0040] The user information in the service request is input into the interceptor for thread isolation, and the user information is converted into a preset format to obtain the converted user information, and the converted user information is used as the user information corresponding to the service request.
[0041] Optionally, the first determining unit is configured to:
[0042] If the service node information in the current service node list is the same as the service node information in the hash ring;
[0043] Based on a consistent hashing algorithm, mapping the user information corresponding to the service request to the hash ring, and obtaining a hash value of the user information on the hash ring;
[0044] According to the hash value of the user information on the hash ring, a target service node corresponding to the service request is determined in the hash ring.
[0045] Optionally, the second determining unit is configured to:
[0046] If the number of service nodes in the current service node list is different from the number of service nodes in the hash ring, then based on the consistent hashing algorithm, the hash ring corresponding to the current service node list is determined according to the service nodes in the current service node list, and, according to the service request, the target service node corresponding to the service request is determined in the hash ring corresponding to the current service node list.
[0047] Optionally, the second determining unit is configured to:
[0048] For each service node in the current service node list, a consistent hashing algorithm is translated a preset number of times according to the node information of the service node to obtain a hash value corresponding to the service node and hash values of several virtual nodes corresponding to the service node; the hash value corresponding to the service node and the hash values of several virtual nodes corresponding to the service node are mapped to a hash ring; wherein the node information of the service node includes at least one of the following information: service name, port number, random number and timestamp.
[0049] Optionally, the second determining unit is configured to:
[0050] According to the user information of the service request, a consistent hash algorithm is performed to translate to obtain a hash value corresponding to the user information;
[0051] According to the hash value corresponding to the user information, a target service node corresponding to the service request is determined in the hash ring corresponding to the current service node list.
[0052] One or more embodiments of the present specification provide a non-transitory computer-readable storage medium, wherein the non-transitory computer-readable storage medium stores computer instructions, wherein the computer instructions are used to enable the computer to execute the service memory cache calling method mentioned above.
[0053] One or more embodiments of the present specification provide an electronic device, including a memory, a processor, and a computer program stored in the memory and executable on the processor, wherein the processor implements any one of the above-mentioned service memory cache calling methods when executing the program.
[0054] One or more embodiments of the present specification provide a non-transitory computer-readable storage medium, characterized in that the non-transitory computer-readable storage medium stores computer instructions, and the computer instructions are used to enable the computer to execute any one of the service memory cache calling methods mentioned above.
[0055] From the above, it can be seen that one or more embodiments of the present specification provide a service memory cache calling method, the method can first obtain a service request, a current service node list and service node information of a hash ring; then, it can be determined whether the service node information corresponding to the current service node list is the same as the service node information of the hash ring; if the service node information in the current service node list is the same as the service node information of the hash ring, then according to the service request, the target service node corresponding to the service request is determined in the hash ring; if the service node information in the current service node list is different from the service node information of the hash ring, then based on the consistent hashing algorithm, according to the service nodes in the current service node list, the hash ring corresponding to the current service node list is determined, and, according to the service request, the target service node corresponding to the service request is determined in the hash ring corresponding to the current service node list; based on the target service node corresponding to the service request, the service memory cache corresponding to the service request is called. It can be seen that the present application can obtain the hash value corresponding to the service node by virtualizing the service node and performing a consistent hashing algorithm operation on the service node. Then, the service node can be assigned to the hash ring according to the hash value corresponding to the service node. The call between services can be fixed to the unchanging hash value node on the hash ring. In this way, the service request of the user end can be hit to the fixed target service node on the hash ring, and the service memory cache data corresponding to the service request can be read from the server corresponding to the target service node. That is, the present application can hit the service request to the target service node, and the non-transaction return data can be cached in memory according to the service node, thereby greatly improving the TPS of the request, reducing the server performance loss, and ensuring the stability of the system. BRIEF DESCRIPTION OF THE DRAWINGS
[0056] In order to more clearly illustrate one or more embodiments of the present specification or the technical solutions in the prior art, the drawings required for use in the embodiments or the description of the prior art will be briefly introduced below. Obviously, the drawings described below are only one or more embodiments of the present specification. For ordinary technicians in this field, other drawings can be obtained based on these drawings without paying creative work.
[0057] Figure 1 A flow chart of a service memory cache calling method provided in one embodiment of the present application;
[0058] Figure 2 A schematic diagram of the system architecture of a service memory cache calling system provided in one embodiment of the present application;
[0059] Figure 3 A schematic diagram of the structure of a service memory cache calling device provided in one embodiment of the present application;
[0060] Figure 4 A schematic diagram of the structure of an electronic device provided by an embodiment of the present invention. DETAILED DESCRIPTION
[0061] In order to make the objectives, technical solutions and advantages of the present disclosure more clearly understood, the present disclosure is further described in detail below in combination with specific embodiments and with reference to the accompanying drawings.
[0062] It should be noted that, unless otherwise defined, the technical terms or scientific terms used in one or more embodiments of this specification should be understood by people with ordinary skills in the field to which this disclosure belongs. The "first", "second" and similar words used in one or more embodiments of this specification do not indicate any order, quantity or importance, but are only used to distinguish different components. "Include" or "comprise" and similar words mean that the elements or objects appearing before the word cover the elements or objects listed after the word and their equivalents, without excluding other elements or objects. "Connect" or "connected" and similar words are not limited to physical or mechanical connections, but may include electrical connections, whether direct or indirect. "Up", "down", "left", "right" and the like are only used to indicate relative positional relationships. When the absolute position of the described object changes, the relative positional relationship may also change accordingly.
[0063] The inventor has found through research that due to the existing inconsistent server Hash cache, most of them will perform the same cache on each server to ensure the hit rate of the cache of concurrent requests from different users. However, if the cached content is large or large, the memory overhead is very large. At the same time, a large amount of hardware costs are increased, resulting in the loss of caching non-transaction data, and in most cases, it will cause cache penetration or other system instability.
[0064] Therefore, the present invention provides a service memory cache calling method, which can first obtain a service request, a current service node list and service node information of a hash ring; then, it can be determined whether the service node information corresponding to the current service node list is the same as the service node information of the hash ring; if the service node information in the current service node list is the same as the service node information of the hash ring, then according to the service request, the target service node corresponding to the service request is determined in the hash ring; if the service node information in the current service node list is different from the service node information of the hash ring, then based on the consistent hashing algorithm, according to the service nodes in the current service node list, the hash ring corresponding to the current service node list is determined, and, according to the service request, the target service node corresponding to the service request is determined in the hash ring corresponding to the current service node list; based on the target service node corresponding to the service request, the service memory cache corresponding to the service request is called. It can be seen that the present application can obtain the hash value corresponding to the service node by virtualizing the service node and performing a consistent hashing algorithm operation on the service node, and then assign the service node to the hash ring according to the hash value corresponding to the service node, and fix the call between services to the unchanged hash value node on the hash ring. In this way, the service request of the user end can hit the fixed target service node on the hash ring, and the service memory cache data corresponding to the service request can be read from the server corresponding to the target service node. That is, the present embodiment can hit the service request to the target service node, and the non-transaction return data can be cached in memory according to the service node, thereby greatly improving the TPS of the request, reducing the server performance loss, and ensuring the stability of the system. It should be emphasized that in an implementation scenario, a service memory cache calling method provided by the present invention can be a consistent hash service memory cache calling method based on RestTemplate service load balancing call. Among them, RestTemplate is a client for accessing Rest services provided by Spring (a lightweight inversion of control (IoC) and aspect-oriented (AOP) container framework). RestTemplate provides a variety of convenient methods for accessing remote Http services, which can greatly improve the efficiency of client writing. REST is a highly expressive state transfer (Representational State Transfer, referred to as REST), which is a design and development method for network applications that can reduce the complexity of development and improve the scalability of the system.
[0065] The technical solutions of the embodiments of the present invention are described in detail below with reference to the accompanying drawings.
[0066] See also Figure 1, shows a service memory cache calling method in an embodiment of the present invention, the method comprising the following steps:
[0067] S101: Obtain a service request, a current service node list and service node information of a hash ring.
[0068] In this embodiment, when the server receives a service request sent by the client, it can first obtain the current service node list and the service node information of the hash ring. It should be noted that the current service node list may include information about the service nodes currently providing services, such as the number of service nodes currently providing services, service names, port numbers, timestamps, hash values, and other information; the service nodes currently providing services can be understood as servers that can currently operate normally to provide data reading functions and store service memory cache data. The service node information of the hash ring can be understood as the service node information on the hash ring determined when a service request (the last service request obtained adjacent to the current service request) is called before obtaining the service request. For example, the service node information may include the number of service nodes on the hash ring, service names, port numbers, timestamps, hash values, and other information.
[0069] It should be noted that, in one implementation of the present embodiment, the method for obtaining the current service node list may be to first determine whether the current service node list is stored in the memory cache, if the current service node list is stored in the memory cache, obtain the current service node list from the memory cache, if the current service node list is not stored in the memory cache, obtain the current service node list from the registration center. It should be noted that, in the present embodiment, when a service node needs to connect to a server to provide a data reading function, the service node needs to first register with the registration center, that is, the registration center can store the node information of the service node that is currently connected to the server and can provide a data reading function, wherein the node information of the service node includes at least one of the following information: service name, port number, random number and timestamp; in this way, the registration center can generate the current service node list according to the node information corresponding to each service node. If the registration center stores the current service node list in memory cache (such as the China Life e-store wings cache framework) in advance, then after obtaining the service request, the current service node list can be directly obtained from the memory cache. If the registration center does not store the current service node list in memory cache in advance, then after obtaining the service request, the current service node list can be obtained from the registration center first, and the current service node list can be cached. In addition, it should be emphasized that in this embodiment, after determining a hash ring when completing a call to a service request (the last service request obtained adjacent to the current service request), the hash ring can be cached locally so that it can be directly obtained. For example, it can be like this Figure 2 As shown, assuming that the service nodes currently connected to the server and capable of providing data reading function include service node 1, service node 2, service node 3 and service node 4, the acquired current service node list includes node information of service node 1, service node 2, service node 3 and service node 4.
[0070] It should be noted that, in one implementation, in order to implement service memory cache calls between service nodes and thus achieve data transmission, in an embodiment, an interceptor may be provided so as to perform thread isolation on each service request through the interceptor. Specifically, after obtaining the service request, the current service node list and the service node information of the hash ring, the method may further include the following steps:
[0071] The user information in the service request is input into the interceptor for thread isolation, and the user information is converted into a preset format to obtain the converted user information, and the converted user information is used as the user information corresponding to the service request.
[0072] Specifically, you can Figure 2 As shown, after the interceptor receives the user information of the service request (i.e., the "user data in the request information" in the figure), the user information of the service request is placed in the interceptor for thread isolation, and then the beam of the custom routing strategy (i.e., the steps to be performed later) is initialized, that is, when the program that needs to call the service memory cache corresponding to the service request is started, the beam of the custom routing strategy is initialized in an automatic injection manner. In addition, the interceptor can extract the user information in the service request. For example, the user information is generally set in the field of the Body or Header part of the service request, so the interceptor can obtain the user information from the Body or Header part of the service request; and, the user information can be formatted according to a preset format to obtain the converted user information. In this way, the converted user information is user information that conforms to the preset format, which is convenient for subsequent data processing, and the converted user information can be used as the user information corresponding to the service request. It should be noted that in some implementations, after the user information is formatted, the user information may also be assembled into fields. For example, if the preset format requires the identifier of the client corresponding to the user information, but the converted user information does not contain the identifier of the corresponding client, the identifier of the corresponding client may be assembled into the converted user information.
[0073] S102: Determine whether the service node information corresponding to the current service node list is the same as the service node information of the hash ring.
[0074] In this embodiment, in order to accurately determine the target service node corresponding to the service request, it is necessary to first determine whether the service node information corresponding to the current service node list is the same as the service node information of the hash ring, so that it can be determined based on the judgment result whether the service nodes on the hash ring need to be readjusted to determine the target service node corresponding to the service request based on the service nodes on the hash ring.
[0075] As an example, determining whether the service node information corresponding to the current service node list is the same as the service node information of the hash ring may be determining whether the number of service nodes in the current service node list is the same as the number of service nodes on the hash ring (excluding virtual nodes corresponding to the service nodes), or determining whether the hash values of each service node in the current service node list completely correspond to the hash values of each service node on the hash ring, or determining whether the service name and / or port number of each service node in the current service node list completely corresponds to the service name and / or port number of each service node on the hash ring.
[0076] It should be emphasized that the method for determining whether the hash value of each service node in the current service node list completely corresponds to the hash value of each service node on the hash ring is as follows: first, based on the consistent hashing algorithm, according to the node information of each service node in the current service node list, the hash ring corresponding to the current service node list is determined. For example, the node information of each service node in the current service node list can be converted to JSON first, and then the hash value of the node information of each service node after the conversion is determined. It should be noted that the node information of each service node in the current service node list is converted to JSON characters by a cache memory (such as the wings cache framework of China Life e-store) and then cached, which can reduce the performance loss of obtaining part of the service node content; specifically, the consistent hashing algorithm can be performed according to the service name (service name) and port number (port) of each service node in the current service node list, or the service name, port number and random number / timestamp, respectively, to obtain the hash value of each service node in the current service node list, and then the hash value of each service node is mapped to a new hash ring to obtain the hash ring corresponding to the current service node list. Next, it is determined whether the hash values of the service nodes on the hash ring corresponding to the current service node list are the same as those on the previous hash ring.
[0077] S103: If the service node information in the current service node list is the same as the service node information in the hash ring, determine a target service node corresponding to the service request in the hash ring according to the service request.
[0078] In this embodiment, if it is determined that the service node information in the current service node list is the same as the service node information of the hash ring, for example, the number of service nodes in the current service node list is the same as the number of service nodes on the hash ring (excluding the virtual nodes corresponding to the service nodes), or the hash value of each service node in the current service node list completely corresponds to the hash value of each service node on the hash ring, or the service name and / or port number of each service node in the current service node list completely corresponds to the service name and / or port number of each service node on the hash ring. This indicates that the service node information of the hash ring is the same as the service node information in the current service node list, that is, the service nodes in the current service node list have not changed from the service nodes corresponding to the generation of the hash ring, that is, the service nodes on the hash ring have not changed (for example, have not increased or decreased), so there is no need to readjust the service nodes on the hash ring, that is, the target service node corresponding to the service request can be directly determined based on the service nodes on the hash ring.
[0079] Then, based on the consistent hashing algorithm, the user information corresponding to the service request can be mapped to the hash ring to obtain the hash value of the user information on the hash ring. As an example, the consistent hashing algorithm can be translated according to the user information corresponding to the service request to obtain the hash value corresponding to the user information, and, according to the hash value corresponding to the user information, the user information can be mapped to the hash ring.
[0080] Then, the target service node corresponding to the service request can be determined in the hash ring according to the hash value of the user information on the hash ring. In one implementation, the consistent hashing algorithm can be translated according to the user information of the service request to obtain the hash value corresponding to the user information. Then, the target service node corresponding to the service request can be determined in the hash ring according to the hash value corresponding to the user information. As an example, on the hash ring, you can start from the node where the user information is mapped on the hash ring along the preset direction of the circle of the hash ring (such as clockwise) until you encounter a service node or a virtual node corresponding to a service node (that is, the first service node or virtual node encountered). At this time, the service node (or the service node corresponding to the virtual node) can be determined as the target service node corresponding to the service request.
[0081] S104: If the service node information in the current service node list is different from the service node information in the hash ring, based on the consistent hashing algorithm, determine the hash ring corresponding to the current service node list according to the service nodes in the current service node list, and, according to the service request, determine the target service node corresponding to the service request in the hash ring corresponding to the current service node list.
[0082] In this embodiment, if the number of service nodes in the current service node list is different from the number of service nodes in the hash ring, for example, the number of service nodes in the current service node list is different from the number of service nodes on the hash ring (excluding virtual nodes corresponding to the service nodes), or the hash values of each service node in the current service node list do not completely correspond to the hash values of each service node on the hash ring, or the service names and / or port numbers of each service node in the current service node list do not completely correspond to the service names and / or port numbers of each service node on the hash ring. It indicates that the service node information of the hash ring is the same as the service node information in the current service node list, that is, the service nodes in the current service node list have changed from the service nodes corresponding to the generation of the hash ring, that is, the service nodes on the hash ring have changed (such as increase or decrease). At this time, in order to ensure that the cache data can be evenly distributed in each server (service node), the service nodes in the hash ring can be balanced and evenly distributed so that the cache data can be evenly distributed in each service node. Therefore, in this embodiment, it is necessary to re-adjust the service nodes on the hash ring, that is, it is not possible to directly determine the target service node corresponding to the service request according to the service node on the hash ring, but it is necessary to re-determine the hash ring according to the service node information in the current service node list, and then determine the target service node corresponding to the service request according to the re-determined hash ring, so that the service nodes in the hash ring re-determined according to the service node information in the current service node list are more balanced and evenly distributed. In this way, the hash ring can automatically distribute the service request evenly to achieve the effect of load balancing. It should be noted that in this embodiment, the hash ring re-determined according to the service node information in the current service node list can be called the hash ring corresponding to the current service node list.
[0083] Then, based on the consistent hashing algorithm, according to the service nodes in the current service node list, the hash ring corresponding to the current service node list can be determined. Specifically, for each service node in the current service node list, according to the node information of the service node, the consistent hashing algorithm can be translated for a preset number of times to obtain the hash value corresponding to the service node and the hash values of several virtual nodes corresponding to the service node; the hash value corresponding to the service node and the hash values of several virtual nodes corresponding to the service node are mapped to a hash ring. For example, the service name (serviceName), port number (Port) of the service node, or the service name, port number and random number / timestamp can be used to translate the consistent hashing algorithm once and translate the consistent hashing algorithm 1000 times to obtain the hash value corresponding to the service node and the hash values of 1000 virtual nodes corresponding to it, and then the service node and its corresponding virtual node are mapped to the hash ring, so as to obtain the hash ring corresponding to the current service node list. It should be noted that it can be mapped to a brand new hash ring, or it can be adjusted on the basis of the original hash ring, which is not limited in this embodiment.
[0084] Then, according to the service request, the target service node corresponding to the service request can be determined in the hash ring corresponding to the current service node list. In one implementation, the consistent hash algorithm can be translated according to the user information of the service request to obtain the hash value corresponding to the user information. Then, according to the hash value corresponding to the user information, the target service node corresponding to the service request can be determined in the hash ring corresponding to the current service node list. As an example, on the hash ring corresponding to the current service node list, the preset direction of the ring of the hash ring (such as clockwise) can be followed from the node where the user information is mapped on the hash ring until a service node or a virtual node corresponding to a service node (i.e., the first service node or virtual node encountered) is met. At this time, the service node (or the service node corresponding to the virtual node) can be determined as the target service node corresponding to the service request. And, when the same service request is received next time, this target service node will be hit again, so that a fixed service node can be selected for access to improve the cache hit rate.
[0085] S105: Based on the target service node corresponding to the service request, call the service memory cache corresponding to the service request.
[0086] After determining the target service node corresponding to the service request, the service request can be sent to the server corresponding to the target service node, so that the server corresponding to the target service node returns the service memory cache corresponding to the service request, thereby completing the call to the service memory cache corresponding to the service request, and then the service memory cache can be used.
[0087] It can be seen that the present embodiment provides a service memory cache calling method, which can first obtain the service request, the current service node list and the service node information of the hash ring; then, it can be determined whether the service node information corresponding to the current service node list is the same as the service node information of the hash ring; if the service node information in the current service node list is the same as the service node information of the hash ring, then according to the service request, the target service node corresponding to the service request is determined in the hash ring; if the service node information in the current service node list is different from the service node information of the hash ring, then based on the consistent hashing algorithm, according to the service nodes in the current service node list, the hash ring corresponding to the current service node list is determined, and, according to the service request, the target service node corresponding to the service request is determined in the hash ring corresponding to the current service node list; based on the target service node corresponding to the service request, the service memory cache corresponding to the service request is called. It can be seen that the present application can obtain the hash value corresponding to the service node by virtualizing the service node and performing a consistent hashing algorithm operation on the service node. Then, the service node can be assigned to the hash ring according to the hash value corresponding to the service node. The call between services can be fixed to the unchanging hash value node on the hash ring. In this way, the service request of the user end can be hit to the fixed target service node on the hash ring, and the service memory cache data corresponding to the service request can be read from the server corresponding to the target service node. That is, the present embodiment can hit the service request to the target service node, and the non-transaction return data can be cached in memory according to the service node, thereby greatly improving the TPS of the request, reducing the server performance loss, and ensuring the stability of the system.
[0088] It should be noted that in this embodiment, the entire content, i.e., all operation processes (i.e., S101-S105), can be packaged into SDK, and switch settings can be added to achieve the effect of out-of-the-box use and on-demand use. In addition, if an external project or system wants to use this consistent Hash function, it can directly reference the SDK corresponding to Wings-consistent-Hash, or use the starter of wings-rmi, which has integrated the consistent Hash function.
[0089] Corresponding to the above-mentioned service memory cache calling method, the embodiment of the present invention provides a service memory cache calling device, the structure of which is as follows: Figure 3 As shown, including:
[0090] An acquisition unit 301 is used to acquire a service request, a current service node list and service node information of a hash ring;
[0091] A judging unit 302 is used to judge whether the service node information corresponding to the current service node list is the same as the service node information of the hash ring;
[0092] A first determining unit 303 is configured to determine, according to the service request, a target service node corresponding to the service request in the hash ring if the service node information in the current service node list is the same as the service node information in the hash ring;
[0093] A second determining unit 304 is configured to determine, based on a consistent hashing algorithm, a hash ring corresponding to the current service node list according to the service nodes in the current service node list if the service node information in the current service node list is different from the service node information in the hash ring, and, based on the service request, determine a target service node corresponding to the service request in the hash ring corresponding to the current service node list;
[0094] The calling unit 305 is configured to call the service request based on the target service node corresponding to the service request. Optionally, obtaining the current service node list includes:
[0095] Determine whether the current service node list is stored in the memory cache;
[0096] If the current service node list is stored in the memory cache, obtaining the current service node list from the memory cache;
[0097] If the current service node list is not stored in the memory cache, the current service node list is obtained from a registration center.
[0098] Optionally, after obtaining the service request, the current service node list and the service node information of the hash ring, the device method further includes: an interception unit, configured to:
[0099] The user information in the service request is input into the interceptor for thread isolation, and the user information is converted into a preset format to obtain the converted user information, and the converted user information is used as the user information corresponding to the service request.
[0100] Optionally, the first determining unit 303 is configured to determine, according to the service request, a target service node corresponding to the service request in the hash ring if the service node information in the current service node list is the same as the service node information of the hash ring, including:
[0101] If the service node information in the current service node list is the same as the service node information in the hash ring;
[0102] Based on a consistent hashing algorithm, mapping the user information corresponding to the service request to the hash ring, and obtaining a hash value of the user information on the hash ring;
[0103] According to the hash value of the user information on the hash ring, a target service node corresponding to the service request is determined in the hash ring.
[0104] Optionally, the second determining unit 304 is configured to determine, based on a consistent hashing algorithm, a hash ring corresponding to the current service node list according to the service nodes in the current service node list if the service node information in the current service node list is different from the service node information in the hash ring, and, according to the service request, determine a target service node corresponding to the service request in the hash ring corresponding to the current service node list, including:
[0105] If the number of service nodes in the current service node list is different from the number of service nodes in the hash ring, then based on the consistent hashing algorithm, the hash ring corresponding to the current service node list is determined according to the service nodes in the current service node list, and, according to the service request, the target service node corresponding to the service request is determined in the hash ring corresponding to the current service node list.
[0106] Optionally, the second determining unit 304 is configured to determine a hash ring corresponding to the current service node list according to the service nodes in the current service node list based on a consistent hashing algorithm, including:
[0107] For each service node in the current service node list, a consistent hashing algorithm is translated a preset number of times according to the node information of the service node to obtain a hash value corresponding to the service node and hash values of several virtual nodes corresponding to the service node; the hash value corresponding to the service node and the hash values of several virtual nodes corresponding to the service node are mapped to a hash ring; wherein the node information of the service node includes at least one of the following information: service name, port number, random number and timestamp.
[0108] Optionally, the second determining unit 304 is configured to determine, according to the service request, a target service node corresponding to the service request in a hash ring corresponding to the current service node list, including:
[0109] According to the user information of the service request, a consistent hash algorithm is performed to translate to obtain a hash value corresponding to the user information;
[0110] According to the hash value corresponding to the user information, a target service node corresponding to the service request is determined in the hash ring corresponding to the current service node list.
[0111] The technical carriers involved in the payment described in the embodiments of this specification may include, for example, Near Field Communication (NFC), WIFI, 3G / 4G / 5G, POS card swiping technology, QR code scanning technology, bar code scanning technology, Bluetooth, infrared, short message service (SMS), multimedia message service (MMS), etc.
[0112] The biological characteristics involved in the biological identification described in the embodiments of this specification may include, for example, eye characteristics, voiceprints, fingerprints, palm prints, heartbeats, pulses, chromosomes, DNA, human tooth marks, etc. Eye prints may include irises, sclera and other biological characteristics.
[0113] It should be noted that the method of one or more embodiments of this specification can be performed by a single device, such as a computer or server. The method of this embodiment can also be applied in a distributed scenario and completed by multiple devices cooperating with each other. In the case of such a distributed scenario, one of the multiple devices can only perform one or more steps in the method of one or more embodiments of this specification, and the multiple devices will interact with each other to complete the described method.
[0114] The above is a description of a specific embodiment of the specification. Other embodiments are within the scope of the appended claims. In some cases, the actions or steps recorded in the claims can be performed in an order different from that in the embodiments and still achieve the desired results. In addition, the processes depicted in the drawings do not necessarily require the specific order or continuous order shown to achieve the desired results. In some embodiments, multitasking and parallel processing are also possible or may be advantageous.
[0115] For the convenience of description, the above devices are described in terms of functions divided into various modules. Of course, when implementing one or more embodiments of this specification, the functions of each module can be implemented in the same or multiple software and / or hardware.
[0116] The apparatus of the above-mentioned embodiment is used to implement the corresponding method in the above-mentioned embodiment, and has the beneficial effects of the corresponding method embodiment, which will not be described in detail here.
[0117] Figure 4 A more specific schematic diagram of the hardware structure of an electronic device provided in this embodiment is shown, and the device may include: a processor 1010, a memory 1020, an input / output interface 1030, a communication interface 1040, and a bus 1050. The processor 1010, the memory 1020, the input / output interface 1030, and the communication interface 1040 are connected to each other through the bus 1050 in the device.
[0118] The processor 1010 can be implemented by a general-purpose CPU (Central Processing Unit), a microprocessor, an application-specific integrated circuit (ASIC), or one or more integrated circuits, and is used to execute relevant programs to implement the technical solutions provided in the embodiments of this specification.
[0119] The memory 1020 may be implemented in the form of ROM (Read Only Memory), RAM (Random Access Memory), static storage device, dynamic storage device, etc. The memory 1020 may store an operating system and other application programs. When the technical solutions provided in the embodiments of this specification are implemented by software or firmware, the relevant program codes are stored in the memory 1020 and are called and executed by the processor 1010.
[0120] The input / output interface 1030 is used to connect the input / output module to realize information input and output. The input / output module can be configured in the device as a component (not shown in the figure), or it can be externally connected to the device to provide corresponding functions. The input device may include a keyboard, a mouse, a touch screen, a microphone, various sensors, etc., and the output device may include a display, a speaker, a vibrator, an indicator light, etc.
[0121] The communication interface 1040 is used to connect a communication module (not shown) to realize communication interaction between the device and other devices. The communication module can realize communication through a wired mode (such as USB, network cable, etc.) or a wireless mode (such as mobile network, WIFI, Bluetooth, etc.).
[0122] The bus 1050 includes a path that transmits information between the various components of the device (eg, the processor 1010, the memory 1020, the input / output interface 1030, and the communication interface 1040).
[0123] It should be noted that, although the above device only shows the processor 1010, the memory 1020, the input / output interface 1030, the communication interface 1040 and the bus 1050, in the specific implementation process, the device may also include other components necessary for normal operation. In addition, it can be understood by those skilled in the art that the above device may also only include the components necessary for implementing the embodiments of the present specification, and does not necessarily include all the components shown in the figure.
[0124] The computer-readable medium of this embodiment includes permanent and non-permanent, removable and non-removable media, and information storage can be implemented by any method or technology. Information can be computer-readable instructions, data structures, modules of programs, or other data. Examples of computer storage media include, but are not limited to, phase change memory (PRAM), static random access memory (SRAM), dynamic random access memory (DRAM), other types of random access memory (RAM), read-only memory (ROM), electrically erasable programmable read-only memory (EEPROM), flash memory or other memory technology, read-only compact disk read-only memory (CD-ROM), digital versatile disk (DVD) or other optical storage, magnetic cassettes, magnetic tape magnetic disk storage or other magnetic storage devices or any other non-transmission media that can be used to store information that can be accessed by a computing device.
[0125] Those skilled in the art should understand that the discussion of any of the above embodiments is merely illustrative and is not intended to imply that the scope of the present disclosure (including the claims) is limited to these examples. In line with the present disclosure, the technical features in the above embodiments or different embodiments may be combined, the steps may be implemented in any order, and there are many other variations of different aspects of one or more embodiments of the present specification as described above, which are not provided in detail for the sake of simplicity.
[0126] In addition, to simplify the description and discussion, and in order not to obscure one or more embodiments of the present specification, known power / ground connections to integrated circuit (IC) chips and other components may or may not be shown in the provided figures. In addition, the device may be shown in the form of a block diagram so as to avoid obscuring one or more embodiments of the present specification, and this also takes into account the fact that the details of the implementation of these block diagram devices are highly dependent on the platform on which one or more embodiments of the present specification will be implemented (i.e., these details should be fully within the scope of understanding of those skilled in the art). Where specific details (e.g., circuits) are set forth to describe exemplary embodiments of the present disclosure, it is apparent to those skilled in the art that one or more embodiments of the present specification may be implemented without these specific details or with variations in these specific details. Therefore, these descriptions should be considered illustrative rather than restrictive.
[0127] Although the present disclosure has been described in conjunction with specific embodiments of the present disclosure, many replacements, modifications and variations of these embodiments will be apparent to those skilled in the art from the foregoing description. For example, other memory architectures (e.g., dynamic RAM (DRAM)) may use the embodiments discussed.
[0128] One or more embodiments of this specification are intended to cover all such substitutions, modifications and variations that fall within the broad scope of the appended claims. Therefore, any omissions, modifications, equivalent substitutions, improvements, etc. made within the spirit and principles of one or more embodiments of this specification should be included in the scope of protection of this disclosure.
Claims
1. A service memory cache calling method, characterized in that: The method comprises: Get the service request, the current service node list and the service node information of the hash ring; Obtaining a current service node list includes: determining whether the current service node list is stored in a memory cache; if the current service node list is stored in the memory cache, obtaining the current service node list from the memory cache; if the current service node list is not stored in the memory cache, obtaining the current service node list from a registration center; After obtaining the service request, the current service node list and the service node information of the hash ring, the method further includes: inputting the user information in the service request into the interceptor for thread isolation, converting the user information into a preset format, obtaining the converted user information, and using the converted user information as the user information corresponding to the service request; Determine whether the service node information corresponding to the current service node list is the same as the service node information of the hash ring; If the service node information in the current service node list is the same as the service node information in the hash ring, determining a target service node corresponding to the service request in the hash ring according to the service request; If the service node information in the current service node list is different from the service node information in the hash ring, based on the consistent hashing algorithm, determine the hash ring corresponding to the current service node list according to the service nodes in the current service node list, and, according to the service request, determine the target service node corresponding to the service request in the hash ring corresponding to the current service node list; The method of determining the hash ring corresponding to the current service node list based on the consistent hashing algorithm according to the service nodes in the current service node list includes: for each service node in the current service node list, translating the consistent hashing algorithm a preset number of times according to the node information of the service node to obtain the hash value corresponding to the service node and the hash values of several virtual nodes corresponding to the service node; mapping the hash value corresponding to the service node and the hash values of several virtual nodes corresponding to the service node to a hash ring; wherein the node information of the service node includes at least one of the following information: service name, port number, random number and timestamp; Based on the target service node corresponding to the service request, calling the service memory cache corresponding to the service request; After determining whether the service node information corresponding to the current service node list is the same as the service node information of the hash ring, the method includes determining whether it is necessary to readjust the service nodes on the hash ring according to the determination result.
2. The method according to claim 1, characterized in that If the service node information in the current service node list is the same as the service node information in the hash ring, determining, according to the service request, a target service node corresponding to the service request in the hash ring includes: If the service node information in the current service node list is the same as the service node information in the hash ring; Based on a consistent hashing algorithm, mapping the user information corresponding to the service request to the hash ring, and obtaining a hash value of the user information on the hash ring; According to the hash value of the user information on the hash ring, a target service node corresponding to the service request is determined in the hash ring.
3. The method according to any one of claims 1-2, characterized in that: If the service node information in the current service node list is different from the service node information in the hash ring, determining the hash ring corresponding to the current service node list based on the consistent hashing algorithm according to the service nodes in the current service node list, and determining, according to the service request, the target service node corresponding to the service request in the hash ring corresponding to the current service node list, including: If the number of service nodes in the current service node list is different from the number of service nodes in the hash ring, then based on the consistent hashing algorithm, the hash ring corresponding to the current service node list is determined according to the service nodes in the current service node list, and, according to the service request, the target service node corresponding to the service request is determined in the hash ring corresponding to the current service node list.
4. The method according to claim 3, characterized in that The determining, according to the service request, a target service node corresponding to the service request in a hash ring corresponding to the current service node list includes: According to the user information of the service request, a consistent hash algorithm is performed to translate to obtain a hash value corresponding to the user information; According to the hash value corresponding to the user information, a target service node corresponding to the service request is determined in the hash ring corresponding to the current service node list.
5. A service memory cache calling device, characterized in that: The device comprises: An acquisition unit, used to acquire service requests, a current service node list and service node information of a hash ring; The acquisition unit is used to determine whether the current service node list is stored in the memory cache; if the current service node list is stored in the memory cache, acquire the current service node list from the memory cache; if the current service node list is not stored in the memory cache, acquire the current service node list from the registration center; An interception unit, configured to, after obtaining the service request, the current service node list and the service node information of the hash ring, input the user information in the service request into the interceptor for thread isolation, convert the user information into a preset format, obtain the converted user information, and use the converted user information as the user information corresponding to the service request; A judging unit, configured to judge whether the service node information corresponding to the current service node list is the same as the service node information of the hash ring; A first determining unit, configured to determine, according to the service request, a target service node corresponding to the service request in the hash ring if the service node information in the current service node list is the same as the service node information in the hash ring; A second determining unit is configured to determine, based on a consistent hashing algorithm, a hash ring corresponding to the current service node list according to the service nodes in the current service node list if the service node information in the current service node list is different from the service node information in the hash ring, and, based on the service request, determine a target service node corresponding to the service request in the hash ring corresponding to the current service node list; The second determination unit is used to translate the consistent hashing algorithm for a preset number of times for each service node in the current service node list according to the node information of the service node, to obtain a hash value corresponding to the service node and hash values of several virtual nodes corresponding to the service node; the hash value corresponding to the service node and the hash values of several virtual nodes corresponding to the service node are mapped to a hash ring; wherein the node information of the service node includes at least one of the following information: service name, port number, random number and timestamp; A calling unit, configured to call a service memory cache corresponding to the service request based on a target service node corresponding to the service request; The third determining unit is used to determine whether the service node information corresponding to the current service node list is the same as the service node information of the hash ring, and then determine whether it is necessary to readjust the service nodes on the hash ring according to the judgment result.
6. An electronic device comprising a memory, a processor, and a computer program stored in the memory and executable on the processor, characterized in that: When the processor executes the program, it implements the service memory cache calling method as described in any one of claims 1 to 4.
7. A non-transitory computer-readable storage medium, characterized in that: The non-transitory computer-readable storage medium stores computer instructions, and the computer instructions are used to enable the computer to execute the service memory cache calling method described in any one of claims 1 to 4.
Citation Information
Patent Citations
Affinity dynamic load balancing method based on consistent hash algorithm
CN107197035A
Cache data distribution method and device, equipment and storage medium
CN110336891A
