Method for delaying calling of Feign remote service method
Through custom configuration classes and delayed method processors, delayed call is implemented in Feign, which solves the problem of delayed call of Feign remote service interface in special scenarios, realizes non-invasive and highly flexible delayed call methods, and simplifies the development process And improves code maintainability.
Patent Information
- Application Number
- CN202410950431.8
- Authority / Receiving Office
- CN · China
- Patent Type
- Applications(China)
- Current Assignee / Owner
- Filing Date
- 2024-07-16
- Publication Date
- 2024-11-12
AI Technical Summary
Feign remote service interface is usually an instant method. When delayed calls are required in some scenarios, existing methods will change the programming order and semantics of business code, increase the difficulty of developers' understanding and may introduce flaws.
By defining the configuration class DelayFeginConfiguration and custom extension DelayFeginBuilder, create a Feign object and intercept the call information in the DelayMethodHandler, save it to the Deque queue, and execute it in the call order to achieve delayed calls.
It realizes the non-invasiveness and high flexibility of delayed calls of Feign remote service methods, simplifies business understanding, enhances code maintainability, and does not introduce new middleware.
Smart Images

Figure CN118939450A_ABST
Abstract
Description
Technical Field
[0001] The present application relates to the field of computer software technology, and in particular to a method for delaying the invocation of a Feign remote service method. Background Art
[0002] Feign remote service interfaces are usually called immediately, but in some special scenarios, they need to be called delayed without changing the order of the business code, so that the implementation is closer to normal programming ideas. For example, this business method involves transactions, and the current method calls the remote service when the transaction is not committed, which will cause the other section to be unable to query data; another scenario is used for cache refresh. Since local memory cache is used between services, notification is required to trigger cache update or invalidation after some business operations; the common practice is to call the method that needs to be delayed again outside it. In this case, the programming sequence structure of the code will change, and the corresponding code semantics and context will be separated, which will cause confusion for subsequent business maintenance. It is found that in the actual development process, it increases the difficulty for developers to understand the normal business process, and it is easy to cause other related defects. Summary of the invention
[0003] The purpose of this application is to provide a method for delaying the call of a Feign remote service method to solve the technical problems raised in the above background.
[0004] To achieve the above purpose, the present application discloses the following technical solution: a method for delaying the call of a Feign remote service method, the method comprising the following steps:
[0005] Step 1: Define and configure DelayFeginConfiguration, which is used to configure the custom extended DelayFeginBuilder;
[0006] Step 2: Specify the configuration class DelayFeginConfiguration through the configuration attribute of FeignClient on the remote service interface;
[0007] Step 3: Create a Fegin object through the extended DelayFeginBuilder;
[0008] Step 4: Create a proxy object for the remote service interface based on the extended Feign object;
[0009] Step 5: Call the remote service interface method;
[0010] Step 6: Intercept the called process service interface method in the custom extended DelayMethodHandler;
[0011] Step 7: Generate call information and save it in a FIFO Deque queue, which is used for subsequent execution in the end method of the interceptor in the order of calling.
[0012] Preferably, the step three specifically includes:
[0013] Implement the InvocationHandlerFactory.MethodHandler interface to customize the DelayMethodHandler object and implement the invoke method therein;
[0014] Inherit the Fegin.Builder object and customize the DelayFeginBuilder implementation. Set the customized implementation of DelayMethodHandler to the Fegin object for use during build time.
[0015] Beneficial effects: The method of delayed calling of the Feign remote service method of the present application is non-invasive to the business process, is configurable in terms of delayed calling, is highly flexible, and is expanded based on existing technologies without introducing new middleware. BRIEF DESCRIPTION OF THE DRAWINGS
[0016] In order to more clearly illustrate the embodiments of the present application or the technical solutions in the prior art, the drawings required for use in the embodiments or the description of the prior art will be briefly introduced below. Obviously, the drawings described below are only some embodiments of the present application. For those skilled in the art, other drawings can be obtained based on these drawings without paying any creative work.
[0017] Figure 1 A flowchart corresponding to an example of delayed calling of Feign remote service interface in the prior art;
[0018] Figure 2 A process diagram corresponding to an example of delayed call of Feign remote service interface in the prior art;
[0019] Figure 3 This is a flowchart for implementing delayed calls of some fegin remote methods through the default Fegin.Builder;
[0020] Figure 4 This is a process diagram of implementing the delayed call of some fegin remote methods through the default Fegin.Builder;
[0021] Figure 5 A flowchart of a method for delaying the call of a Feign remote service method provided in an embodiment of the present application;
[0022] Figure 6 A process diagram of a method for delaying the call of a Feign remote service method provided in an embodiment of the present application;
[0023] Figure 7 This is a schematic diagram of the implementation scheme for delaying the call of some fegin remote methods through the default Fegin.Builder;
[0024] Figure 8 A schematic diagram of the implementation scheme corresponding to the method for delaying the call of the Feign remote service method provided in an embodiment of the present application. DETAILED DESCRIPTION
[0025] The technical solutions in the embodiments of the present application are described clearly and completely below. Obviously, the described embodiments are only part of the embodiments of the present application, not all of the embodiments. Based on the embodiments in the present application, all other embodiments obtained by ordinary technicians in this field without creative work are within the scope of protection of the present application.
[0026] In this article, the term "comprising" is intended to cover non-exclusive inclusion, so that a process, method, article or device including a series of elements includes not only those elements, but also includes other elements not explicitly listed, or also includes elements inherent to such process, method, article or device. In the absence of more restrictions, the elements defined by the sentence "comprising..." do not exclude the presence of other identical elements in the process, method, article or device including the elements.
[0027] like Figure 1 and Figure 2 As shown in the figure below, for some special scenarios where Feign remote service interfaces need to be delayed in calling, the common practice is to call the method that needs to be delayed again outside. As shown in the figure below, a business operation is divided into two steps, that is, 1.1 and 1.2 perform business operations first, and then perform refresh operations 2.1 and 2.2 in another external process, where 2.1 and 2.2 are both remote service methods. This existing common practice will change the programming sequence structure of the code, and the corresponding code semantics and context will be separated, which will cause confusion for subsequent business maintenance; in addition, it is found that in the actual development process, it increases the difficulty for developers to understand the normal business process, and it is easy to cause other related defects.
[0028] Secondly, combined Figure 3 and Figure 4As shown, the existing method of implementing delayed calling of some fegin remote methods through the default Fegin.Builder includes the following steps:
[0029] Create a Fegin object through the default Fegin.Builder: the base object for subsequent creation of service interface proxy objects;
[0030] Create a proxy object of the service interface based on the Fegin object;
[0031] Execute method calls of service interfaces in business;
[0032] The method call will be intercepted by Fegin's default InvocationHandlerFactory.MethodHandler implementation, and the call information will be generated to initiate a remote call immediately.
[0033] In this regard, this embodiment discloses a method for implementing delayed calling of some fegin remote methods without changing the normal business process or intruding the code. Specifically, Figure 5 and Figure 6 As shown, the method for delaying the call of the Feign remote service method of this embodiment includes the following steps:
[0034] Step 1: Define and configure DelayFeginConfiguration, which is used to configure the custom extended DelayFeginBuilder;
[0035] Step 2: Specify the configuration class DelayFeginConfiguration through the configuration attribute of FeignClient on the remote service interface;
[0036] Step 3: Create a Fegin object through the extended DelayFeginBuilder;
[0037] Step 4: Create a proxy object for the remote service interface based on the extended Feign object;
[0038] Step 5: Call the remote service interface method;
[0039] Step 6: Intercept the called process service interface method in the custom extended DelayMethodHandler;
[0040] Step 7: Generate call information and save it in a FIFO Deque queue, which is used for subsequent execution in the end method of the interceptor in the order of calling.
[0041] like Figure 7 As shown, since the default Fegin.Builder implementation of Fegin is relatively closed, it does not support direct modification of some content, and the default related classes of fegin can only be accessed under the fegin package, and cannot be directly extended from the outside. Therefore, it is necessary to customize and extend some content, mainly to cover the build method in Fegin.Builder, and the other is to implement the InvocationHandlerFactory.MethodHandler interface by yourself. Therefore, combined with Figure 8 As shown, in this embodiment, step three specifically includes:
[0042] Implement the InvocationHandlerFactory.MethodHandler interface to customize the DelayMethodHandler object and implement the invoke method therein;
[0043] Inherit the Fegin.Builder object and customize the DelayFeginBuilder implementation. Set the customized implementation of DelayMethodHandler to the Fegin object for use during build time.
[0044] Through the above, the method of delayed calling of Feign remote service methods in this embodiment supports delayed calling of fegin remote methods in some special scenarios (that is, some business scenarios that require delayed calling proposed in the background technology), so that there is no need to pay special attention to some programming order, transactions and other related issues in ordinary business implementation; it simplifies the difficulty of developers' understanding of business implementation, and on the other hand increases the maintainability of the code.
[0045] In some feasible solutions, in the method of delayed calling of the Feign remote service method in this embodiment, an asynchronous thread can also be defined in the invoke method of DelayMethodHandler, and the call information is set to be executed after a certain time. However, this approach cannot control the specific execution node very well. For example, it can only delay for a certain time. If the subsequent execution of the business exceeds this delay time, it will cause a failure. It should be noted that the asynchronous thread solution proposed in this text is only an optional method that can solve the corresponding technical problems. Although there are some inevitable technical problems, adaptive improvements based on the inevitable technical problems should fall within the scope of protection required by this text, and this adaptive improvement will not be repeated in this text.
[0046] In summary, the method of delayed calling of the Feign remote service method in this embodiment is non-invasive to the business process, configurable in delayed calling, highly flexible, and based on the expansion of existing technologies without introducing new middleware.
[0047] Finally, it should be noted that the above is only a preferred embodiment of the present application and is not intended to limit the present application. Although the present application has been described in detail with reference to the aforementioned embodiments, it is still possible for those skilled in the art to modify the technical solutions described in the aforementioned embodiments or to make equivalent substitutions for some of the technical features therein. Any modifications, equivalent substitutions, improvements, etc. made within the spirit and principles of the present application shall be included in the protection scope of the present application.
Claims
1. A method for delaying the call of a Feign remote service method. It is characterized in that The method comprises the following steps: Step 1: Define and configure DelayFeginConfiguration, which is used to configure the custom extended DelayFeginBuilder; Step 2: Specify the configuration class DelayFeginConfiguration through the configuration attribute of FeignClient on the remote service interface; Step 3: Create a Fegin object through the extended DelayFeginBuilder; Step 4: Create a proxy object for the remote service interface based on the extended Feign object; Step 5: Call the remote service interface method; Step 6: Intercept the called process service interface method in the custom extended DelayMethodHandler; Step 7: Generate call information and save it in a FIFO Deque queue, which is used for subsequent execution in the end method of the interceptor in the order of calling.
2. The method for delaying the invocation of a Feign remote service method according to claim 1, It is characterized in that The step three specifically includes: Implement the InvocationHandlerFactory.MethodHandler interface to customize the DelayMethodHandler object and implement the invoke method therein; Inherit the Fegin.Builder object and customize the DelayFeginBuilder implementation. Set the customized implementation of DelayMethodHandler to the Fegin object for use during build time.
