CN117742991A - Method and device for acquiring user information by micro-service and electronic equipment - Google Patents

Method and device for acquiring user information by micro-service and electronic equipment Download PDF

Info

Publication number
CN117742991A
CN117742991A CN202311704633.6A CN202311704633A CN117742991A CN 117742991 A CN117742991 A CN 117742991A CN 202311704633 A CN202311704633 A CN 202311704633A CN 117742991 A CN117742991 A CN 117742991A
Authority
CN
China
Prior art keywords
service
thread
information
micro
context information
Prior art date
Legal status (The legal status is an assumption and is not a legal conclusion. Google has not performed a legal analysis and makes no representation as to the accuracy of the status listed.)
Pending
Application number
CN202311704633.6A
Other languages
Chinese (zh)
Inventor
池祥
Current Assignee (The listed assignees may be inaccurate. Google has not performed a legal analysis and makes no representation or warranty as to the accuracy of the list.)
Tianyi Safety Technology Co Ltd
Original Assignee
Tianyi Safety Technology Co Ltd
Priority date (The priority date is an assumption and is not a legal conclusion. Google has not performed a legal analysis and makes no representation as to the accuracy of the date listed.)
Filing date
Publication date
Application filed by Tianyi Safety Technology Co Ltd filed Critical Tianyi Safety Technology Co Ltd
Priority to CN202311704633.6A priority Critical patent/CN117742991A/en
Publication of CN117742991A publication Critical patent/CN117742991A/en
Pending legal-status Critical Current

Links

Landscapes

  • Information Transfer Between Computers (AREA)

Abstract

The present invention relates to the field of computer internet application technology architecture, and in particular, to a method, an apparatus, and an electronic device for obtaining user information by a micro service. According to the method, through the first service, the context information containing the user information and used by the first thread in the first service is obtained, and corresponding data information is generated based on the context information; transmitting the data information to a second service; acquiring context information based on the received data information by a second thread used by a second service, and binding the context information with the second thread; in response to the operation of acquiring the user information, the user information is acquired from the context information through the second thread, so as to provide a method for acquiring the user information by the micro service, which avoids causing code intrusion.

Description

