Lua-based request body protocol conversion plug-in and calling method thereof

Through the Lua-based request body protocol conversion plug-in, the problem of JSON and XML conversion delay and dependence on third-party services in the API gateway is solved, efficient data format conversion and hot update functions are realized, and the flexibility and security of the API gateway are improved. sex.

CN119254845APending Publication Date: 2025-01-03UNICLOUD TECH CO LTD
View PDF 0 Cites 0 Cited by

Patent Information

Application Number
CN202411118637.0
Authority / Receiving Office
CN · China
Patent Type
Applications(China)
Current Assignee / Owner
Filing Date
2024-08-15
Publication Date
2025-01-03

AI Technical Summary

Technical Problem

Traditional JSON and XML conversion plug-ins require calling third-party Java services, which have delay problems and cannot be converted when the service is updated or crashed, which cannot meet the API gateway's needs to minimize the requesting party's data structure conversion.

Method used

Design a request body protocol conversion plug-in based on Lua language, including JSON and XML library modules, function conversion modules, exception handling modules, unit testing modules and performance optimization modules, to realize the mutual conversion between JSON and XML, and support hot updates. Rely on third-party services.

Benefits of technology

It realizes efficient mutual conversion between JSON and XML, reduces call time loss, supports ready-to-use hot update operations, no need to modify configuration files or rely on third-party services, and improves the flexibility and security of API gateways.

✦ Generated by Eureka AI based on patent content.

Smart Images

  • Figure CN119254845A_ABST
    Figure CN119254845A_ABST
Patent Text Reader

Abstract

The invention provides a Lua-based request body protocol conversion plug-in and a calling method thereof. The Lua-based request body protocol conversion plug-in comprises a plug-in structure, a database, a function conversion module, an exception handling module, a unit test module and a performance optimization module. The protocol conversion plug-in has the beneficial effects that the protocol conversion plug-in can be started for any API interface in the API gateway. After the plug-in is started, protocol conversion is carried out on the requested message in the JSON format. The back-end service is called through the converted message, and certainly, the request header is rewritten, so that the standard of XML calling is met. And after the rear end returns the XML data, the plug-in is automatically converted into the JSON format. The plug-in does not depend on a third-party JAVA service, and the problems of communication delay and the like caused by third-party service calling are reduced. Meanwhile, whether the plug-in is started or not is known only by an API provider, and a front-end calling interface is not perceived at all.
Need to check novelty before this filing date? Find Prior Art

Description

Technical Field

[0001] The present invention belongs to the technical field of basic capability / API gateway, and in particular to a Lua-based request body protocol conversion plug-in and a calling method thereof. Background Art

[0002] When the API gateway is used as the hub for front-end and back-end calls, its call volume and call scenarios are gradually increasing in the actual application process. In addition to providing a unified data structure and calling method for external calls, it is also critical for the API gateway to support multi-protocol back-end parameter conversion. It is particularly important to minimize the number of requests from the requester to call in the data structure (JSON format) specified by the API gateway, and to support the conversion of the data structure into the required XML format in accordance with the back-end service protocol to the greatest extent. In addition, the XML format data returned by the back-end needs to be converted into JSON and returned to the caller. Realize the conversion between JSON and XML.

[0003] Traditional JSON and XML conversion plug-ins need to call third-party Java services from the API gateway, which has a certain delay. If the service is updated or crashed, the conversion cannot be performed. Therefore, a request body protocol conversion plug-in based on Lua language and supporting hot updates that can convert JSON and XML to each other is particularly important.

[0004] The birth of this plug-in realizes a one-time update of the gateway plug-in. When the background service needs to receive and return in XML, the plug-in is enabled. This function can be achieved by turning off the plug-in when it is not needed. During use, there is no need to modify the configuration file, update and use, no reliance on third-party services, call and convert, return and convert, which greatly reduces the call time loss. Summary of the invention

[0005] In view of this, the present invention aims to propose a Lua-based request body protocol conversion plug-in and a calling method thereof to solve at least one problem existing in the above-mentioned prior art.

[0006] To achieve the above object, the technical solution of the present invention is achieved as follows:

[0007] In the first aspect, the present invention provides a request body protocol conversion plug-in based on Lua, including a plug-in structure, a database, a function conversion module, an exception handling module, a unit testing module and a performance optimization module. The plug-in structure and the function conversion module are both communicatively connected to the database, and the function conversion module is also communicatively connected to the exception handling module, the unit testing module and the performance optimization module respectively.

