A generalized asynchronous calling method and device based on Dubbo
By customizing callback Filter in the Dubbo framework and setting a unified callback processing method, the problem that the Dubbo framework cannot implement asynchronous generalized calls is solved, asynchronous calls are implemented, and the functions of generalized calls are expanded.
Patent Information
- Application Number
- CN202111386402.6
- Authority / Receiving Office
- CN · China
- Patent Type
- Patents(China)
- Current Assignee / Owner
- Filing Date
- 2021-11-22
- Publication Date
- 2025-02-14
- Estimated Expiration
- 2041-11-22
AI Technical Summary
The Dubbo framework currently only supports synchronous generalization calls, and cannot implement asynchronous generalization calls, and cannot meet the needs of a large number of service consumers that require asynchronous calls.
Through the Dubbo SPI mechanism, customize the callback Filter and set a unified callback processing method in the Filter implementation to avoid using Dubbo's default generalized addressing callbacks, thereby implementing asynchronous calls.
It implements direct callback processing method set in the custom callback Filter when service callback is called directly, which solves the problem that the Dubbo framework cannot implement asynchronous generalized calls and expands the functional implementation of generalized calls.
Smart Images

Figure CN114020371B_ABST
Abstract
Description
Technical Field
[0001] The present invention relates to the technical field of service calling, and in particular to a generalized asynchronous calling method and device based on Dubbo. Background Art
[0002] The Dubbo framework itself supports generalized calls, but generalized calls are mainly aimed at synchronous calling methods. For a large number of service consumers that require asynchronous calls, generalized calls are a more scenario-appropriate implementation. However, the current Dubbo service only supports synchronous generalized calls and cannot implement asynchronous generalized calls. Summary of the invention
[0003] In view of the technical defects in the prior art, the purpose of the embodiments of the present invention is to provide a generalized asynchronous calling method and device based on Dubbo that can realize asynchronous generalized calling.
[0004] To achieve the above objectives, in a first aspect, an embodiment of the present invention provides a generalized asynchronous calling method based on Dubbo, which is applied to the Dubbo framework and is also based on the Dubbo SPI mechanism. The method includes:
[0005] Customize callback Filter through service consumers;
[0006] Configure the callback Filter and add plain text files and content in the Resources directory;
[0007] A unified callback processing method is set in the implementation of the Filter to avoid using Dubbo's default generalized addressing callback;
[0008] Perform call initialization so that when generalized calls are made, the serial number is set to the global context;
[0009] The service provider is used to execute the service, and when the service is called back, a direct call is made according to the callback processing method set in the Filter to implement an asynchronous call.
[0010] Preferably, the custom callback Filter includes inheriting the com.alibaba.dubbo.rpc.Filter interface;
[0011] The added plain text file is META-INF / dubbo / com.alibaba.dubbo.rpc.Filter.
[0012] Preferably, the unified callback processing method is specifically as follows:
[0013] Configure the Filter and use the annotation @Activate(group=Constants.CONSUMER, value=“rpcOnReturn”).
[0014] Preferably, the calling initialization specifically includes:
[0015] Get the GenericService for the generalized call;
[0016] Set the GenericService asynchronous flag to true and set the callback Filter to a custom Filter;
[0017] A unified serial number is set, and the serial number is set in the global context, so that the request callback carries the corresponding serial number ID.
[0018] In a second aspect, an embodiment of the present invention provides a generalized asynchronous calling device based on Dubbo, which is applied to the Dubbo framework and based on the Dubbo SPI mechanism. The calling device includes:
[0019] Custom modules for:
[0020] Customize callback Filter through service consumers;
[0021] Configure the callback Filter and add plain text files and content in the Resources directory;
[0022] A unified callback processing method is set in the implementation of the Filter to avoid using Dubbo's default generalized addressing callback;
[0023] The initialization module is used to perform call initialization so that the serial number is set to the global context when a generalized call is made;
[0024] The asynchronous call module is used to:
[0025] The service provider is used to execute the service, and when the service is called back, a direct call is made according to the callback processing method set in the Filter to implement an asynchronous call.
[0026] Preferably, the custom callback Filter includes inheriting the com.alibaba.dubbo.rpc.Filter interface;
[0027] The added plain text file is META-INF / dubbo / com.alibaba.dubbo.rpc.Filter.
[0028] Preferably, the unified callback processing method is specifically as follows:
[0029] Configure the Filter and use the annotation @Activate(group=Constants.CONSUMER, value=“rpcOnReturn”).
[0030] Preferably, the calling initialization specifically includes:
[0031] Get the GenericService for the generalized call;
[0032] Set the GenericService asynchronous flag to true and set the callback Filter to a custom Filter;
[0033] A unified serial number is set, and the serial number is set in the global context, so that the request callback carries the corresponding serial number ID.
[0034] In the implementation of the embodiment of the present invention, the Dubbo SPI mechanism is used to customize the callback Filter and initialization, and to set a unified callback processing method to avoid using the default generalized addressing callback of Dubbo; thereby, when the service callback is made, a direct call is made according to the callback processing method set in the Filter to implement an asynchronous call. BRIEF DESCRIPTION OF THE DRAWINGS
[0035] In order to more clearly illustrate the specific implementation of the present invention or the technical solution in the prior art, the drawings required for use in the specific implementation or the description of the prior art are briefly introduced below.
[0036] Figure 1 It is a flowchart of a generalized asynchronous calling method based on Dubbo provided by an embodiment of the present invention;
[0037] Figure 2 It is a structural diagram of a generalized asynchronous calling device based on Dubbo provided by the present invention. DETAILED DESCRIPTION
[0038] The following will be combined with the drawings in the embodiments of the present invention to clearly and completely describe the technical solutions in the embodiments of the present invention. Obviously, the described embodiments are 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 are within the scope of protection of the present invention.
[0039] In order to better understand the embodiments of the present invention, the related technical contents involved are first described as follows:
[0040] Dubbo is a high-performance, lightweight open source Java RPC framework that provides three core capabilities: interface-oriented remote method invocation, intelligent fault tolerance and load balancing, and automatic service registration and discovery;
[0041] Filter: filter;
[0042] Resources: resources;
[0043] GenericService: generalized call; the advantage of using GenericService to call the interface is that there is no need to locally rely on the interface reference of the service provider. You only need to know the full class name, method name, and parameter list of the interface to call the Dubbo method and get the return value.
[0044] Please refer to Figure 1 , an embodiment of the present invention provides a generalized asynchronous calling method based on Dubbo, which is applied to the Dubbo framework and based on the Dubbo SPI mechanism. The method includes:
[0045] S101, customize the callback Filter through the service consumer.
[0046] Specifically, the custom callback Filter includes inheriting the com.alibaba.dubbo.rpc.Filter interface.
[0047] S102, configure the callback Filter and add plain text files and content in the Resources directory.
[0048] Specifically, the added plain text file is as follows:
[0049] META-INF / dubbo / com.alibaba.dubbo.rpc.Filter;
[0050] The content is written as xxx=xxx.xxx.xxxFilter.
[0051] S103, setting a unified callback processing method in the implementation of the Filter to avoid using the default generalized addressing callback of Dubbo.
[0052] Specifically, the unified callback processing method is as follows:
[0053] Configure the Filter and use the annotation @Activate(group=Constants.CONSUMER, value=“rpcOnReturn”).
[0054] In this field, the callback processing method can also be understood as a callback processing method. Once the corresponding value is obtained, the Filter will take effect after adding the Activate annotation.
[0055] S104, performing call initialization so that when a generalized call is made, the serial number is set in the global context.
[0056] Specifically, the calling initialization specifically includes:
[0057] Get the GenericService for the generalized call;
[0058] Set the GenericService asynchronous flag to true and set the callback Filter to a custom Filter;
[0059] A unified serial number is set, and the serial number is set in the global context, so that the request callback carries the corresponding serial number ID.
[0060] S105, using the service provider to execute the service, and when the service is called back, directly calling according to the callback processing method set in the Filter to achieve asynchronous calling.
[0061] Specifically, after initiating the generalized call, the service provider executes the service, performs a service callback, and enters the custom filter. Then the custom filter does not perform callback addressing, but directly calls the specified location according to the unified callback processing method set, that is, the "rpcOnReturn" mentioned above, to complete the generalized asynchronous call;
[0062] At the same time, the results after the callback are uniformly processed using external business processing to implement specific business.
[0063] The entire process does not rely on third-party components, and completes generalized asynchronous calls based on the services of the Dubbo framework itself, expanding the functional implementation of generalized calls.
[0064] From the above description, it can be concluded that the method provided in the embodiment of the present invention is implemented, through the Dubbo SPI mechanism, a customized callback Filter and initialization are used, and a unified callback processing method is set to avoid using the default generalized addressing callback of Dubbo; thereby, when a service callback is made, a direct call is made according to the callback processing method set in the Filter to implement an asynchronous call.
[0065] Based on the same inventive concept, the embodiment of the present invention also provides a generalized asynchronous calling device based on Dubbo, which is applied to the Dubbo framework and based on the Dubbo SPI mechanism. Figure 2As shown, the calling device includes:
[0066] Custom modules for:
[0067] Customize callback Filter through service consumers;
[0068] Configure the callback Filter and add plain text files and content in the Resources directory;
[0069] A unified callback processing method is set in the implementation of the Filter to avoid using Dubbo's default generalized addressing callback.
[0070] Specifically, the custom callback Filter includes inheriting the com.alibaba.dubbo.rpc.Filter interface;
[0071] The added plain text file is META-INF / dubbo / com.alibaba.dubbo.rpc.Filter;
[0072] The specific method of setting a unified callback processing method is as follows:
[0073] Configure the Filter and use the annotation @Activate(group=Constants.CONSUMER, value=“rpcOnReturn”).
[0074] The initialization module is used to perform call initialization so that the serial number is set to the global context when a generalized call is made; the call initialization is still performed through the service consumer.
[0075] Specifically, the calling initialization specifically includes:
[0076] Get the GenericService for the generic call;
[0077] Set the GenericService asynchronous flag to true and set the callback Filter to a custom Filter;
[0078] A unified serial number is set, and the serial number is set in the global context, so that the request callback carries the corresponding serial number ID.
[0079] The asynchronous call module is used to:
[0080] The service provider is used to execute the service, and when the service is called back, a direct call is made according to the callback processing method set in the Filter to implement an asynchronous call.
[0081] It should be noted that, for a more specific working process of the generalized asynchronous calling device, please refer to the aforementioned generalized asynchronous calling method embodiment section, which will not be repeated here.
[0082] The entire solution does not rely on third-party components, but completes generalized asynchronous calls based on the services of the Dubbo framework itself, expanding the functional implementation of generalized calls.
[0083] Those of ordinary skill in the art will appreciate that the modules of each example described in conjunction with the embodiments disclosed herein can be implemented in electronic hardware, computer software, or a combination of the two. In order to clearly illustrate the interchangeability of hardware and software, the composition and steps of each example have been generally described in the above description according to function. Whether these functions are performed in hardware or software depends on the specific application and design constraints of the technical solution. Professional and technical personnel can use different methods to implement the described functions for each specific application, but such implementation should not be considered to be beyond the scope of the present invention.
[0084] In the several embodiments provided in this application, it should be understood that the disclosed modules and methods can be implemented in other ways. For example, the device embodiments described above are only schematic, for example, the division of the modules is only a logical function division, and there may be other division methods in actual implementation, such as multiple modules or components can be combined or integrated into another system, or some features can be ignored or not executed.
[0085] The above is only a specific embodiment of the present invention, but the protection scope of the present invention is not limited thereto. Any technician familiar with the technical field can easily think of various equivalent modifications or replacements within the technical scope disclosed by the present invention, and these modifications or replacements should be included in the protection scope of the present invention. Therefore, the protection scope of the present invention shall be based on the protection scope of the claims.
Claims
1. A generalized asynchronous calling method based on Dubbo, applied to the Dubbo framework, characterized in that: Also based on the DubboSPI mechanism, the method includes: Customize callback Filter through service consumers; Configure the callback Filter and add plain text files and content in the Resources directory; A unified callback processing method is set in the implementation of the Filter to avoid using Dubbo's default generalized addressing callback; Perform call initialization so that when generalized calls are made, the serial number is set to the global context; Utilize the service provider to execute the service, and when the service is called back, directly call it according to the callback processing method set in the Filter to realize asynchronous calling; The custom callback Filter includes inheriting the com.alibaba.dubbo.rpc.Filter interface; The added plain text file is META-INF / dubbo / com.alibaba.dubbo.rpc.Filter; The specific method of setting a unified callback processing method is as follows: Configure Filter and use the annotation @Activate(group=Constants.CONSUMER,value="rpcOnReturn"); After initiating the generalized call, the service provider executes the service, performs a service callback, and enters the custom filter. Then the custom filter does not perform callback addressing, but directly calls the specified location according to the unified callback processing method set to complete the generalized asynchronous call.
2. A generalized asynchronous calling method based on Dubbo as claimed in claim 1, characterized in that: The calling initialization specifically includes: Get the GenericService for the generic call; Set the GenericService asynchronous flag to true and set the callback Filter to a custom Filter; A unified serial number is set, and the serial number is set in the global context, so that the request callback carries the corresponding serial number ID.
3. A generalized asynchronous calling device based on Dubbo, characterized in that: Applied to the Dubbo framework and based on the DubboSPI mechanism, the calling device includes: Custom modules for: Customize callback Filter through service consumers; Configure the callback Filter and add plain text files and content in the Resources directory; A unified callback processing method is set in the implementation of the Filter to avoid using Dubbo's default generalized addressing callback; The initialization module is used to perform call initialization so that the serial number is set to the global context when a generalized call is made; The asynchronous call module is used to: Utilize the service provider to execute the service, and when the service is called back, directly call it according to the callback processing method set in the Filter to realize asynchronous calling; The custom callback Filter includes inheriting the com.alibaba.dubbo.rpc.Filter interface; The added plain text file is META-INF / dubbo / com.alibaba.dubbo.rpc.Filter; The specific method of setting a unified callback processing method is as follows: Configure Filter and use the annotation @Activate(group=Constants.CONSUMER,value="rpcOnReturn"); After initiating the generalized call, the service provider executes the service, performs a service callback, and enters the custom filter. Then the custom filter does not perform callback addressing, but directly calls the specified location according to the unified callback processing method set to complete the generalized asynchronous call.
4. A generalized asynchronous calling device based on Dubbo as claimed in claim 3, characterized in that: The calling initialization specifically includes: Get the GenericService for the generic call; Set the GenericService asynchronous flag to true and set the callback Filter to a custom Filter; A unified serial number is set, and the serial number is set in the global context, so that the request callback carries the corresponding serial number ID.