Method and device for acquiring user information by micro-service and electronic equipment
Technical Field
The present invention relates to the field of computer internet application technology architecture, and in particular, to a method, an apparatus, and an electronic device for obtaining user information by a micro service.
Background
Under the current evolution of architecture, due to the rapid development of services, complex single applications are generally split into multiple independent services, each of which is dedicated to performing a specific service function, and these services can be deployed, extended and maintained independently by API communication.
Thus, due to business logic, each individual service may have a need to obtain current login user information.
In the related art, when a front-end user carries an access token (token), the token is passed to a back-end business system. Each micro-service in the business system can carry out self-defined annotation on each micro-service application interface, and the self-defined annotation is packed into a common jar package. When each micro service needs to acquire the user information, the common jar packet is introduced, and the token is decrypted by using the annotation in the common jar packet, so that the micro service acquires the user information from the token.
However, in the process of carrying out custom annotation on the micro-service application interface, the related art modifies codes in the service system, so that the invasion of the codes is caused, and the normal operation of the service system may be affected.
Disclosure of Invention
The embodiment of the application provides a method, a device and electronic equipment for acquiring user information by micro-services, which are used for providing a method for acquiring user information by micro-services for avoiding code invasion.
In a first aspect, an embodiment of the present application provides a method for obtaining user information by a micro service, where the method includes:
Acquiring context information containing user information used by a first thread in a first service through the first service, and generating corresponding data information based on the context information; transmitting the data information to a second service;
acquiring the context information based on the received data information through a second thread used by the second service, and binding the context information with the second thread; the second thread is used for processing a user request associated with the data information;
and responding to the operation of acquiring the user information, and acquiring the user information from the context information through the second thread.
According to the scheme, the context information containing the user information is transmitted to the second service, so that code invasion can be avoided, compared with the process that the second service needs to acquire relevant notes from the jar package and decrypt the token in the related technology, the second service in the application can acquire the user information based on the received context information, steps executed in the process that the micro service acquires the user information are reduced, the efficiency of acquiring the user information by the micro service is improved, and the speed of responding the user by the micro service is further improved.
In some optional embodiments, the generating the corresponding data information based on the context information includes:
carrying out serialization processing on the context information through the first service, and assembling the context information after the serialization processing to obtain the data information;
the obtaining, by the second thread used by the second service, the context information includes:
and performing deserialization processing on the context information after the serialization processing in the received data information through a second thread used by the second service to obtain the context information in the data information.
In some optional embodiments, if the first service is a micro service gateway, the second service is a first micro service;
before the obtaining, by the first service, the context information including the user information used by the first thread in the first service, the method further includes:
responding to the operation of a target user, and acquiring user information of the target user from an operation request of the target user through the micro service gateway;
placing the user information in context information of a first thread used by the micro service gateway through the micro service gateway; the first thread is a thread used by the micro-service gateway to process the operation request.
In some alternative embodiments, if the first service is a first micro service, the second service is a second micro service;
then before the data information is sent to the second service, further comprising:
a second micro-service is determined from at least one micro-service other than the first micro-service based on the request link through the first service.
In some optional embodiments, if after the data information is sent to the second service, before the second thread used by the second service obtains the context information based on the received data information, the method further includes: the second service processes a user request associated with the data information using an initial thread;
then after said sending said data information to a second service and before said obtaining said context information based on said received data information by said second thread used by said second service, further comprising:
acquiring the context information from the received data information through an initial thread used by the second service, and binding the context information with the initial thread;
processing the user request by the second thread when the initial thread forwards the user request to the second thread;
The second thread used by the second service obtains the context information based on the received data information, including:
copying the context information bound by the initial thread through a second thread used by the second service.
In some alternative embodiments, after the second thread detects that the thread pool triggers a user request for a handover operation for the initial thread, the second thread schedules at least one thread in the thread pool for determining based on the user request.
In a second aspect, an embodiment of the present application provides an apparatus for a micro service to obtain user information, where the apparatus includes:
the generating device is used for acquiring context information containing user information used by a first thread in the first service through the first service, and generating corresponding data information based on the context information; transmitting the data information to a second service;
binding means for obtaining, by a second thread used by the second service, the context information based on the received data information, and binding the context information with the second thread; the second thread is used for processing a user request associated with the data information;
And the acquisition device is used for responding to the operation of acquiring the user information and acquiring the user information from the context information through the second thread.
In some optional embodiments, the generating device is specifically configured to:
carrying out serialization processing on the context information through a first service, and assembling the context information after the serialization processing to obtain the data information;
the binding means are specifically for:
and performing deserialization processing on the context information after the serialization processing in the received data information through a second thread used by the second service to obtain the context information in the data information.
In some optional embodiments, if the first service is a micro service gateway, the second service is a first micro service; the generating means are further for:
responding to the operation of a target user, and acquiring user information of the target user from an operation request of the target user through the micro service gateway;
placing the user information in context information of a first thread used by the micro service gateway through the micro service gateway; the first thread is a thread used by the micro-service gateway to process the operation request.
In some alternative embodiments, if the first service is a first micro service, the second service is a second micro service; the generating means are further for:
a second micro-service is determined from at least one micro-service other than the first micro-service based on the request link through the first service.
In some optional embodiments, if after the data information is sent to the second service, before the second thread used by the second service obtains the context information based on the received data information, the method further includes: the second service processes a user request associated with the data information using an initial thread; the binding means is further for:
acquiring the context information from the received data information through an initial thread used by the second service, and binding the context information with the initial thread;
processing the user request by the second thread when the initial thread forwards the user request to the second thread;
the binding means are specifically for:
copying the context information bound by the initial thread through a second thread used by the second service.
In some alternative embodiments, after the second thread detects that the thread pool triggers a user request for a handover operation for the initial thread, the second thread schedules at least one thread in the thread pool for determining based on the user request.
In a third aspect, an embodiment of the present application provides an electronic device, including at least one processor and at least one memory, where the memory stores a computer program that, when executed by the processor, causes the processor to perform the steps of any of the methods of the first aspect.
In a fourth aspect, embodiments of the present application provide a computer readable storage medium storing a computer program executable by a processor, the program when run on the processor causing the processor to perform the steps of any of the methods of the first aspect described above.
In a fifth aspect, embodiments of the present application provide a computer program product comprising a computer program stored in a computer readable storage medium; when the computer program is read from the computer readable storage medium by a processor of an electronic device, the processor executes the computer program, causing the electronic device to perform the steps of any of the methods of the first aspect.
Drawings
In order to more clearly illustrate the technical solutions of the embodiments of the present application, the drawings that are needed in the description of the embodiments will be briefly described below, it being obvious that the drawings in the following description are only some embodiments of the present application, and that other drawings may be obtained according to these drawings without inventive effort for a person skilled in the art.
Fig. 1 is a schematic view of an application scenario provided in an embodiment of the present application;
FIG. 2 is a flowchart of a method for a micro service to obtain user information according to an embodiment of the present application;
fig. 3 is a flowchart of a first micro service for obtaining user information according to an embodiment of the present application;
fig. 4 is a flowchart of a first micro service for obtaining user information according to an embodiment of the present application;
fig. 5 is a flowchart of a second micro service for obtaining user information according to an embodiment of the present application;
fig. 6 is a flowchart of a second micro service for obtaining user information according to an embodiment of the present application;
FIG. 7 is a flowchart of an overall implementation provided in an embodiment of the present application;
fig. 8 is an overall schematic diagram of obtaining user information according to an embodiment of the present application;
FIG. 9 is a schematic diagram of a thread switch according to an embodiment of the present disclosure;
fig. 10 is a schematic diagram of an apparatus for acquiring user information by a micro service according to an embodiment of the present application;
fig. 11 is a schematic diagram of an electronic device according to an embodiment of the present application;
fig. 12 is a schematic diagram of an electronic device according to an embodiment of the present application.
Detailed Description
For the purpose of promoting an understanding of the principles and advantages of this application, reference will now be made in detail to the drawings, in which it is apparent that the embodiments described are only some, but not all embodiments of the application. All other embodiments, which can be made by one of ordinary skill in the art without undue burden from the present disclosure, are within the scope of the present disclosure.
The terms "first," "second," and the like, are used for descriptive purposes only and are not to be construed as indicating or implying relative importance or implicitly indicating the number of technical features indicated. Thus, a feature defining "a first" or "a second" may explicitly or implicitly include one or more such feature. In the description of the present application, unless otherwise indicated, the meaning of "a plurality" is two or more.
In the description of the present application, it should be noted that, unless explicitly stated and limited otherwise, the term "connected" should be interpreted broadly, for example, as being directly connected, or indirectly connected through an intermediate medium, or as being in communication with the inside of two devices. The specific meaning of the terms in this application will be understood by those of ordinary skill in the art as the case may be.
In order to facilitate understanding of the method, the device and the electronic equipment for acquiring user information by the micro service provided in the embodiments of the present application, a part of terms in the embodiments of the present application are explained below to facilitate understanding by those skilled in the art.
1. Request link: the micro-service receives the request from the portal until the request responds to the result, and a plurality of micro-service sequences are passed through in the whole request processing process. The micro-service entity through which the request passes can be imagined as a node, and the request path is imagined as an edge, so that the whole request process can be abstracted into a directed acyclic graph, namely, a request link of a user.
2. Thread switching: thread switching refers to the process by which an operating system or thread scheduler transfers the execution rights of a CPU from one thread to another in multi-threaded programming. Thread switching allows multiple threads to share CPU resources to achieve concurrent execution. The purpose of thread switching is to ensure that each thread is able to acquire CPU time in a relatively fair amount of time, thereby enabling concurrent execution of multiple tasks.
3. Thread context loss (Thread Context Switch): thread context loss refers to the situation that when an operating system needs to switch to execute different threads under a multithreading environment, some data may be lost or damaged due to the need to save the context information of the current thread and load the context information of the next thread.
In order to make the objects, technical solutions and advantageous effects of the present application more apparent, the present application will be further described in detail with reference to the accompanying drawings and examples. It should be understood that the specific embodiments described herein are for purposes of illustration only and are not intended to limit the present application.
The method for acquiring user information by using the micro-service in the embodiment of the present application may be applied to a server, as shown in fig. 1, in which an application scenario is shown in the embodiment of the present application, after a target user triggers an operation by using a terminal device 10, the terminal device 10 sends an operation request to a server 11, and the server 11 includes a micro-service gateway and a plurality of micro-services (e.g., a first micro-service and a second micro-service shown in fig. 1).
It should be noted that the present application does not limit the number of micro services.
Wherein a wired transmission and/or a wireless transmission can be performed between the terminal device 10 and the server 11.
In the embodiment of the present application, the terminal device 10 is an electronic device used by a user, and the electronic device may be a personal computer, a mobile phone, a tablet computer, a notebook, an intelligent voice interaction device, a medical image device, or a computer device with a certain computing capability.
In this embodiment of the present application, the server 11 may be an independent physical server, or may be a server cluster or a distributed system formed by a plurality of physical servers, or may be a cloud server that provides cloud services, cloud databases, cloud computing, cloud storage, cloud functions, network services, cloud communication, middleware services, domain name services, security services, a content delivery network (Content Delivery Network, CDN), and basic cloud computing services such as big data and an artificial intelligent platform.
Of course, the method provided in the embodiment of the present application is not limited to the application scenario shown in fig. 1, but may be used in other possible application scenarios, and the embodiment of the present application is not limited thereto.
As shown in fig. 2, a flowchart of a method for obtaining user information by a micro service according to an embodiment of the present application is implemented, and specific steps are as follows:
step S201, obtaining context information containing user information used by a first thread in a first service through the first service, and generating corresponding data information based on the context information;
Step S202, data information is sent to a second service through a first service;
step S203, acquiring context information based on the received data information through a second thread used by a second service, and binding the context information with the second thread;
wherein the second thread is for processing a user request associated with the data information;
step S204, in response to the operation of acquiring the user information, acquiring the user information from the context information through the second thread.
Based on the implementation flowchart shown in fig. 2, if the first service is a micro service gateway, the second service is the first micro service; the flowchart for obtaining the user information by the first micro service in the embodiment of the present application is shown in fig. 3, and the specific steps are as follows:
step S301, responding to the operation of a target user, and acquiring user information of the target user from an operation request of the target user through a micro service gateway;
optionally, in the embodiment of the present application, the micro service gateway obtains a user token from the received operation request of the target user, and analyzes the token to obtain user information of the target user corresponding to the token.
Illustratively, after obtaining the user token, the embodiments of the present application may parse the token through the authentication module.
Wherein, the user information of the target user at least comprises: target user name, target user ID, target user account. The present application is not limited to the content included in the user information.
Optionally, the application may call the user token parsing module through the micro service gateway, so that the micro service gateway obtains the user information of the target user from the received operation request of the target user.
Step S302, user information is placed in context information of a first thread used by a micro-service gateway through the micro-service gateway;
the first thread is a thread for processing the operation request by the micro-service gateway.
The context information is information in context.
Alternatively, the embodiment of the application may place the user information in the context information of the first thread used by the micro service gateway through the above-mentioned user token parsing module.
In the embodiment of the application, the micro-service gateway calls the token parsing module to acquire the user information, sets the user information into context through a thread mechanism of Java, and binds the user information with the first thread.
Step S303, obtaining context information containing user information used by a first thread in the micro-service gateway through the micro-service gateway, and generating corresponding data information based on the context information;
Optionally, in the embodiment of the present application, the microservice gateway performs serialization processing on the context information, and assembles the context information after the serialization processing to obtain data information;
for example, the data information in the embodiment of the present application may be a request Header (Http Header), that is, the context information after the serialization processing is assembled to obtain the request Header.
Optionally, the application may call the bytecode automatic processing thread switching module through the micro service gateway to enhance the micro service request outlet, call the bytecode automatic processing thread switching module through the micro service gateway to obtain context information including user information used by a first thread in the micro service gateway, call serialization capability to process the obtained context information, and store the serialized context information in a request Header (Http Header) to transfer to a downstream service (for example, the first micro service).
The micro-service gateway can call the custom context module through the micro-service gateway, and when the micro-service gateway calls the serialization capability through the byte code automatic processing thread switching module, the byte code automatic processing thread switching module calls the custom context module to realize the serialization processing of the context information.
It should be noted that, the micro-service request export is enhanced, such as a common Http client (HttpClient, okHttp, restTemplate), to intercept the operation of the Http client.
Step S304, data information is sent to a first micro service through a micro service gateway;
when the micro service gateway transmits the data information to the first micro service, the user request associated with the data information is also forwarded to the first micro service, and the first micro service processes the user request.
The first micro service may be selected from at least one micro service based on business logic and a user request associated with data information for the micro service gateway.
For example, if the user request associated with the data information is a submit order request, the micro-service gateway may select an order micro-service from the at least one micro-service based on the business logic, and take the order micro-service as the first micro-service.
It should be noted that, if the operation triggered by the user at the terminal is an order submitting operation, the user request associated with the data information may be an order submitting request.
Step S305, obtaining context information through a second thread used by the first micro-service, and binding the context information with the second thread;
Wherein the second thread is for processing a user request associated with the data information;
optionally, in the embodiment of the present application, the second thread used by the first micro service performs deserialization processing on the context information after the serialization processing in the received data information, so as to obtain the context information in the data information.
Optionally, the application may enhance the micro-service request entry by calling the bytecode automatic processing thread switching module by the first micro-service, intercept the data information, acquire the serialized context information from the data information, call the deserialization capability to process the acquired serialized context information, and play back the deserialized context information to the second thread.
The method and the device can call the self-defined context module through the first micro-service, and when the first micro-service calls the anti-serialization capability through the byte code automatic processing thread switching module, the byte code automatic processing thread switching module calls the self-defined context module to realize the anti-serialization processing of the data information.
And step S306, responding to the operation of acquiring the user information, and acquiring the user information from the context information through the second thread.
It should be noted that, in the present application, the operation of acquiring the user information may be triggered by the program code.
In this embodiment, if after step S304, before step S305, the method further includes: the first micro-service processes user requests associated with data information using an initial thread.
That is, if the initial thread was used to process the user request before the second thread was used by the first micro-service, and the initial thread is the first thread used by the first micro-service to process the user request.
The embodiment of the application provides a flowchart for acquiring user information by the first micro service shown in fig. 4, which specifically includes the following steps:
step S401, responding to the operation of a target user, and acquiring the user information of the target user from the operation request of the target user through a micro service gateway;
step S402, placing user information into context information of a first thread used by a micro-service gateway through the micro-service gateway;
step S403, obtaining context information containing user information used by a first thread in the micro-service gateway through the micro-service gateway, and generating corresponding data information based on the context information;
step S404, data information is sent to a first micro service through a micro service gateway;
Step S405, acquiring context information from received data information through an initial thread used by a first micro-service, and binding the context information with the initial thread;
optionally, in the embodiment of the present application, the initial thread used by the first micro service performs deserialization processing on the context information after the serialization processing in the received data information, so as to obtain the context information in the data information.
Optionally, the application may enhance the micro-service request entry by calling the bytecode automatic processing thread switching module by the first micro-service, intercept the data information, acquire the serialized context information from the data information, call the deserialization capability to process the acquired serialized context information, and play back the deserialized context information to the second thread.
The method and the device can call the self-defined context module through the first micro-service, and when the first micro-service calls the anti-serialization capability through the byte code automatic processing thread switching module, the byte code automatic processing thread switching module calls the self-defined context module to realize the anti-serialization processing of the data information.
Step S406, the initial thread transfers the user request to the second thread;
step S407, when the second thread receives the user request, the second thread processes the user request;
after detecting that the initial thread triggers the user request transfer operation for the thread pool, the second thread dispatches at least one thread in the thread pool to determine based on the user request.
Alternatively, the second thread may be a child of the initial thread, or a thread not associated with the initial thread that is scheduled for a thread pool (wookpool);
it should be noted that the second thread may be created for the initial thread, and after the initial thread creates the second thread, the user request associated with the data information is forwarded to the second thread.
Wherein the second thread may be pre-created for the initial thread.
Alternatively, embodiments of the present application may determine that the initial thread triggers the user request handoff operation in several ways:
the first mode, the initial thread hands over the user request to the sub-thread;
and secondly, the initial thread submits the user request to a thread pool.
Alternatively, after the user request is forwarded to the second thread, the user request may be bound to the second thread.
When the initial thread forwards the user request to the second thread, it is determined that the first micro-service is switched.
Step S408, copying the context information bound by the initial thread through a second thread used by the first micro-service, and binding the context information with the second thread;
in implementation, when the first micro-service detects that the thread switching occurs, the second thread is automatically triggered to copy the context information bound by the initial thread, and the context information is bound with the second thread.
Optionally, the application may copy the context information bound by the initial thread by calling an automatic processing thread switching module through the first micro-service, and bind the copied context information with the second thread.
Through step S408, the submitting operation of the user request of the thread pool can be enhanced, so that the second thread can be ensured to acquire the user information in the context information, data loss is prevented, and the situation that the context information is lost when the initial thread forwards the user request to the second thread, so that the second thread cannot acquire the user information is avoided.
The thread pool is a Java thread pool.
It should be noted that, the present application does not limit the number of second threads in the first micro service, and if there are a plurality of second threads, the first second thread (for example, the second thread 1) transfers the user request to the next second thread (for example, the second thread 2) and the second thread 2 copies the context information bound by the second thread 1 and binds the context information to the second thread 2 according to the time sequence of using each second thread by the first micro service. The specific method principle is referred to step S407 and step S408, and will not be described here again.
Step S409, user information is acquired from the context information through the second thread in response to the operation of acquiring the user information.
It should be noted that the method principles of step S401 to step S404 are the same as those of step S301 to step S304, and will not be described herein.
Based on the implementation flowchart shown in fig. 2, if the first service is the first micro service, the second service is the second micro service; the flowchart for obtaining the user information by the second micro service in the embodiment of the present application is shown in fig. 5, and the specific steps are as follows:
step S501, obtaining context information containing user information used by a first thread in a first micro-service through the first micro-service, and generating corresponding data information based on the context information;
the context information including the user information used by the first thread in the first microservice is obtained through step S305 or step S408.
Optionally, in the embodiment of the present application, the first microservice performs serializing processing on the context information, and assembles the context information after serializing processing to obtain the data information;
for example, the data information in the embodiment of the present application may be a request Header (Http Header), that is, the context information after the serialization processing is assembled to obtain the request Header.
Optionally, the application may enhance the micro service request exit by calling the bytecode automatic processing thread switching module by the first micro service, and obtain context information including user information used by the first thread in the first micro service by calling the bytecode automatic processing thread switching module by the first micro service, call serialization capability to process the obtained context information, store the serialized context information in a request Header (Http Header), and transmit the serialized context information to a downstream service (for example, a second micro service).
The method and the device can call the self-defined context module through the first micro-service, and when the first micro-service calls the serialization capability through the byte code automatic processing thread switching module, the byte code automatic processing thread switching module calls the self-defined context module to realize the serialization processing of the context information.
It should be noted that, the micro-service request export is enhanced, such as a common Http client (HttpClient, okHttp, restTemplate), to intercept the operation of the Http client.
Step S502, determining a second micro-service from at least one micro-service except the first micro-service through the first micro-service;
The second micro-service may be selected from at least one micro-service other than the first micro-service based on business logic and user requests associated with data information for the first micro-service.
For example, if the user request associated with the data information is a submit order request and the first micro-service is an order micro-service, the order micro-service may select an inventory micro-service from at least one micro-service other than the first micro-service based on the business logic, and the inventory micro-service is the second micro-service.
It should be noted that at least one of the first micro-service, the second micro-service, etc. is a service downstream of the service.
Step S503, data information is sent to a second micro-service through a first micro-service;
step S504, obtaining context information through a second thread used by a second micro-service, and binding the context information with the second thread;
wherein the second thread is for processing a user request associated with the data information;
optionally, in the embodiment of the present application, the second thread used by the second micro service performs deserialization processing on the context information after the serialization processing in the received data information, so as to obtain the context information in the data information.
Optionally, the application may enhance the micro-service request entry by calling the bytecode automatic processing thread switching module by the first micro-service, intercept the data information, acquire the serialized context information from the data information, call the deserialization capability to process the acquired serialized context information, and play back the deserialized context information to the second thread.
The method and the device can call the self-defined context module through the first micro-service, and when the first micro-service calls the anti-serialization capability through the byte code automatic processing thread switching module, the byte code automatic processing thread switching module calls the self-defined context module to realize the anti-serialization processing of the data information.
Step S505, in response to the operation of obtaining the user information, obtaining the user information from the context information through the second thread.
In practice, if after step S503, before step S504, the method further comprises: the second micro-service processes user requests associated with data information using an initial thread.
That is, if the initial thread has been used to process the user request before the second micro-service uses the second thread, the initial thread is the first thread used by the second micro-service to process the user request.
The embodiment of the application provides a flowchart for acquiring user information by the second micro service shown in fig. 6, which specifically includes the following steps:
step S601, obtaining context information containing user information used by a first thread in a first micro-service through the first micro-service, and generating corresponding data information based on the context information;
step S602, determining a second micro-service from at least one micro-service except the first micro-service through the first micro-service;
step S603, transmitting the data information to the second micro-service through the first micro-service;
step S604, obtaining context information from the received data information through an initial thread used by the second micro-service, and binding the context information with the initial thread;
optionally, in the embodiment of the present application, the initial thread used by the second micro service performs deserialization processing on the context information after the serialization processing in the received data information, so as to obtain the context information in the data information.
Step S605, the initial thread transfers the user request to the second thread;
step S606, when the second thread receives the user request, the second thread processes the user request;
after detecting that the initial thread triggers the user request transfer operation for the thread pool, the second thread dispatches at least one thread in the thread pool to determine based on the user request.
Alternatively, the second thread may be a child of the initial thread, or a thread not associated with the initial thread that is scheduled for a thread pool (wookpool);
it should be noted that the second thread may be created for the initial thread, and after the initial thread creates the second thread, the user request associated with the data information is forwarded to the second thread.
Wherein the second thread may be pre-created for the initial thread.
Alternatively, embodiments of the present application may determine that the initial thread triggers the user request handoff operation in several ways:
the first mode, the initial thread hands over the user request to the sub-thread;
and secondly, the initial thread submits the user request to a thread pool.
Alternatively, after the user request is forwarded to the second thread, the user request may be bound to the second thread.
When the initial thread forwards the user request to the second thread, it is determined that the second micro-service is in thread switch.
Step S607, copying the context information bound by the initial thread through a second thread used by a second micro-service, and binding the context information with the second thread;
in the implementation, when the second micro service detects that the thread switching occurs, the second thread is automatically triggered to copy the context information bound by the initial thread, and the context information is bound with the second thread.
Optionally, the application may copy the context information bound by the initial thread by calling an automatic processing thread switching module through the first micro-service, and bind the copied context information with the second thread.
Through step S607, the user request submitting operation of the Java thread pool can be enhanced, so that the second thread can be ensured to acquire the user information in the context information, data loss is prevented, and the situation that the context information is lost when the initial thread forwards the user request to the second thread, so that the second thread cannot acquire the user information is avoided.
It should be noted that, the present application does not limit the number of second threads in the second micro service, and if there are a plurality of second threads, the first used second thread (for example, second thread 1) forwards the user request to the next used second thread (for example, second thread 2), and the second thread 2 copies the context information bound by the second thread 1 and binds the context information to the second thread 2 according to the time sequence of using each second thread by the second micro service. The specific method principle is referred to step S606 and step S607, and will not be described herein.
Step S608, in response to the operation of obtaining the user information, obtaining the user information from the context information through the second thread.
It should be noted that the method principles of step S601 to step S603 are the same as those of step S501 to step S503, and will not be described herein.
As shown in fig. 7, an overall implementation flowchart of the embodiment of the present application includes the following specific steps:
step S701, responding to the operation of a target user, and acquiring the user information of the target user from the operation request of the target user through a micro service gateway;
optionally, the application may set a user token analysis module in the micro service gateway, so that the micro service gateway obtains a user token from the received operation request, and invokes the authentication module to perform token analysis through the user token analysis module to obtain user information corresponding to the token.
Step S702, placing user information into context information of a first thread used by a micro-service gateway through the micro-service gateway;
optionally, the user token parsing module sets the user information into the custom context information by using a context tool.
Step S703, obtaining context information containing user information used by a first thread in the micro-service gateway through the micro-service gateway, carrying out serialization processing on the context information, and assembling the context information after the serialization processing to obtain data information;
Step S704, data information is sent to a first micro service through a micro service gateway;
step S705, obtaining context information from the received data information through an initial thread used by the first micro-service, and binding the context information with the initial thread;
optionally, in the embodiment of the present application, the initial thread used by the first micro service performs deserialization processing on the context information after the serialization processing in the received data information, so as to obtain the context information in the data information.
Step S706, the initial thread transfers the user request to the second thread;
step S707, when the second thread receives the user request, the second thread processes the user request;
step S708, copying the context information bound by the initial thread through a second thread used by the first micro-service, and binding the context information with the second thread;
step S709, in response to the operation of obtaining the user information, obtaining the user information from the context information through the second thread.
Step 7010, obtaining context information containing user information used by a second thread in a first micro-service through the first micro-service, carrying out serialization processing on the context information, and assembling the context information after the serialization processing to obtain data information;
Step S7011, determining, by the first micro-service, a second micro-service from at least one micro-service other than the first micro-service;
step S7012, the data information is sent to the second micro service through the first micro service.
The process of the second micro service obtaining the user information from the data information may refer to the process of step S504 and step S505, or the process of step S604-step S608, which is not described herein.
Based on fig. 7, an overall schematic diagram of acquiring user information as shown in fig. 8 is provided in an embodiment of the present application. After triggering operation, the terminal sends an operation request to the micro-service gateway, the micro-service gateway firstly executes 1, analyzes a token carried in the operation request, acquires user information, then executes 2, places the user information into context information, executes 3, carries out serialization transmission on the context information, and transmits the context information to downstream service (for example, a first micro-service); and the first micro-service executes 4, deserializes the context information, plays back the context information, executes 5 when the thread switching is triggered in the first micro-service, copies the context information to the sub-thread, and intercepts the thread switching operation generated in the user request process. Then executing 6, carrying out serialization transmission on the context information, and transmitting the context information to a second micro-service; the second microservice performs 7, deserializes the context information, and plays back the context information.
Wherein, the user information can be put into the Context information by using the Context tool class in 2.
As shown in fig. 9, a thread switching diagram is shown in the embodiment of the present application based on the thread switching process performed by the first micro service in fig. 8. After the main thread submits the user request to the thread pool, intercepting thread switching operation occurring in the process of the user request, copying context information bound with the main thread (for example, a 'main thread snapshot' in fig. 9), and submitting the user request to the thread pool; the thread pool dispatches at least one thread in the thread pool based on the user request, and forwards the user request to the sub-thread for processing, and the sub-thread copies the context information bound by the main thread for playback in the sub-thread.
As shown in fig. 10, the present application provides an apparatus for acquiring user information by a micro service, including:
generating means 1001, configured to obtain, by a first service, context information including user information used by a first thread in the first service, and generate corresponding data information based on the context information; transmitting the data information to a second service;
binding means 1002, configured to obtain, by a second thread used by the second service, the context information based on the received data information, and bind the context information to the second thread; the second thread is used for processing a user request associated with the data information;
And obtaining means 1003, configured to obtain, in response to an operation of obtaining the user information, the user information from the context information through the second thread.
In some alternative embodiments, the generating device 1001 is specifically configured to:
carrying out serialization processing on the context information through a first service, and assembling the context information after the serialization processing to obtain the data information;
the binding means 1002 is specifically configured to:
and performing deserialization processing on the context information after the serialization processing in the received data information through a second thread used by the second service to obtain the context information in the data information.
In some optional embodiments, if the first service is a micro service gateway, the second service is a first micro service; the generating means 1001 is further configured to:
responding to the operation of a target user, and acquiring user information of the target user from an operation request of the target user through the micro service gateway;
placing the user information in context information of a first thread used by the micro service gateway through the micro service gateway; the first thread is a thread used by the micro-service gateway to process the operation request.
In some alternative embodiments, if the first service is a first micro service, the second service is a second micro service; the generating means 1001 is further configured to:
a second micro-service is determined from at least one micro-service other than the first micro-service based on the request link through the first service.
In some optional embodiments, if after the data information is sent to the second service, before the second thread used by the second service obtains the context information based on the received data information, the method further includes: the second service processes a user request associated with the data information using an initial thread; the binding means 1002 is further adapted to:
acquiring the context information from the received data information through an initial thread used by the second service, and binding the context information with the initial thread;
processing the user request by the second thread when the initial thread forwards the user request to the second thread;
the binding means 1002 is specifically configured to:
copying the context information bound by the initial thread through a second thread used by the second service.
In some alternative embodiments, after the second thread detects that the thread pool triggers a user request for a handover operation for the initial thread, the second thread schedules at least one thread in the thread pool for determining based on the user request.
Based on the same inventive concept as the above embodiment, the present application further provides an electronic device, and the principle of solving the problem of the electronic device is similar to that of the above embodiment, so that the implementation of the electronic device may refer to the implementation of the above method, and the repetition is omitted.
Referring to fig. 11, the electronic device 110 may include at least a processor 111, and a memory 112. Wherein the memory 112 stores program code which, when executed by the processor 111, causes the processor 111 to perform the steps performed by the micro service acquisition user information method in the above embodiments of the present application.
An electronic device 120 according to this embodiment of the present application is described below with reference to fig. 12. The electronic device 120 of fig. 12 is merely an example and should not be construed to limit the functionality and scope of use of embodiments of the present application in any way.
As shown in fig. 12, the electronic device 120 is in the form of a general-purpose electronic device. Components of electronic device 120 may include, but are not limited to: at least one processing unit 121, the above-mentioned at least one memory unit 122, a bus 123 connecting the different system components, including the memory unit 122 and the processing unit 121.
Bus 123 represents one or more of several types of bus structures, including a memory bus or memory controller, a peripheral bus, a processor, and a local bus using any of a variety of bus architectures.
The storage unit 122 may include readable media in the form of volatile memory, such as Random Access Memory (RAM) 1221 and/or cache memory 1222, and may further include Read Only Memory (ROM) 1223.
The storage unit 122 may also include a program/utility 1212 having a set (at least one) of program modules 1224, such program modules 1224 including, but not limited to: an operating system, one or more application programs, other program modules, and program data, each or some combination of which may include an implementation of a network environment.
The electronic device 120 may also communicate with one or more external devices 124 (e.g., keyboard, pointing device, etc.), one or more devices that enable objects to interact with the electronic device 120, and/or any device (e.g., router, modem, etc.) that enables the electronic device 120 to communicate with one or more other electronic devices. Such communication may occur through an input/output (I/O) interface 125. Also, the electronic device 120 may communicate with one or more networks such as a Local Area Network (LAN), a Wide Area Network (WAN), and/or a public network, such as the Internet, through a network adapter 126. As shown, network adapter 126 communicates with other modules for electronic device 120 over bus 123. It should be appreciated that although not shown, other hardware and/or software modules may be used in connection with electronic device 120, including, but not limited to: microcode, device drivers, redundant processors, external disk drive arrays, RAID systems, tape drives, data backup storage systems, and the like.
It will be appreciated by those skilled in the art that embodiments of the present application may be provided as a method, system, or computer program product. Accordingly, the present application may take the form of an entirely hardware embodiment, an entirely software embodiment, or an embodiment combining software and hardware aspects. Furthermore, the present application may take the form of a computer program product embodied on one or more computer-usable storage media (including, but not limited to, disk storage, CD-ROM, optical storage, and the like) having computer-usable program code embodied therein.
The present application is described with reference to flowchart illustrations and/or block diagrams of methods, apparatus (systems) and computer program products according to the application. It will be understood that each flow and/or block of the flowchart illustrations and/or block diagrams, and combinations of flows and/or blocks in the flowchart illustrations and/or block diagrams, can be implemented by computer program instructions. These computer program instructions may be provided to a processor of a general purpose computer, special purpose computer, embedded processor, or other programmable data processing apparatus to produce a machine, such that the instructions, which execute via the processor of the computer or other programmable data processing apparatus, create means for implementing the functions specified in the flowchart flow or flows and/or block diagram block or blocks.
These computer program instructions may also be stored in a computer-readable memory that can direct a computer or other programmable data processing apparatus to function in a particular manner, such that the instructions stored in the computer-readable memory produce an article of manufacture including instruction means which implement the function specified in the flowchart flow or flows and/or block diagram block or blocks.
These computer program instructions may also be loaded onto a computer or other programmable data processing apparatus to cause a series of operational steps to be performed on the computer or other programmable apparatus to produce a computer implemented process such that the instructions which execute on the computer or other programmable apparatus provide steps for implementing the functions specified in the flowchart flow or flows and/or block diagram block or blocks.
It will be apparent to those skilled in the art that various modifications and variations can be made in the present application without departing from the spirit or scope of the application. Thus, if such modifications and variations of the present application fall within the scope of the claims and the equivalents thereof, the present application is intended to cover such modifications and variations.