[0008] Furthermore, the plug-in structure includes a main entry file module and a configuration file module, both of which are connected to the database for communication. The main entry file module is used to initialize all necessary libraries and define main operation functions, and the configuration file module is used to provide configuration parameters.

[0009] Furthermore, the database includes a JSON library module and an XML library module, wherein the XML library module is used for parsing and generating data in JSON format, and the XML library module is used for parsing and generating data in XML format.

[0010] Further, the function conversion module includes a JSON to XML conversion function and an XML to JSON conversion function, wherein the JSON to XML conversion function includes:

[0011] Input: JSON string;

[0012] Processing: Parse into Lua table and then convert to XML format;

[0013] Output: XML string;

[0014] XML to JSON conversion functions include:

[0015] Input: XML string;

[0016] Processing: Parse into Lua table and then convert to JSON format;

[0017] Output: JSON string.

[0018] Furthermore, the exception handling module is used to capture JSON parsing errors and XML parsing errors.

[0019] Furthermore, the unit test module is used to write test cases to ensure the correctness of each conversion function.

[0020] Furthermore, the performance optimization module is used to optimize the processing of large data sets.

[0021] In a second aspect, based on the same concept, the present invention also provides a method for calling a request body protocol conversion plug-in based on Lua, comprising the following steps:

[0022] First, use the plug-in with the Unicloud API Gateway or APISIX Gateway and place it in the configuration plugins; the plug-in will take effect after restarting the gateway;

[0023] Select any API, enable the plug-in, configure the plug-in, and click Save. The plug-in will take effect immediately without restarting the gateway.

[0024] When the plug-in is turned on, any call request that complies with the specification will convert the request body from JSON format to XML format and call the backend service;

[0025] The backend service returns a complete XML message, which the plug-in obtains and converts. If successful, it is converted into JSON format and returned to the calling end.

[0026] If the backend service is modified and still requires protocol conversion, no operation is required. If the backend service does not need to use the protocol conversion function, just delete the plug-in and it will be invalid immediately.

[0027] Compared with the prior art, the Lua-based request body protocol conversion plug-in and its calling method described in the present invention have the following advantages:

[0028] The present invention discloses a Lua-based request body protocol conversion plug-in and its calling method. Through the present invention, the protocol conversion plug-in can be enabled for any API interface in the API gateway. After the plug-in is enabled, the protocol conversion will be performed on the requested JSON format message. The back-end service is called through the converted message, and of course the request header will be rewritten at the same time to meet the XML call specifications. After the back-end returns the XML data, the plug-in will automatically convert it to JSON format. The plug-in does not rely on third-party JAVA services, reducing problems such as communication delays caused by third-party service calls. At the same time, only the API provider knows whether the plug-in is enabled, and the front-end calling interface has no perception. BRIEF DESCRIPTION OF THE DRAWINGS

[0029] The accompanying drawings constituting a part of the present invention are used to provide a further understanding of the present invention. The exemplary embodiments of the present invention and their descriptions are used to explain the present invention and do not constitute an improper limitation of the present invention. In the accompanying drawings:

[0030] Figure 1 A schematic diagram of plug-in activation configuration according to an embodiment of the present invention;

[0031] Figure 2 This is a schematic diagram of an example of plug-in calling according to an embodiment of the present invention. DETAILED DESCRIPTION

[0032] It should be noted that, in the absence of conflict, the embodiments of the present invention and the features in the embodiments may be combined with each other.

[0033] In the description of the present invention, it should be understood that the terms "center", "longitudinal", "lateral", "up", "down", "front", "back", "left", "right", "vertical", "horizontal", "top", "bottom", "inside", "outside" and the like indicate positions or positional relationships based on the positions or positional relationships shown in the accompanying drawings, and are only for the convenience of describing the present invention and simplifying the description, rather than indicating or implying that the device or element referred to must have a specific orientation, be constructed and operated in a specific orientation, and therefore cannot be understood as limiting the present invention. In addition, the terms "first", "second", and the like are only used for descriptive purposes, and cannot be understood as indicating or implying relative importance or implicitly indicating the number of technical features indicated. Thus, features defined as "first", "second", and the like may explicitly or implicitly include one or more of the features. In the description of the present invention, unless otherwise specified, "multiple" means two or more.

