A method and device for distributing and acquiring data across modules in the same container in microservices
By adopting event publishing and subscription solutions in microservices, performance loss and transaction control problems in cross-module interaction are solved, flexible data distribution acquisition and two-way interaction are realized, and upstream and downstream transaction control is supported.
Patent Information
- Application Number
- CN202411823981.X
- Authority / Receiving Office
- CN · China
- Patent Type
- Applications(China)
- Current Assignee / Owner
- Filing Date
- 2024-12-12
- Publication Date
- 2025-04-04
AI Technical Summary
The inter-span module interactions in existing microservices have performance losses and are unable to solve upstream and downstream transactions, and the Spring event mechanism is not suitable for this scenario.
Provides a method of data distribution and acquisition across modules of the same container in microservices, replaces remote calls through event publishing and subscription schemes, supports upstream and downstream transaction control, and realizes two-way interaction.
It realizes a more flexible event publishing and subscription solution, saves unnecessary performance overhead, supports upstream and downstream transaction control, and developers do not need to pay attention to upstream and downstream module code connections, and focus on business implementation.
Smart Images

Figure CN119759609A_ABST
Abstract
Description
Technical Field
[0001] The present application belongs to the field of microservice communication technology, and specifically relates to a method and device for distributing and acquiring data across modules in the same container in a microservice. Technical Background
[0002] Due to the particularity of some microservice low-code platforms, the development of business modules is often isolated, but modules can be freely combined through service orchestration during deployment, so there are relatively large problems in the interaction between modules in the App. The existing solution is basically to use message middleware or remote calls for cross-module interaction, but there is undoubtedly unnecessary performance loss and the problem of being unable to solve upstream and downstream transactions. The event mechanism provided by Spring is even less suitable for this scenario.
[0003] Technical content
[0004] In view of the above-mentioned shortcomings and deficiencies of the prior art, the present invention provides a method and device for distributing and acquiring data across modules in the same container in a microservice, so as to realize a more flexible event publishing and subscription solution that allows developers to focus on business implementation without having to pay attention to the code connection between upstream and downstream modules.
[0005] In order to achieve the above object, the main technical solutions adopted by the present invention include:
[0006] In a first aspect, an embodiment of the present invention provides a method for distributing and acquiring data across modules in the same container in a microservice, including:
[0007] Declare the subject, at least define the business unique identifier, execution mode and thread pool;
[0008] Declare the subscriber, declare the @ConsumerHandler annotation in the custom class and integrate the AbstractConsumerHandler class. In addition, the @ConsumerHandler annotation needs to provide the following attributes: the unique identifier of the subject service to be monitored;
[0009] Scan all the classes in the APP that declare @ConsumerHandler annotations and integrate AbstractConsumerHandler, and organize them into the local subscriber pool;
[0010] The event publisher publishes the event message to the topic, and the topic obtains the corresponding subscriber bean according to the configuration rules and executes the target subscriber logic.
[0011] Optionally, the topic obtains the corresponding subscriber bean according to the configuration rules and executes the target subscriber logic, including: obtaining the topic object through the configuration center of the topic; if the execution mode of the topic is synchronous, obtaining one of the multiple current topic subscribers for execution, and returning the execution result of the subscriber to the event publisher.
[0012] Optionally, the topic obtains the corresponding subscriber bean according to the configuration rules and executes the target subscriber logic, including: obtaining the topic object through the configuration center of the topic; if the execution mode of the topic is asynchronous, obtaining all subscribers of the current topic, and executing the current subscriber according to the thread pool configured for the topic.
[0013] Optionally, when the execution mode of the topic is synchronous, if the publisher has a transaction, the subscriber business will be automatically added to the current transaction; if the publisher does not have a transaction, it is determined whether the subscriber has implemented the transaction using transaction annotations or programmatic transactions; if the subscriber has a transaction, a new transaction execution is started.
[0014] Optionally, when the execution mode of the topic is asynchronous, if the subscriber uses transaction annotations or programmatic transactions, the downstream subscriber will start a new transaction execution after the upstream business transaction execution is completed.
[0015] Optionally, the event publisher publishes the event message to the topic, including: using message publisher.send to publish the message, wherein the send method contains parameters including at least the unique identifier of the topic business and the message content.
[0016] Optionally, the topic declaration method includes: directly defining a topic list in an application configuration file based on a springboot project; and / or configuring the topic by configuring beans.
[0017] Optionally, the custom class declares the @ConsumerHandler annotation and integrates the AbstractConsumerHandler class: the AbstractConsumerHandler abstract class contains an execute abstract method, and the custom class inherits and overrides the execute method; the parameters contained in the execute method include: the subject information object and the message content.
[0018] Optionally, the custom class declares the @ConsumerHandler annotation and integrates the AbstractConsumerHandler class: the @ConsumerHandler internally references the @Component annotation and indicates that the @ConsumerHandler can only be applied to the class.
[0019] In the second aspect, an embodiment of the present invention also provides a data distribution and acquisition device for the same container across modules in a microservice, the device comprising a processor and a memory, the memory storing at least one instruction, at least one program, a code set or an instruction set, the at least one instruction, the at least one program, the code set or the instruction set being loaded and executed by the processor to implement the aforementioned data distribution and acquisition method for the same container across modules in a microservice.
[0020] This technical solution, on the one hand, replaces remote calls with event publishing and subscription, saving unnecessary performance overhead and supporting upstream and downstream transaction control; on the other hand, compared with the one-way interaction of the Spring event mechanism, the current technology provides the function of receiving the return result after publishing a message, realizing two-way interaction. Therefore, developers do not need to pay attention to the connection between the upstream and downstream module codes, but only need to focus on business implementation.
[0021] Instruction Manual
[0022] Figure 1 A flowchart of a method for distributing and acquiring data across modules in the same container in a microservice provided by an embodiment of the present invention;
[0023] Figure 2 A simplified diagram of the execution of a method for distributing and acquiring data across modules in the same container in a microservice provided by an embodiment of the present invention. DETAILED DESCRIPTION
[0024] In order to make the above-mentioned objects, features and advantages of the present invention more obvious and easy to understand, the specific embodiments of the present invention are described in detail below in conjunction with the accompanying drawings. Although certain embodiments of the present invention are shown in the accompanying drawings, it should be understood that the present invention can be implemented in various forms and should not be interpreted as being limited to the embodiments described herein. On the contrary, these embodiments are provided to provide a more thorough and complete understanding of the present invention. It should be understood that the drawings and embodiments of the present invention are only for exemplary purposes and are not intended to limit the scope of protection of the present invention.
[0025] It should be understood that the various steps described in the method embodiments of the present invention may be performed in different orders and / or in parallel. In addition, the method embodiments may include additional steps and / or omit the steps shown. The scope of the present invention is not limited in this respect.
[0026] The term "including" and its variations used in this document are open inclusions, that is, "including but not limited to". The term "based on" means "based at least in part on". The term "one embodiment" means "at least one embodiment"; the term "another embodiment" means "at least one other embodiment"; the term "some embodiments" means "at least some embodiments"; the term "optionally" means "optional embodiments". Relevant definitions of other terms will be given in the following description. It should be noted that the concepts of "first", "second", etc. mentioned in the present invention are only used to distinguish different devices, modules or units, and are not used to limit the order or interdependence of the functions performed by these devices, modules or units.
[0027] It should be noted that the modifications of "one" and "plurality" mentioned in the present invention are illustrative rather than restrictive, and those skilled in the art should understand that, unless otherwise clearly indicated in the context, it should be understood as "one or more".
[0028] like Figure 1 As shown, an embodiment of the present invention provides a method for distributing and acquiring data across modules in the same container in a microservice, comprising the following steps:
[0029] S1. Declare the subject, at least define the business unique identifier, execution mode and thread pool.
[0030] Specifically, the technical framework provides two ways to configure topics: (1) For springboot projects, you can define the topic list directly in the application configuration file. You need to define the unique identifier, description, execution mode (synchronous / asynchronous), and thread pool of the topic (thread isolation between topics during asynchronous interaction). (2) If you are not used to using application configuration files or the Spring framework, you can also configure it by configuring beans. The framework provides a topic configuration center through the factory mode. You can inject the topic configuration center into any bean and call the method to register the topic.
[0031] Therefore, based on the above two configuration ideas, the following methods can be used to implement the technical framework: (1) Use the design idea of the factory pattern to declare a configuration center bean. The configuration center contains a topic list and a topic registration method.
[0032] (2) Declare a registrar. After the registrar bean is initialized, it will scan the corresponding application configuration file and the topic definition list registered by the configuration class, and register it to the configuration center.
[0033] S2. Declare the subscriber, declare the @ConsumerHandler annotation in the custom class and integrate the AbstractConsumerHandler class. The additional @ConsumerHandler annotation needs to provide properties including: the unique identifier of the subject business that needs to be monitored.
[0034] Specifically, @ConsumerHandler references the @Component annotation internally and indicates that @ConsumerHandler can only be applied to classes. In this way, when a custom class uses the @ConsumerHandler annotation, it will be declared as a bean at the same time. Through the unique identifier of the topic to be monitored, the current custom class will be identified by the technical framework as a subscriber of a specified topic.
[0035] The AbstractConsumerHandler abstract class contains the execute abstract method. Custom classes need to inherit and override this execute method. The execute method contains parameters: topic information object and message content. The business end can write its own business logic in execute.
[0036] S3. Scan all classes in the app that declare @ConsumerHandler annotations and integrate AbstractConsumerHandler, and organize them into the local subscriber pool.
[0037] Specifically, after the APP is started, it will automatically scan and establish a mapping relationship between the topic and the subscriber through the unique identifier of the topic and store it in the memory.
[0038] S4. The event publisher publishes the event message to the topic. The topic obtains the corresponding subscriber bean according to the configuration rules and executes the target subscriber logic.
[0039] Specifically, the technical module encapsulates a message publisher, including the send static method. You can directly use the message publisher.send to publish messages, where the send method contains parameters: topic unique identifier, message content. Therefore, after the downstream module subscribes to the Topic, the upstream module will obtain the qualified downstream business Bean through bean call after sending the message and execute the corresponding downstream business. The execution method is as follows:
[0040] After calling send, you can get the topic object from the configuration center according to the topic to know whether the execution mode of this topic is synchronous or asynchronous.
[0041] If the execution mode is synchronous, it will get one of the multiple current topic subscribers to execute and return the subscriber's return result to the upstream module, that is, the publisher. Among them, if the upstream module has a transaction, the subscriber business will be automatically added to the current transaction; if there is no transaction upstream, it will determine whether the downstream subscriber has implemented the transaction using transaction annotations or programmatic transactions. If there is a transaction downstream, a new transaction will be started downstream for execution.
[0042] If the execution mode is asynchronous, all subscribers of the current topic will be obtained and the topic will obtain the thread pool configured by the topic and execute the current subscriber. Among them, if the downstream subscriber uses transaction annotations or programmatic transactions, the downstream subscriber will start a new transaction execution after the upstream business transaction is completed.
[0043] In the above way, all business modules only need to pay attention to the topic. The upstream business uses the topic to define the tracking point and send the message out. The downstream module does not need to pay attention to what the upstream module is, but only needs to pay attention to whether its own business needs to subscribe to this topic.
[0044] At the same time, an embodiment of the present invention also provides a data distribution acquisition device for the same container across modules in a microservice, including: a processor and a memory, wherein at least one instruction, at least one program, code set or instruction set is stored in the memory, and the at least one instruction, at least one program, code set or instruction set is loaded and executed by the processor to implement a data distribution acquisition method for the same container across modules in a microservice in the technical solution. Since the effect achieved by the device in this embodiment is the same as the effect achieved by the data distribution acquisition method for the same container across modules in a microservice in the embodiment, it will not be repeated here.
[0045] It will be appreciated by those skilled in the art that embodiments of the present invention may be provided as methods, systems or computer program products. Therefore, the present invention may take the form of a complete hardware embodiment, a complete software embodiment, or an embodiment combining software and hardware. Furthermore, the present invention may take the form of a computer program product implemented on one or more computer-usable storage media (including but not limited to disk storage, CD-ROM, optical storage, etc.) containing computer-usable program code.
[0046] The present invention is described with reference to the flowcharts and / or block diagrams of the methods, devices (systems) and computer program products according to the embodiments of the present invention. It should be understood that each process and / or box in the flowchart and / or block diagram, as well as the combination of the processes and / or boxes in the flowchart and / or block diagram, can be implemented by computer program instructions.
[0047] The electronic device includes a computing unit, which can perform various appropriate actions and processes according to a computer program stored in a read-only memory (ROM) or a computer program loaded from a storage unit into a random access memory (RAM). In the RAM, various programs and data required for the operation of the device can also be stored. The computing unit, ROM and RAM are connected to each other through a bus. An input / output (I / O) interface is also connected to the bus.
[0048] A computer system may include clients and servers. Clients and servers are generally remote from each other and usually interact through a communication network. The relationship of client and server is generated by computer programs running on respective computers and having a client-server relationship to each other.
[0049] A person of ordinary skill in the art can understand that all or part of the processes in the above-mentioned embodiment method can be completed by instructing the relevant hardware through a computer program, and the program can be stored in a computer-readable storage medium. When the program is executed, it can include the processes of the embodiments of the above-mentioned methods. Among them, the storage medium can be a disk, an optical disk, a read-only memory (ROM) or a random access memory (RAM), etc. In the present application, the unit described as a separate component may or may not be physically separated, and the component displayed as a unit may or may not be a physical unit, that is, it may be located in one place, or it may be distributed on multiple network units. Some or all of the units can be selected according to actual needs to achieve the purpose of the embodiment of the present invention. In addition, each functional unit in each embodiment of the present invention can be integrated in a processing unit, or each unit can exist physically separately, or two or more units can be integrated in one unit. The above-mentioned integrated unit can be implemented in the form of hardware or in the form of a software functional unit.
[0050] Although the present invention is disclosed as above, the protection scope of the present invention is not limited thereto. Those skilled in the art may make various changes and modifications without departing from the spirit and scope of the present invention, and these changes and modifications will fall within the protection scope of the present invention.
Claims
1. A method for distributing and acquiring data across modules in the same container in a microservice, characterized in that include: Declare the subject, at least define the business unique identifier, execution mode and thread pool; Declare the subscriber, declare the @ConsumerHandler annotation in the custom class and integrate the AbstractConsumerHandler class. In addition, the @ConsumerHandler annotation needs to provide the following attributes: the unique identifier of the subject service to be monitored; Scan all the classes in the APP that declare @ConsumerHandler annotations and integrate AbstractConsumerHandler, and organize them into the local subscriber pool; The event publisher publishes the event message to the topic, and the topic obtains the corresponding subscriber bean according to the configuration rules and executes the target subscriber logic.
2. A method for distributing and acquiring data across modules in the same container in a microservice as claimed in claim 1, characterized in that The subject obtains the corresponding subscriber bean according to the configuration rules and executes the target subscriber logic, including: The topic object is obtained through the configuration center of the topic; if the execution mode of the topic is synchronous, one of the multiple current topic subscribers is obtained for execution, and the execution result of the subscriber is returned to the event publisher.
3. A method for distributing and acquiring data across modules in the same container in a microservice as claimed in claim 1, characterized in that The subject obtains the corresponding subscriber bean according to the configuration rules and executes the target subscriber logic, including: The topic object is obtained through the configuration center of the topic; if the execution mode of the topic is asynchronous, all subscribers of the current topic are obtained, and the current subscriber is executed according to the thread pool configured for the topic.
4. A method for distributing and acquiring data across modules in the same container in a microservice as claimed in claim 2, characterized in that When the execution mode of the topic is synchronous, if the publisher has a transaction, the subscriber business will be automatically added to the current transaction; if the publisher does not have a transaction, it is determined whether the subscriber has implemented the transaction using transaction annotations or programmatic transactions; if the subscriber has a transaction, a new transaction execution is started.
5. A method for distributing and acquiring data across modules in the same container in a microservice as claimed in claim 3, characterized in that When the execution mode of the topic is asynchronous, if the subscriber uses transaction annotations or programmatic transactions, the downstream subscriber will start a new transaction execution after the upstream business transaction execution is completed.
6. A method for distributing and acquiring data across modules in the same container in a microservice as claimed in claim 1, characterized in that The event publisher publishes the event message to the topic, including: Use the message publisher .send to publish the message, wherein the send method contains parameters including at least the unique identifier of the subject business and the message content.
7. A method for distributing and acquiring data across modules in the same container in a microservice as claimed in claim 1, characterized in that The method of subject declaration includes: Based on the springboot project, directly define the topic list in the application configuration file; and / or, configure the topic by configuring beans.
8. A method for distributing and acquiring data across modules in the same container in a microservice as claimed in claim 1, characterized in that The custom class declares the @ConsumerHandler annotation and integrates the AbstractConsumerHandler class: The AbstractConsumerHandler abstract class contains an execute abstract method, and the custom class inherits and overrides the execute method; the parameters contained in the execute method include: the subject information object and the message content.
9. A method for distributing and acquiring data across modules in the same container in a microservice as claimed in claim 1, characterized in that The custom class declares the @ConsumerHandler annotation and integrates the AbstractConsumerHandler class: the @ConsumerHandler internally references the @Component annotation and indicates that the @ConsumerHandler can only be applied to the class.
10. A data distribution and acquisition device for the same container and across modules in a microservice, characterized in that: The device includes a processor and a memory, wherein the memory stores at least one instruction, at least one program, a code set or an instruction set, and the at least one instruction, the at least one program, the code set or the instruction set is loaded and executed by the processor to implement a method for distributing and acquiring data across modules in the same container in a microservice as described in any one of claims 1 to 9.