Claims (10)

1. A method for a micro-service to obtain user information, the method comprising:
acquiring context information containing user information used by a first thread in a first service through the first service, and generating corresponding data information based on the context information; transmitting the data information to a second service;
acquiring the context information based on the received data information through a second thread used by the second service, and binding the context information with the second thread; the second thread is used for processing a user request associated with the data information;
and responding to the operation of acquiring the user information, and acquiring the user information from the context information through the second thread.
2. The method of claim 1, wherein the generating the respective data information based on the context information comprises:
carrying out serialization processing on the context information through the first service, and assembling the context information after the serialization processing to obtain the data information;
the obtaining, by the second thread used by the second service, the context information includes:
and performing deserialization processing on the context information after the serialization processing in the received data information through a second thread used by the second service to obtain the context information in the data information.
3. The method of claim 1 or 2, wherein if the first service is a micro-service gateway, the second service is a first micro-service;
before the obtaining, by the first service, the context information including the user information used by the first thread in the first service, the method further includes:
responding to the operation of a target user, and acquiring user information of the target user from an operation request of the target user through the micro service gateway;
placing the user information in context information of a first thread used by the micro service gateway through the micro service gateway; the first thread is a thread used by the micro-service gateway to process the operation request.
4. The method of claim 1 or 2, wherein if the first service is a first micro-service, the second service is a second micro-service;
then before the data information is sent to the second service, further comprising:
a second micro-service is determined from at least one micro-service other than the first micro-service by the first micro-service.
5. The method of claim 1, further comprising, if after the data information is sent to a second service, before the second thread used by the second service obtains the context information based on the received data information: the second service processes a user request associated with the data information using an initial thread;
Then after said sending said data information to a second service and before said obtaining said context information based on said received data information by said second thread used by said second service, further comprising:
acquiring the context information from the received data information through an initial thread used by the second service, and binding the context information with the initial thread;
processing the user request by the second thread when the initial thread forwards the user request to the second thread;
the second thread, used by the second service, obtains the context information based on the received data information, including:
copying the context information bound by the initial thread through a second thread used by the second service.
6. The method of claim 5, wherein the second thread, upon detecting that the initial thread triggers a user request handoff operation for a thread pool, schedules at least one thread in the thread pool based on the user request.
7. An apparatus for micro-services to obtain user information, the apparatus comprising:
The generating device is used for acquiring context information containing user information used by a first thread in the first service through the first service, and generating corresponding data information based on the context information; transmitting the data information to a second service;
binding means for obtaining, by a second thread used by the second service, the context information based on the received data information, and binding the context information with the second thread; the second thread is used for processing a user request associated with the data information;
and the acquisition device is used for responding to the operation of acquiring the user information and acquiring the user information from the context information through the second thread.
8. An electronic device comprising at least one processor and at least one memory, wherein the memory stores a computer program that, when executed by the processor, causes the processor to perform the method of any of claims 1-6.
9. A computer readable storage medium, characterized in that it stores a computer program executable by a computer, which when run on the computer causes the computer to perform the method according to any one of claims 1 to 6.
10. A computer program product, characterized in that the computer program product, when called by a computer, causes the computer to perform the method according to any of claims 1-6.
CN202311704633.6A 2023-12-12 2023-12-12 Method and device for acquiring user information by micro-service and electronic equipment Pending CN117742991A (en)

