Service discovery method and device based on cache mechanism, and electronic equipment
By using the combination of cache mechanism and full quantity and incremental update strategy in the registration center, the full quantities of micro -service institutional examples are synchronized with the problem of huge storage pressure of the system server and the large resource occupation. Network bandwidth and system resources.
Patent Information
- Application Number
- CN202411495217.4
- Authority / Receiving Office
- CN · China
- Patent Type
- Applications(China)
- Current Assignee / Owner
- Filing Date
- 2024-10-24
- Publication Date
- 2025-01-24
- Estimated Expiration
- 2044-10-24
AI Technical Summary
Full synchronization of microservice instances has huge storage pressure on the system server and occupies a large amount of system resources.
Using a service -based service discovery method, when the micro -service instance information stored in the server cluster is updated, the change event pushed by the server cluster is received, and the local cache of the registered center cache is updated based on the event type. When receiving service discovery requests on the service consumption side, the service version number and updated local cache determine service discovery strategies based on service discovery requests, including full -value update strategies and incremental update strategies, generate service configuration files and return.
By reducing the access pressure of the server cluster, saving network bandwidth and saving system resources, the full quantities of micro -service institutional example synchronization to the storage pressure of the system server and the problem of excessive resources.
Smart Images

Figure CN119363813A_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 the theory of microservices matures, more and more Internet companies are adopting microservice architecture to support business development. The service discovery mechanism of microservice instances is provided by the registration center. Generally, service discovery can directly use etcd (distributed key-value storage system) to obtain data change notifications efficiently. However, this method of exposing etcd directly to the client loses the protection of etcd. As the number of users gradually increases, the load pressure of etcd increases. In addition, when consumers request services from the registration center, the registration center provides the latest available instance data in full every time, so that consumers can fully update the status data of local service providers. However, full synchronization as the only data synchronization method also has disadvantages, such as long time consumption, large network bandwidth occupation, and large system resource occupation. As the amount of data increases, the impact of the above disadvantages will become more prominent.
[0003] To address the above-mentioned problems, no effective solution has been proposed yet. Summary of the invention
[0004] The embodiments of the present invention provide a service discovery method and device based on a cache mechanism, and an electronic device, so as to at least solve 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.
[0005] According to one aspect of an embodiment of the present invention, a service discovery method based on a cache mechanism is provided, comprising: when 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 instance change information; determining an 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; when a service discovery request from a service consumer is received, determining a service discovery strategy based on a service version number carried in the service discovery request and an 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; and 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 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 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 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 in 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 determining 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 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 an 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 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 a 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 a service discovery request is received from a 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, used to receive an interaction request from a service provider; a parsing module, used to parse 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; a first update module, used to update 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 is subscribing 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 is subscribing 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 is not subscribing 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 locally cached by 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, used to add 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, used to receive a multidimensional attribute query request transmitted by the business data end, and call the multidimensional query interface based on the multidimensional attribute query request to obtain an 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, there is also provided an electronic device, 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, 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 a service discovery request from a 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 prior art. 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 which update method to adopt based on the requested service version number and the service information stored in the local cache of the registration center. Compared with the prior art that all adopts the full update method, the update mechanism of the combination of full update and incremental update is more reasonable, achieving 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. 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 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 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 flow chart of an optional quantitative calculation method of service instance differences according to an embodiment of the present invention;
[0032] Fig. 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] Fig.10 It 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 scheme of the present invention, the technical scheme 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 described embodiments 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 creative work should fall within the scope of protection of the present invention.
[0035] It should be noted that the terms "first", "second", etc. in the specification 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 data 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 that are 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 based on a lightweight database and a loosely coupled service location of 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 is described in detail below in conjunction with various embodiments.
[0039] Embodiment 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 the 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 entity of the embodiment of the present invention may be a microservice distributed control system, especially a registration center in the system. It should be noted that the prior art uses a registration center to achieve loose coupling of service locations and efficient service discovery in order to cope with the increasing number of microservice instances and the highly dynamic nature of the addresses exposed by the services themselves. The runtime status of service instances often changes, causing the service caller to need to perceive downtime and the perception speed to be slow.
[0042] Figure 1 is an optional operation relationship diagram based on a 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 the service address. 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 changes in the operating status of the called party (i.e., the service provider), thereby achieving decoupling between the service provider and the service consumer.
[0046] Based on the above operation relationship, the present invention proposes a service discovery method based on a cache mechanism. Figure 2 is a flowchart 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 comprises 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 a local cache of a 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 the 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] In the process of synchronizing etcd data to the local cache by the registration center, 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 local cache of the registration center includes: instance information module and 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 saved in the version change information module. After each cache update, the instance information module and the version change information module automatically check whether the data is consistent, and use each other as the basis for updating to adjust the cache data to a synchronized state.
[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 without duplication or omission of version numbers, and realize the complete service discovery function.
[0052] When the microservice instance information in the etcd cluster's own memory changes, the etcd 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 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.
[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 needs to first initiate an interaction request for the etcd cluster to the registration center. The business processing layer of the registration center will parse the interaction request and then execute the interaction operation information of the interaction request to complete the update.
[0055] Optionally, the interactive operation type may include interface operation types such as adding operation, deleting operation, setting lease operation and renewing 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 can provide multi-dimensional data query services.
[0057] In actual projects of the prior art, decision support needs to be provided for management and maintenance. However, etcd is a lightweight data storage and needs to store the necessary data for hotspots. If etcd is used to implement additional storage of different dimensions, it does not meet its lightweight storage positioning. Therefore, the etcd service discovery function cannot meet the requirements of actual projects. However, in the embodiment of the present invention, after improving the local cache of the registration center, the local cache can be used to implement multi-dimensional storage of data, and then provide multi-dimensional data query services to business clients through interface calls.
[0058] Figure 3 is an optional architecture diagram of a registration center according to an embodiment of the present invention, such as Figure 3 As shown, the registration center business processing layer in the 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 registration center local cache that is synchronously updated 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 registration center architecture, when a change event pushed by a server cluster is received, step S202 is continued to be executed to determine the event type of the change event, and the registration 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 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; and 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 in time, 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 in 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 the 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, and the version change cache module will also increase the version number and specific event information accordingly.
[0064] Step S203, when 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.
[0065] Optionally, step S203 includes: determining whether the service consumer 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 is subscribing to the target microservice for the first time, determining the service discovery strategy as a full update strategy; when it is determined that the service consumer is not subscribing to the target microservice for the first time, determining the service discovery strategy as 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 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 has subscribed to the target microservice for the first time; if not, determining that the service consumer has not subscribed to the target microservice for the first time.
[0068] Figure 4 is a flowchart 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 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 a service configuration file based on the incremental instance information of the target microservice.
[0072] In view of 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 an 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 a 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 the 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 prior art. 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 which update method to adopt based on the requested service version number and the service information stored in the local cache of the registration center. Compared with the prior art that all adopts the full update method, 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 that the full synchronization of microservice instances in the related technology puts huge storage pressure on the system server and occupies a large amount of system resources.
[0078] The present invention is described below in conjunction with another specific implementation manner.
[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 is an optional operation logic diagram of a service consumer according to an embodiment of the present invention, such as Figure 6As shown in the figure, the request parameters when the consumer sends a subscription request 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 still 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 obtain service changes. Full update means that consumers fully overwrite local cached data. In the incremental update scenario, when the update operation type returned to the consumer by the program is modify, the consumer needs to update the instance data under the corresponding service to be consistent with the returned result. When the update operation type is add, the consumer needs to add the specific information of this service and its instance in the returned result. When the update operation type is delete, the consumer needs to delete all instance data of this service in the cache. The above operation logic can keep consumers consistent with the latest situation of the registration center, and finally perceive the online and offline of the service.
[0083] Figure 7 is an optional service subscription logic diagram according to an embodiment of the present invention, such as Figure 7 As shown in the figure, the key in the version change cache module is the version number, the value is the event operation type (put or delete), and the instance information array involved in the update. At the same time, in order to prevent unlimited memory growth, the version change cache module only stores a certain number of historical versions. Assume that 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, then 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 in full. 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: When req_ver is not equal to maxver, the version change difference between req_ver and maxver is calculated, and the version change difference is quantified digitally to determine and incrementally return the difference instance data.
[0087] The detailed logic of calculating the service instance difference between req_ver and maxver in step 3 is described below. Figure 8 is a flowchart of an optional method for quantitatively calculating service instance differences according to an embodiment of the present invention. Figure 8 As shown, the method comprises the following steps:
[0088] Step 3-1, traverse the services and instances under the services involved in the change versions between req_ver and maxver, quantify each instance according to the event type, whether it is 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 special data change module changed. The data structure is map[env][service][ip+port]int. Finally, the int type value in this map is the cumulative change value of the above quantization.
[0089] 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.
[0090] Step 3-3, if the instance information cache module does not have instance data for 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.
[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 what is returned to the consumer is the instance data of the service that the consumer needs to add locally; (2) otherwise, what is 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 the number of requests is large, a large number of requests request a hot key (that is, a certain environment value) at the same time. According to the logic of the existing technology, when the first request accesses, it will lock and block 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 used 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 logic of full subscription, and 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 stock modules in a certain environment, and can also filter according to service or software parameters;
[0100] ③Query service interface, which supports querying all stock 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 IPs involved contained in the data in 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 locating link anomalies 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 see 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, which can display the current data situation in multiple dimensions and facilitate management statistics and operation and maintenance.
[0108] The present invention is described below in conjunction with another optional embodiment.
[0109] Embodiment 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] Fig. 9 is a schematic diagram of an optional service discovery device based on a cache mechanism according to an embodiment of the present invention. Fig. 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 used 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 used 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, wherein the service discovery strategy includes: full update strategy and incremental update strategy.
[0115] The generating unit 94 is used 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 by the determination unit 93 based on the service version number carried by 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, and finally generate a service configuration file based on the service discovery strategy through the generation unit 94, and return the service configuration file 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 the 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 prior art. 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 which update method to adopt based on the requested service version number and the service information stored in the local cache of the registration center. Compared with the prior art 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 that the full synchronization of microservice instances in the related technology 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 in 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 in the version change cache module cached locally in 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 is subscribing 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 is not subscribing 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 locally cached by 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 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 an attribute data query result.
[0124] The above-mentioned service discovery device based on the cache mechanism may also include a processor and a memory. The above-mentioned receiving unit 91, updating unit 92, determining unit 93, generating 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 corresponding functions.
[0125] The above processor includes a kernel, which calls the corresponding program unit from the memory. The kernel can be set to one or more, and 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 by adjusting the kernel parameters, and the service configuration file is generated based on the service discovery strategy, and the service configuration file is returned 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 storage 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 initializes the following method steps: when microservice instance information stored in a server cluster is updated, receiving a change event pushed by a 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 a registration center based on the event type and the instance change information; when 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.
[0128] According to another aspect of an embodiment of the present invention, a computer-readable storage medium is also provided, the computer-readable storage medium including 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 service discovery methods based on the cache mechanism in the above-mentioned embodiment 1.
[0129] According to another aspect of an embodiment of the present invention, there is also provided an electronic device, 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 one.
[0130] Fig.10 1 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. Fig.10 As shown, the electronic device may include one or more ( Fig.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 a 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 can be understood by those skilled in the art that Fig.10 The structure shown is only for illustration and does not limit the structure of the above electronic device. Fig.10 More or fewer components as shown, or with Fig.10 Different configurations are shown.
[0131] The serial numbers of the above embodiments of the present invention are only for description 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 emphasis. 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 schematic. For example, the division of the units can be a logical function division. There may be other division methods in actual implementation. For example, 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 separated, and the components shown as units may or may not be physical units, that is, they may be located in one place or distributed on 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, each functional unit in each embodiment of the present invention may be integrated into one processing unit, or each unit may exist physically separately, or two or more units may be integrated into one unit. The above-mentioned integrated unit may be implemented in the form of hardware or in the form of 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, in essence, 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, and the computer software product is stored in a storage medium, including a number of instructions for a computer device (which can be a personal computer, a server or a 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 and other 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 principle of the present invention. These improvements and modifications should also be regarded as 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; In the case of 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, wherein the service discovery strategy includes: a full update strategy and an 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, characterized in that: The service discovery method further 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; 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, characterized in that: 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; The version change data corresponding to the change event is inserted into the version change cache module cached locally in the registration center.
4. The service discovery method according to claim 1, characterized in that: The step of 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 includes: Determine whether the service consumer subscribes 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 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 is not subscribing to the target microservice for the first time, the service discovery strategy is determined to be the incremental update strategy.
5. The service discovery method according to claim 4, characterized in that: 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.
6. The service discovery method according to claim 4, characterized in that: The step of generating a service configuration file based on the service discovery strategy also includes: In the case where it is determined that the service discovery strategy is an incremental update strategy, querying 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 the incremental instance information of the target microservice in the instance information cache module; The service configuration file is generated based on the incremental instance information of the target microservice.
7. The service discovery method according to claim 1, characterized in that: The service discovery method further includes: Adding a multi-dimensional query interface in 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 terminal is received, and the multidimensional query interface is called based on the multidimensional attribute query request to obtain an attribute data query result.
8. 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, configured to 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 when receiving a service discovery request from a service consumer, wherein the service discovery strategy includes: a full update strategy and an incremental update strategy; A generating unit is used to generate a service configuration file based on the service discovery strategy, and return the service configuration file to the service consumer.
9. 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 as described in any one of claims 1 to 7.
10. 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 7.
Citation Information
Patent Citations
Data updating method and device, storage medium and electronic equipment
CN112235378A
Local cache synchronization method and system based on micro-service and version number mechanism
CN117171271A
Method for uniformly registering, discovering and calling multiple types of services
CN118714191A
Service discovery for a multi-tenant identity and data security management cloud service
US20180041515A1
Data management for a multi-tenant identity cloud service
US20180063143A1