[0034] In the description of the present invention, it should be noted that, unless otherwise clearly specified and limited, the terms "installed", "connected", and "connected" should be understood in a broad sense, for example, it can be a fixed connection, a detachable connection, or an integral connection; it can be a mechanical connection or an electrical connection; it can be a direct connection, or it can be indirectly connected through an intermediate medium, or it can be the internal communication of two components. For ordinary technicians in this field, the specific meanings of the above terms in the present invention can be understood by specific circumstances.

[0035] The present invention will be described in detail below with reference to the accompanying drawings and in conjunction with embodiments.

[0036] like Figure 1 to Figure 2 As shown, a Lua-based request body protocol conversion plug-in includes a plug-in structure, a database, a function conversion module, an exception handling module, a unit testing module and a performance optimization module. The plug-in structure and the function conversion module are both communicated with the database, and the function conversion module is also communicated with the exception handling module, the unit testing module and the performance optimization module respectively.

[0037] In a preferred embodiment of the present invention, a request body protocol conversion plug-in developed by Lua is implemented, which mainly involves the conversion between JSON and XML. The specific content may include the following parts:

[0038] 1. Plugin structure

[0039] Main entry file module: initializes all necessary libraries and defines the main operation functions.

[0040] Configuration file module: If necessary, you can provide configuration parameters to make the plugin more flexible.

[0041] 2. Database

[0042] JSON library module: such as cjson or dkjson, used for parsing and generating JSON format.

[0043] XML library modules: such as xml2lua or lua-xml, used for parsing and generating XML format.

[0044] 3. Function conversion module

[0045] JSON to XML conversion function:

[0046] Input: JSON string

[0047] Processing: Parsed into Lua table and then converted to XML format.

[0048] Output: XML string

[0049] XML to JSON conversion function:

[0050] Input: XML string

[0051] Processing: Parsed into Lua table and then converted to JSON format.

[0052] Output: JSON string

[0053] 4. Exception handling module

[0054] Exception capture: including JSON parsing errors, XML parsing errors, etc. and providing error information.

[0055] 5. Unit Testing Module

[0056] Write test cases to ensure the correctness of each conversion function.

[0057] 6.Performance optimization module

[0058] If needed, the processing of large data sets can be optimized, such as using iterators or streaming. The specific implementation will vary depending on project requirements and usage scenarios.

[0059] The present invention develops a request body protocol conversion (json-xml conversion) plug-in through Lua. After configuring the API gateway, the plug-in is enabled for the required API interface to realize the conversion between JSON format and XML format. When the background service is adjusted, the original parameter forwarding function can be realized by simply canceling the plug-in. The caller is unaware, and the receiver can change the parameter format information of the call as needed. And the plug-in does not need to import any conversion template in advance, and only needs the requester to pass in the correct JSON message to perform the conversion. The practicality and security of the interface are greatly increased.

[0060] Through the present invention, the protocol conversion plug-in can be enabled for any API interface in the API gateway. After the plug-in is enabled, the protocol conversion will be performed on the requested JSON format message. The back-end service is called through the converted message, and of course the request header will be rewritten at the same time to meet the specifications of XML calls. After the back-end returns the XML data, the plug-in will automatically convert it to JSON format. The plug-in does not rely on third-party JAVA services, reducing problems such as communication delays caused by third-party service calls. At the same time, only the API provider knows whether the plug-in is enabled, and the front-end calling interface has no perception.

[0061] Considering that the backend service of the API interface may be adjusted during actual use, the API parameters will also be adjusted accordingly. Traditional protocol conversion plug-ins need to import templates and update the code before they can continue to be used. This plug-in can omit this step. As long as the caller passes in the correct JSON format message, it can be directly converted into XML to call the backend. If the backend service update does not require the API gateway to perform XML conversion in the middle, then simply cancel the plug-in selection. It truly achieves a hot update operation that is ready to use. Therefore, the plug-in configuration is also extremely simple, and you only need to set the enable flag.

[0062] A method for calling a request body protocol conversion plug-in based on Lua, comprising:

[0063] First, use this plug-in with the Unicloud API Gateway or APISIX Gateway and place it in the configuration plugins. The plug-in will take effect after restarting the gateway.

[0064] Select any API, enable the plugin, and follow the instructions in the attached image. Figure 1 After configuring in this way, click Save and the plug-in will take effect immediately without restarting the gateway.

[0065] When the plug-in is enabled, any call request that complies with the specification will convert the request body from JSON format to XML format and call the backend service.

[0066] The backend service returns a complete XML message, which the plug-in obtains and converts, and returns to the caller after successful conversion into JSON format.

