Microservice protocol conversion method, device and electronic device
By deploying the protocol configuration center and target interface in the microservice gateway, extracting and applying the protocol conversion template for protocol conversion, the code complexity and interface diversity caused by microservice protocol conversion in the existing technology is solved, and the service code is realized Simplify and hot deployment of protocol changes.
Patent Information
- Application Number
- CN202210906706.9
- Authority / Receiving Office
- CN · China
- Patent Type
- Patents(China)
- Current Assignee / Owner
- Filing Date
- 2022-07-29
- Publication Date
- 2024-12-10
- Estimated Expiration
- 2042-07-29
AI Technical Summary
In the prior art, the microservice protocol conversion needs to be carried out internally, resulting in increased code bloat and complexity, and the inability to effectively handle requests from multiple client systems. The server needs to provide multiple interfaces for each client system, which adds The complexity of the server.
Deploy an independent protocol configuration center in the microservice gateway, save the protocol conversion templates required by each microservice to connect to each client system, receive request data through the target interface and extract the corresponding target template from the protocol configuration center for protocol conversion, and convert the conversion The subsequent data is sent to the server to avoid protocol conversion within the service and to support hot deployment protocol changes.
Reduces the complexity of service code, allows a microservice to receive requests from multiple client systems, reduces the number of exposed interfaces, and implements hot deployment of service protocols, without restarting and publishing services, and improves work efficiency.
Smart Images

