Service discovery method and device based on cache mechanism, and electronic device
By pushing change events to the local cache of the registry center and selecting a full or incremental update strategy, the storage pressure on the system server caused by full synchronization of microservice instances is resolved, achieving more efficient resource utilization and network bandwidth saving.
Patent Information
- Application Number
- CN202411495217.4
- Authority / Receiving Office
- CN · China
- Patent Type
- Patents(China)
- Current Assignee / Owner
- Filing Date
- 2024-10-24
- Publication Date
- 2025-10-21
- Estimated Expiration
- 2044-10-24
AI Technical Summary
The full synchronization of microservice instances puts enormous pressure on the system server's storage and resource consumption, a problem that existing technologies have not been able to effectively solve.
A service discovery method based on caching mechanism is adopted. When the microservice instance information stored in the server cluster is updated, the change event is pushed to the local cache of the registry center, and the service configuration file is generated by selecting a full or incremental update strategy according to the service version number and caching strategy.
It reduces the access pressure on the server cluster, saves network bandwidth and system resources, implements a more reasonable update mechanism, and solves the storage pressure problem of full synchronization on the system server.
Smart Images

Figure CN119363813B_ABST
Abstract
Description
Technical Field
[0001] The present invention relates to the technical field of microservice architecture, and in particular to a service discovery method and device based on a cache mechanism, and an electronic device. Background Art
[0002] As microservices theory matures, more and more internet companies are adopting microservice architectures to support business development. The service discovery mechanism for microservice instances is provided by a registry. Typically, service discovery can directly and efficiently obtain data change notifications using etcd (a distributed key-value storage system). However, this method of exposing etcd directly to clients loses its protection, increasing the load on etcd as the number of users increases. Furthermore, when consumers request services from the registry, the registry always provides the latest available instance data, allowing consumers to fully update their local service provider status data. However, full synchronization, as the sole method of data synchronization, also has drawbacks, such as being time-consuming, requiring significant network bandwidth, and consuming a significant amount of system resources. These drawbacks become more pronounced as data volume grows.
[0003] To address the above-mentioned problems, no effective solutions have been proposed so far. Summary of the Invention
[0004] The embodiments of the present invention provide a service discovery method and device, and an electronic device based on a cache mechanism, so as to at least solve the technical problem in the related art that full synchronization of microservice instances puts huge storage pressure on the system server and occupies a large amount of system resources.
[0005] According to one aspect of an embodiment of the present invention, a service discovery method based on a cache mechanism is provided, comprising: in a case where microservice instance information stored in a server cluster is updated, receiving a change event pushed by the server cluster, wherein the change event is generated based on the instance change information; determining the event type of the change event, and updating a local cache of a registration center based on the event type and the instance change information; in a case where a service discovery request is received from a service consumer, determining a service discovery strategy based on the service version number carried in the service discovery request and the updated local cache of the registration center, wherein the service discovery strategy includes: a full update strategy and an incremental update strategy; generating a service configuration file based on the service discovery strategy, and returning the service configuration file to the service consumer.
[0006] Furthermore, the service discovery method also includes: receiving an interaction request from a service provider; parsing the interaction request to obtain interaction operation information, wherein the interaction operation information includes the interaction operation type and interaction operation content requested by the service provider; and updating the microservice instance information in the server cluster based on the interaction operation information.
[0007] Furthermore, the step of updating the local cache of the registration center based on the event type and the instance change information includes: updating the existing instance information corresponding to the instance change information in the instance information cache module of the local cache of the registration center based on the event type and the instance change information; generating a change version number for the change event, and generating version change data corresponding to the change event with the change version number as the key and the instance change information as the value; inserting the version change data corresponding to the change event into the version change cache module of the local cache of the registration center.
[0008] Furthermore, the step of determining the service discovery strategy based on the service version number carried by the service discovery request and the updated local cache of the registration center includes: determining whether the service consumer end is subscribing to the target microservice for the first time based on the service version number carried by the service discovery request; when it is determined that the service consumer end is subscribing to the target microservice for the first time, determining that the service discovery strategy is the full update strategy; when it is determined that the service consumer end is not subscribing to the target microservice for the first time, determining that the service discovery strategy is the incremental update strategy.
[0009] Furthermore, the step of generating a service configuration file based on the service discovery strategy includes: when determining that the service discovery strategy is a full update strategy, extracting the full instance information of the target microservice from the instance information cache module cached locally in the registration center; and generating the service configuration file based on the full instance information of the target microservice.
[0010] Furthermore, the step of generating a service configuration file based on the service discovery strategy also includes: when it is determined that the service discovery strategy is an incremental update strategy, querying the version change cache module locally cached by the registration center based on the service version number to obtain a query result, wherein the query result is used to record all version change data corresponding to the target microservice; based on the version change data indicated by the query result, extracting the incremental instance information of the target microservice in the instance information cache module; and generating the service configuration file based on the incremental instance information of the target microservice.
[0011] Furthermore, the service discovery method also includes: adding a multidimensional query interface in the instance information cache module locally cached in the registration center, wherein the multidimensional query interface includes at least: an environment interface, a software interface, a module interface, a service interface and a method interface; receiving a multidimensional attribute query request transmitted by the business data end, and calling the multidimensional query interface based on the multidimensional attribute query request to obtain the attribute data query result.
[0012] According to another aspect of an embodiment of the present invention, a service discovery device based on a cache mechanism is also provided, including: a receiving unit for receiving a change event pushed by the server cluster when the microservice instance information stored in the server cluster is updated, wherein the change event is generated based on the instance change information; an updating unit for determining the event type of the change event, and updating the local cache of the registration center based on the event type and the instance change information; a determining unit for determining the service discovery strategy based on the service version number carried by the service discovery request and the updated local cache of the registration center when receiving a service discovery request from the service consumer, wherein the service discovery strategy includes: a full update strategy and an incremental update strategy; a generating unit for generating a service configuration file based on the service discovery strategy, and returning the service configuration file to the service consumer.
[0013] Furthermore, the service discovery device also includes: a receiving module for receiving an interaction request from a service provider; a parsing module for parsing the interaction request to obtain interaction operation information, wherein the interaction operation information includes the interaction operation type and interaction operation content requested by the service provider; and a first update module for updating the microservice instance information in the server cluster based on the interaction operation information.
[0014] Furthermore, the update unit includes: a second update module, used to update the existing instance information corresponding to the instance change information in the instance information cache module locally cached by the registration center based on the event type and the instance change information; a first generation module, used to generate a change version number for the change event, and use the change version number as a key and the instance change information as a value to generate version change data corresponding to the change event; an insertion module, used to insert the version change data corresponding to the change event into the version change cache module locally cached by the registration center.
[0015] Furthermore, the determination unit includes: a first determination module, used to determine whether the service consumer end subscribes to the target microservice for the first time based on the service version number carried by the service discovery request; a second determination module, used to determine that the service discovery strategy is the full update strategy when it is determined that the service consumer end subscribes to the target microservice for the first time; and a third determination module, used to determine that the service discovery strategy is the incremental update strategy when it is determined that the service consumer end does not subscribe to the target microservice for the first time.
[0016] Furthermore, the generation unit includes: a first extraction module, which is used to extract the full instance information of the target microservice from the instance information cache module cached locally in the registration center when it is determined that the service discovery strategy is a full update strategy; and a second generation module, which is used to generate the service configuration file based on the full instance information of the target microservice.
[0017] Furthermore, the generation unit also includes: a query module, which is used to query the version change cache module of the local cache of the registration center based on the service version number to obtain a query result when it is determined that the service discovery strategy is an incremental update strategy, wherein the query result is used to record all version change data corresponding to the target microservice; a second extraction module, which is used to extract the incremental instance information of the target microservice in the instance information cache module based on the version change data indicated by the query result; and a third generation module, which is used to generate the service configuration file based on the incremental instance information of the target microservice.
[0018] Furthermore, the service discovery device also includes: an adding module for adding a multidimensional query interface in the instance information cache module locally cached in the registration center, wherein the multidimensional query interface includes at least: an environment interface, a software interface, a module interface, a service interface and a method interface; a calling module for receiving a multidimensional attribute query request transmitted by the business data end, and calling the multidimensional query interface based on the multidimensional attribute query request to obtain the attribute data query result.
[0019] According to another aspect of an embodiment of the present invention, a computer-readable storage medium is further provided, wherein the computer-readable storage medium includes a stored computer program, wherein when the computer program is running, the device where the computer-readable storage medium is located is controlled to execute any one of the above-mentioned service discovery methods based on the cache mechanism.
[0020] According to another aspect of an embodiment of the present invention, an electronic device is also provided, comprising one or more processors and a memory, wherein the memory is used to store one or more programs, wherein when the one or more programs are executed by the one or more processors, the one or more processors implement any one of the above-mentioned service discovery methods based on the cache mechanism.
[0021] In the present invention, a service discovery method based on a cache mechanism is proposed. When the microservice instance information stored in the server cluster is updated, a change event pushed by the server cluster is first received, wherein the change event is generated based on the instance change information. Then, the event type of the change event is determined, and the local cache of the registration center is updated based on the event type and the instance change information. Then, when a service discovery request from the service consumer is received, a service discovery strategy is determined based on the service version number carried in the service discovery request and the updated local cache of the registration center, wherein the service discovery strategy includes: a full update strategy and an incremental update strategy. Finally, a service configuration file is generated based on the service discovery strategy, and the service configuration file is returned to the service consumer.
[0022] In the present invention, when the microservice instance information stored in the server cluster is updated, the change event and instance change information are synchronized to the local cache of the registration center, so that the service subscription and service query of the service consumer end are directly obtained from the local cache of the registration center, which greatly reduces the access pressure of the server cluster compared with the existing technology. In addition, the present invention also proposes that the service subscription discovery of the service consumer end adopts a combination of full update and incremental update, and determines the update method based on the requested service version number and the service information stored in the local cache of the registration center. Compared with the existing technology that adopts all full updates, the update mechanism of the combination of full update and incremental update is more reasonable, which achieves the technical effect of saving network bandwidth and saving system resources, and thus solves the technical problem in the related technology that the full synchronization of microservice instances puts huge storage pressure on the system server and occupies a large amount of system resources. BRIEF DESCRIPTION OF THE DRAWINGS
[0023] The drawings described herein are used to provide a further understanding of the present invention and constitute a part of this application. The exemplary embodiments of the present invention and their descriptions are used to explain the present invention and do not constitute an improper limitation of the present invention. In the drawings:
[0024] Figure 1 This is an optional operation relationship diagram based on a registration center according to an embodiment of the present invention;
[0025] Figure 2 is a flowchart of an optional service discovery method based on a cache mechanism according to an embodiment of the present invention;
[0026] Figure 3 is an architectural diagram of an optional registration center according to an embodiment of the present invention;
[0027] Figure 4 is a flowchart of an optional service subscription processing method according to an embodiment of the present invention;
[0028] Figure 5 is a schematic diagram of an optional multi-dimensional data output interface according to an embodiment of the present invention;
[0029] Figure 6 This is a schematic diagram of an optional operation logic of a service consumer according to an embodiment of the present invention;
[0030] Figure 7 is an optional service subscription logic diagram according to an embodiment of the present invention;
[0031] Figure 8 is a flowchart of an optional method for quantitatively calculating service instance differences according to an embodiment of the present invention;
[0032] Figure 9 is a schematic diagram of an optional service discovery device based on a cache mechanism according to an embodiment of the present invention;
[0033] Figure 10 The present invention is a hardware structure block diagram of an electronic device (or mobile device) for a service discovery method based on a cache mechanism according to an embodiment of the present invention. DETAILED DESCRIPTION
[0034] In order to enable those skilled in the art to better understand the solutions of the present invention, the technical solutions in the embodiments of the present invention will be clearly and completely described below in conjunction with the drawings in the embodiments of the present invention. Obviously, the embodiments described are only part of the embodiments of the present invention, not all of the embodiments. Based on the embodiments of the present invention, all other embodiments obtained by ordinary technicians in this field without making creative efforts should fall within the scope of protection of the present invention.
[0035] It should be noted that the terms "first", "second", etc. in the description and claims of the present invention and the above-mentioned drawings are used to distinguish similar objects and are not necessarily used to describe a specific order or sequence. It should be understood that the numbers used in this way can be interchanged where appropriate, so that the embodiments of the present invention described herein can be implemented in an order other than those illustrated or described herein. In addition, the terms "including" and "having" and any variations thereof are intended to cover non-exclusive inclusions. For example, a process, method, system, product or device that includes a series of steps or units is not necessarily limited to those steps or units clearly listed, but may include other steps or units that are not clearly listed or inherent to these processes, methods, products or devices.
[0036] The following embodiments of the present invention can be applied to various systems / applications / devices that require distributed control of microservices, and can implement a service discovery product with loose coupling of service locations based on a lightweight database and a registration center.
[0037] When the microservice instance information stored in the server cluster is updated, the present invention synchronizes the change event and instance change information to the local cache of the registration center. The service subscription discovery at the service consumer end adopts a combination of full update and incremental update. The update method is determined based on the requested service version number and the service information stored in the local cache of the registration center. The update mechanism is more reasonable and can save network bandwidth and system resources.
[0038] The present invention will be described in detail below with reference to various embodiments.
[0039] Example 1
[0040] According to an embodiment of the present invention, an embodiment of a service discovery method based on a caching mechanism is provided. It should be noted that the steps shown in the flowchart of the accompanying drawings can be executed in a computer system such as a set of computer executable instructions, and although a logical order is shown in the flowchart, in some cases, the steps shown or described can be executed in an order different from that shown here.
[0041] The implementing body of the embodiment of the present invention can be a microservice distributed control system, especially the registration center in the system. It should be noted that the registration center is used in the prior art to achieve loose coupling of service locations and efficient service discovery to cope with the increasing number of microservice instances and the high dynamics of the addresses exposed by the services themselves. The runtime status of service instances often changes, resulting in the service caller needing to perceive the downtime and the slow perception speed.
[0042] Figure 1 This is an optional operation relationship diagram based on the registration center according to an embodiment of the present invention, such as Figure 1 As shown, the service operation process based on the registration center involves three roles: the registration center, the registration center client service consumer (hereinafter referred to as the "service consumer end") and the registration center client service provider (hereinafter referred to as the "service provider end").
[0043] The service provider registers the service address of the microservice it provides with the registration center and sends heartbeats to the registration center regularly to report the operating status;
[0044] The service consumer actively subscribes to services from the registration center regularly and quickly perceives changes in the operating status of the service provider through the registration center;
[0045] The registration center is used to store service addresses. When the operating status of the service provider changes, the status information of the existing service instances in the local cache is updated in a timely manner. The service consumer subscribes to the changed data from the registration center, or the registration center actively informs the changed data, so that the service caller (i.e., the service consumer) can quickly perceive the operating status changes of the callee (i.e., the service provider), thereby achieving decoupling between the service provider and the service consumer.
[0046] Based on the above operating relationship, the present invention proposes a service discovery method based on a cache mechanism. Figure 2 is a flow chart of an optional service discovery method based on a cache mechanism according to an embodiment of the present invention. Figure 2 As shown, the method includes the following steps:
[0047] Step S201: When the microservice instance information stored in the server cluster is updated, a change event pushed by the server cluster is received, wherein the change event is generated based on the instance change information.
[0048] It should be noted that, in response to the problem of high storage load pressure of etcd (distributed key-value storage system) mentioned in the prior art, an embodiment of the present invention proposes a storage strategy that combines a server cluster (i.e., an etcd lightweight database cluster) with the local cache of the registration center. The server cluster still has the function of regularly storing the full amount of existing service instance data, but the registration center will synchronize a copy of etcd data to the local cache. Service subscription and data query are both performed in the local cache, which can reduce the access load pressure of the server cluster.
[0049] When the registration center synchronizes etcd data to the local cache, the available data synchronization methods include at least: the server cluster actively pushes change events, and the registration center periodically requests change instance information.
[0050] The registry's local cache includes an instance information module and a version change information module. Each time a change event is received, the instance information module saves the latest version of the microservice instance information based on the instance change information. The version change information module generates a change version number for the changed microservice and generates version change data, which is then saved to the version change information module. After each cache update, the instance information module and the version change information module automatically compare data for consistency, using each other as the basis for updates to synchronize the cached data.
[0051] The storage strategy that combines the above server cluster with the local cache of the registration center can implement the full incremental update subscription logic with no duplication or omission of version numbers, and realize complete service discovery function.
[0052] When the microservice instance information in the etcd cluster's own memory changes, the cluster will actively push the change event to the local cache of the registration center. The embodiment of the present invention also describes the situation where the memory of the etcd cluster changes. Optionally, the service discovery method also includes: receiving an interaction request from the service provider; parsing the interaction request to obtain interaction operation information, wherein the interaction operation information includes the interaction operation type and interaction operation content requested by the service provider; and updating the microservice instance information in the server cluster based on the interaction operation information.
[0053] It should be noted that the etcd cluster is used to store the microservice instance information registered by the service provider with the registration center. In order to maintain the availability of registered microservices, the service provider will update the microservice instance information (adding new content, deleting content, etc.) from time to time to obtain instance change information for the etcd storage cluster.
[0054] The service provider cannot bypass the registration center to update the etcd storage cluster information. It must first initiate an interaction request for the etcd cluster to the registration center. The registration center's business processing layer will parse the interaction request and then execute the interaction operation information of the interaction request to complete the update.
[0055] Optionally, the interaction operation type may include interface operation types such as adding operation, deleting operation, setting lease operation, and renewing lease operation.
[0056] Facing business clients (i.e., service providers and consumers), the business processing layer of the registration center not only undertakes the service subscription and discovery functions, but also provides multi-dimensional data query services.
[0057] In existing projects, decision support for management and maintenance is required. However, etcd is a lightweight data storage service that needs to store essential data for hotspots. Using etcd to implement additional storage for different dimensions falls short of its lightweight storage role, and as a result, etcd's service discovery functionality cannot meet real-world project requirements. However, in the embodiments of the present invention, improvements are made to the local cache of the registry center, enabling multi-dimensional data storage. This local cache can then be used to provide multi-dimensional data query services to business clients via interface calls.
[0058] Figure 3 This is an optional architecture diagram of a registration center according to an embodiment of the present invention. Figure 3 As shown in the figure, the business processing layer of the registration center in this architecture supports receiving interactive operations from business clients to the etcd cluster, and directly operates the cluster database to perform interactive operations (such as registration, heartbeat, deletion, etc.); when the etcd cluster information changes, the business processing layer receives the change events actively pushed by the etcd cluster to synchronously update the local cache, at least updating the instance information cache module and the version information change cache module; based on the local cache of the registration center that is synchronized with the etcd cluster, the business processing layer can realize the service subscription requests and multi-dimensional data query functions received from business clients.
[0059] Based on the above registry center architecture, when a change event pushed by the server cluster is received, step S202 is continued to determine the event type of the change event, and the registry center local cache is updated based on the event type and instance change information.
[0060] Optionally, the step of updating the local cache of the registration center based on the event type and instance change information in step S202 includes: updating the existing instance information corresponding to the instance change information in the instance information cache module of the local cache of the registration center based on the event type and instance change information; generating a change version number for the change event, and generating version change data corresponding to the change event with the change version number as the key and the instance change information as the value; inserting the version change data corresponding to the change event into the version change cache module of the local cache of the registration center.
[0061] It should be noted that the data stored in etcd is the latest, and change events are pushed to the registration center based on the latest data changes. Event types include but are not limited to: delete and put.
[0062] When a microservice in etcd exceeds the set heartbeat timeout, the timed-out microservice is taken offline, the microservice instance is deleted promptly, and the event type is defined as delete. After the registration center receives the delete change event, it deletes the corresponding microservice instance information in the instance information cache module and inserts a version change data into the version change cache module. The key is the version number of this event, and the value is the specific content of this event.
[0063] When a microservice is registered for the first time or its heartbeat is activated for the first time in etcd, etcd will add new modifications and define the event type as put. After the registration center receives the put change event, it will add or modify the corresponding microservice instance information in the instance information cache module. The version change cache module will also increase the version number and specific event information accordingly.
[0064] Step S203: upon receiving a service discovery request from a service consumer, determining a service discovery strategy based on the service version number carried in the service discovery request and the updated local cache of the registration center. The service discovery strategy includes a full update strategy and an incremental update strategy.
[0065] Optionally, step S203 includes: determining whether the service consumer end is subscribing to the target microservice for the first time based on the service version number carried in the service discovery request; when it is determined that the service consumer end is subscribing to the target microservice for the first time, determining that the service discovery strategy is a full update strategy; when it is determined that the service consumer end is not subscribing to the target microservice for the first time, determining that the service discovery strategy is an incremental update strategy.
[0066] It should be noted that if the service consumer is not subscribing to the target microservice for the first time, the service version number carried in the service discovery request is the version number of the target microservice instance currently used in the service consumer's own environment. If the service consumer is subscribing to the target microservice for the first time, the service discovery request will carry the initial version number of the target microservice.
[0067] Furthermore, the step of determining whether the service consumer end has subscribed to the target microservice for the first time based on the service version number carried in the service discovery request includes: verifying whether the service version number exists in the initial version number set; if so, determining that the service consumer end has subscribed to the target microservice for the first time; if not, determining that the service consumer end has not subscribed to the target microservice for the first time.
[0068] Figure 4 is a flow chart of an optional service subscription processing method according to an embodiment of the present invention. Figure 4As shown in the figure, the service consumer makes a subscription request based on the local cache version of the registration center. The registration center combines two local caches (instance information cache module and version change information cache module) to determine the service discovery strategy applicable to the service version that the service consumer needs to subscribe to, and returns the service strategy to the service consumer. At the same time, the watch interface in the monitoring channel between etcd and the registration center continues to perform monitoring work. When the microservice instance information stored in etcd changes, the two local caches are controlled to add and delete the corresponding instance information and record the interface operations involved in each version.
[0069] Step S204: Generate a service configuration file based on the service discovery strategy, and return the service configuration file to the service consumer.
[0070] Optionally, the step of generating a service configuration file based on the service discovery strategy in step S204 includes: when determining that the service discovery strategy is a full update strategy, extracting the full instance information of the target microservice from the instance information cache module cached locally in the registration center; and generating a service configuration file based on the full instance information of the target microservice.
[0071] Optionally, the step of generating a service configuration file based on the service discovery strategy in step S204 also includes: when it is determined that the service discovery strategy is an incremental update strategy, querying the version change cache module cached locally in the registration center based on the service version number to obtain a query result, wherein the query result is used to record all version change data corresponding to the target microservice; based on the version change data indicated by the query result, extracting the incremental instance information of the target microservice in the instance information cache module; and generating a service configuration file based on the incremental instance information of the target microservice.
[0072] Due to the small amount of data stored in etcd, it is generally used only as a database for hot data storage, not as a database for application data persistence. In the embodiment of the present invention, the instance information cache module in the local cache of the registration center is also used to provide multi-dimensional data storage and multi-dimensional data query.
[0073] It should be noted that each microservice instance (environment-service name-ip:port) itself has attribute data, such as interface, version, method, software, module, etc., which can be divided into different dimensions for management according to management and operation needs, and supports the management platform to obtain multi-dimensional data through interface calls.
[0074] Figure 5 is a schematic diagram of an optional multi-dimensional data output interface according to an embodiment of the present invention, such as Figure 5 As shown, the instance information cache module includes the following interfaces: environment interface, software interface, module interface, service interface and ip:port interface (ie, method interface).
[0075] Optionally, the service discovery method also includes: adding a multidimensional query interface in the instance information cache module cached locally in the registration center, wherein the multidimensional query interface includes at least: an environment interface, a software interface, a module interface, a service interface and a method interface; receiving a multidimensional attribute query request transmitted by the business data end, and calling the multidimensional query interface based on the multidimensional attribute query request to obtain the attribute data query result.
[0076] Through the above steps S201 to S204, when the microservice instance information stored in the server cluster is updated, the change event pushed by the server cluster can be received first, wherein the change event is generated based on the instance change information, and then the event type of the change event is determined, and the local cache of the registration center is updated based on the event type and the instance change information. Then, when the service discovery request from the service consumer is received, the service discovery strategy is determined based on the service version number carried in the service discovery request and the updated local cache of the registration center, wherein the service discovery strategy includes: full update strategy and incremental update strategy. Finally, a service configuration file is generated based on the service discovery strategy, and the service configuration file is returned to the service consumer.
[0077] In an embodiment of the present invention, when the microservice instance information stored in the server cluster is updated, the change event and instance change information are synchronized to the local cache of the registration center, so that the service subscription and service query of the service consumer end are directly obtained from the local cache of the registration center, which greatly reduces the access pressure of the server cluster compared with the existing technology. In addition, the present invention also proposes that the service subscription discovery of the service consumer end adopts a combination of full update and incremental update, and determines the update method based on the requested service version number and the service information stored in the local cache of the registration center. Compared with the existing technology that adopts all full updates, the update mechanism of the combination of full update and incremental update is more reasonable, which achieves the technical effect of saving network bandwidth and saving system resources, thereby solving the technical problem in the related technology that the full synchronization of microservice instances puts huge storage pressure on the system server and occupies a large amount of system resources.
[0078] The present invention will be described below in conjunction with another specific embodiment.
[0079] Service discovery means that the service consumer (i.e., consumer) obtains a list of available services from the subscription interface provided by the registration center. The list provides information about all microservice instances registered with the registration center.
[0080] Figure 6 This is a schematic diagram of an optional operation logic of a service consumer according to an embodiment of the present invention. Figure 6As shown in the figure, when consumers send subscription requests, the request parameters include the environment and version number, and the information returned by the registration center to the consumer is the update status of each microservice in the current environment. The return result is the service array corresponding to the change event, with the service as the basic unit, the service name, the update operation type (modify, delete, add), all available instances and attribute data under each service.
[0081] It should be noted that the microservice instance of the registration center has two states: available and unavailable. When the service provider (i.e., consumer) registers for the first time, it is considered to be invalid and unavailable. When the first heartbeat is sent and subsequent heartbeats are continuously sent, the state of this instance is considered to be healthy and available.
[0082] Consumers subscribe to receive updates on service changes. A full update means that the consumer completely overwrites their locally cached data. In incremental update scenarios, if the update operation type returned to the consumer is "modify," the consumer must update the instance data of the corresponding service to match the returned result. If the update operation type is "add," the consumer must add specific information about the service and its instances to the returned result. If the update operation type is "delete," the consumer must delete all instance data of the service in the cache. This operational logic ensures that consumers are kept up-to-date with the registration center and are ultimately aware of service launches and delistings.
[0083] Figure 7 This is an optional service subscription logic diagram according to an embodiment of the present invention. Figure 7 As shown, the key in the version change cache module is the version number, and the value is the event operation type (put or delete) and the instance information array involved in the update. To prevent unlimited memory growth, the version change cache module only stores a certain number of historical versions. Assuming the consumer's request version is req_ver, the environment is req_env, and the maximum version number of the version change cache module is maxver, the service subscription logic is as follows:
[0084] Step 1: Determine whether req_ver exists in the version change cache module. If not, it is determined to be a full update type, and all available instances in this environment are directly returned. Otherwise, it is determined to be an incremental update type, and go to step 2.
[0085] Step 2: Determine whether req_ver is equal to maxver. If they are equal, the incremental environmental service instance information returned to the consumer is empty, which means that the data currently stored locally by the consumer is already the latest and does not need to be updated.
[0086] Step 3: If req_ver is not equal to maxver, calculate the version change difference between req_ver and maxver, quantify the version change difference with a number, and then determine and incrementally return the difference instance data.
[0087] The following describes in detail the detailed logic for calculating the service instance difference between req_ver and maxver in step 3. Figure 8 FIG. 1 is a flow chart of an optional quantitative calculation method for service instance differences according to an embodiment of the present invention. Figure 8 As shown, the method includes the following steps:
[0088] In step 3-1, traverse the services and instances under the services involved in the change versions between req_ver and maxver, quantize each instance based on the event type (put or delete), set the quantization value corresponding to put to 1, and the quantization value corresponding to delete to -1, calculate the cumulative change value of each instance, and store it in a dedicated data change module called changed. The data structure is map[env][service][ip+port]int. The final int value in this map is the cumulative change value of the above quantization.
[0089] In step 3-2, if changed[env] is empty, it is considered that there is no difference in the version, and the data returned to the consumer is set to empty, that is, the consumer does not need to make any updates. If it is not empty, then step 3-3 is performed.
[0090] In step 3-3, if the instance information cache module does not contain instance data of a service, and the service also involves a delete operation (the cumulative delete quantization value is greater than 0), the delete type will be returned to the consumer, indicating that the consumer needs to delete all instances of the service in the local cache accordingly. If the instance data corresponding to the service in the instance information cache module is not empty, then step 3-4 will be performed.
[0091] Step 3-4, traverse the services involved in changed[env] and examine the number of available instances in the current service and the cumulative value of add and delete for each instance in the service. When the number of available instances is 0, if the instance delete value is greater than 0, then the service needs to be deleted.
[0092] When the number of available instances is not 0, (1) if the number of available instances is equal to the cumulative value of add and delete is 0, then the data returned to the consumer is the instance data of the service that the consumer needs to add locally, (2) otherwise, the data returned to the consumer is the modify type, and the consumer needs to update the instance status of the service locally.
[0093] Finally, the scenario of full update is explained.
[0094] The total amount of data is huge. When there are many requests, a large number of requests request a hot key (that is, a certain environment value) at the same time. According to the logic of existing technology, when the first request accesses, it locks and blocks other requests. After the first request ends, other blocked requests will repeat this process, resulting in a lot of repetitive work, low efficiency, and slow interface response.
[0095] In the embodiment of the present invention, singleflight is adopted for optimization, with the environment as the key. When a large number of requests want to fully subscribe to the environment data, the first request will completely go through the full subscription logic. All available instance data will generate a universal full data according to the special response return data and return it to the consumer. The second, third and more repeated requests will directly reuse the data generated by the first request, saving memory and CPU overhead and improving response speed.
[0096] The registration center of the embodiment of the present invention not only needs to provide basic main functions such as service registration and discovery, but also develops a multi-dimensional service query interface to facilitate daily operation and maintenance and to output more diverse data. These data are all queried and output based on the instance cache data module.
[0097] The multi-dimensional query interface includes query environment, module, service, and instance data to meet daily operation and maintenance needs:
[0098] ① Query environment interface, which supports obtaining the existing environment data of the registration center;
[0099] ② Query module interface, which supports querying all existing modules in a certain environment and can also filter according to service or software parameters;
[0100] ③ Query service interface, which supports querying all existing services in a certain environment and can also filter according to software or module parameters;
[0101] ④ Query instance interface, which supports querying all existing instances in a certain environment and can also filter by service or software parameters;
[0102] ⑤Query the version number interface, which supports querying the latest version number value stored in the current version change cache module. This version data is closely related to the service subscription interface and is also one of the data variables of the backend database etcd. Its status needs to be output to view.
[0103] The multi-dimensional interface data query proposed in the embodiment of the present invention facilitates statistics on the number of existing environments, the number of running services, the number of service instances, and the number of involved IPs contained in the data of the current registration center, and accurately outputs statistics on the number of users contained in the current registration center.
[0104] The embodiments of the present invention can also solve the problem of link abnormality positioning more quickly and conveniently. For example, if you want to check which IP:port a service is deployed on, when the service cannot be adjusted and an error is reported that the service does not exist, it is necessary to determine whether the instance corresponding to the service has completed registration and heartbeat in the registration center. Or when an error is reported that the instance does not exist, it is also necessary to check whether the specific content fields of each instance do not match. The troubleshooting of these problems is implemented based on the multi-dimensional data query interface provided by the registration center.
[0105] The beneficial effect of the embodiment of the present invention is that by caching etcd data in the local cache of the registration center, the client does not need to interact directly with etcd, thereby protecting etcd and alleviating the access pressure of etcd.
[0106] At the same time, the embodiment of the present invention also adopts a service subscription method that combines full and incremental data to achieve service discovery. For the full update method, it is only used when the consumer subscribes for the first time, and singleflight is added to alleviate the pressure of repeatedly requesting the same full data, thereby improving interface performance. For the more frequently used incremental update method, the add and delete operations are digitally quantified, and the incremental operation type (i.e., modify / delete / add operation) of the current version based on the historical version is determined based on the historical version and the operation change. The digital quantification process only involves the addition and subtraction of integer variables. The method is simple and efficient, and incremental update, as the main update method for service subscription, can achieve no duplication or omission and logical completeness.
[0107] The embodiment of the present invention also adopts a method of caching etcd data locally, so that the registration center can perform multi-dimensional data storage and processing on all synchronized data, and can display the current data situation in multiple dimensions, facilitating management statistics and operation and maintenance.
[0108] The present invention is described below in conjunction with another optional embodiment.
[0109] Example 2
[0110] A service discovery device based on a cache mechanism provided in this embodiment includes multiple implementation units, each of which corresponds to each implementation step in the above-mentioned embodiment 1.
[0111] Figure 9 FIG. 1 is a schematic diagram of an optional service discovery device based on a cache mechanism according to an embodiment of the present invention. Figure 9As shown, the device may include: a receiving unit 91, an updating unit 92, a determining unit 93, and a generating unit 94.
[0112] The receiving unit 91 is configured to receive a change event pushed by the server cluster when the microservice instance information stored in the server cluster is updated, wherein the change event is generated based on the instance change information.
[0113] The updating unit 92 is configured to determine the event type of the change event and update the local cache of the registration center based on the event type and the instance change information.
[0114] The determination unit 93 is used to determine the service discovery strategy based on the service version number carried in the service discovery request and the updated local cache of the registration center when receiving the service discovery request from the service consumer. The service discovery strategy includes: full update strategy and incremental update strategy.
[0115] The generating unit 94 is configured to generate a service configuration file based on the service discovery strategy and return the service configuration file to the service consumer.
[0116] The above-mentioned service discovery device based on the cache mechanism can first receive the change event pushed by the server cluster through the receiving unit 91 when the microservice instance information stored in the server cluster is updated, wherein the change event is generated based on the instance change information, and then determine the event type of the change event through the updating unit 92, and update the local cache of the registration center based on the event type and the instance change information. Then, when receiving the service discovery request from the service consumer, the service discovery strategy is determined based on the service version number carried by the service discovery request and the updated local cache of the registration center through the determination unit 93, wherein the service discovery strategy includes: full update strategy and incremental update strategy. Finally, the service configuration file is generated based on the service discovery strategy through the generation unit 94, and the service configuration file is returned to the service consumer.
[0117] In an embodiment of the present invention, when the microservice instance information stored in the server cluster is updated, the change event and instance change information are synchronized to the local cache of the registration center, so that the service subscription and service query of the service consumer end are directly obtained from the local cache of the registration center, which greatly reduces the access pressure of the server cluster compared with the existing technology. In addition, the present invention also proposes that the service subscription discovery of the service consumer end adopts a combination of full update and incremental update, and determines the update method based on the requested service version number and the service information stored in the local cache of the registration center. Compared with the existing technology that adopts all full updates, the update mechanism of the combination of full update and incremental update is more reasonable, which achieves the technical effect of saving network bandwidth and saving system resources, thereby solving the technical problem in the related technology that the full synchronization of microservice instances puts huge storage pressure on the system server and occupies a large amount of system resources.
[0118] Optionally, the service discovery device also includes: a receiving module for receiving an interaction request from a service provider; a parsing module for parsing the interaction request to obtain interaction operation information, wherein the interaction operation information includes the interaction operation type and interaction operation content requested by the service provider; and a first update module for updating the microservice instance information in the server cluster based on the interaction operation information.
[0119] Optionally, the update unit includes: a second update module, used to update the existing instance information corresponding to the instance change information in the instance information cache module cached locally by the registration center based on the event type and instance change information; a first generation module, used to generate a change version number for the change event, and generate version change data corresponding to the change event with the change version number as the key and the instance change information as the value; an insertion module, used to insert the version change data corresponding to the change event into the version change cache module cached locally by the registration center.
[0120] Optionally, the determination unit includes: a first determination module, used to determine whether the service consumer end subscribes to the target microservice for the first time based on the service version number carried in the service discovery request; a second determination module, used to determine that the service discovery strategy is a full update strategy when it is determined that the service consumer end subscribes to the target microservice for the first time; a third determination module, used to determine that the service discovery strategy is an incremental update strategy when it is determined that the service consumer end does not subscribe to the target microservice for the first time.
[0121] Optionally, the generation unit includes: a first extraction module, used to extract the full instance information of the target microservice from the instance information cache module cached locally in the registration center when determining that the service discovery strategy is a full update strategy; a second generation module, used to generate a service configuration file based on the full instance information of the target microservice.
[0122] Optionally, the generation unit also includes: a query module, which is used to query the version change cache module cached locally in the registration center based on the service version number when it is determined that the service discovery strategy is an incremental update strategy, and obtain a query result, wherein the query result is used to record all version change data corresponding to the target microservice; a second extraction module, which is used to extract the incremental instance information of the target microservice in the instance information cache module based on the version change data indicated by the query result; and a third generation module, which is used to generate a service configuration file based on the incremental instance information of the target microservice.
[0123] Optionally, the service discovery device also includes: an adding module for adding a multidimensional query interface in the instance information cache module cached locally in the registration center, wherein the multidimensional query interface includes at least: an environment interface, a software interface, a module interface, a service interface and a method interface; a calling module for receiving a multidimensional attribute query request transmitted by the business data end, and calling the multidimensional query interface based on the multidimensional attribute query request to obtain the attribute data query result.
[0124] The above-mentioned service discovery device based on the cache mechanism can also include a processor and a memory. The above-mentioned receiving unit 91, update unit 92, determination unit 93, generation unit 94, etc. are all stored in the memory as program units, and the processor executes the above-mentioned program units stored in the memory to realize the corresponding functions.
[0125] The processor includes a kernel, which retrieves the corresponding program unit from memory. One or more kernels can be configured. By adjusting kernel parameters, the kernel determines a service discovery strategy based on the service version number carried in the service discovery request and the updated local cache of the registry. The kernel generates a service configuration file based on the service discovery strategy and returns the service configuration file to the service consumer.
[0126] The above-mentioned memory may include non-permanent memory in a computer-readable medium, random access memory (RAM) and / or non-volatile memory, such as read-only memory (ROM) or flash RAM, and the memory includes at least one memory chip.
[0127] The present application also provides a computer program product, which, when executed on a data processing device, is suitable for executing a program that is initialized with the following method steps: when the microservice instance information stored in the server cluster is updated, receiving a change event pushed by the server cluster, wherein the change event is generated based on the instance change information; determining the event type of the change event, and updating the local cache of the registration center based on the event type and the instance change information; when receiving a service discovery request from the service consumer, determining the service discovery strategy based on the service version number carried in the service discovery request and the updated local cache of the registration center, wherein the service discovery strategy includes: a full update strategy and an incremental update strategy; generating a service configuration file based on the service discovery strategy, and returning the service configuration file to the service consumer.
[0128] According to another aspect of an embodiment of the present invention, a computer-readable storage medium is further provided, which includes a stored computer program, wherein when the computer program is running, the device where the computer-readable storage medium is located is controlled to execute the service discovery method based on the cache mechanism of any one of the above-mentioned embodiments.
[0129] According to another aspect of an embodiment of the present invention, an electronic device is also provided, comprising one or more processors and a memory, wherein the memory is used to store one or more programs, wherein when the one or more programs are executed by one or more processors, the one or more processors implement the service discovery method based on the cache mechanism of any one of the above-mentioned embodiments.
[0130] Figure 10 FIG is a hardware structure block diagram of an electronic device (or mobile device) for a service discovery method based on a cache mechanism according to an embodiment of the present invention. Figure 10 As shown, the electronic device may include one or more ( Figure 10 (The processor 1002a, processor 1002b, ..., processor 1002n are used to illustrate) processor 1002 (processor 1002 may include but is not limited to a processing device such as a microprocessor MCU or a programmable logic device FPGA), and memory 1004 for storing data. In addition, it may also include: a display, an input / output interface (I / O interface), a universal serial bus (USB) port (which may be included as one of the ports of the I / O interface), a network interface, a keyboard, a power supply and / or a camera. It will be understood by those skilled in the art that Figure 10 The structure shown is only for illustration and does not limit the structure of the above electronic device. Figure 10 More or fewer components than shown, or with Figure 10 Different configurations shown.
[0131] The serial numbers of the above embodiments of the present invention are for description only and do not represent the advantages or disadvantages of the embodiments.
[0132] In the above embodiments of the present invention, the description of each embodiment has its own focus. For parts that are not described in detail in a certain embodiment, reference can be made to the relevant descriptions of other embodiments.
[0133] In the several embodiments provided in this application, it should be understood that the disclosed technical content can be implemented in other ways. Among them, the device embodiments described above are only exemplary. For example, the division of the units can be a logical function division. In actual implementation, there may be other division methods, such as multiple units or components can be combined or integrated into another system, or some features can be ignored or not executed. Another point is that the mutual coupling or direct coupling or communication connection shown or discussed can be through some interfaces, indirect coupling or communication connection of units or modules, which can be electrical or other forms.
[0134] The units described as separate components may or may not be physically separate, and the components shown as units may or may not be physical units, that is, they may be located in one place or distributed across multiple units. Some or all of the units may be selected according to actual needs to achieve the purpose of the present embodiment.
[0135] In addition, the functional units in the various embodiments of the present invention may be integrated into a single processing unit, each unit may exist physically separately, or two or more units may be integrated into a single unit. The aforementioned integrated units may be implemented in the form of hardware or software functional units.
[0136] If the integrated unit is implemented in the form of a software functional unit and sold or used as an independent product, it can be stored in a computer-readable storage medium. Based on this understanding, the technical solution of the present invention, or the part that contributes to the prior art, or all or part of the technical solution can be embodied in the form of a software product. The computer software product is stored in a storage medium and includes several instructions for enabling a computer device (which can be a personal computer, server or network device, etc.) to perform all or part of the steps of the method described in each embodiment of the present invention. The aforementioned storage medium includes: U disk, read-only memory (ROM, Read-Only Memory), random access memory (RAM, Random Access Memory), mobile hard disk, magnetic disk or optical disk, etc. Various media that can store program codes.
[0137] The above is only a preferred embodiment of the present invention. It should be pointed out that for ordinary technicians in this technical field, several improvements and modifications can be made without departing from the principles of the present invention. These improvements and modifications should also be regarded as within the scope of protection of the present invention.
Claims
1. A service discovery method based on a cache mechanism, characterized in that: include: When the microservice instance information stored in the server cluster is updated, receiving a change event pushed by the server cluster, wherein the change event is generated based on the instance change information; Determine the event type of the change event, and update the local cache of the registration center based on the event type and the instance change information; When receiving a service discovery request from a service consumer, a service discovery strategy is determined based on the service version number carried in the service discovery request and the updated local cache of the registration center, wherein the service discovery strategy includes: a full update strategy and an incremental update strategy. The step of determining the service discovery strategy based on the service version number carried by the service discovery request and the updated local cache of the registration center includes: determining whether the service consumer end subscribes to the target microservice for the first time based on the service version number carried by the service discovery request; if it is determined that the service consumer end subscribes to the target microservice for the first time, determining that the service discovery strategy is the full update strategy; if it is determined that the service consumer end does not subscribe to the target microservice for the first time, determining that the service discovery strategy is the incremental update strategy; A service configuration file is generated based on the service discovery strategy, and the service configuration file is returned to the service consumer.
2. The service discovery method according to claim 1, wherein: The service discovery method further includes: Receive interaction requests from service providers; Parsing the interaction request to obtain interaction operation information, wherein the interaction operation information includes the interaction operation type and interaction operation content requested by the service provider; The microservice instance information in the server cluster is updated based on the interactive operation information.
3. The service discovery method according to claim 1, wherein: The step of updating the local cache of the registration center based on the event type and the instance change information includes: Based on the event type and the instance change information, the existing instance information corresponding to the instance change information in the instance information cache module cached locally in the registration center is updated; Generate a change version number for the change event, and generate version change data corresponding to the change event using the change version number as a key and the instance change information as a value; Insert the version change data corresponding to the change event into the version change cache module cached locally in the registration center.
4. The service discovery method according to claim 1, wherein: The step of generating a service configuration file based on the service discovery strategy includes: When it is determined that the service discovery strategy is a full update strategy, extracting the full instance information of the target microservice in the instance information cache module cached locally in the registration center; The service configuration file is generated based on the full instance information of the target microservice.
5. The service discovery method according to claim 1, wherein: The step of generating a service configuration file based on the service discovery strategy further includes: When it is determined that the service discovery strategy is an incremental update strategy, query the version change cache module of the local cache of the registration center based on the service version number to obtain a query result, wherein the query result is used to record all version change data corresponding to the target microservice; Based on the version change data indicated by the query result, extracting incremental instance information of the target microservice in an instance information cache module; The service configuration file is generated based on the incremental instance information of the target microservice.
6. The service discovery method according to claim 1, wherein: The service discovery method further includes: Adding a multi-dimensional query interface to the instance information cache module of the local cache of the registration center, wherein the multi-dimensional query interface at least includes: an environment interface, a software interface, a module interface, a service interface and a method interface; A multidimensional attribute query request transmitted by a business data end is received, and the multidimensional query interface is called based on the multidimensional attribute query request to obtain an attribute data query result.
7. A service discovery device based on a cache mechanism, characterized in that: include: A receiving unit, configured to receive a change event pushed by the server cluster when the microservice instance information stored in the server cluster is updated, wherein the change event is generated based on the instance change information; An updating unit, configured to determine an event type of the change event, and update a local cache of a registration center based on the event type and the instance change information; A determination unit is configured to, upon receiving a service discovery request from a service consumer, determine a service discovery strategy based on the service version number carried in the service discovery request and the updated local cache of the registration center, wherein the service discovery strategy includes: a full update strategy and an incremental update strategy; The determination unit includes: a first determination module, which is used to determine whether the service consumer end subscribes to the target microservice for the first time based on the service version number carried by the service discovery request; a second determination module, which is used to determine that the service discovery strategy is the full update strategy when it is determined that the service consumer end subscribes to the target microservice for the first time; and a third determination module, which is used to determine that the service discovery strategy is the incremental update strategy when it is determined that the service consumer end does not subscribe to the target microservice for the first time. A generating unit is configured to generate a service configuration file based on the service discovery strategy and return the service configuration file to the service consumer.
8. A computer-readable storage medium, characterized in that The computer-readable storage medium includes a stored computer program, wherein when the computer program is running, the device where the computer-readable storage medium is located is controlled to execute the service discovery method based on the cache mechanism according to any one of claims 1 to 6.
9. An electronic device, characterized in that: It includes one or more processors and a memory, wherein the memory is used to store one or more programs, wherein when the one or more programs are executed by the one or more processors, the one or more processors implement the service discovery method based on the cache mechanism as described in any one of claims 1 to 6.
Citation Information
Patent Citations
Data management for a multi-tenant identity cloud service
US20180063143A1
Dynamic configuration management method and system in microservice framework
WO2020237797A1