Priority Applications (1)

Application Number Priority Date Filing Date Title
CN202311704633.6A CN117742991A (en) 2023-12-12 2023-12-12 Method and device for acquiring user information by micro-service and electronic equipment

Applications Claiming Priority (1)

Application Number Priority Date Filing Date Title
CN202311704633.6A CN117742991A (en) 2023-12-12 2023-12-12 Method and device for acquiring user information by micro-service and electronic equipment

Publications (1)

Publication Number Publication Date
CN117742991A true CN117742991A (en) 2024-03-22

Family

ID=90251953

Family Applications (1)

Application Number Title Priority Date Filing Date
CN202311704633.6A Pending CN117742991A (en) 2023-12-12 2023-12-12 Method and device for acquiring user information by micro-service and electronic equipment

Country Status (1)

Country Link
CN (1) CN117742991A (en)

Similar Documents

Publication Publication Date Title
Gray et al. D'Agents: Applications and performance of a mobile‐agent system
EP3837604B1 (en) In situ triggered function as a service within a service mesh
EP3221789A1 (en) Method and system for code offloading in mobile computing
CN110716748A (en) Service processing method and device, computer readable medium and electronic equipment
WO2021057925A1 (en) Intelligent device control method and apparatus, intelligent device and storage medium
CN108733496B (en) Event processing method and device
Ghezzi et al. Programming language support to context-aware adaptation: a case-study with Erlang
CN112286698A (en) Remote procedure call method and device and remote procedure call execution method
US11531526B1 (en) Creating portable serverless applications
Gallidabino et al. The Liquid WebWorker API for Horizontal Offloading of Stateless Computations
Sandru et al. Building an open-source platform-as-a-service with intelligent management of multiple cloud resources
US11494184B1 (en) Creation of transportability container files for serverless applications
Silvestre et al. Flexibility and coordination in event-based, loosely coupled, distributed systems
US9537931B2 (en) Dynamic object oriented remote instantiation
US9819725B1 (en) System, method, and computer program for remotely controlling mobile web applications installed on a mobile device
CN108470125B (en) OLE redirection method and system based on virtual desktop
CN117742991A (en) Method and device for acquiring user information by micro-service and electronic equipment
US9058225B2 (en) Secure and reliable mechanism to provide a single object instance in a clustered system
US20230177192A1 (en) Secure compartmented access infrastructure for sensitive databases
US11513833B1 (en) Event listener interface for container-based execution of serverless functions
US10489213B2 (en) Execution of a method at a cluster of nodes
CN114039981A (en) Message processing method, device, server and storage medium
Basanta‐Val et al. Fine tuning of the multiplexing facilities of Java's Remote Method Invocation
Ayres et al. Stage: Python with actors
CN112579190A (en) Webpage embedded application interaction method and device

Legal Events

Date Code Title Description
PB01 Publication
PB01 Publication
SE01 Entry into force of request for substantive examination
SE01 Entry into force of request for substantive examination