Microservice calling method, device, computer equipment and storage medium
By using target annotations and target sections in microservice calls and using underlying tool classes for processing, the problem of redundant code in microservice calls is solved, and a more efficient call process is achieved.
Patent Information
- Application Number
- CN202211559228.5
- Authority / Receiving Office
- CN · China
- Patent Type
- Patents(China)
- Current Assignee / Owner
- Filing Date
- 2022-12-06
- Publication Date
- 2025-08-26
- Estimated Expiration
- 2042-12-06
AI Technical Summary
There is a lot of redundant code in existing microservice call processing steps, resulting in inefficient processing.
By applying target annotations and target sections in the target interface, intercepting call requests, obtaining the running environment, and using preset underlying tool classes to perform service call processing, reducing redundant code and improving processing efficiency.
Reduces redundant code in business code and improves the efficiency of microservice call processing.
Smart Images

Figure CN115794437B_ABST
Abstract
Description
Technical Field
[0001] The present application relates to the field of big data technology, and in particular to a method, apparatus, computer equipment, and storage medium for invoking microservices. Background Art
[0002] In front-end development, microservices are the most popular software architecture. As business scale grows, the performance limitations of monolithic services are severely hampering the development of the software industry. Consequently, microservices have emerged and are being applied across various industries.
[0003] As financial companies grow, they often implement multiple concurrent invocation methods across their microservices. For example, ESG 1.0, ESG 2.0, and NACOS (architecture-encapsulated) often coexist. It's also common for a single service to call different external services using all three methods. Existing microservice invocation processing typically involves assembling parameters, setting header information, invoking specific tools, parsing responses, and performing preliminary checks to determine if there are any anomalies. This process increases the size of business code and results in a significant amount of redundant code. Furthermore, the complex microservice invocation process reduces efficiency. Summary of the Invention
[0004] The purpose of the embodiments of the present application is to propose a microservice calling method, apparatus, computer equipment and storage medium to solve the technical problem that the existing microservice calling processing steps contain a large amount of redundant code, which reduces the processing efficiency of the microservice calling.
[0005] In order to solve the above technical problems, the present application provides a method for calling a microservice, which adopts the following technical solution:
[0006] Determine whether a call request corresponding to a target interface of a microservice is received, triggered by a user through a client; wherein the call request carries the service name of the microservice and the interface name of the target interface, and a target annotation is pre-defined in a proxy method corresponding to the target interface;
[0007] If so, intercept the call request based on the target aspect corresponding to the target interface and obtain the current operating environment;
[0008] If the operating environment does not belong to the preset unit test environment, executing the proxy method corresponding to the target interface to obtain interface information and service information corresponding to the target interface;
[0009] Parse the target annotation to obtain the corresponding request type and calling method, and construct the target input parameter based on the interface information, the service information and the request type;
[0010] Based on the target input parameter and the calling method, a preset underlying tool class is used to perform a service call process corresponding to the microservice, and a target output parameter corresponding to the target input parameter is obtained from the target interface;
[0011] The target output parameter is converted to obtain corresponding feedback data, and the feedback data is returned to the client.
[0012] Furthermore, the step of intercepting the call request based on the target aspect corresponding to the target interface and obtaining the current operating environment specifically includes:
[0013] Obtaining the target annotation predefined in the execution method in the target interface;
[0014] Acquire the target section corresponding to the target annotation;
[0015] intercepting the call request based on the target aspect;
[0016] In the target section, the current operating environment is obtained.
[0017] Furthermore, the step of performing service call processing corresponding to the microservice using a preset underlying tool class based on the target input parameter and the calling method, and obtaining a target output parameter corresponding to the target input parameter returned by the target interface, specifically includes:
[0018] Calling the underlying tool class, assembling the calling method with the interface information, the service information, and the request type in the target input parameter to generate a corresponding access request;
[0019] Sending the access request to the microservice, so that the microservice processes the access request and generates corresponding return data, and uses the return data as the target output parameter;
[0020] Receive the target output parameter returned by the microservice through the target interface.
[0021] Furthermore, the step of converting the target output parameter to obtain corresponding feedback data and returning the feedback data to the client specifically includes:
[0022] Call the preset information configuration platform;
[0023] Acquire a predefined return value type corresponding to the target interface from the information configuration platform;
[0024] Convert the target output parameter based on the return value type to obtain corresponding target object data, and use the target object data as the feedback data;
[0025] The feedback data is returned to the client.
[0026] Furthermore, the step of converting the target output parameter based on the return value type to obtain corresponding target object data specifically includes:
[0027] Determine a conversion method corresponding to the return value type;
[0028] Calling a conversion tool corresponding to the conversion method;
[0029] Performing conversion processing on the target output parameter based on the conversion tool to obtain converted parameter data;
[0030] The converted parameter data is used as the target object data.
[0031] Furthermore, after the step of intercepting the call request based on the target aspect corresponding to the target interface and obtaining the current operating environment, the method further includes:
[0032] If the operating environment belongs to the unit test environment, obtaining a target method corresponding to the target interface;
[0033] Executing the target method to obtain corresponding simulation data;
[0034] The simulation data is returned to the client.
[0035] Furthermore, before the step of obtaining the target method corresponding to the target interface, the method further includes:
[0036] Get the preset simulation code;
[0037] Writing the simulation code into a preset method template to obtain a processed method template;
[0038] The processed method template is used as the target method.
[0039] In order to solve the above technical problems, the embodiment of the present application further provides a microservice calling device, which adopts the following technical solution:
[0040] A judgment module is used to judge whether a call request corresponding to a target interface of a microservice is received, triggered by a user through a client; wherein the call request carries the service name of the microservice and the interface name of the target interface, and a target annotation is pre-defined in the proxy method corresponding to the target interface;
[0041] An interception module, configured to, if yes, intercept the call request based on a target aspect corresponding to the target interface and obtain the current operating environment;
[0042] A first acquisition module is configured to execute the proxy method corresponding to the target interface and acquire interface information and service information corresponding to the target interface if the operating environment does not belong to the preset unit test environment;
[0043] A parsing module, configured to parse the target annotation to obtain the corresponding request type and calling method, and construct a target input parameter based on the interface information, the service information and the request type;
[0044] A first processing module is configured to perform service call processing corresponding to the microservice using a preset underlying tool class based on the target input parameter and the calling method, and obtain a target output parameter corresponding to the target input parameter returned by the target interface;
[0045] The second processing module is used to convert the target output parameter to obtain corresponding feedback data, and return the feedback data to the client.
[0046] In order to solve the above technical problems, the embodiment of the present application further provides a computer device, which adopts the following technical solution:
[0047] Determine whether a call request corresponding to a target interface of a microservice is received, triggered by a user through a client; wherein the call request carries the service name of the microservice and the interface name of the target interface, and a target annotation is pre-defined in a proxy method corresponding to the target interface;
[0048] If so, intercept the call request based on the target aspect corresponding to the target interface and obtain the current operating environment;
[0049] If the operating environment does not belong to the preset unit test environment, executing the proxy method corresponding to the target interface to obtain interface information and service information corresponding to the target interface;
[0050] Parse the target annotation to obtain the corresponding request type and calling method, and construct the target input parameter based on the interface information, the service information and the request type;
[0051] Based on the target input parameter and the calling method, a preset underlying tool class is used to perform a service call process corresponding to the microservice, and a target output parameter corresponding to the target input parameter is obtained from the target interface;
[0052] The target output parameter is converted to obtain corresponding feedback data, and the feedback data is returned to the client.
[0053] In order to solve the above technical problems, the embodiment of the present application further provides a computer-readable storage medium, which adopts the following technical solution:
[0054] Determine whether a call request corresponding to a target interface of a microservice is received, triggered by a user through a client; wherein the call request carries the service name of the microservice and the interface name of the target interface, and a target annotation is pre-defined in a proxy method corresponding to the target interface;
[0055] If so, intercept the call request based on the target aspect corresponding to the target interface and obtain the current operating environment;
[0056] If the operating environment does not belong to the preset unit test environment, executing the proxy method corresponding to the target interface to obtain interface information and service information corresponding to the target interface;
[0057] Parse the target annotation to obtain the corresponding request type and calling method, and construct the target input parameter based on the interface information, the service information and the request type;
[0058] Based on the target input parameter and the calling method, a preset underlying tool class is used to perform a service call process corresponding to the microservice, and a target output parameter corresponding to the target input parameter is obtained from the target interface;
[0059] The target output parameter is converted to obtain corresponding feedback data, and the feedback data is returned to the client.
[0060] Compared with the prior art, the embodiments of the present application have the following beneficial effects:
[0061] After receiving a call request corresponding to the target interface of a microservice triggered by a user through a client, the embodiment of the present application intercepts the call request based on the target aspect corresponding to the target interface and obtains the current operating environment. If the operating environment does not belong to the preset unit test environment, the proxy method corresponding to the target interface is executed to obtain the interface information and service information corresponding to the target interface. Then, the target annotation is parsed to obtain the corresponding request type and calling method, and the target input parameter is constructed based on the interface information, the service information and the request type. Subsequently, based on the target input parameter and the calling method, the preset underlying tool class is used to perform the service call processing corresponding to the microservice, and the target output parameter corresponding to the target input parameter returned by the target interface is obtained. Finally, the target output parameter is converted to obtain the corresponding feedback data, and the feedback data is returned to the client. The embodiment of the present application reduces the generation of a large amount of redundant code in the business code by applying the target annotation to the target interface corresponding to the microservice to perform the calling process of the microservice, and the service call processing corresponding to the microservice can be quickly performed through the use of the underlying tool class, thereby effectively improving the processing efficiency of the microservice call. BRIEF DESCRIPTION OF THE DRAWINGS
[0062] In order to more clearly illustrate the solutions in this application, a brief introduction will be given below to the drawings required for use in the description of the embodiments of this application. Obviously, the drawings described below are some embodiments of this application. For ordinary technicians in this field, other drawings can be obtained based on these drawings without any creative work.
[0063] Figure 1 is an exemplary system architecture diagram to which the present application may be applied;
[0064] Figure 2 A flowchart of an embodiment of a method for calling a microservice according to the present application;
[0065] Figure 3 This is a structural diagram of an embodiment of a microservice invocation device according to the present application;
[0066] Figure 4 It is a structural diagram of an embodiment of a computer device according to the present application. DETAILED DESCRIPTION
[0067] Unless otherwise defined, all technical and scientific terms used herein have the same meanings as commonly understood by those skilled in the art to which this application belongs. The terms used in the specification of the application are for the purpose of describing specific embodiments only and are not intended to limit this application. The terms "including" and "having" and any variations thereof in the specification and claims of this application and the above-mentioned drawings are intended to cover non-exclusive inclusions. The terms "first", "second", etc. in the specification and claims of this application or the above-mentioned drawings are used to distinguish different objects, not to describe a specific order.
[0068] References herein to "embodiments" mean that a particular feature, structure, or characteristic described in connection with the embodiments may be included in at least one embodiment of the present application. The appearance of this phrase in various places in the specification does not necessarily refer to the same embodiment, nor does it constitute an independent or alternative embodiment that is mutually exclusive of other embodiments. It is understood, both explicitly and implicitly, by those skilled in the art that the embodiments described herein may be combined with other embodiments.
[0069] In order to enable those skilled in the art to better understand the solution of the present application, the technical solution in the embodiments of the present application will be clearly and completely described below in conjunction with the accompanying drawings.
[0070] like Figure 1 As shown, system architecture 100 may include terminal devices 101, 102, 103, a network 104, and a server 105. Network 104 is a medium for providing communication links between terminal devices 101, 102, 103 and server 105. Network 104 may include various connection types, such as wired or wireless communication links or fiber optic cables.
[0071] Users can use terminal devices 101, 102, and 103 to interact with server 105 via network 104 to receive or send messages, etc. Various communication client applications can be installed on terminal devices 101, 102, and 103, such as web browser applications, shopping applications, search applications, instant messaging tools, email clients, social platform software, etc.
[0072] The terminal devices 101, 102, and 103 can be various electronic devices with display screens and supporting web browsing, including but not limited to smart phones, tablet computers, e-book readers, MP3 players (Moving Picture Experts Group Audio Layer III), MP4 players (Moving Picture Experts Group Audio Layer IV), laptop computers, desktop computers, etc.
[0073] The server 105 may be a server that provides various services, such as a background server that provides support for web pages displayed on the terminal devices 101 , 102 , and 103 .
[0074] It should be noted that the microservice calling method provided in the embodiments of the present application is generally executed by a server / terminal device, and accordingly, the microservice calling device is generally set in the server / terminal device.
[0075] It should be understood that Figure 1 The number of terminal devices, networks and servers in the embodiment is merely illustrative. Any number of terminal devices, networks and servers may be provided as required.
[0076] Continue to refer Figure 2 , shows a flow chart of an embodiment of a method for calling a microservice according to the present application. The method for calling a microservice includes the following steps:
[0077] Step S201: Determine whether a call request corresponding to a target interface of a microservice is received, triggered by a user through a client.
[0078] In this embodiment, the calling method of the microservice runs on the electronic device (for example Figure 1The server / terminal device shown in the figure) can obtain the call request through a wired connection or a wireless connection. It should be noted that the above-mentioned wireless connection method may include but is not limited to 3G / 4G / 5G connection, Wi-Fi connection, Bluetooth connection, Wi MAX connection, Zigbee connection, UWB (ultrasound) connection, and other wireless connection methods currently known or to be developed in the future. The interface may be an API (Application Programming Interface). The API is a collection of definitions, programs and protocols, through which mutual communication between computer software can be achieved. The call request carries the service name of the microservice and the interface name of the target interface, and a target annotation is pre-defined in the proxy method corresponding to the target interface. The above-mentioned execution method refers to the core method in the target interface corresponding to the call request of the above-mentioned microservice, which is specifically used to respond to the call request of the microservice and has the function of generating interface feedback data corresponding to the call request of the microservice. In addition, when developers develop interfaces that rely on external microservices, they only need to refer to the interface documentation of the external microservice to define the input and output parameter models, determine the service to which the external microservice belongs and the calling method. You can define an interface for this external microservice, and the method content can return null or mock data for use with JUNIT unit testing.
[0079] Step S202: If yes, intercept the call request based on the target aspect corresponding to the target interface and obtain the current operating environment.
[0080] In this embodiment, the target aspect is a target aspect associated with the target annotation of the target interface. The specific implementation process of intercepting the call request based on the target aspect corresponding to the target interface and obtaining the current operating environment will be further described in detail in subsequent specific embodiments of this application and will not be elaborated on here. Among them, the target annotation is a pre-built annotation based on the actual microservice call requirements, and the annotation parameters can be configured by using a preset information configuration platform. Specifically, the microservice defines a Java class in its respective project and adds a Spring annotation to the class, making this class a class that can be programmed with the Spring aspect. The target annotation includes the @RemoteServie annotation and the @RemoteHandler annotation. The serviceName in the @RemoteServie annotation corresponds to the service name, consumption code, or app ID of the external service in the configuration center; the url in the @RemoteHandler annotation corresponds to the interface address of the external service in the configuration center, supporting dynamic updates of the configuration center. The default calling mode is NACOS, which can be manually configured as ESG1.0 or ESG2.0. The default request type is POST with BODY request (POST request, parameters are placed in BODY), which can be manually configured as GET with BODY request (GET request, parameters are placed in BODY), GET parameters concatenated with URL (GET request, parameters concatenated after URL), or parameters as part of URL (GET request, parameters as part of URL).
[0081] The sample code is as follows:
[0082] @S l f4j
[0083] @RemoteSe rv ie(ser vi ceName="remote.nps.se rvi ceName")
[0084] @Component
[0085] pub liccl ass NPSRemoteServ ie{
[0086] @RemoteHand ler(uri="remote.nps.ur i.pageOccpat i ons")
[0087] pub lic NPSPageOccpat i onsResponse pageOccpat i ons( NPSPageOccpati onsRequest pageOccpat i ons npsPageOccpat i onsRequest){r etu rn nu ll;}
[0088] }
[0089] Furthermore, @RemoteHandler can also be used in other combinations, as shown below:
[0090] @RemoteHand l er(ur i = "r emote.se rv i ce1.ur i.ap i 1") / / Default usage;
[0091] @RemoteHand l er(ur i="re emote.ser vi ce2.ur i.ap i 2",ca ll Mode=Ca ll ModeEnum.ESG_1,reqType=RequestTypeEnum.POST) / / post request of ESG1.0;
[0092] @RemoteHand l er(ur i="remote.ser vi ce3.ur i.ap i 3", ca ll Mo de = Ca ll ModeEnum.ESG_2, reqType = RequestTypeEnum.GET_PARAM, paramF iel ds = {"appCode", "um"}) / / Get request for ESG2.0.
[0093] Step S203: If the operating environment does not belong to the preset unit test environment, execute the proxy method corresponding to the target interface to obtain interface information and service information corresponding to the target interface.
[0094] In this embodiment, the preset unit test environment may refer to the JUNIT unit test environment. If the running environment does not belong to the unit test environment, it indicates that the current running environment belongs to the microservice call environment. In this case, the proxy method corresponding to the target interface will be executed to parse the interface information and service information corresponding to the target interface from the annotation. The interface information may include the interface address of the target interface, and the service information may include the service type of the microservice.
[0095] Step S204: parse the target annotation to obtain the corresponding request type and calling method, and construct the target input parameter based on the interface information, the service information and the request type.
[0096] In this embodiment, the target annotation is parsed to obtain the corresponding request type and calling method. The request type may include the type of the request method, for example, a GET request, a POST request, etc. The calling method may include a NACOS calling method, an ESG1.0 calling method, and an ESG2.0 calling method. The target input parameters are used to call the microservice and include the interface information, the service information, and the request type. Furthermore, if recording the input parameters is required, a parameter log corresponding to the target input parameters is saved.
[0097] Step S205: Based on the target input parameter and the calling method, a preset underlying tool class is used to perform service call processing corresponding to the microservice, and a target output parameter corresponding to the target input parameter returned by the target interface is obtained.
[0098] In this embodiment, the above-mentioned target input and the calling method are used to perform the service call processing corresponding to the microservice using the preset underlying tool class to obtain the target output parameter corresponding to the target input parameter returned by the target interface. This application will further describe this in detail in the subsequent specific embodiments, so it will not be elaborated on here. Among them, if it is necessary to record the output parameter demand, a parameter output log corresponding to the target output parameter is saved.
[0099] Step S206: convert the target output parameter to obtain corresponding feedback data, and return the feedback data to the client.
[0100] In this embodiment, the above-mentioned specific implementation process of converting the target output parameters to obtain corresponding feedback data and returning the feedback data to the client will be further described in detail in subsequent specific embodiments of this application and will not be elaborated on here.
[0101] After receiving a call request corresponding to the target interface of a microservice triggered by a user through a client, the present application intercepts the call request based on the target aspect corresponding to the target interface and obtains the current operating environment. If the operating environment does not belong to the preset unit test environment, the proxy method corresponding to the target interface is executed to obtain the interface information and service information corresponding to the target interface. Then, the target annotation is parsed to obtain the corresponding request type and calling method, and the target input parameter is constructed based on the interface information, the service information and the request type. Subsequently, based on the target input parameter and the calling method, the preset underlying tool class is used to perform the service call processing corresponding to the microservice, and the target output parameter corresponding to the target input parameter returned by the target interface is obtained. Finally, the target output parameter is converted and processed to obtain the corresponding feedback data, and the feedback data is returned to the client. The present application reduces the generation of a large amount of redundant code in the business code by applying the target annotation to the target interface corresponding to the microservice to perform the calling process of the microservice, and the service call processing corresponding to the microservice can be quickly performed through the use of the underlying tool class, thereby effectively improving the processing efficiency of the microservice call.
[0102] In some optional implementations, step S202 includes the following steps:
[0103] The target annotation pre-defined in the execution method in the target interface is obtained.
[0104] In this embodiment, the above-mentioned execution method refers to the core method in the target interface corresponding to the call request of the above-mentioned microservice, which is specifically used to respond to the call request of the microservice and has the function of generating interface feedback data corresponding to the call request of the microservice.
[0105] Obtain the target section corresponding to the target annotation.
[0106] In this embodiment, the target aspect refers to the code snippet that cuts into a specified class and method. The classes and methods cut into are called entry points. The target aspect, also known as the programming aspect, refers to the programming concept of dynamically cutting code into a specified method or location in a class at runtime.
[0107] The call request is intercepted based on the target aspect.
[0108] In this embodiment, the call request can be intercepted in the target aspect. After obtaining the target aspect, the call request sent to the microservice can be intercepted by the target aspect. In this way, the target aspect isolates the various parts of the service access, thereby reducing the coupling between the various parts of the business logic, improving the reusability of the program, and thus improving development efficiency. A preset interception method can be defined in the target annotated java class, and the interception method can be woven before the cut point to intercept the call request. The above-mentioned interception method may include a doBefore method.
[0109] In the target section, the current operating environment is obtained.
[0110] In this embodiment, corresponding judgment or logic processing may also be performed in the target aspect, such as obtaining the current operating environment, which may include a unit test environment or a non-unit test environment, that is, a normal service access environment.
[0111] This application obtains the target annotation pre-defined in the execution method in the target interface; then obtains the target aspect corresponding to the target annotation; and then intercepts the call request based on the target aspect, and obtains the current running environment in the target aspect. This application intercepts the call request sent to the microservice by using the target aspect corresponding to the target annotation of the target interface, so as to isolate the various parts of the service access through the target aspect, thereby reducing the coupling between the various parts of the business logic, improving the reusability of the program, and helping to improve development efficiency.
[0112] In some optional implementations of this embodiment, step S205 includes the following steps:
[0113] The underlying tool class is called, and the calling method is assembled with the interface information, the service information, and the request type in the target input parameter to generate a corresponding access request.
[0114] In this embodiment, the calling method can be assembled with the interface information, the service information and the request type in the target input according to the above-mentioned underlying tool class to be used, so as to generate an access request for accessing the above-mentioned microservice. Among them, the above-mentioned underlying tool class is specifically an interface implementation class corresponding to the interface class used to access the microservice. Specifically, since the interface class and the interface implementation class of a microservice have a mapping relationship, when a microservice needs to be accessed, by calling the interface class corresponding to the microservice, it is possible to know which interface implementation class in the virtual machine needs to be run, and then by running the obtained interface implementation class in the virtual machine, the calling method corresponding to the facial interface is assembled with the interface information, the service information and the request type in the target input, and an access request for the microservice can be obtained.
[0115] The access request is sent to the microservice, so that the microservice processes the access request and generates corresponding return data, and uses the return data as the target output parameter.
[0116] In this embodiment, before sending an access request to a microservice, an underlying encapsulation method corresponding to the service information (i.e., the service type) is invoked. This underlying encapsulation method is then used to encapsulate the access request, resulting in a processed access request, which is then sent to the microservice. This encapsulation of the access request using the underlying encapsulation method may include performing certain logical filtering on the access request, such as verifying the correctness of relevant parameters and verifying the correctness of the data format of the relevant parameters. This logical filtering can filter out non-compliant access requests, ensuring that only compliant access requests are sent to the microservice.
[0117] Receive the target output parameter returned by the microservice through the target interface.
[0118] In this embodiment, after the access request is sent to the microservice, the microservice processes the access request to perform service access and generates corresponding return data, namely the target output parameter, and the target output parameter returned through the target interface.
[0119] This application calls the underlying tool class, assembles the calling method with the interface information, service information, and request type in the target input parameter, generates a corresponding access request, and then sends the access request to the microservice, so that the microservice processes the access request and generates corresponding return data to obtain the target output parameter, and receives the target output parameter returned by the microservice through the target interface. This realizes the convenient use of the preset underlying tool class to perform service call processing corresponding to the microservice, thereby quickly and accurately obtaining the target output parameter corresponding to the target input parameter returned by the target interface.
[0120] In some optional implementations, step S206 includes the following steps:
[0121] Call the preset information configuration platform.
[0122] In this embodiment, the information configuration platform is a pre-built platform for storing and managing interface information. The information configuration platform generates and configures an interface configuration table corresponding to each microservice interface. The interface configuration table includes various interface configuration information corresponding to each interface. The interface configuration information may include at least the interface name, interface type, interface address, interface request method, interface return value type, and conversion methods corresponding to various return value types.
[0123] A predefined return value type corresponding to the target interface is obtained from the information configuration platform.
[0124] In this embodiment, a target interface configuration table corresponding to the target interface may be first obtained from the information configuration platform, and then the predefined return value type corresponding to the target interface may be queried from the target interface configuration table.
[0125] The target output parameter is converted based on the return value type to obtain corresponding target object data, and the target object data is used as the feedback data.
[0126] In this embodiment, the above-mentioned specific implementation process of converting the target output parameter based on the return value type to obtain the corresponding target object data will be further described in detail in subsequent specific embodiments of this application and will not be elaborated on here.
[0127] The feedback data is returned to the client.
[0128] This application calls a preset information configuration platform, obtains a predefined return value type corresponding to the target interface from the information configuration platform, and then converts the target output parameter based on the return value type to obtain the corresponding target object data, and returns the target object data as the feedback data to the client. This application can quickly and accurately convert the target output parameter to obtain the corresponding feedback data based on the information query processing of the information configuration platform, thereby improving the efficiency of generating feedback data and ensuring the accuracy of generating feedback data.
[0129] In some optional implementations, converting the target output parameter based on the return value type to obtain corresponding target object data includes the following steps:
[0130] Determine the conversion method corresponding to the return value type.
[0131] In this embodiment, after obtaining the return value type corresponding to the target interface, the conversion method corresponding to the return value type of the target interface can be further found from all conversion methods corresponding to various return value types stored in the target interface configuration table corresponding to the target interface.
[0132] Calling a conversion tool corresponding to the conversion method.
[0133] In this embodiment, the conversion tool refers to a converter that matches the return value type of the target interface.
[0134] The target output parameter is converted based on the conversion tool to obtain converted parameter data.
[0135] In this embodiment, converting the target output parameter based on the conversion tool means converting the data format of the target output parameter using the above-mentioned conversion tool, and obtaining the result of converting the data format of the target output parameter returned by the conversion tool to obtain the above-mentioned converted parameter data.
[0136] The converted parameter data is used as the target object data.
[0137] The present application determines the conversion method corresponding to the return value type, then calls the conversion tool corresponding to the conversion method, and then converts the target output parameter based on the conversion tool to obtain the converted parameter data, and uses the converted parameter data as the target object data. By using the conversion tool, the present application can quickly and accurately convert the target output parameter based on the conversion tool to obtain the required target object data as feedback data, thereby improving the generation efficiency of the feedback data and ensuring the generation accuracy of the feedback data.
[0138] In some optional implementations of this embodiment, after step S202, the electronic device may further perform the following steps:
[0139] If the operating environment belongs to the unit test environment, a target method corresponding to the target interface is obtained.
[0140] In this embodiment, the above-mentioned target method is a method pre-built by developers according to actual unit testing requirements for use in JUNIT unit testing of the target interface. The method content of the target method is used to return simulation data related to the target interface in the JUNIT unit testing environment. Simulation data is also called mock data.
[0141] Execute the target method to obtain corresponding simulation data.
[0142] In this embodiment, by executing the above target method, the test data mocked in the unit test environment, namely the simulated data, will be returned.
[0143] The simulation data is returned to the client.
[0144] After obtaining the current operating environment, if the application detects that the operating environment belongs to the unit test environment, it obtains the target method corresponding to the target interface, executes the target method to obtain the corresponding simulation data, and returns the simulation data to the client. When the application detects that the current operating environment belongs to the unit test environment, it will intelligently and directly execute the target method corresponding to the target interface to return the corresponding simulation data, thereby effectively avoiding the failure of the unit test due to the unavailable external service, and improving the intelligence of the unit test processing.
[0145] In some optional implementations of this embodiment, before the step of acquiring the target method corresponding to the target interface, the electronic device may further perform the following steps:
[0146] Get the preset simulation code.
[0147] In this embodiment, the simulation code is pre-written by the developer according to the actual unit test requirements and is used to cooperate with the junit unit test of the target interface. The simulation code can also be called mock code.
[0148] The simulation code is written into a preset method template to obtain a processed method template.
[0149] In this embodiment, the above method template is a general test method template pre-built according to actual unit test requirements and used for various interfaces.
[0150] The processed method template is used as the target method.
[0151] In this embodiment, the generated target method refers to the unit test method in the target interface, which is specifically used to respond to the unit test processing of the target interface and has the function of generating test data corresponding to the unit test request of the target interface.
[0152] This application obtains preset simulation code and then writes the simulation code into a preset method template. The processed method template obtained is the target method. This application uses the method of writing the simulation code into the method template to generate the target method, so that there is no need to introduce additional mock-related dependencies in the process of unit testing the target interface, thereby realizing unit testing that is not affected by external service shadows and improving the intelligence of unit testing processing.
[0153] It should be emphasized that in order to further ensure the privacy and security of the above feedback data, the above feedback data can also be stored in a blockchain node.
[0154] The blockchain referred to in this application is a new application model of computer technologies such as distributed data storage, peer-to-peer transmission, consensus mechanisms, and encryption algorithms. Blockchain is essentially a decentralized database, a series of data blocks generated using cryptographic methods. Each data block contains information about a batch of network transactions, which is used to verify the validity of the information (anti-counterfeiting) and generate the next block. Blockchain can include the blockchain underlying platform, the platform product service layer, and the application service layer.
[0155] The embodiments of the present application can acquire and process relevant data based on artificial intelligence technology. Artificial intelligence (AI) refers to the theories, methods, technologies, and application systems that use digital computers or machines controlled by digital computers to simulate, extend, and expand human intelligence, perceive the environment, acquire knowledge, and use that knowledge to achieve optimal results.
[0156] Fundamental AI technologies generally include sensors, dedicated AI chips, cloud computing, distributed storage, big data processing, operating / interaction systems, and mechatronics. AI software technologies primarily encompass computer vision, robotics, biometrics, speech processing, natural language processing, and machine learning / deep learning.
[0157] Those skilled in the art will appreciate that all or part of the processes in the above-described method embodiments can be implemented by instructing related hardware via computer-readable instructions. The computer-readable instructions can be stored in a computer-readable storage medium, and when the program is executed, it can include the processes in the above-described method embodiments. The aforementioned storage medium can be a non-volatile storage medium such as a magnetic disk, an optical disk, a read-only memory (ROM), or a random access memory (RAM).
[0158] It should be understood that although the steps in the flowcharts of the accompanying drawings are shown in sequence as indicated by the arrows, these steps are not necessarily executed in the order indicated by the arrows. Unless otherwise specified herein, there is no strict order restriction on the execution of these steps, and they can be executed in other orders. Moreover, at least some of the steps in the flowcharts of the accompanying drawings may include multiple sub-steps or multiple stages, and these sub-steps or stages are not necessarily executed at the same time, but can be executed at different times, and their execution order is not necessarily sequential, but can be executed in turn or alternately with other steps or at least a portion of the sub-steps or stages of other steps.
[0159] Further references Figure 3 , as a response to the above Figure 2 The present application provides an embodiment of a microservice calling device, which is similar to the embodiment of the microservice calling device. Figure 2 Corresponding to the method embodiment shown, the device can be specifically applied to various electronic devices.
[0160] like Figure 3 As shown, the microservice calling device 300 of this embodiment includes: a judgment module 301, an interception module 302, a first acquisition module 303, a parsing module 304, a first processing module 305 and a second processing module 306.
[0161] The judgment module 301 is used to judge whether a call request corresponding to the target interface of the microservice is received, which is triggered by the user through the client; wherein the call request carries the service name of the microservice and the interface name of the target interface, and a target annotation is pre-defined in the proxy method corresponding to the target interface;
[0162] Interception module 302, configured to intercept the call request based on the target aspect corresponding to the target interface and obtain the current operating environment;
[0163] A first acquisition module 303 is configured to execute the proxy method corresponding to the target interface and acquire interface information and service information corresponding to the target interface if the operating environment does not belong to the preset unit test environment;
[0164] The parsing module 304 is used to parse the target annotation to obtain the corresponding request type and calling method, and construct the target input parameter based on the interface information, the service information and the request type;
[0165] The first processing module 305 is configured to perform a service call process corresponding to the microservice using a preset underlying tool class based on the target input parameter and the calling method, and obtain a target output parameter corresponding to the target input parameter returned by the target interface;
[0166] The second processing module 306 is configured to convert the target output parameter to obtain corresponding feedback data, and return the feedback data to the client.
[0167] In this embodiment, the operations performed by the above modules or units correspond one-to-one to the steps of the microservice calling method in the aforementioned embodiment, and are not repeated here.
[0168] In some optional implementations of this embodiment, the interception module 302 includes:
[0169] A first acquisition submodule is configured to acquire the target annotation predefined in the execution method in the target interface;
[0170] A second acquisition submodule is used to acquire the target section corresponding to the target annotation;
[0171] An interception submodule, configured to intercept the call request based on the target aspect;
[0172] The third acquisition submodule is used to acquire the current operating environment in the target section.
[0173] In this embodiment, the operations performed by the above modules or units correspond one-to-one to the steps of the microservice calling method in the aforementioned embodiment, and are not repeated here.
[0174] In some optional implementations of this embodiment, the first processing module 305 includes:
[0175] A first generation submodule is configured to call the underlying tool class, assemble the calling method with the interface information, the service information, and the request type in the target input parameter, and generate a corresponding access request;
[0176] A second generating submodule is configured to send the access request to the microservice, so that the microservice processes the access request and generates corresponding return data, and uses the return data as the target output parameter;
[0177] The receiving submodule is used to receive the target output parameter returned by the microservice through the target interface.
[0178] In this embodiment, the operations performed by the above modules or units correspond one-to-one to the steps of the microservice calling method in the aforementioned embodiment, and are not repeated here.
[0179] In some optional implementations of this embodiment, the second processing module 306 includes:
[0180] The calling submodule is used to call the preset information configuration platform;
[0181] A fourth acquisition submodule, configured to acquire a predefined return value type corresponding to the target interface from the information configuration platform;
[0182] a conversion submodule, configured to convert the target output parameter based on the return value type to obtain corresponding target object data, and use the target object data as the feedback data;
[0183] The return submodule is used to return the feedback data to the client.
[0184] In this embodiment, the operations performed by the above modules or units correspond one-to-one to the steps of the microservice calling method in the aforementioned embodiment, and are not repeated here.
[0185] In some optional implementations of this embodiment, the conversion submodule includes:
[0186] A first determining unit, configured to determine a conversion method corresponding to the return value type;
[0187] A calling unit, configured to call a conversion tool corresponding to the conversion method;
[0188] A conversion unit, configured to convert the target output parameter based on the conversion tool to obtain converted parameter data;
[0189] The second determining unit is configured to use the converted parameter data as the target object data.
[0190] In this embodiment, the operations performed by the above modules or units correspond one-to-one to the steps of the microservice calling method in the aforementioned embodiment, and are not repeated here.
[0191] In some optional implementations of this embodiment, the microservice calling device further includes:
[0192] A second acquisition module is configured to acquire a target method corresponding to the target interface if the operating environment belongs to the unit test environment;
[0193] An execution module, configured to execute the target method to obtain corresponding simulation data;
[0194] A return module is used to return the simulation data to the client.
[0195] In this embodiment, the operations performed by the above modules or units correspond one-to-one to the steps of the microservice calling method in the aforementioned embodiment, and are not repeated here.
[0196] In some optional implementations of this embodiment, the microservice calling device further includes:
[0197] A third acquisition module is used to obtain a preset simulation code;
[0198] A writing module, used for writing the simulation code into a preset method template to obtain a processed method template;
[0199] A determination module is used to use the processed method template as the target method.
[0200] In this embodiment, the operations performed by the above modules or units correspond one-to-one to the steps of the microservice calling method in the aforementioned embodiment, and are not repeated here.
[0201] To solve the above technical problems, the present application also provides a computer device. Figure 4 , Figure 4 This is a basic structural block diagram of the computer device in this embodiment.
[0202] The computer device 4 includes a memory 41, a processor 42, and a network interface 43 that are interconnected through a system bus. It should be noted that the figure only shows a computer device 4 having components 41-43, but it should be understood that it is not required to implement all the components shown, and more or fewer components can be implemented instead. Among them, those skilled in the art will understand that the computer device here is a device that can automatically perform numerical calculations and / or information processing according to pre-set or stored instructions, and its hardware includes but is not limited to microprocessors, application-specific integrated circuits (ASICs), field-programmable gate arrays (FPGAs), digital signal processors (DSPs), embedded devices, etc.
[0203] The computer device may be a desktop computer, notebook computer, PDA, cloud server, etc. The computer device may interact with the user via a keyboard, mouse, remote control, touchpad, or voice control device.
[0204] The memory 41 includes at least one type of readable storage medium, including flash memory, hard disk, multimedia card, card-type memory (e.g., SD or DX memory), random access memory (RAM), static random access memory (SRAM), read-only memory (ROM), electrically erasable programmable read-only memory (EEPROM), programmable read-only memory (PROM), magnetic memory, magnetic disk, optical disk, etc. In some embodiments, the memory 41 can be an internal storage unit of the computer device 4, such as the hard disk or memory of the computer device 4. In other embodiments, the memory 41 can also be an external storage device of the computer device 4, such as a plug-in hard disk equipped on the computer device 4, a smart memory card (SMC), a secure digital (SD) card, a flash memory card, etc. Of course, the memory 41 can also include both the internal storage unit of the computer device 4 and its external storage device. In this embodiment, the memory 41 is generally used to store the operating system and various application software installed on the computer device 4, such as computer-readable instructions for calling methods of microservices. In addition, the memory 41 can also be used to temporarily store various types of data that have been output or are to be output.
[0205] In some embodiments, the processor 42 may be a central processing unit (CPU), a controller, a microcontroller, a microprocessor, or other data processing chip. The processor 42 is generally used to control the overall operation of the computer device 4. In this embodiment, the processor 42 is used to execute computer-readable instructions stored in the memory 41 or process data, such as computer-readable instructions for executing a method for calling the microservice.
[0206] The network interface 43 may include a wireless network interface or a wired network interface. The network interface 43 is generally used to establish a communication connection between the computer device 4 and other electronic devices.
[0207] Compared with the prior art, the embodiments of the present application have the following beneficial effects:
[0208] In an embodiment of the present application, after receiving a call request corresponding to the target interface of a microservice triggered by a user through a client, the call request will be intercepted based on the target aspect corresponding to the target interface, and the current operating environment will be obtained. If the operating environment does not belong to the preset unit test environment, the proxy method corresponding to the target interface will be executed to obtain the interface information and service information corresponding to the target interface. Then, the target annotation will be parsed to obtain the corresponding request type and calling method, and the target input parameter will be constructed based on the interface information, the service information and the request type. Subsequently, based on the target input parameter and the calling method, the service call processing corresponding to the microservice will be performed using the preset underlying tool class, and the target output parameter corresponding to the target input parameter returned by the target interface will be obtained. Finally, the target output parameter will be converted to obtain corresponding feedback data, and the feedback data will be returned to the client. In an embodiment of the present application, by applying the target annotation to the target interface corresponding to the microservice to perform the calling process of the microservice, the generation of a large amount of redundant code in the business code can be reduced, and the service call processing corresponding to the microservice can be quickly performed through the use of the underlying tool class, thereby effectively improving the processing efficiency of the microservice call.
[0209] The present application also provides another embodiment, namely, providing a computer-readable storage medium, wherein the computer-readable storage medium stores computer-readable instructions, and the computer-readable instructions can be executed by at least one processor to enable the at least one processor to perform the steps of the microservice calling method as described above.
[0210] Compared with the prior art, the embodiments of the present application have the following beneficial effects:
[0211] In an embodiment of the present application, after receiving a call request corresponding to the target interface of a microservice triggered by a user through a client, the call request will be intercepted based on the target aspect corresponding to the target interface, and the current operating environment will be obtained. If the operating environment does not belong to the preset unit test environment, the proxy method corresponding to the target interface will be executed to obtain the interface information and service information corresponding to the target interface. Then, the target annotation will be parsed to obtain the corresponding request type and calling method, and the target input parameter will be constructed based on the interface information, the service information and the request type. Subsequently, based on the target input parameter and the calling method, the service call processing corresponding to the microservice will be performed using the preset underlying tool class, and the target output parameter corresponding to the target input parameter returned by the target interface will be obtained. Finally, the target output parameter will be converted to obtain corresponding feedback data, and the feedback data will be returned to the client. In an embodiment of the present application, by applying the target annotation to the target interface corresponding to the microservice to perform the calling process of the microservice, the generation of a large amount of redundant code in the business code can be reduced, and the service call processing corresponding to the microservice can be quickly performed through the use of the underlying tool class, thereby effectively improving the processing efficiency of the microservice call.
[0212] Through the description of the above implementation methods, those skilled in the art can clearly understand that the above-mentioned embodiment methods can be implemented by means of software plus the necessary general hardware platform, and of course can also be implemented by hardware, but in many cases the former is a better implementation method. Based on this understanding, the technical solution of the present application, or the part that contributes to the prior art, can be embodied in the form of a software product, which is stored in a storage medium (such as ROM / RAM, magnetic disk, optical disk), and includes a number of instructions for enabling a terminal device (which can be a mobile phone, computer, server, air conditioner, or network device, etc.) to execute the methods described in each embodiment of the present application.
[0213] Obviously, the embodiments described above are only some of the embodiments of the present application, rather than all of the embodiments. The preferred embodiments of the present application are given in the accompanying drawings, but they do not limit the patent scope of the present application. The present application can be implemented in many different forms. On the contrary, the purpose of providing these embodiments is to make the understanding of the disclosure of the present application more thorough and comprehensive. Although the present application has been described in detail with reference to the aforementioned embodiments, for those skilled in the art, it is still possible to modify the technical solutions described in the aforementioned specific embodiments, or to make equivalent replacements for some of the technical features therein. Any equivalent structure made using the contents of the present application specification and the accompanying drawings, directly or indirectly used in other related technical fields, is also within the scope of patent protection of the present application.
Claims
1. A method for calling a microservice, characterized in that: The steps include: Determine whether a call request corresponding to a target interface of a microservice is received, triggered by a user through a client; wherein the call request carries the service name of the microservice and the interface name of the target interface, and a target annotation is pre-defined in a proxy method corresponding to the target interface; If so, intercept the call request based on the target aspect corresponding to the target interface and obtain the current operating environment; If the operating environment does not belong to the preset unit test environment, executing the proxy method corresponding to the target interface to obtain interface information and service information corresponding to the target interface; Parse the target annotation to obtain the corresponding request type and calling method, and construct the target input parameter based on the interface information, the service information and the request type; Based on the target input parameter and the calling method, a preset underlying tool class is used to perform a service call process corresponding to the microservice, and a target output parameter corresponding to the target input parameter is obtained from the target interface; Convert the target output parameter to obtain corresponding feedback data, and return the feedback data to the client; The step of intercepting the call request based on the target aspect corresponding to the target interface and obtaining the current operating environment specifically includes: Obtain the target annotation predefined in the proxy method in the target interface; Acquire the target section corresponding to the target annotation; intercepting the call request based on the target aspect; In the target section, obtaining the current operating environment; Among them, the step of performing service call processing corresponding to the microservice based on the target input parameter and the calling method using a preset underlying tool class to obtain the target output parameter corresponding to the target input parameter returned by the target interface specifically includes: Calling the underlying tool class, assembling the calling method with the interface information, the service information, and the request type in the target input parameter to generate a corresponding access request; Sending the access request to the microservice, so that the microservice processes the access request and generates corresponding return data, and uses the return data as the target output parameter; Receive the target output parameter returned by the microservice through the target interface.
2. The microservice calling method according to claim 1, characterized in that: The step of converting the target output parameter to obtain corresponding feedback data and returning the feedback data to the client specifically includes: Call the preset information configuration platform; Acquire a predefined return value type corresponding to the target interface from the information configuration platform; Convert the target output parameter based on the return value type to obtain corresponding target object data, and use the target object data as the feedback data; The feedback data is returned to the client.
3. The microservice calling method according to claim 2, characterized in that: The step of converting the target output parameter based on the return value type to obtain corresponding target object data specifically includes: Determine a conversion method corresponding to the return value type; Calling a conversion tool corresponding to the conversion method; Performing conversion processing on the target output parameter based on the conversion tool to obtain converted parameter data; The converted parameter data is used as the target object data.
4. The microservice calling method according to claim 1, characterized in that: After the step of intercepting the call request based on the target aspect corresponding to the target interface and obtaining the current operating environment, the method further includes: If the operating environment belongs to the unit test environment, obtaining a target method corresponding to the target interface; Executing the target method to obtain corresponding simulation data; The simulation data is returned to the client.
5. The microservice calling method according to claim 4, characterized in that: Before the step of acquiring the target method corresponding to the target interface, the method further includes: Get the preset simulation code; Writing the simulation code into a preset method template to obtain a processed method template; The processed method template is used as the target method.
6. A microservice calling device, characterized in that: include: A judgment module is used to judge whether a call request corresponding to a target interface of a microservice is received, triggered by a user through a client; wherein the call request carries the service name of the microservice and the interface name of the target interface, and a target annotation is pre-defined in the proxy method corresponding to the target interface; An interception module, configured to, if yes, intercept the call request based on a target aspect corresponding to the target interface and obtain a current operating environment; A first acquisition module is configured to execute the proxy method corresponding to the target interface and acquire interface information and service information corresponding to the target interface if the operating environment does not belong to the preset unit test environment; A parsing module, configured to parse the target annotation to obtain the corresponding request type and calling method, and construct a target input parameter based on the interface information, the service information and the request type; A first processing module is configured to perform service call processing corresponding to the microservice using a preset underlying tool class based on the target input parameter and the calling method, and obtain a target output parameter corresponding to the target input parameter returned by the target interface; A second processing module is used to convert the target output parameter to obtain corresponding feedback data, and return the feedback data to the client; Among them, the interception module includes: A first acquisition submodule is used to acquire the target annotation predefined in the proxy method in the target interface; A second acquisition submodule is used to acquire the target section corresponding to the target annotation; An interception submodule, configured to intercept the call request based on the target aspect; A third acquisition submodule is used to obtain the current operating environment in the target section; The first processing module includes: A first generation submodule is configured to call the underlying tool class, assemble the calling method with the interface information, the service information, and the request type in the target input parameter, and generate a corresponding access request; A second generating submodule is configured to send the access request to the microservice, so that the microservice processes the access request and generates corresponding return data, and uses the return data as the target output parameter; The receiving submodule is used to receive the target output parameter returned by the microservice through the target interface.
7. A computer device comprising a memory and a processor, wherein the memory stores computer-readable instructions, and when the processor executes the computer-readable instructions, the steps of the microservice calling method according to any one of claims 1 to 5 are implemented.
8. A computer-readable storage medium, characterized in that The computer-readable storage medium stores computer-readable instructions, which, when executed by a processor, implement the steps of the microservice calling method according to any one of claims 1 to 5.
Citation Information
Patent Citations
Interface data simulation method and device, storage medium and electronic equipment
CN109739656A
API auditing method and device based on micro-service architecture, equipment and storage medium
CN112231379A