Information transmission method and device based on SpringCloud micro-service and electronic equipment
By using Transmittable local thread variable component (TransmittableThreadLocal) to store and pass user token information in SpringCloud microservice architecture, the data competition and inconsistency of token passing among microservices in a multi-thread environment is solved, and a higher level of data is achieved. System stability and security.
Patent Information
- Application Number
- CN202411589645.3
- Authority / Receiving Office
- CN · China
- Patent Type
- Applications(China)
- Current Assignee / Owner
- Filing Date
- 2024-11-08
- Publication Date
- 2025-02-07
- Estimated Expiration
- Not applicable · inactive patent
AI Technical Summary
In multi-threaded application scenarios, the existing technology is difficult to effectively solve the problem of user token delivery between microservices, and it is prone to data competition and inconsistency, which affects the security and stability of the system.
By creating a Transmittable local thread variable component (TransmittableThreadLocal), the user token information is stored and passed in a multi-thread environment, ensuring the specific request information between different threads, including user token information.
It realizes the secure and standardized transmission of user token information in a multi-threaded environment, avoids data competition and inconsistency problems, and improves the performance, security and stability of the system.
Smart Images

Figure CN119402567A_ABST
Abstract
Description
Technical Field
[0001] The present application relates to the technical field of microservice architecture, and more specifically to an information transmission method, device and electronic device based on SpringCloud microservice. Background Art
[0002] In today's software development field, microservice architecture has become the mainstream choice for building large and complex application systems due to its high scalability, flexibility and independence. As a mature microservice development framework, SpringCloud provides developers with a wealth of components and tools to implement service registration and discovery, configuration management, load balancing, fault tolerance and other functions.
[0003] In the SpringCloud microservice architecture, user tokens (tokens, such as user name / ID and other user identity information, as well as time-limited authorization information required by the business) are usually used for user authentication. For this, the token can be carried in the Feign (a declarative HTTP client) call request to achieve token transfer between microservices. However, this method can easily expose the token, and frequent communication will also affect system performance, and it is not suitable for multi-threaded application scenarios.
[0004] Therefore, in multi-threaded application scenarios, shared cache is currently used to store user tokens to solve the problem of token transfer between microservices, but this is prone to problems such as data competition and inconsistency, which seriously affects the security and stability of the system. Summary of the invention
[0005] In view of the above problems, this application provides the following solutions:
[0006] The first aspect of the present application provides an information transmission method based on SpringCloud microservices, the method comprising:
[0007] Create a transferable local thread variable component;
[0008] Write the acquired user token information into the transmittable local thread variable component for storage; the user token information is used to verify the identity of the user who requests to call the SpringCloud microservice;
[0009] In response to a call operation on a first SpringCloud microservice, a first call request is transmitted to the first SpringCloud microservice using the user token information stored in the transmittable local thread variable component; the first call request includes the corresponding user token information.
[0010] In a possible implementation, the calling operation for the first SpringCloud microservice includes executing a task request based on the first SpringCloud microservice in a multi-threaded environment;
[0011] The first SpringCloud microservice is a SpringCloud microservice that needs to be called to implement the requested task;
[0012] The transferable local thread variable component is used to implement the transmission of specific request information between different threads in the multi-threaded environment to implement the requested execution task; the specific request information includes the user token information.
[0013] In a possible implementation, the multi-threaded environment refers to a scenario in which a parent thread and at least one child thread are created to call a SpringCloud microservice to perform multiple tasks, wherein the multiple tasks include a first task performed by the parent thread and corresponding second tasks performed by each of the child threads, and the first task and the second task are associated tasks;
[0014] In the process in which the parent thread responds to a task execution request for executing the first task based on the first SpringCloud microservice, the method further includes:
[0015] In response to the parent thread initiating the at least one child thread, passing the specific request information transmitted by the parent thread to each of the child threads;
[0016] In response to the task execution request corresponding to the second task executed based on the second SpringCloud microservice, each of the child threads transmits a second call request to the second SpringCloud microservice; the second call request includes the specific request information received from the main thread by the corresponding child thread.
[0017] In a possible implementation, the transferring of the specific request information transmitted by the parent thread to each of the child threads includes:
[0018] Acquire the specific request information transmitted by the parent thread through the transmittable local thread variable component;
[0019] The acquired specific request information is transmitted to each of the sub-threads.
[0020] In a possible implementation, the multi-threaded environment refers to a scenario in which a SpringCloud microservice is called to execute multiple tasks based on a thread reuse mechanism through a thread pool;
[0021] The using the user token information stored in the transmittable local thread variable component to transmit the first call request to the first SpringCloud microservice includes:
[0022] Submitting a third task requested to be executed to the thread pool; the third task is any one of the multiple tasks;
[0023] Determining, by means of the transmittable local thread variable component, that the current thread does not have request information for the third task, clearing information for historical tasks in the current thread; wherein the current thread refers to a thread in the thread pool for executing the third task, and the request information includes the user token information;
[0024] configuring the request information for the third task to the current thread, and obtaining a first call request including the request information;
[0025] The first call request is transmitted to the first SpringCloud microservice through the current thread to implement the third task.
[0026] In a possible implementation, the method further includes:
[0027] In response to an interface extension request for the transmittable local thread variable component, obtaining an information transfer rule for the requested extension interface; the information transfer rule indicates a transfer method for information required to execute each task based on the requested extension interface;
[0028] An extended interface supporting the information transmission rule is configured for the transmittable local thread variable component, so as to transmit information required for the task to the thread executing the task through the extended interface according to the information transmission rule.
[0029] In a possible implementation, the using the user token information stored in the transmittable local thread variable component to transmit the first call request to the first SpringCloud microservice includes:
[0030] Intercepting an initial call request generated by a client for a first SpringCloud microservice, and reading the corresponding user token information from the transmittable local thread variable component;
[0031] The read user token information is added to the initial call request, and the obtained first call request is transmitted to the first SpringCloud microservice.
[0032] In a possible implementation, the method further includes:
[0033] In the process of obtaining the user token information, capturing abnormal events;
[0034] Based on the exception handling mechanism, the abnormal event is handled, and the acquisition process of the user token information or the acquisition process corresponding to the abnormal event is re-executed.
[0035] The second aspect of the present application provides an information transmission device based on SpringCloud microservices, the device comprising:
[0036] Create a module for creating transferable local thread variable components;
[0037] A storage module is used to write the acquired user token information into the transmittable local thread variable component for storage; the user token information is used to verify the identity of the user who requests to call the SpringCloud microservice;
[0038] A transmission module is used to respond to a call operation on a first SpringCloud microservice, and transmit a first call request to the first SpringCloud microservice by using the user token information stored in the transmittable local thread variable component; the first call request includes the user token information.
[0039] A third aspect of the present application provides an electronic device, the electronic device comprising: at least one memory and at least one processor, wherein:
[0040] The memory is used to store computer program instructions;
[0041] The processor is used to load and execute the computer program instructions to implement the following steps:
[0042] Create a transferable local thread variable component;
[0043] Write the acquired user token information into the transmittable local thread variable component for storage; the user token information is used to verify the identity of the user who requests to call the SpringCloud microservice;
[0044] In response to a call operation on a first SpringCloud microservice, a first call request is transmitted to the first SpringCloud microservice using the user token information stored in the transmittable local thread variable component; the first call request includes the user token information.
[0045] It can be seen that in the information transmission method based on SpringCloud microservices proposed in this application, the created TransmittableThreadLocal component (transmittable local thread variable component) is used to store user token information, so as to realize the transmission of user token information (user Token) between SpringCloud microservices. The characteristics of the TransmittableThreadLocal component are utilized to ensure the standardization and security of user token information transmission, and it can better adapt to multi-threaded environments, avoid data competition and inconsistency, and has wider applicability and practicality. BRIEF DESCRIPTION OF THE DRAWINGS
[0046] The above and other features, advantages and aspects of the embodiments of the present disclosure will become more apparent with reference to the following detailed description in conjunction with the accompanying drawings. Throughout the accompanying drawings, the same or similar reference numerals represent the same or similar elements. It should be understood that the drawings are schematic and the originals and elements are not necessarily drawn to scale.
[0047] Figure 1 A flow chart of the information transmission method based on SpringCloud microservices proposed in Example 1 of the present application;
[0048] Figure 2 This is a flow chart of the information transmission method based on SpringCloud microservices proposed in Example 2 of the present application;
[0049] Figure 3 This is a flow chart of the information transmission method based on SpringCloud microservices proposed in Example 3 of the present application;
[0050] Figure 4 A schematic diagram of the structure of an information transmission device based on SpringCloud microservices proposed in an embodiment of the present application;
[0051] Figure 5 A schematic diagram of the hardware structure of an electronic device proposed in an embodiment of the present application. DETAILED DESCRIPTION
[0052] The embodiments of the present application are described below in conjunction with the drawings in the embodiments of the present application. The terms used in the implementation mode of the present application are only used to explain the specific embodiments of the present application and are not intended to limit the present application. The embodiments of the present application are described below in conjunction with the drawings. It is known to those of ordinary skill in the art that with the development of technology and the emergence of new scenarios, the technical solutions provided in the embodiments of the present application are also applicable to similar technical problems.
[0053] The terms "first", "second" etc. in the specification and claims of the present application and the above-mentioned drawings are used to distinguish similar objects, and need not be used to describe a specific order or sequential order. It should be understood that the terms used in this way can be interchangeable in appropriate circumstances, and this is only to describe the distinction mode adopted by the objects of the same attributes when describing in the embodiments of the present application. "If" can be interpreted as "at the time of" or "when" or "in response to determining" etc., and can be understood according to the context. In addition, the terms "including" and "having" and any of their variations are intended to cover non-exclusive inclusions, so that the process, method, product or equipment comprising a series of units need not be limited to those units, but may include other units that are not clearly listed or inherent to these processes, methods, products or equipment.
[0054] It should be understood that before using the technical solutions disclosed in the embodiments of this application, the type, scope of use, usage scenarios, etc. of the user identity information involved in this application should be informed to the user and the user's authorization should be obtained in an appropriate manner in accordance with relevant laws and regulations.
[0055] In view of the description of the background technology part, this application proposes an information transmission method based on SpringCloud microservices. In the method proposed in this application, it is proposed to use a transmittable local thread variable component (TransmittableThreadLocal) to store and transmit user tokens and other specified request information, to ensure the security and efficiency of user token transmission, and to adapt to multi-threaded environments without additional processing, and to avoid problems such as data inconsistency, user token loss or transmission errors, to ensure the performance, security and stability of the system. The following will be combined with the accompanying drawings to introduce in detail the information transmission method based on SpringCloud microservices in the embodiment of this application.
[0056] Reference Figure 1 , is a flow chart of the information transmission method based on SpringCloud microservices proposed in Example 1 of the present application. The method can be applied to electronic devices, such as Figure 1 As shown, the information transmission method based on SpringCloud microservices may include but is not limited to the following steps:
[0057] Step S11, creating a transmittable local thread variable component;
[0058] In the embodiment of the present application, in order to adapt to the processing requirements in a multi-threaded environment and provide developers with a reliable and flexible multi-threaded context information transmission solution, a transmittable local thread variable component (TransmittableThreadLocal, TTL, a Java library) that supports the information transmission function between multiple threads will be created first, and its thread pool transparency and isolation, compatibility, and automatic resource cleanup characteristics will be used to implement various tasks supported by the SpringCloud microservice architecture. This application does not limit the creation and implementation method of the TransmittableThreadLocal component.
[0059] Combined with the above description of the technical solution of this application, in order to realize the transmission of user tokens (i.e. user token information) between SpringCloud microservices, in the process of creating the TransmittableThreadLocal component, it is necessary to configure it to have the function of storing user tokens. Therefore, its configuration file may include but is not limited to the following contents:
[0060] import com.alibaba.ttl.TransmittableThreadLocal;
[0061] TransmittableThreadLocal <string>tokenStorage=newTransmittableThreadLocal<>();
[0062] It can be seen that after obtaining new transmittable local thread variables, such as updated user token information, or user token information of new users, they can be written to the TransmittableThreadLocal component in time to replace the corresponding original transmittable local thread variables, or store new transmittable local thread variables to ensure that the corresponding user can obtain the required latest transmittable local thread variables from the TransmittableThreadLocal component in the subsequent access to the SpringCloud microservice application.
[0063] Step S12, writing the acquired user token information into the transmittable local thread variable component for storage; the user token information is used to verify the identity of the user who requests to call the SpringCloud microservice;
[0064] Following the above analysis, since the TransmittableThreadLocal component created in this application is configured with the function of storing user token information, after user login or authorization is implemented in the SpringCloud microservice architecture, the user token information assigned to the user by the server is obtained, that is, the user Token, and the user token information is stored in the created TransmittableThreadLocal component, so that the TransmittableThreadLocal component can realize the Token transmission function between multiple threads.
[0065] Combined with the system composition of the SpringCloud microservice architecture, you can use username and password verification to complete user login in the authentication service, complete user authorization in the authorization service, and obtain the user token information assigned to the user, so that in the subsequent access to the SpringCloud microservice process, based on the user token information, you can authenticate the access user. This application does not limit the user login and authorization implementation methods.
[0066] In a possible implementation, combined with the configuration content of the TransmittableThreadLocal component in the above example to implement the Token storage function, the storage of user Tokens can be implemented in the following ways, but not limited to:
[0067] String token="obtained_token_value";
[0068] tokenStorage.set(token);
[0069] The above-mentioned stored token represents the acquired user token information, which may include user identity information such as user name (userName), user ID (userld), and time-limited authorization information / authentication information required to be transmitted by various businesses supported by the SpringCloud microservice architecture. This application does not restrict the content of user token information. It should be noted that the implementation of user token information in this application is legal.
[0070] Step S13, in response to the calling operation on the first SpringCloud microservice, using the user token information stored in the transmittable local thread variable component, transmit a first calling request to the first SpringCloud microservice; the first calling request includes the corresponding user token information.
[0071] In the communication between SpringCloud microservices, other SpringCloud microservices can be called through the Feign client (a declarative HTTP (Hypertext Transfer Protocol) client). In this way, in any application based on SpringCloud microservices, it is necessary to call a SpringCloud microservice (for the convenience of description, it can be recorded as the first SpringCloud microservice, that is, the microservice that implements the current task under the SpringCloud microservice architecture) to implement the current task of the application. In response to the call operation for the first SpringCloud microservice, before the Feign client sends the call request, the corresponding user token information in the TransmittableThreadLocal component is obtained, and after automatically adding it to the request header Header, the first call request containing the user token information is sent to call the first SpringCloud microservice to implement the current task.
[0072] It can be seen that this application uses the created TransmittableThreadLocal component to store user token information, realizes the transmission of user token information between SpringCloud microservices, and utilizes the characteristics of the TransmittableThreadLocal component to ensure the standardization and security of user token information transmission, and can better adapt to multi-threaded environments, avoid data competition and inconsistency, and has wider applicability and practicality.
[0073] Based on the above analysis, in the actual application of this application, in the response process of task execution requests based on SpringCloud microservices in a multi-threaded environment, the TransmittableThreadLocal component can realize the transmission of specific request information between different threads (such as user token information or other information required for task execution, etc., this application does not limit the content contained in the specific request information) to realize the requested execution task. This application does not limit the application scenarios represented by the multi-threaded environment.
[0074] Reference Figure 2 , which is a flow chart of the information transmission method based on SpringCloud microservices proposed in Example 2 of the present application. This embodiment can describe an optional implementation method of how the information transmission method based on SpringCloud microservices proposed in the present application is adapted to a multi-threaded environment, such as Figure 2 As shown, in the case where the multi-threaded environment refers to a scenario where a parent thread and at least one child thread are created to call a SpringCloud microservice to perform multiple tasks, the information transmission method based on the SpringCloud microservice proposed in this embodiment may include but is not limited to the following steps:
[0075] Step S21, creating a transmittable local thread variable component;
[0076] Step S22, writing the acquired user token information into the transmittable local thread variable component for storage; the user token information is used to verify the identity of the user who requests to call the SpringCloud microservice;
[0077] Regarding the implementation process of step S21 and step S22, reference may be made to the description of the corresponding part of the above embodiment, which will not be described in detail in this embodiment.
[0078] Step S23, responding to the task execution request based on the first SpringCloud microservice to execute the first task through the parent thread, reading the corresponding user token information stored in the transmittable local thread variable component, and transmitting the first call request containing the user token information to the first SpringCloud microservice;
[0079] Step S24, in the process of the parent thread responding to the task execution request, in response to the parent thread initiating at least one child thread, obtaining specific request information transmitted by the parent thread through the transmittable local thread variable component;
[0080] Step S25, the local thread variable component can transmit the acquired specific request information to each activated sub-thread;
[0081] In the multi-threaded environment proposed in the embodiment of the present application, the multiple tasks to be executed are recorded as the first task executed by the parent thread and the corresponding second tasks executed by each child thread. The first task and each second task are associated tasks. The content of each task can be determined based on the scenario represented by the multi-threaded environment. The present application does not impose any restrictions on the content of the tasks executed by the parent and child threads respectively.
[0082] Based on this, in order to enhance the transmission mechanism between parent and child threads, for asynchronous tasks in a multi-threaded environment or tasks that need to be executed in a thread pool, in scenarios where specific request information such as user Tokens need to be passed between tasks, this application uses the information transmission mechanism configured by the created TransmittableThreadLocal component to pass the specific request information in the parent thread to each child thread called by the parent thread without adding additional processing, automatically ensuring the correct transmission and inheritance of specific request information.
[0083] For example, in a Web application, when the parent thread needs to respond to the task execution request, some specific request information such as user information can be set in the parent thread, such as by calling the set method of the TransmittableThreadLocal component, setting the specific request information in the parent thread and storing it in the TransmittableThreadLocal component. In this way, when subsequent asynchronous tasks are executed in the child thread, if the parent thread creates and starts at least one child thread for executing asynchronous tasks, the TransmittableThreadLocal component can be used to pass the specific request information of the main thread to the child thread, so that each started child thread can directly obtain the specific request information set by the parent thread through the get method of the TransmittableThreadLocal component.
[0084] It can be seen that during the execution of the first task by the parent thread, the stored user token information can be obtained from the TransmittableThreadLocal component, added to the request header of the first SpringCloud microservice called by this request, and the obtained first call request with the user token information is transmitted to the first SpringCloud microservice to realize the user identity authentication for the first task executed by the parent thread. If the parent thread needs to execute the second task asynchronously during the execution of the first task, according to the method described above, the parent thread will call at least one child thread to implement it, and use the TransmittableThreadLocal component to transmit the specific request information configured by the main thread in response to the task execution request, such as the user Token in the request header of the first call request transmitted by the main thread, to the child thread securely and completely, ensuring the accuracy and consistency of the transmission of specific request information in the parent and child threads, and improving the stability and reliability of the system in high concurrency scenarios.
[0085] Step S26, in response to the task execution request based on the second SpringCloud microservice to execute the corresponding second task, each child thread transmits a second call request to the second SpringCloud microservice; the second call request includes specific request information received by the corresponding child thread from the main thread.
[0086] Following the above analysis, each child thread started by the parent thread directly transmits the specific request information from the parent thread through the TransmittableThreadLocal component. In the process of executing the corresponding asynchronous task, which is recorded as the second task, the specific request information is added to the request header of the second SpringCloud microservice required to execute the second task. The second call request containing the specific request information is transmitted to the corresponding second SpringCloud microservice to realize the asynchronous task.
[0087] It can be seen that in the embodiment of the present application, the created TransmittableThreadLocal component (also referred to as an object) allows the child thread to inherit the value (i.e., the value variable) in the parent thread, such as the above-mentioned user Token and other specific request information, which solves the problem of Token data sharing and transmission between parent and child threads in a multi-threaded environment, and avoids the technical problem that the child thread cannot directly obtain the specific request information set by the parent thread, resulting in inconsistent data and competition between the parent and child threads, which affects the reliability and stability of the system.
[0088] In some other embodiments proposed in the present application, the present application can also use the created TransmittableThreadLocal component to ensure the correct transmission of Token information in a multi-threaded scenario based on a thread pool. In the scenario where the thread pool calls the SpringCloud microservice based on the thread reuse mechanism to execute multiple tasks, it can ensure that each task can correctly obtain its own context information during execution without being affected by previous tasks.
[0089] Based on this, refer to Figure 3 The flowchart of the information transmission method based on SpringCloud microservices proposed in the third embodiment of the present application is shown. This embodiment describes how to respond to the call operation of the first SpringCloud microservice based on the thread reuse mechanism. For other steps of the information transmission method based on SpringCloud microservices, please refer to the description of the corresponding part of the context embodiment. Figure 3 As shown, the method may include:
[0090] Step S31, submitting the third task requested to be executed to the thread pool;
[0091] In an embodiment of the present application, in a scenario where a SpringCloud microservice is called to execute multiple tasks based on a thread reuse mechanism through a thread pool, for the convenience of description, the task executed by the thread pool is recorded as a third task, that is, any task of the multiple tasks that need to be executed in this scenario, and the thread executing the third task in the thread pool is recorded as the current thread.
[0092] Step S32, determining that the current thread does not have request information for the third task through the created transmittable local thread variable component, and clearing the information for the historical task in the current thread; the request information includes user token information;
[0093] Step S33, configuring the request information for the third task to the current thread, and obtaining a first call request including the request information;
[0094] Step S34: transmit the first call request to the first SpringCloud microservice through the current thread to implement the third task.
[0095] In an embodiment of the present application, when a third task is submitted to the thread pool, the TransmittableThreadLocal component will detect whether the current thread is configured with request information (such as value variables, user Token, etc.) before the third task is executed. If so, it will further determine whether it belongs to the request information of the third task submitted this time. If so, the third task will continue to be executed; if not, it will clear all the information generated by the current thread's execution of historical tasks, that is, the residual value in the current scene, and then configure the request information for the third task to the current thread to reuse the current thread to implement the third task.
[0096] It can be seen that in the embodiments of the present application, in the process of threads in the reused thread pool executing tasks, it can be ensured that each task executed by the thread pool can obtain correct context information, avoid being affected by the tasks previously executed by the reused thread, and ensure the reliability and accuracy of the thread pool executing each task.
[0097] In some other embodiments proposed in the present application, it is also possible to implement extended functions based on the TransmittableThreadLocal component to meet the customized requirements of certain application scenarios, such as implementing a customized information transmission strategy to determine which information needs to be transmitted and which does not need to be transmitted according to business / task processing requirements. Based on this, in response to an interface extension request for a transmittable local thread variable component, an information transmission rule for the requested extension interface is obtained, and the information transmission rule indicates the transmission method for the information required to execute each task based on the requested extension interface. The transmittable local thread variable component is thus configured with an extended interface that supports the information transmission rule, so that the information required for the task, such as the specific request information in the multi-threaded environment described above, can be transmitted to the thread executing the task through the extended interface in accordance with the information transmission rule.
[0098] For example, in a financial transaction system (implemented based on the SpringCloud microservice architecture), different context information delivery methods can be customized according to the type and stage of the transaction to ensure that in complex transaction processes (such as multiple tasks executed by different threads), threads in each link can correctly obtain the required information, such as using the TransmittableThreadLocal component to pass the information required for the thread to execute the task to the thread in accordance with but not limited to the method described above, and the thread uses the received information to execute the task. The implementation process is not described in detail in this application.
[0099] In the information transmission method based on SpringCloud microservices proposed in the above-mentioned embodiments of the present application, for each thread executing the requested task, the Feign client automatically adds the user token information used to implement user identity authentication to the request header of the call request before sending a call request to the SpringCloud microservice that implements the task. The initial call request can be intercepted by the interceptor configured in the Feign client, and the corresponding user token information in the TransmittableThreadLocal component is added to the request header of the initial call request, and then the obtained call request is sent to the corresponding SpringCloud microservice.
[0100] Based on this, in the response process of the call operation for the first SpringCloud microservice, the initial call request generated by the client (i.e., the Feign client) for the first SpringCloud microservice can be intercepted, and the corresponding user token information can be read from the transmittable local thread variable component (TransmittableThreadLocal component), and the read user token information is added to the initial call request, and then the obtained first call request is transmitted to the first SpringCloud microservice.
[0101] It can be seen that this application uses the Feign interceptor to realize the automatic addition of Token when communicating between microservices. Through this global interception method, the request header information of the current SpringCloud microservice is automatically set to the request header of the call request to be sent by the Feign client. There is no need for developers to manually write request headers, which reduces the manual operations of developers, improves development efficiency, and ensures the standardization and security of Token transmission.
[0102] In some other embodiments, in the information transmission method based on SpringCloud microservices proposed in various embodiments of the present application, the exceptions that occur during the acquisition and setting of the user token can be handled based on the exception handling mechanism to ensure that any unexpected exceptions can be properly handled in a timely manner and will not cause the application to crash. Therefore, in the process of acquiring user token information (user token), the exception event is captured, such as using a try-catch block in a programming language (java) to capture any exception event. After that, based on the exception handling mechanism, after handling the exception event, the acquisition process or the acquisition process corresponding to the exception event is re-executed.
[0103] Of course, in other implementation processes of the method proposed in the embodiments of the present application, if corresponding abnormal events occur, such as user authentication failure, invalid or expired user Token, user Token format error, network and other factors leading to failure to obtain user Token, and errors in adding user Token to the request header, etc., one or more of the abnormal events can be handled based on the exception handling mechanism to improve the robustness of the application.
[0104] The above introduces the information transmission method based on SpringCloud microservices provided in the embodiment of the present application. The following will introduce a device for executing the above-mentioned information transmission method based on SpringCloud microservices.
[0105] Reference Figure 4 , which is a schematic diagram of the structure of the information transmission device based on SpringCloud microservices proposed in the embodiment of the present application, such as Figure 4 As shown, the information transmission device based on SpringCloud microservice may include:
[0106] A creation module 410, for creating a transferable local thread variable component;
[0107] The storage module 420 is used to write the acquired user token information into the transmittable local thread variable component for storage; the user token information is used to verify the identity of the user who requests to call the SpringCloud microservice.
[0108] The transmission module 430 is used to respond to the call operation on the first SpringCloud microservice, and transmit a first call request to the first SpringCloud microservice using the user token information stored in the transmittable local thread variable component; the first call request includes the user token information.
[0109] In some embodiments, if the above-mentioned calling operation for the first SpringCloud microservice includes a task execution request based on the first SpringCloud microservice in a multi-threaded environment, the first SpringCloud microservice is the SpringCloud microservice that needs to be called to implement the requested task. The specific request information (which may include user token information) can be transmitted between different threads in the multi-threaded environment through a transmittable local thread variable component to implement the requested task, ensure the accuracy and consistency of user token information transmission in the multi-threaded environment, and improve the stability and reliability of the system in high concurrency scenarios.
[0110] In a possible implementation, if the multi-threaded environment refers to a scenario in which a parent thread and at least one child thread are created to call a SpringCloud microservice to perform multiple tasks, the multiple tasks include a first task performed by the parent thread and corresponding second tasks performed by each of the child threads, and the first task and the second task are associated tasks, the transmission module 430 may further include:
[0111] A specific request information transmission module, configured to transmit the specific request information transmitted by the parent thread to each of the child threads in response to the parent thread invoking the at least one child thread;
[0112] The second call request transmission module is used to transmit a second call request to the second SpringCloud microservice in response to a task execution request corresponding to the second task based on the second SpringCloud microservice through each of the child threads; the second call request includes the specific request information received from the main thread by the corresponding child thread.
[0113] Optionally, the specific request information delivery module may include:
[0114] A specific request information acquisition unit, configured to acquire the specific request information transmitted by the parent thread through the transmittable local thread variable component;
[0115] The specific request information transmission unit is used to transmit the acquired specific request information to each of the sub-threads.
[0116] In another possible implementation, if the multi-threaded environment refers to a scenario in which a SpringCloud microservice is called to execute multiple tasks based on a thread reuse mechanism through a thread pool, the transmission module 430 may include:
[0117] A task submitting unit, configured to submit a third task requested for execution to the thread pool; the third task is any one of the multiple tasks;
[0118] an information clearing unit, configured to determine, through the transmittable local thread variable component, that the current thread does not have request information for the third task, and clear information for the historical task in the current thread; wherein the current thread refers to a thread in the thread pool for executing the third task, and the request information includes the user token information;
[0119] A current thread configuration unit, configured to configure the request information for the third task to the current thread, and obtain a first call request including the request information;
[0120] The first call request transmission unit is used to transmit the first call request to the first SpringCloud microservice through the current thread to achieve the third task.
[0121] In some embodiments, the information transmission device based on SpringCloud microservices proposed in this application may also include:
[0122] An information transmission rule obtaining module, configured to obtain, in response to an interface extension request for the transmittable local thread variable component, an information transmission rule for the requested extension interface; the information transmission rule indicates a transmission method for information required to execute each task based on the requested extension interface;
[0123] The extended interface configuration module is used to configure an extended interface supporting the information transmission rule for the transmittable local thread variable component, so as to transmit the information required for the task to the thread executing the task through the extended interface according to the information transmission rule.
[0124] In some further embodiments, the transmission module 430 may include:
[0125] An interception unit, configured to intercept an initial call request generated by a client for a first SpringCloud microservice, and read the corresponding user token information from the transmittable local thread variable component;
[0126] The user token information adding unit is used to add the read user token information to the initial call request, and transmit the obtained first call request to the first SpringCloud microservice.
[0127] In some embodiments, the information transmission device based on SpringCloud microservices proposed in this application may also include:
[0128] An abnormal event capture module is used to capture abnormal events during the acquisition of the user token information;
[0129] The exception handling module is used to handle the abnormal event based on the exception handling mechanism, and re-execute the acquisition process of the user token information or the acquisition process corresponding to the abnormal event.
[0130] An embodiment of the present application also provides a computer program product, including computer-readable instructions. When the computer-readable instructions are executed on an electronic device, the electronic device implements any one of the information transmission methods based on SpringCloud microservices provided in the embodiments of the present application.
[0131] Wherein, when loading and executing computer-readable instructions on a computer, the process or function described in accordance with the embodiment of the present application is generated in whole or in part. The computer may be a general-purpose computer, a special-purpose computer, a computer network, or other programmable devices. Computer-readable instructions may be stored in a computer-readable storage medium, or transmitted from one computer-readable storage medium to another computer-readable storage medium. For example, computer-readable instructions may be transmitted from a website site, a computer, a training device, or a data center by wired (e.g., coaxial cable, optical fiber, digital subscriber line (DSL)) or wireless (e.g., infrared, wireless, microwave, etc.) mode to another website site, computer, training device, or data center.
[0132] Reference Figure 5 , is a schematic diagram of the hardware structure of an electronic device proposed in an embodiment of the present application. The electronic device may be a user terminal such as a mobile phone, a tablet computer, a wearable device, an augmented reality (AR) / virtual reality (VR) device, a laptop computer, a netbook or a robot. The present application does not limit the product form of the electronic device. Figure 5 As shown, under the SpringCloud microservice architecture, the electronic device may include at least one memory 510 and at least one processor 520, wherein:
[0133] At least one memory 510 and at least one processor 520 can communicate with each other via a bus. The bus can be a peripheral component interconnect (PCI) bus or an extended industry standard architecture (EISA) bus. The bus can be divided into an address bus, a data bus, a control bus, etc. For ease of representation, Figure 5 Only one bidirectional line is used in the diagram, but this does not mean that there is only one bus or only one type of bus.
[0134] The memory 510 can be used to store computer instructions and data, and may include an instruction storage area and a data storage area. The data storage area can store various data, such as user token information, various intermediate data generated during the execution of the information transmission method based on SpringCloud microservices, etc.; the instruction storage area can store software units such as operating systems, applications, and computer instructions required for at least one function, etc. In an embodiment of the present application, the memory 510 can provide the processor 520 with computer program instructions for implementing the information transmission method based on SpringCloud microservices in an embodiment of the present application, as well as data or other software and hardware resources required during the execution of the information transmission method based on SpringCloud microservices.
[0135] The processor 520, as the control center of the electronic device, can use various interfaces and lines to connect various parts of the entire electronic device, and implement the various steps of the information transmission method based on SpringCloud microservices proposed in the embodiment of the present application by loading and executing the computer program instructions of the memory 510. The implementation process can refer to the description of the corresponding part of the method embodiment above. Other components of the electronic device (such as communication elements, one or more input components and output components, etc.) can also be called to implement corresponding functions.
[0136] In the embodiment of the present application, the memory 510 may include a volatile memory (volatile memory), such as a random access memory (RAM), and may also include a non-volatile memory (non-volatile memory), such as a read-only memory (ROM), a flash memory, a hard drive drive (HDD) or a solid state drive (SSD), etc. The processor 520 may include any one or more of a central processing unit (CPU), a graphics processing unit (GPU), a microprocessor (MP), a digital signal processor (DSP), an application specific integrated circuit (ASIC) or a field-programmable gate array (FPGA).
[0137] In actual applications, the electronic device may also include a first communication element for realizing communication between various components inside the electronic device, which may include but is not limited to: a general-purpose input / output (GPIO) interface, a USB interface, a universal asynchronous receiver / transmitter (UART) interface, etc.; it may also include a second communication element for realizing communication between the electronic device and an external device (such as an external display device or a server, etc.), which may include but is not limited to: a wireless communication element that supports wireless communication methods such as wifi, 5G / 6G, Global System of Mobile communication (GSM), General Packet Radio Service (GPRS) and / or near-field channel methods.
[0138] It should be understood that Figure 5 The structure of the electronic device shown does not constitute a limitation on the electronic device in the embodiment of the present application. In practical applications, the electronic device may include Figure 5 More or fewer components as shown, or combinations of certain components, such as various sensors, display screens, audio players, audio collectors, power supply components, radio frequency units, and other input / output components, etc., are not listed one by one in this application.
[0139] A computer-readable storage medium is also provided in an embodiment of the present application. The storage medium carries one or more computer programs. When one or more computer programs are executed by an electronic device, the electronic device can implement any one of the information transmission methods based on SpringCloud microservices provided in an embodiment of the present application.
[0140] The computer-readable storage medium may be any available medium that can be stored by a computer or a data storage device such as a training device or a data center that includes one or more available media. The available medium may be a magnetic medium (e.g., a floppy disk, a hard disk, a tape), an optical medium (e.g., a DVD), or a semiconductor medium (e.g., a solid-state disk (SSD)).
[0141] It should also be noted that the device embodiments described above are merely schematic, wherein the units described as separate components may or may not be physically separated, and the components displayed as units may or may not be physical units, that is, they may be located in one place, or they may be distributed over multiple network units. Some or all of the modules may be selected according to actual needs to achieve the purpose of the scheme of this embodiment. In addition, in the drawings of the device embodiments provided by the present application, the connection relationship between the modules indicates that there is a communication connection between them, which may be specifically implemented as one or more communication buses or signal lines.
[0142] Through the description of the above implementation mode, the technicians in the field can clearly understand that the present application can be implemented by means of software plus necessary general hardware, and of course, it can also be implemented by special hardware including special integrated circuits, special CPUs, special memories, special components, etc. In general, all functions completed by computer programs can be easily implemented by corresponding hardware, and the specific hardware structure used to implement the same function can also be various, such as analog circuits, digital circuits or special circuits. However, for the present application, software program implementation is a better implementation mode in more cases. Based on such an understanding, the technical solution of the present application is essentially or the part that contributes to the prior art can be embodied in the form of a software product, which is stored in a readable storage medium, such as a computer floppy disk, a U disk, a mobile hard disk, a ROM, a RAM, a disk or an optical disk, etc., including a number of instructions to enable a computer device (which can be a personal computer, a training device, or a network device, etc.) to execute the methods described in each embodiment of the present application.
[0143] In the above embodiments, all or part of the embodiments can be implemented by software, hardware, firmware or any combination thereof. When implemented by software, all or part of the embodiments can be implemented in the form of a computer program product. In this specification, each embodiment is described in a progressive manner, and each embodiment focuses on the differences from other embodiments. The same and similar parts between the embodiments can be referred to each other. For the device and electronic device disclosed in the embodiment, since it corresponds to the method disclosed in the embodiment, the description is relatively simple, and the relevant parts can be referred to the method part description.< / string>
Claims
1. An information transmission method based on SpringCloud microservices, characterized in that: The method comprises: Create a transferable local thread variable component; Write the acquired user token information into the transmittable local thread variable component for storage; the user token information is used to verify the identity of the user who requests to call the SpringCloud microservice; In response to a call operation on a first SpringCloud microservice, a first call request is transmitted to the first SpringCloud microservice using the user token information stored in the transmittable local thread variable component; the first call request includes the corresponding user token information.
2. The method according to claim 1, characterized in that The calling operation for the first SpringCloud microservice includes a task execution request based on the first SpringCloud microservice in a multi-threaded environment; The first SpringCloud microservice is a SpringCloud microservice that needs to be called to implement the requested task; The transferable local thread variable component is used to implement the transmission of specific request information between different threads in the multi-threaded environment to implement the requested execution task; the specific request information includes the user token information.
3. The method according to claim 2, characterized in that The multi-threaded environment refers to a scenario in which a parent thread and at least one child thread are created to call a SpringCloud microservice to perform multiple tasks, wherein the multiple tasks include a first task performed by the parent thread and corresponding second tasks performed by each of the child threads, and the first task and the second task are associated tasks; In the process in which the parent thread responds to a task execution request for executing the first task based on the first SpringCloud microservice, the method further includes: In response to the parent thread initiating the at least one child thread, passing the specific request information transmitted by the parent thread to each of the child threads; In response to the task execution request corresponding to the second task executed based on the second SpringCloud microservice, each of the child threads transmits a second call request to the second SpringCloud microservice; the second call request includes the specific request information received from the main thread by the corresponding child thread.
4. The method according to claim 3, characterized in that The transmitting the specific request information transmitted by the parent thread to each of the child threads includes: Acquire the specific request information transmitted by the parent thread through the transmittable local thread variable component; The acquired specific request information is transmitted to each of the sub-threads.
5. The method according to claim 2, characterized in that: The multi-threaded environment refers to a scenario where SpringCloud microservices are called to execute multiple tasks based on a thread reuse mechanism through a thread pool; The using the user token information stored in the transmittable local thread variable component to transmit the first call request to the first SpringCloud microservice includes: Submitting a third task requested to be executed to the thread pool; the third task is any one of the multiple tasks; Determining, by means of the transmittable local thread variable component, that the current thread does not have request information for the third task, clearing information for historical tasks in the current thread; wherein the current thread refers to a thread in the thread pool for executing the third task, and the request information includes the user token information; configuring the request information for the third task to the current thread, and obtaining a first call request including the request information; The first call request is transmitted to the first SpringCloud microservice through the current thread to implement the third task.
6. The method according to claim 2, characterized in that The method further comprises: In response to an interface extension request for the transmittable local thread variable component, obtaining an information transfer rule for the requested extension interface; the information transfer rule indicates a transfer method for information required to execute each task based on the requested extension interface; An extended interface supporting the information transmission rule is configured for the transmittable local thread variable component, so as to transmit information required for the task to the thread executing the task through the extended interface according to the information transmission rule.
7. The method according to any one of claims 1 to 6, characterized in that: The using the user token information stored in the transmittable local thread variable component to transmit the first call request to the first SpringCloud microservice includes: Intercepting an initial call request generated by a client for a first SpringCloud microservice, and reading the corresponding user token information from the transmittable local thread variable component; The read user token information is added to the initial call request, and the obtained first call request is transmitted to the first SpringCloud microservice.
8. The method according to any one of claims 1 to 6, characterized in that: The method further comprises: In the process of obtaining the user token information, capturing abnormal events; Based on the exception handling mechanism, the abnormal event is handled, and the acquisition process of the user token information or the acquisition process corresponding to the abnormal event is re-executed.
9. An information transmission device based on SpringCloud microservices, characterized in that: The device comprises: Create a module for creating transferable local thread variable components; A storage module is used to write the acquired user token information into the transmittable local thread variable component for storage; the user token information is used to verify the identity of the user who requests to call the SpringCloud microservice; A transmission module is used to respond to a call operation on a first SpringCloud microservice, and transmit a first call request to the first SpringCloud microservice by using the user token information stored in the transmittable local thread variable component; the first call request includes the user token information.
10. An electronic device, characterized in that: The electronic device comprises: at least one memory and at least one processor, wherein: The memory is used to store computer program instructions; The processor is used to load and execute the computer program instructions to implement the following steps: Create a transferable local thread variable component; Write the acquired user token information into the transmittable local thread variable component for storage; the user token information is used to verify the identity of the user who requests to call the SpringCloud microservice; In response to a call operation on a first SpringCloud microservice, a first call request is transmitted to the first SpringCloud microservice using the user token information stored in the transmittable local thread variable component; the first call request includes the user token information.
Citation Information
Patent Citations
Micro-service room access management method, equipment, device and storage medium
CN113742714A
Method and device for transmitting information between father and son threads, equipment and medium
CN114564320A
System and method for calling micro service
CN115865399A
Microservice update system
US20200348921A1
Microservice invoking method and apparatus, device and medium
WO2021179841A1