Figure CN115499518B_ABST
Abstract
Description
Technical Field
[0001] The present invention relates to the field of Internet technology, and in particular to a microservice protocol conversion method, device and electronic device. Background Art
[0002] At present, the microservice of services is the general trend, and the service openness is increasing, such as the voice call microservice and SMS microservice provided by operators. Client systems such as takeaway applets, express delivery applets, and e-commerce websites can be secondary developed based on the above microservices, so that third-party applications can request the functions of the above microservices; the service provider provides a callable interface for the service and exposes the interface so that third-party applications can send requests to the microservices through the exposed interface. Usually, the client system needs to call a large number of microservices. At present, in order to avoid the client system storing too much microservice information and too many repeated communications, the communication between the client system and the microservice is generally coordinated based on the microservice gateway. However, the communication protocol inside the client system is usually different from the communication protocol inside the microservice. In order to solve the communication problem, protocol conversion is required. As disclosed in patent document CN113438251A, the request data generated by the client system is converted into data that can be transmitted inside the microservice with the help of protocol conversion information. After being processed by the microservice, the response data is converted into the protocol form of the client system with the help of protocol conversion information, and the response data is fed back. However, in the existing technology, the protocol conversion work is mainly performed within the service, and it needs to be parsed according to different protocols. If multiple different client systems call the current microservice, the service needs to provide an exposed interface for each client system, such as Figure 1 As shown, this approach leads to bloated and repeated code and increases the complexity of the server.
[0003] Afterwards, if Figure 2 As shown, in order to solve the above problems, patent document CN113242221A proposes a protocol conversion method. When the client system requests a microservice, the microservice gateway directly obtains the corresponding microservice from the server according to the request of the client system, and then performs protocol conversion and binary data processing through the obtained microservice. However, the location of the protocol conversion in this method is still inside the service, and the disadvantage of bloated service code is not solved. Summary of the invention
[0004] In view of this, an embodiment of the present invention provides a microservice protocol conversion method, device and electronic device, thereby reducing the complexity of the service code.
[0005] According to the first aspect, an embodiment of the present invention provides a microservice protocol conversion method, which is applied to a target interface, and the target interface is deployed in a microservice gateway. The method includes: receiving request data sent by a client system; extracting a target template corresponding to the request data from a protocol conversion template saved in a protocol configuration center based on the request data; converting the request data into second request data of a target protocol through the target template, and sending the second request data to a server corresponding to the target interface, wherein the target protocol is a communication protocol supported internally by the server.
[0006] Optionally, extracting a target template corresponding to the request data from a protocol conversion template saved in a protocol configuration center based on the request data includes: pulling service metadata information of the configured interface from the protocol configuration center and caching the service metadata information; extracting the target template from the cached service metadata information based on the request data.
[0007] Optionally, extracting the target template from the cached service metadata information based on the request data includes: determining the target metadata information from the cached service metadata information according to the URL in the request data; and extracting the target template from the target metadata information according to the client system source in the request data.
[0008] Optionally, when a protocol supported by the server changes, the server sends change information to the protocol configuration center so that the protocol configuration center modifies the saved protocol conversion template.
[0009] Optionally, the receiving the request data sent by the client system includes: receiving request data respectively sent by a preset number of client systems, and performing protocol conversion on the request data of each client system in parallel through the target interface.
[0010] Optionally, converting the request data into second request data in the target protocol format through the target template includes: obtaining a JSON object from the request data according to a JSON path field recorded in the target template; and mapping the JSON object to the second request data.
[0011] According to the second aspect, an embodiment of the present invention provides a microservice protocol conversion device, which is applied to a target interface, and the target interface is deployed in a microservice gateway. The device includes: a data receiving module, which is used to receive request data sent by a client system; a template matching module, which is used to extract a target template corresponding to the request data from a protocol conversion template saved in a protocol configuration center based on the request data; a protocol conversion module, which is used to convert the request data into second request data in the form of a target protocol through the target template, and send the second request data to a server corresponding to the target interface, and the target protocol is a communication protocol supported internally by the server.
[0012] Optionally, the template matching module includes: a metadata matching module, used to pull service metadata information of the configured interface from the protocol configuration center and cache the service metadata information; a template extraction module, used to extract the target template from the cached service metadata information based on the request data.
[0013] According to the third aspect, an embodiment of the present invention provides an electronic device, comprising: a memory and a processor, the memory and the processor being communicatively connected to each other, the memory storing computer instructions, and the processor executing the method described in the first aspect or any optional implementation manner of the first aspect by executing the computer instructions.
[0014] According to a fourth aspect, an embodiment of the present invention provides a computer-readable storage medium, wherein the computer-readable storage medium stores computer instructions, and the computer instructions are used to enable the computer to execute the method described in the first aspect or any optional implementation manner of the first aspect.
[0015] The technical solution provided by this application has the following advantages:
[0016] The technical solution provided by the present application deploys an independent protocol configuration center, and saves the protocol conversion templates required for each microservice to connect to each client system to the protocol configuration center. The protocol conversion template is used to convert the request data into the second request data in the form of a string that can be directly used by the server. When the client system requests data from the microservice through the target interface of the microservice gateway, the target interface directly retrieves the corresponding protocol conversion template from the protocol configuration center according to the client system to which the request data belongs. Thereby, the protocol conversion is performed in the microservice gateway, and then the converted second request data is sent to the server, and the second request data is parsed into a binary stream for use at the service layer. Thereby, the protocol conversion work of the microservice does not need to be performed within the microservice, reducing the complexity of the service code.
[0017] In addition, the target interface of a microservice can simultaneously receive request data from multiple different client systems, and dispatch the corresponding protocol conversion template from the protocol configuration center according to the system source of each request data, and perform protocol conversion processing at the same time. This allows a microservice to only expose one interface in the microservice gateway, further reducing the complexity of the service code. Moreover, when the protocol supported by the microservice changes, it is only necessary to modify the corresponding protocol conversion template in the protocol configuration center without adjusting the service code. Compared with the existing technology, changes in the service protocol can be hot-deployed without restarting and publishing the service, which improves work efficiency. BRIEF DESCRIPTION OF THE DRAWINGS
[0018] The features and advantages of the present invention will be more clearly understood by referring to the accompanying drawings, which are schematic and should not be construed as limiting the present invention in any way. In the accompanying drawings:
[0019] Figure 1 The schematic diagram of the principle of protocol conversion in the prior art is shown;
[0020] Figure 2 Another schematic diagram of the protocol conversion principle in the prior art is shown;
[0021] Figure 3 A schematic diagram of the steps of a microservice protocol conversion method in one embodiment of the present invention is shown;
[0022] Figure 4 A schematic diagram of the structure of a microservice protocol conversion method in one embodiment of the present invention is shown;
[0023] Figure 5 A schematic diagram of the structure of a microservice protocol conversion device in one embodiment of the present invention is shown;
[0024] Figure 6 A schematic structural diagram of an electronic device in one embodiment of the present invention is shown. DETAILED DESCRIPTION
[0025] In order to make the purpose, technical solutions and advantages of the embodiments of the present invention clearer, the technical solutions in the embodiments of the present invention will be clearly and completely described below in conjunction with the drawings in the embodiments of the present invention. Obviously, the described embodiments are part of the embodiments of the present invention, not all of the embodiments. Based on the embodiments of the present invention, all other embodiments obtained by those skilled in the art without creative work are within the scope of protection of the present invention.
[0026] See also Figure 3 and Figure 4In one embodiment, a microservice protocol conversion method is applied to a target interface, and the target interface is deployed in a microservice gateway, and specifically includes the following steps:
[0027] Step S101: receiving request data sent by a client system.
[0028] Step S102: extracting a target template corresponding to the request data from the protocol conversion templates stored in the protocol configuration center based on the request data.
[0029] Step S103: converting the request data into second request data of the target protocol through the target template, and sending the second request data to the server corresponding to the target interface, where the target protocol is a communication protocol supported internally by the server.
[0030] In the prior art, a complete process of requesting a microservice includes: 1. The microservice creates a service interface for a certain client system; 2. Then, an exposure interface is created for the service interface and registered in the microservice gateway; 3. The client system is registered in the microservice gateway; 4. When the client needs to use the function of the microservice, the client sends the request data, and the microservice gateway will match it according to the previous registration situation, so that the client system calls the exposure interface registered in the microservice gateway, so that the client and the microservice communicate; 5. After the microservice receives the request data, it performs protocol conversion and uses the request data. In an embodiment of the present invention, a protocol configuration center is independently set up, and the protocol configuration center is used to manage a large number of protocol conversion templates. The protocol conversion template includes the conversion protocols required for various client systems to connect to various microservices, and is used to convert the JSON object in the request data sent by the client into a string that can be directly used by the backend microservice. Each exposure interface registered in the microservice gateway can directly extract the corresponding protocol conversion template from the protocol configuration center. When the client system requests data from the target interface (regarded as an exposed interface) in the microservice gateway, the target interface receives the request data sent by the client system, and searches the protocol conversion template saved in the protocol configuration center according to the source of the request data to find a target template corresponding to the current client system and the current microservice. The target interface then uses the target template to convert the request data into the second request data of the target protocol supported by the server, and then sends the second request data to the server corresponding to the target interface, so that the server can process it directly. As a result, the server no longer needs to develop code for the protocol conversion part, and the complexity of the service code is significantly reduced.
[0031] In addition, in this embodiment, since the protocol conversion work of the service does not involve the service itself, an exposed interface can also connect to multiple client systems at the same time. Assuming that a preset number of client systems send request data to the target interface respectively, the target interface calls the corresponding protocol conversion template from the protocol configuration center according to their system sources for each request data, and performs protocol conversion at the same time, reducing the number of exposed interfaces in the microservice gateway and further reducing the complexity of the service code.
[0032] In addition, the prior art puts the protocol conversion in the service layer. Because the conversion is done inside the service, the service code needs to be modified and the service can only take effect after it is published. In this embodiment, when the service on the server side needs to modify the protocol, the server side only needs to send the protocol change information to the protocol configuration center, so that the protocol configuration center can modify the saved protocol conversion template. This realizes the hot deployment of the conversion protocol and improves the overall work efficiency of the system.
[0033] Specifically, in one embodiment, the above step S102 specifically includes the following steps:
[0034] Step 1: Pull the service metadata information of the configured interface from the protocol configuration center and cache the service metadata information.
[0035] Step 2: Extract the target template from the cached service metadata information based on the request data.
[0036] Specifically, in this embodiment, the microservice gateway can pull the service metadata information of the configured interface from the protocol configuration center at a preset time, with a timing strategy, or when receiving request data from a client system. Each service metadata information includes all protocol conversion templates for the corresponding service. Use the URL of the exposed interface as the key and the metadata as the value to cache the service metadata information locally and set the cache expiration time. This will improve the speed of data query when the protocol conversion template needs to be extracted later.
[0037] Specifically, in one embodiment, the above step 2 specifically includes the following steps:
[0038] Step 3: Determine the target metadata information from the cached service metadata information according to the URL in the request data.
[0039] Step 4: Extract the target template from the target metadata information according to the client system source in the request data.
[0040] Specifically, when the client system sends a request, the microservice gateway identifies which microservice the client system calls through the URL in the http protocol request data, thereby determining the target metadata information. A microservice can be provided to multiple different client systems for calling, so the target metadata information also includes multiple protocol conversion templates. Further, the identity of the client system currently calling the microservice is determined through the system source in the request data, so that the corresponding target template is accurately extracted from the target metadata information for protocol conversion.
[0041] Specifically, in one embodiment, the above step S103 specifically includes the following steps:
[0042] Step 5: Get the JSON object from the request data according to the JSON path field recorded in the target template.
[0043] Step 6: Map the JSON object to the second request data.
[0044] Specifically, the fields configured in the protocol conversion template are filled in as JSON paths, and the required data corresponding to the JSON path is obtained from the request data. According to the path field, the corresponding JSON object is obtained from the request data, and then the JSON is converted into a string (second request data) that can be used by the server, and then the second request data is sent to the backend server. After the server processes the data, it feeds back to the microservice gateway. After the microservice gateway continues the protocol conversion, it obtains the data in the json format of the http protocol and gives it to the client system. In this way, the protocol conversion process is implemented in the microservice gateway, without the need for internal conversion in the service, reducing the complexity of the service code.
[0045] Through the above steps, the technical solution provided by the present application deploys an independent protocol configuration center, and saves the protocol conversion templates required for each microservice to connect to each client system to the protocol configuration center. The protocol conversion template is used to convert the request data into the second request data in the form of a string that can be directly used by the server. When the client system requests data from the microservice through the target interface of the microservice gateway, the target interface directly retrieves the corresponding protocol conversion template from the protocol configuration center according to the client system to which the request data belongs. Thereby, the protocol conversion is performed in the microservice gateway, and then the converted second request data is sent to the server, and the second request data is parsed into a binary stream for use at the service layer. Thereby, the protocol conversion work of the microservice does not need to be performed within the microservice, reducing the complexity of the service code.
[0046] In addition, the target interface of a microservice can simultaneously receive request data from multiple different client systems, and dispatch the corresponding protocol conversion template from the protocol configuration center according to the system source of each request data, and perform protocol conversion processing at the same time. This allows a microservice to only expose one interface in the microservice gateway, further reducing the complexity of the service code. Moreover, when the protocol supported by the microservice changes, it is only necessary to modify the corresponding protocol conversion template in the protocol configuration center without adjusting the service code. Compared with the existing technology, changes in the service protocol can be hot-deployed without restarting and publishing the service, which improves work efficiency.
[0047] like Figure 5 As shown, this embodiment also provides a microservice protocol conversion device, which is applied to a target interface, and the target interface is deployed in a microservice gateway. The device includes:
[0048] The data receiving module 101 is used to receive the request data sent by the client system. For details, please refer to the relevant description of step S101 in the above method embodiment, which will not be repeated here.
[0049] The template matching module 102 is used to extract the target template corresponding to the request data from the protocol conversion template stored in the protocol configuration center based on the request data. For details, please refer to the relevant description of step S102 in the above method embodiment, which will not be repeated here.
[0050] The protocol conversion module 103 is used to convert the request data into second request data in the form of a target protocol through a target template, and send the second request data to the server corresponding to the target interface, where the target protocol is a communication protocol supported internally by the server. For details, see the relevant description of step S103 in the above method embodiment, which will not be repeated here.
[0051] Specifically, in one embodiment, the template matching module specifically includes:
[0052] The metadata matching module is used to pull the service metadata information of the configured interface from the protocol configuration center and cache the service metadata information. For details, please refer to the relevant description of step 1 in the above method embodiment, which will not be repeated here.
[0053] The template extraction module is used to extract the target template from the cached service metadata information based on the request data. For details, please refer to the relevant description of step 2 in the above method embodiment, which will not be repeated here.
[0054] A microservice protocol conversion device provided in an embodiment of the present invention is used to execute a microservice protocol conversion method provided in the above embodiment. Its implementation method and principle are the same. For details, please refer to the relevant description of the above method embodiment, which will not be repeated here.
[0055] Through the collaborative cooperation of the above-mentioned components, the technical solution provided by this application deploys an independent protocol configuration center, and saves the protocol conversion templates required for each microservice to connect to each client system to the protocol configuration center. The protocol conversion template is used to convert the request data into the second request data in the form of a string that can be directly used by the server. When the client system requests data from the microservice through the target interface of the microservice gateway, the target interface directly retrieves the corresponding protocol conversion template from the protocol configuration center according to the client system to which the request data belongs. Thereby, the protocol conversion is performed in the microservice gateway, and then the converted second request data is sent to the server, and the second request data is parsed into a binary stream for use at the service layer. Thereby, the protocol conversion work of the microservice does not need to be performed within the microservice, reducing the complexity of the service code.
[0056] In addition, the target interface of a microservice can simultaneously receive request data from multiple different client systems, and dispatch the corresponding protocol conversion template from the protocol configuration center according to the system source of each request data, and perform protocol conversion processing at the same time. This allows a microservice to only expose one interface in the microservice gateway, further reducing the complexity of the service code. Moreover, when the protocol supported by the microservice changes, it is only necessary to modify the corresponding protocol conversion template in the protocol configuration center without adjusting the service code. Compared with the existing technology, changes in the service protocol can be hot-deployed without restarting and publishing the service, which improves work efficiency.
[0057] Figure 6 An electronic device according to an embodiment of the present invention is shown, the device includes a processor 901 and a memory 902, which can be connected via a bus or other means. Figure 6 The example of connecting through bus is taken in the following.
[0058] The processor 901 may be a central processing unit (CPU). The processor 901 may also be other general-purpose processors, digital signal processors (DSP), application-specific integrated circuits (ASIC), field-programmable gate arrays (FPGA) or other programmable logic devices, discrete gate or transistor logic devices, discrete hardware components, or a combination of the above chips.
[0059] The memory 902 is a non-transitory computer-readable storage medium that can be used to store non-transitory software programs, non-transitory computer executable programs and modules, such as program instructions / modules corresponding to the methods in the above method embodiments. The processor 901 executes various functional applications and data processing of the processor by running the non-transitory software programs, instructions and modules stored in the memory 902, that is, implementing the methods in the above method embodiments.
[0060] The memory 902 may include a program storage area and a data storage area, wherein the program storage area may store an operating system, an application required by at least one function; the data storage area may store data created by the processor 901, etc. In addition, the memory 902 may include a high-speed random access memory, and may also include a non-volatile memory, such as at least one disk storage device, a flash memory device, or other non-volatile solid-state storage device. In some embodiments, the memory 902 may optionally include a memory remotely arranged relative to the processor 901, and these remote memories may be connected to the processor 901 via a network. Examples of the above-mentioned network include, but are not limited to, the Internet, an intranet, a local area network, a mobile communication network, and combinations thereof.
[0061] One or more modules are stored in the memory 902 , and when executed by the processor 901 , the method in the above method embodiment is executed.
[0062] The specific details of the above electronic device can be understood by referring to the corresponding descriptions and effects in the above method embodiments, and will not be repeated here.
[0063] Those skilled in the art can understand that all or part of the processes in the above-mentioned embodiments can be implemented by instructing the relevant hardware through a computer program, and the implemented program can be stored in a computer-readable storage medium, and when the program is executed, it can include the processes of the embodiments of the above-mentioned methods. Among them, the storage medium can be a disk, an optical disk, a read-only memory (ROM), a random access memory (RAM), a flash memory, a hard disk drive (HDD) or a solid-state drive (SSD), etc.; the storage medium can also include a combination of the above-mentioned types of memory.
[0064] Although the embodiments of the present invention have been described in conjunction with the accompanying drawings, those skilled in the art may make various modifications and variations without departing from the spirit and scope of the present invention, and such modifications and variations are all within the scope defined by the appended claims.
Claims
1. A microservice protocol conversion method, It is characterized in that Applied to a target interface, the target interface is deployed in a microservice gateway, the target interface corresponds to only one microservice inside the server, and the method includes: Receive request data sent by the client system; Based on the request data, a target template corresponding to the request data is extracted from the protocol conversion template saved by the protocol configuration center, wherein a target interface of a microservice can simultaneously receive request data sent by multiple different client systems, and search the protocol conversion templates saved by the protocol configuration center according to the source of the request data to find a target template corresponding to the current client system and the current microservice. The protocol configuration center is used to manage a large number of protocol conversion templates, and the protocol conversion templates include conversion protocols required for various client systems to connect to various microservices; The request data is converted into second request data of a target protocol through the target template, and the second request data is sent to a server corresponding to a target interface, wherein the target protocol is a communication protocol supported internally by the server.
2. The method according to claim 1, It is characterized in that The extracting a target template corresponding to the request data from a protocol conversion template stored in a protocol configuration center based on the request data includes: Pulling service metadata information of the configured interface from the protocol configuration center, and caching the service metadata information; The target template is extracted from the cached service metadata information based on the request data.
3. The method according to claim 2, It is characterized in that The extracting the target template from the cached service metadata information based on the request data includes: Determining target metadata information from cached service metadata information according to the URL in the request data; The target template is extracted from the target metadata information according to the client system source in the request data.
4. The method according to claim 1, It is characterized in that When the protocol supported by the server changes, the server sends the change information to the protocol configuration center so that the protocol configuration center modifies the saved protocol conversion template.
5. The method according to claim 1, It is characterized in that The receiving request data sent by the client system includes: Request data respectively sent by a preset number of client systems are received, so as to perform protocol conversion on the request data of each client system in parallel through the target interface.
6. The method according to claim 1, It is characterized in that The step of converting the request data into second request data in a target protocol format by using the target template includes: Obtaining a JSON object from the request data according to the JSON path field recorded in the target template; The JSON object is mapped to the second request data.
7. A microservice protocol conversion device, It is characterized in that Applied to a target interface, the target interface is deployed in a microservice gateway, the target interface corresponds to only one microservice inside the server, and the device includes: A data receiving module, used for receiving request data sent by the client system; A template matching module is used to extract a target template corresponding to the request data from the protocol conversion template stored in the protocol configuration center based on the request data, wherein a target interface of a microservice can simultaneously receive request data sent by multiple different client systems, and search the protocol conversion template stored in the protocol configuration center according to the source of the request data to find a target template corresponding to the current client system and the current microservice. The protocol configuration center is used to manage a large number of protocol conversion templates, and the protocol conversion templates include conversion protocols required for various client systems to connect to various microservices; The protocol conversion module is used to convert the request data into second request data in the form of a target protocol through the target template, and send the second request data to the server corresponding to the target interface. The target protocol is a communication protocol supported internally by the server.
8. The device according to claim 7, It is characterized in that The template matching module comprises: A metadata matching module, used to pull service metadata information of configured interfaces from the protocol configuration center and cache the service metadata information; A template extraction module is used to extract the target template from the cached service metadata information based on the request data.
9. An electronic device, It is characterized in that include: A memory and a processor, wherein the memory and the processor are communicatively connected to each other, the memory stores computer instructions, and the processor executes the method according to any one of claims 1 to 6 by executing the computer instructions.
10. A computer-readable storage medium, It is characterized in that The computer-readable storage medium stores computer instructions, and the computer instructions are used to enable the computer to execute the method according to any one of claims 1 to 6.
