Response code internationalization method based on Spring Cloud Gateway
Through Spring Cloud Gateway's response code internationalization method, the response code internationalization needs of microservices are centrally processed, and the problem of cumbersome response code maintenance is solved, and the simplified international maintenance process and improved system maintainability are achieved.
Patent Information
- Application Number
- CN202411198342.9
- Authority / Receiving Office
- CN · China
- Patent Type
- Applications(China)
- Current Assignee / Owner
- Filing Date
- 2024-08-29
- Publication Date
- 2024-12-13
AI Technical Summary
Under the microservice architecture, the international maintenance of the response code is scattered, updated cumbersome and error-prone, and changes are required in the resource packages of multiple services, and after the change, the service needs to be restarted, resulting in a brief interruption of the service.
The response code internationalization method based on Spring Cloud Gateway is adopted, and the request is received through a unified entrance, forwarded to the microservice, resolved the request address and language requirements, and called the translation module for the response code translation and replacement, realizing centralized processing and dynamic configuration of the response code.
It significantly reduces the complexity and workload of international maintenance, improves the maintainability and scalability of the system, supports dynamic configuration without restarting services, and simplifies the development process.
Smart Images

Figure CN119135746A_ABST
Abstract
Description
Technical Field
[0001] The present invention relates to the field of service gateway data processing, and in particular to a response code internationalization method based on Spring Cloud Gateway. Background Art
[0002] Currently, under the microservice architecture, the internationalization of response codes is supported by managing language resource packages in each service. The existing technology modifies the internationalization content of response codes in the following steps: 1. The service to which the response code belongs needs to be clearly identified. 2. Find the corresponding language resource package under the service and put the translated response code content into the property file of the corresponding language. 3. Restart the service for the configuration content to take effect.
[0003] However, existing technologies are difficult to update and maintain: when the internationalization of response codes needs to be updated or modified, corresponding changes need to be made in the resource packages of multiple services. This can make the update process cumbersome and error-prone, and it is necessary to ensure that all services are updated synchronously. Moreover, after changing the language resource package, it is usually necessary to restart the service for the configuration content to take effect. This may cause a brief interruption or unavailability of the service, which has a certain impact on the production environment. Summary of the invention
[0004] The purpose of the present invention is to solve the problems of decentralized maintenance and heavy workload of internationalization of response codes in the prior art, and to provide a response code internationalization method based on Spring Cloud Gateway. The unified processing and configuration of response codes are realized through Spring Cloud Gateway, which significantly reduces the complexity and workload of internationalization maintenance and improves the maintainability and scalability of the system.
[0005] The technical solution adopted by the present invention is: The response code internationalization method based on Spring Cloud Gateway includes the following steps: Step 1: When the client sends a request, Spring Cloud Gateway receives the client request as the unified entry point of the service; Step 2: Spring Cloud Gateway forwards the client request to the corresponding microservice. After the microservice processes the request, it returns the original response message to Spring Cloud Gateway. Step 3: Spring Cloud Gateway parses the request address (URL) requested by the client, which includes path and parameter information. Step 4, matching the request address obtained by parsing with the predefined internationalization filter to determine whether the request address matches the internationalization filter successfully; if so, determining that the client request needs to be internationalized; otherwise, not processing the original response message, and executing step 8; Step 5: Spring Cloud Gateway parses the client request to obtain the target language expected by the client; determines whether the target language is the same as the default language of the system; if so, no internationalization is required to end the internationalization process, and processes the request according to normal business logic; otherwise, continues to execute the internationalization process to step 6; Furthermore, Spring Cloud Gateway parses the Accept-Language field in the request header or the language field in the request parameter to obtain the target language expected by the client. Determine whether the target language is the same as the default language of the system; if so, the language expected by the client is consistent with the language supported by the system by default, and no internationalization processing is required. In this case, the system can directly end the internationalization process and process the request according to normal business logic; otherwise, the target language is different from the default language of the system, which means that the client expects to receive the response in a different language. In this case, the system needs to continue to execute the internationalization processing process, including calling the translation module for translation and other steps.
[0006] Step 6: Send the combination of the original response code and the target language to the translation service. The translation service translates the original response code into the target language and returns the translated content to Spring Cloud Gateway. Specifically, the combination of the response code and the target language is sent to the translation service, which, after receiving the request, performs a search based on the provided response code and target language. The translation module uses a pre-populated multilingual message library that contains translations for various response codes. The translation module will return the message corresponding to the original response code that has been translated into the target language.
[0007] Step 7: After receiving the translated content, Spring Cloud Gateway finds the original response code description in the response message and replaces it with the received translated content to rebuild the response data message. Step 8: Spring Cloud Gateway returns the response data packet to the client of the corresponding request.
[0008] The present invention adopts the above technical solution and has the following technical features: 1. Spring Cloud Gateway serves as a unified entrance to receive requests sent by the client. 2. The client request is forwarded to the corresponding microservice, and the microservice returns the original response message to Spring Cloud Gateway after processing the request; 3. Determine the internationalization requirements, obtain the request address to determine whether the internationalization filter is turned on. If so, execute the next step, if not, return the original message to the requesting client and end the process. 4. Get the Accept-Language header or language parameter of the request to determine the target language expected by the client. If the language is the same as the default language of the system, there is no need to translate, directly end the internationalization process, and return the original message to the requesting client; otherwise, continue to the next step. 5. Extract the response code from the response message, and send the response code and the target language to the translation module in combination with the determined target language. The translation module returns the translation content of the corresponding target language to Spring Cloud Gateway. 6. Use the translation content returned by the translation module to replace the original response code description in the response message. After the replacement is completed, the response data message is reconstructed and returned to the client that sent the request.
[0009] Compared with the prior art, the beneficial effects of the present invention are: Spring Cloud Gateway, as the entry and exit of network requests, can centrally handle the internationalization requirements of response codes of all microservices. This eliminates the need for developers to implement internationalization logic in each microservice, greatly simplifying the development process. Through a unified configuration method, the complexity and workload of internationalization maintenance can be greatly reduced, and development efficiency can be improved. Dynamic filter configuration and dynamic data configuration are supported, which allows the configuration of response code internationalization to be dynamically added, modified, and deleted at runtime without restarting the service. BRIEF DESCRIPTION OF THE DRAWINGS
[0010] The present invention is further described in detail below with reference to the accompanying drawings and specific embodiments; Figure 1 The figure is a flow chart of the response code internationalization method based on Spring Cloud Gateway of the present invention. Implementation
[0011] In order to make the purpose, technical solutions and advantages of the embodiments of the present application clearer, the technical solutions in the embodiments of the present application will be clearly and completely described below in conjunction with the drawings in the embodiments of the present application.
[0012] like Figure 1 As shown, the present invention discloses a response code internationalization method based on Spring Cloud Gateway, comprising the following steps: Step 1: When the client sends a request, Spring Cloud Gateway receives the client request as the unified entry point of the service; Step 2: Spring Cloud Gateway forwards the client request to the corresponding microservice. After the microservice processes the request, it returns the original response message to Spring Cloud Gateway. Step 3: Spring Cloud Gateway parses the request address (URL) requested by the client, which includes path and parameter information. Step 4, match the request address obtained by parsing with the predefined internationalization filter to determine whether the request address matches the internationalization filter successfully; if so, it is determined that the client request needs to be internationalized; otherwise, no processing is performed on the original response message, and steps 5, 6, and 7 are skipped to execute step 8; Step 5: Spring Cloud Gateway parses the client request to obtain the target language expected by the client; determines whether the target language is the same as the default language of the system; if so, no internationalization is required to end the internationalization process, and processes the request according to normal business logic; otherwise, continues to execute the internationalization process to step 6; Furthermore, Spring Cloud Gateway parses the Accept-Language field in the request header or the language field in the request parameter to obtain the target language expected by the client. Determine whether the target language is the same as the default language of the system; if so, the language expected by the client is consistent with the language supported by the system by default, and no internationalization processing is required. In this case, the system can directly end the internationalization process and process the request according to normal business logic; otherwise, the target language is different from the default language of the system, which means that the client expects to receive the response in a different language. In this case, the system needs to continue to execute the internationalization processing process, including calling the translation module for translation and other steps.
[0013] Step 6: Send the combination of the original response code and the target language to the translation service. The translation service translates the original response code into the target language and returns the translated content to Spring Cloud Gateway. Specifically, the combination of the response code and the target language is sent to the translation service, which, after receiving the request, performs a search based on the provided response code and target language. The translation module uses a pre-populated multilingual message library that contains translations for various response codes. The translation module will return the message corresponding to the original response code that has been translated into the target language.
[0014] Step 7: After receiving the translated content, Spring Cloud Gateway finds the original response code description in the response message and replaces it with the received translated content to rebuild the response data message. Step 8: Spring Cloud Gateway returns the response data packet to the client of the corresponding request.
[0015] The present invention adopts the above technical solution and has the following technical features: 1. Spring Cloud Gateway serves as a unified entrance to receive requests sent by the client. 2. The client request is forwarded to the corresponding microservice, and the microservice returns the original response message to Spring Cloud Gateway after processing the request; 3. Determine the internationalization requirements, obtain the request address to determine whether the internationalization filter is turned on. If so, execute the next step, if not, return the original message to the client, and end the process. 4. Get the Accept-Language header or language parameter of the request to determine the target language expected by the client. If the language is the same as the default language of the system, no translation is required, and the original message is returned to the client to end the internationalization process; otherwise, continue to the next step. 5. Extract the response code from the response message, and send the response code and the target language to the translation module in combination with the determined target language. The translation module will return the translation content corresponding to the target language. 6. Use the translation content returned by the translation module to replace the original response code description in the response message. After the replacement is completed, the response data message is reconstructed and returned to the client that sent the request.
[0016] Compared with the prior art, the beneficial effects of the present invention are: Spring Cloud Gateway, as the entry and exit of network requests, can centrally handle the internationalization requirements of response codes of all microservices. This eliminates the need for developers to implement internationalization logic in each microservice, greatly simplifying the development process. Through a unified configuration method, the complexity and workload of internationalization maintenance can be greatly reduced, and development efficiency can be improved. Dynamic filter configuration and dynamic data configuration are supported, which allows the configuration of response code internationalization to be dynamically added, modified, and deleted at runtime without restarting the service.
[0017] Obviously, the described embodiments are part of the embodiments of the present application, rather than all of the embodiments. In the absence of conflict, the embodiments in the present application and the features in the embodiments can be combined with each other. The components of the embodiments of the present application generally described and shown in the drawings here can be arranged and designed in various different configurations. Therefore, the detailed description of the embodiments of the present application is not intended to limit the scope of the application claimed for protection, but merely represents the selected embodiments of the present application. Based on the embodiments in the present application, all other embodiments obtained by ordinary technicians of the art without making creative work are within the scope of protection of the present application.
Claims
1. The response code internationalization method based on Spring Cloud Gateway is characterized by: It includes the following steps: Step 1: When the client sends a request, Spring Cloud Gateway receives the client request as the unified entry point of the service; Step 2: Spring Cloud Gateway forwards the client request to the corresponding microservice. After the microservice processes the request, it returns the original response message to Spring Cloud Gateway. Step 3: Spring Cloud Gateway resolves the request address requested by the client. Step 4, matching the parsed request address with a predefined internationalization filter to determine whether the request address matches the internationalization filter successfully; If yes, it is determined that the client request needs to be internationalized; otherwise, no processing is performed on the original response message, and step 8 is executed; Step 5: Spring Cloud Gateway parses the client request to obtain the target language expected by the client; determines whether the target language is the same as the default language of the system; if so, no internationalization is required to end the internationalization process, and processes the request according to normal business logic; Otherwise, continue with the internationalization process to step 6; Step 6: Send the combination of the original response code and the target language to the translation service. The translation service translates the original response code into the target language and returns the translated content to Spring Cloud Gateway. Step 7: After receiving the translated content, Spring Cloud Gateway finds the original response code description in the response message and replaces it with the received translated content to rebuild the response data message. Step 8: Spring Cloud Gateway returns the response data packet to the client of the corresponding request.
2. The response code internationalization method based on Spring Cloud Gateway according to claim 1, characterized in that: The request address in step 3 includes path and parameter information.
3. The response code internationalization method based on Spring Cloud Gateway according to claim 1 is characterized in that: In step 5, Spring Cloud Gateway parses the Accept-Language field in the request header or the language field in the request parameter to obtain the target language expected by the client.
4. The response code internationalization method based on Spring Cloud Gateway according to claim 1, characterized in that: In step 6, after the translation service receives the translation request, it searches for a translation module of the translation service according to the provided original response code and the target language; the translation module returns the translation content corresponding to the original response code translated into the target language.
5. The response code internationalization method based on Spring Cloud Gateway according to claim 4 is characterized in that: The translation module uses a pre-populated multi-language message library that contains translations for various response codes.
Citation Information
Cited By
Multi-language front-end resource automatic construction method and system based on construction stage
CN121638279A
A multi-language front-end resource automatic construction method and system based on a construction stage
CN121638279B