[0067] If the backend service is modified and still requires protocol conversion, no operation is required. If the backend service does not need to use the protocol conversion function, the plug-in can be deleted and it will be invalid immediately. This greatly reduces other redundant operations.

[0068] Therefore, the operation logic of configuration is effective and calling is adding is realized. At the same time, the relevant request body format is only known to the interface provider. If the interface is not called from the gateway, any caller will not be able to call successfully by calling the request message of the API gateway alone, which greatly increases the security of the interface calling through the gateway. The caller is unaware and the receiver processes the business logic normally according to the request body. At the same time, the plug-in does not need to rely on third-party Java services, which minimizes the request delay problem caused by calling third-party services.

[0069] Example 1

[0070] like Figure 1 As shown in the figure, after selecting the API that needs to enable the constant plug-in, you can add "xml-json-transform" by configuring the plug-in content.

[0071] "flag": the activation flag of the plugin

[0072] After configuration, the overall usage process is as follows Figure 2 As shown, when a request enters the API gateway, the system will make a judgment based on the plug-in information configured in the API. If the plug-in is enabled, Figure 1 The value configured in the example is verified. After the verification is passed, the JSON format message carried by the request call is processed and converted into an XML message to request the backend service. After the call is completed, the backend service will return the XML message, which will be processed into a JSON message through the plug-in and returned to the caller. This realizes the closed-loop operation of the entire call process.

[0073] This plug-in has strong adaptability and flexibility. The plug-in is written in Lua. You only need to enable the plug-in to convert it immediately without restarting any service. At the same time, the plug-in can be configured and used in any 2.0+ version of APISIX.

[0074] The above description is only a preferred embodiment of the present invention and is not intended to limit the present invention. Any modifications, equivalent substitutions, improvements, etc. made within the spirit and principle of the present invention should be included in the protection scope of the present invention.

Claims

1. A Lua-based request body protocol conversion plug-in, characterized in that: It includes a plug-in structure, a database, a function conversion module, an exception handling module, a unit testing module and a performance optimization module. The plug-in structure and the function conversion module are both connected to the database for communication, and the function conversion module is also connected to the exception handling module, the unit testing module and the performance optimization module for communication.

2. The Lua-based request body protocol conversion plug-in according to claim 1, characterized in that: The plug-in structure includes a main entry file module and a configuration file module. Both the main entry file module and the configuration file module are connected to the database for communication. The main entry file module is used to initialize all necessary libraries and define main operation functions, and the configuration file module is used to provide configuration parameters.

3. The Lua-based request body protocol conversion plug-in according to claim 1, characterized in that: The database includes a JSON library module and an XML library module. The XML library module is used for parsing and generating JSON format, and the XML library module is used for parsing and generating XML format.

4. The Lua-based request body protocol conversion plug-in according to claim 1, characterized in that: The function conversion module includes a JSON to XML conversion function and an XML to JSON conversion function, wherein the JSON to XML conversion function includes: Input: JSON string; Processing: Parse into Lua table and then convert to XML format; Output: XML string; XML to JSON conversion functions include: Input: XML string; Processing: Parse into Lua table and then convert to JSON format; Output: JSON string.

5. The Lua-based request body protocol conversion plug-in according to claim 1, characterized in that: The exception handling module is used to capture JSON parsing errors and XML parsing errors.

6. The Lua-based request body protocol conversion plug-in according to claim 1, characterized in that: The unit test module is used to write test cases to ensure the correctness of each conversion function.

7. The Lua-based request body protocol conversion plug-in according to claim 1, characterized in that: The performance optimization module is used to optimize the processing of large data sets.

8. A method for calling a Lua-based request body protocol conversion plug-in, applied to a Lua-based request body protocol conversion plug-in according to any one of claims 1 to 7, characterized in that: The following steps are involved: First, use the plug-in with the Unicloud API Gateway or APISIX Gateway and place it in the configuration plugins; The plug-in takes effect after restarting the gateway; Select any API, enable the plug-in, configure the plug-in, and click Save. The plug-in will take effect immediately without restarting the gateway. When the plug-in is turned on, any call request that complies with the specification will convert the request body from JSON format to XML format and call the backend service; The backend service returns a complete XML message, which the plug-in obtains and converts. If successful, it is converted into JSON format and returned to the calling end. If the backend service is modified and still requires protocol conversion, no operation is required. If the backend service does not need to use the protocol conversion function, just delete the plug-in and it will be invalid immediately.