A session maintenance method and related apparatus
Patent Information
- Authority / Receiving Office
- CN · China
- Patent Type
- Applications(China)
- Current Assignee / Owner
- NEUSOFT CORP
- Filing Date
- 2026-03-03
- Publication Date
- 2026-06-09
AI Technical Summary
In cluster load scenarios with high concurrency and high stability requirements, the random load forwarding strategy of the service gateway causes the session persistence mechanism of monolithic applications to fail, affecting the normal operation of system functions.
By pre-building and caching a session mapping table in the target service gateway, the correspondence between the gateway session identifier and the session credentials of each business node in the monolithic application cluster is recorded. High-availability random load forwarding is achieved by using the gateway node scheduling strategy. The session credentials of the target business node are quickly located and matched by the gateway session identifier, ensuring that session requests are accurately identified and forwarded.
Based on highly available random load forwarding, it effectively guarantees the session persistence function of monolithic applications, ensures the continuous maintenance of client session state, realizes a transparent session persistence closed loop, and reduces system upgrade and maintenance costs.
Smart Images

Figure CN122179468A_ABST
Abstract
Description
Technical Field
[0001] This application relates to the field of computer network technology, and in particular to a session persistence method and related apparatus. Background Technology
[0002] Service gateways are core entry points in modern software architectures, widely used in microservice architectures and unified management of application programming interfaces (APIs). The core function of a service gateway is to receive various requests from clients, and based on the request's URL, HTTP headers, and other relevant parameters, to route and forward the requests. Simultaneously, service gateways also undertake several critical support functions, including security control, protocol conversion, rate limiting and circuit breaking, and logging, ensuring stable system operation.
[0003] In cluster load scenarios demanding high concurrency and stability, the industry commonly uses service gateways to proxy backend monolithic applications for unified traffic management and centralized control. However, to meet high availability requirements, gateways typically employ load balancing strategies such as random distribution, which can render the original session persistence mechanism of the monolithic application ineffective. In this situation, client requests may be routed to business nodes that do not store the corresponding session state, leading to abnormal business logic execution and impacting the normal operation of the system.
[0004] Therefore, how to effectively guarantee the session persistence function of monolithic applications while achieving high availability random load forwarding has become a technical problem that urgently needs to be solved in the field of computer network technology. Summary of the Invention
[0005] To address the aforementioned issues, this application provides a session persistence method that can effectively guarantee the session persistence function of a monolithic application while achieving high availability random load forwarding.
[0006] The embodiments of this application disclose the following technical solutions: The first aspect of this application provides a session persistence method applied to a target service gateway, comprising: After determining that the session request sent by the client carries a gateway session identifier and that the target service gateway caches a session mapping table, the target business node in the monolithic application cluster for processing the session request is determined through the gateway node scheduling strategy. The session credentials corresponding to the target service node are determined using the gateway session identifier and the session mapping table; the session mapping table includes the correspondence between the gateway session identifier and the session credentials of the service nodes in the monolithic application cluster. The session request is updated based on the session credentials corresponding to the target service node to obtain the target session request; The target session request is routed to the target service node for processing, and a service response is generated. Obtain the service response fed back by the target service node, and send the service response carrying the gateway session identifier to the client.
[0007] In one alternative implementation, the method further includes: If it is determined that the session request does not carry the gateway session identifier, then each of the service nodes is logged in based on the pre-stored username and password corresponding to the client; Receive session credentials from each of the aforementioned service nodes; The session mapping table is constructed based on the routing information and session credentials of each service node; the routing information includes address, service port and service request path; Cache the session mapping table to the target service gateway; Send the gateway session identifier to the client.
[0008] In one alternative implementation, the method further includes: If the session request carries the gateway session identifier and the target service gateway does not cache the session mapping table, then each of the business nodes is logged in based on the pre-stored username and password corresponding to the client. Receive session credentials from each of the aforementioned service nodes; The session mapping table is constructed based on the routing information and session credentials of each service node; the routing information includes address, service port and service request path; Cache the session mapping table to the target service gateway; Send the gateway session identifier to the client.
[0009] In one optional implementation, if it is determined that the session request does not carry the gateway session identifier, then logging into each of the service nodes based on the pre-stored username and password corresponding to the client includes: Based on the client's address, perform security verification on the client; If the client passes the security verification, it logs into each of the business nodes based on the username and password. If the client fails the security verification, an authentication error message is sent to the client.
[0010] In one alternative implementation, the method further includes: Determine whether the cache duration of the session mapping table in the target service gateway exceeds the preset target duration; If the cache duration of the session mapping table in the target service gateway exceeds the target duration, then the cached session mapping table in the target service gateway is cleared.
[0011] In one optional implementation, constructing the session mapping table based on the routing information and session credentials of each service node includes: The gateway session identifier is determined based on high-entropy random numbers and routing information of each service node; The session mapping table is constructed based on the gateway session identifier, the session credentials of each service node, and the routing information of each service node.
[0012] In one alternative implementation, the method further includes: If the session credentials of the business node are updated, an updated session mapping table is generated based on the updated session credentials.
[0013] In one alternative implementation, the gateway node scheduling strategy is a round-robin strategy, a random strategy, or a load balancing strategy.
[0014] A second aspect of this application provides a session persistence device applied to a target serving gateway, the device comprising: The target service node determination module is used to determine the target service node in the monolithic application cluster that is used to process the session request after the client sends a session request carrying a gateway session identifier and the target service gateway has a cached session mapping table. The target session credential determination module is used to determine the session credential corresponding to the target service node through the gateway session identifier and the session mapping table; the session mapping table includes the correspondence between the gateway session identifier and the session credentials of the service nodes in the monolithic application cluster. The target session request generation module is used to update the session request based on the session credentials corresponding to the target service node to obtain the target session request; The target session request processing module is used to route the target session request to the target business node for processing and generate a business response. The service response feedback module is used to obtain the service response fed back by the target service node and send the service response carrying the gateway session identifier to the client.
[0015] A third aspect of this application provides a computer-readable storage medium having a computer program stored thereon, which, when executed by a processor, implements the steps of the method described in any implementation of the first aspect.
[0016] A fourth aspect of this application provides an electronic device, comprising: A memory on which computer programs are stored; A processor for executing the computer program in the memory to implement the steps of the method described in any implementation of the first aspect.
[0017] Compared with the prior art, this application has the following beneficial effects: The core of this application's solution relies on a pre-built and cached session mapping table, which maps gateway session identifiers to session credentials of each business node in the monolithic application cluster. After confirming that the client session request carries the gateway session identifier and that the mapping table is cached in the service gateway, a highly available random load balancing forwarding strategy is implemented to the monolithic application cluster. After selecting the target business node, the solution further leverages this mapping relationship to quickly locate and match the corresponding session credentials of the target business node using the gateway session identifier, completing credential replacement and forwarding of the session request. This ensures that regardless of which business node the request is randomly forwarded to, that node can accurately identify the client session identity using the injected session credentials, thus maintaining the session in the monolithic application. By returning the gateway session identifier to the client in the business response, continuous maintenance of the client-side session state is achieved, forming a complete closed loop for session persistence. The solution disclosed in this application effectively guarantees the session persistence function of the monolithic application while achieving highly available random load balancing forwarding. Attached Figure Description
[0018] To more clearly illustrate the technical solutions in the embodiments of this application or the prior art, the drawings used in the description of the embodiments or the prior art will be briefly introduced below. Obviously, the drawings described below are only some embodiments of this application. For those skilled in the art, other drawings can be obtained based on these drawings without creative effort.
[0019] Figure 1 A schematic diagram illustrating a single-application session persistence method based on cookies, provided as an embodiment of this application; Figure 2 This is a schematic diagram illustrating session persistence based on Nginx in a cluster load scenario, as provided in an embodiment of this application. Figure 3 This is a schematic diagram illustrating session persistence based on a service gateway in a cluster load scenario, as provided in an embodiment of this application. Figure 4A flowchart of a session persistence method provided in an embodiment of this application; Figure 5 An interactive diagram illustrating a session persistence method based on a target service gateway, provided as an embodiment of this application; Figure 6 This is a schematic diagram of a session holding device provided in an embodiment of this application. Detailed Implementation
[0020] The browser and the server exchange data based on the HTTP protocol. However, the HTTP protocol itself is a stateless protocol, and the server cannot persistently record the client's access state. Each request initiated by the client is regarded as a brand new connection by the server. Therefore, the server cannot directly identify the identity of the client that initiates the request.
[0021] Figure 1 This is a schematic diagram illustrating how a monolithic application maintains sessions based on cookies, as provided in an embodiment of this application. (Combined with...) Figure 1 As shown, in traditional business scenarios where multiple clients access a single monolithic application, the monolithic application achieves browser client identification and session persistence in the following way: First, the monolithic application, acting as the server, generates a dedicated session for the client and stores it locally. This session records relevant client information, thereby identifying the user. Then, the server encapsulates the session association information into a cookie and sends it to the client. This cookie is stored on the client side and is automatically appended to the request header when the client initiates a session request, and sent to the server along with the request. Upon receiving the request, the server parses the cookie in the request header to obtain session information, accurately distinguishing the client to which the request belongs, thus completing user authentication and achieving session persistence.
[0022] With the rapid development of internet technology and business scale, cluster load balancing has become the mainstream application scenario. Figure 2 This diagram illustrates a session persistence implementation based on Nginx under a cluster load scenario, as provided in an embodiment of this application. Figure 2 As shown, in business scenarios where multiple clients access a monolithic application cluster, if session persistence is required without modifying the proxied backend monolithic application, the industry typically adopts the approach of introducing an Nginx load balancer and configuring a session stickiness strategy to achieve session sharing. Specifically, after Nginx receives all requests initiated by the client, it continuously forwards requests from the same client to fixed backend monolithic application nodes in the cluster according to preset session stickiness rules, ensuring that all requests from that client are processed by nodes that already hold its session information, thereby achieving session persistence in the cluster scenario.
[0023] Because Nginx load balancers only support fixed session persistence strategies, they suffer from a lack of flexibility in request forwarding, an inability to achieve true random load distribution, and a tendency to cause uneven load distribution across backend nodes, thus limiting overall system availability. Therefore, in cluster-based load balancing scenarios with high requirements for both interface forwarding performance and stability, a service gateway is typically used to proxy the backend monolithic application, enabling unified access and centralized management of backend business nodes.
[0024] However, since the service gateway does not support session sticking strategy, and the business needs to ensure the high availability of the system through random forwarding mode, the original session persistence mechanism of the monolithic application is difficult to function properly. Client requests may be forwarded to monolithic service nodes that do not store the corresponding session information, ultimately affecting the normal progress of the business.
[0025] Figure 3 This diagram illustrates a session persistence mechanism based on a service gateway in a cluster load scenario, as provided in an embodiment of this application. Figure 3 As shown, when a client (browser) initiates a request carrying a cookie, the service gateway, without a session persistence strategy, will randomly forward the request to any node in the backend monolithic application cluster based on high availability requirements. Since the forwarding is completely random, the request may be assigned to a node that does not store the client's session information, causing the original session persistence mechanism of the monolithic application to fail, the server to be unable to complete identity authentication, and thus triggering business anomalies.
[0026] Traditional solutions typically employ a pre-login-based shared cache session synchronization approach to address the fundamental contradiction between the stateless, high-availability load balancing requirements of service gateways and the stateful session mechanisms of traditional monolithic applications in cluster load scenarios with high demands for interface forwarding performance and stability.
[0027] The pre-login shared cache session synchronization solution refers to triggering a business login interface call before calling the business interface to re-acquire the session. Simultaneously, to ensure all nodes in the cluster can recognize this session, the monolithic application needs to be modified to introduce a shared cache component to achieve cross-node sharing of session information. However, this solution is highly intrusive to the business application. It not only adds an extra login call to the request chain, increasing its complexity, but the introduction of the shared cache component also increases the business complexity of the monolithic application and significantly raises deployment and maintenance costs, bringing new technical challenges to the system.
[0028] To address the aforementioned issues, this application provides a session persistence method applied to a target service gateway. The method includes: determining, after confirming that the client-sent session request carries a gateway session identifier and that the target service gateway caches a session mapping table, determining the target business node in the monolithic application cluster to handle the session request using a gateway node scheduling strategy; determining the session credentials corresponding to the target business node using the gateway session identifier and the session mapping table; updating the session request based on the target session credentials to generate a target session request; and forwarding the target session request to the target business node for processing.
[0029] The core of this application's solution relies on a pre-built and cached session mapping table, which maps gateway session identifiers to session credentials of each business node in the monolithic application cluster. After confirming that the client session request carries the gateway session identifier and that the mapping table is cached in the service gateway, the request is first forwarded to the monolithic application cluster using a gateway node scheduling strategy, ensuring high availability and random load balancing. After selecting the target business node, the solution further leverages this mapping relationship to quickly locate and match the corresponding session credentials of the target business node using the gateway session identifier, completing credential replacement and forwarding of the session request. This ensures that regardless of which business node the request is randomly forwarded to, that node can accurately identify the client session identity using the injected session credentials, achieving session persistence for the monolithic application. Finally, by returning the same gateway session identifier to the client in the business response, continuous maintenance of the client-side session state is achieved, forming a complete closed-loop session persistence mechanism. The solution disclosed in this application effectively guarantees the session persistence function of the monolithic application while achieving high availability and random load balancing.
[0030] To enable those skilled in the art to better understand the present application, the technical solutions in the embodiments of the present application will be clearly and completely described below with reference to the accompanying drawings. Obviously, the described embodiments are only some embodiments of the present application, and not all embodiments. Based on the embodiments in the present application, all other embodiments obtained by those of ordinary skill in the art without creative effort are within the scope of protection of the present application.
[0031] Figure 4 This is a flowchart of a session persistence method provided in an embodiment of this application. Figure 4 The session persistence method in [the context] is applied to the target service gateway. Combined with... Figure 4 As shown, the session persistence method provided in this application includes: S401, after determining that the session request sent by the client carries a gateway session identifier and that the target service gateway caches a session mapping table, the target business node in the monolithic application cluster for processing the session request is determined through the gateway node scheduling strategy.
[0032] The session persistence method provided in this application is mainly applied to system architectures containing multiple clients, a target service gateway, and a monolithic application cluster. The clients are typically front-end applications such as browsers, and the monolithic application cluster consists of multiple business nodes (also referred to as monolithic applications). The clients send session requests to the monolithic application cluster through the target service gateway, which coordinates the identification, routing, and persistence of the sessions.
[0033] The session mapping table in this application records the correspondence between the gateway session identifier and the session credentials of each business node in the monolithic application cluster, which is used to realize the unified mapping and management of distributed session states at the gateway level.
[0034] The session mapping table can be represented as: GS-ID → {node1:cookie1,node2:cookie2,…}. Here, node1 represents business node 1 in the monolithic application cluster, and node2 represents business node 2 in the monolithic application cluster; cookie1 is the session credential maintained by business node 1, and cookie2 is the session credential maintained by business node 2.
[0035] In this application, the Gateway-Session-ID (GS-ID) refers to the cookie information generated by the target service gateway and sent to the client, which is used to uniquely identify a user session at the gateway level.
[0036] For example, a high-entropy random number generator with encrypted security can be used. This high-entropy random number is then combined with the routing information of each business node in the monolithic application cluster to determine the gateway session identifier. The gateway session identifier remains unchanged throughout the entire session lifecycle. This fixed gateway session identifier ensures that the client always carries the same identity credentials in multiple consecutive requests, providing a stable index for the target service gateway to perform accurate routing based on the session mapping table.
[0037] The gateway node scheduling strategy in this application refers to the decision logic by which the target service gateway distributes requests among multiple service nodes, including but not limited to round-robin strategies, random strategies, or load balancing strategies based on real-time load. This scheduling strategy can support session-associated routing mechanisms while ensuring cluster load balancing.
[0038] In practice, when the target service gateway receives a session request initiated by the client, if it detects that the request carries a valid gateway session identifier and that a session mapping table exists in the target service gateway's local cache, then according to the established gateway node scheduling strategy, it selects a target business node from the business nodes corresponding to the mapping table to handle this session request.
[0039] S402, determine the session credentials corresponding to the target service node through the gateway session identifier and the session mapping table.
[0040] As mentioned earlier, the session mapping table includes the mapping between gateway session identifiers and session credentials of business nodes in a monolithic application cluster. After determining the target business node, the session credentials corresponding to the target application node can be determined through the gateway session identifier and the session mapping table.
[0041] For example, if the target application node is node1, the session credential cookie1 corresponding to node1 can be extracted by querying the entry in the session mapping table that corresponds to the current gateway session identifier.
[0042] S403, update the session request based on the session credentials corresponding to the target service node to obtain the target session request.
[0043] The target service gateway will extract the session credentials bound to the target business node from the session mapping table and dynamically inject the session credentials into the HTTP request header to be forwarded, thus obtaining the target session request. This operation usually involves overwriting or appending the original Cookie field in the session request, so that the target session request forwarded to the target business node can carry the valid session information identified and maintained by that node.
[0044] Understandably, the target session request not only retains the semantics and payload of the client's original session request, but also carries session credentials for maintaining the session state on the target business node, thereby ensuring that subsequent business logic can be executed in the correct session context and achieving seamless session persistence in a cluster environment.
[0045] S404, the target session request is routed to the target service node for processing, and a service response is generated.
[0046] After completing the request update, the target service gateway sends the target session request to the target business node (e.g., node1) determined in step S401. When the target business node receives the request, it parses the session credential (i.e., cookie1) in the request header. Since this session credential is a valid session identifier issued and maintained by this node, the target business node will recognize that the current request is in a legitimate and continuous session context, and will execute the business logic normally accordingly to generate a business response.
[0047] S405, obtain the service response fed back by the target service node, and send the service response carrying the gateway session identifier to the client.
[0048] After the target service node completes its processing, it generates a response and returns it to the target service gateway. Upon receiving this response, the target service gateway adds a gateway session identifier, identical to the one carried in the session request sent by the client in step S401, to the response header and returns it to the client. This application achieves a dual key function by feeding back the same gateway session identifier to the client: firstly, it proactively refreshes the validity period of the client's local identifier, maintaining session liveness; secondly, it reconstructs session signaling on the response side that is completely consistent with the request side, ensuring that subsequent requests initiated by the client can carry the correct gateway session identifier. Through this mechanism, the target service gateway maintains the consistency of the session identifier on the client side, ensuring that subsequent requests from the same session can be accurately identified and routed to the correct service node using this gateway session identifier, ultimately achieving transparent session persistence in a high-availability cluster environment.
[0049] Figure 4 The session persistence method provided relies on a pre-built and cached session mapping table, which maps gateway session identifiers to session credentials of each business node in the monolithic application cluster. After confirming that the client session request carries the gateway session identifier and the session mapping table is cached in the service gateway, the request is forwarded to the monolithic application cluster with high availability random load balancing through the gateway node scheduling strategy, meeting the business layer's requirements for high system availability. At the same time, based on this mapping relationship, the session credentials corresponding to the target business node are quickly located and matched through the gateway session identifier, thereby completing the request update and forwarding. This ensures that no matter which business node the request is randomly forwarded to, that node can identify the client session identity through the injected session credentials, thus achieving session persistence for the monolithic application. Ultimately, it can effectively guarantee the session persistence function of the monolithic application while achieving high availability random load balancing.
[0050] In one alternative implementation, when the target service gateway receives a session request from a client and determines that the client's session request does not carry the gateway session identifier, the target service gateway will execute the following process: A1, The target service gateway performs security verification on the client based on the client's address.
[0051] The target service gateway stores a preset address whitelist, which contains a list of client network addresses that are allowed to access the service.
[0052] After receiving a session request from a client, the target service gateway extracts the client's network address and compares it with the address whitelist to perform security verification. If the network address of the client initiating the session request exists in the preset address whitelist, the client is determined to have passed security verification, and the process proceeds to step A2; if the network address of the client initiating the session request is not in the preset address whitelist, the client is determined to have failed security verification, and the target service gateway will return an authentication error message to the client.
[0053] A2, the target service gateway initiates authentication login requests to each business node in the monolithic application cluster based on the pre-stored username and password corresponding to the client, in order to log in to each business node in the monolithic application cluster.
[0054] A3, the target service gateway receives session credentials (i.e., cookie information corresponding to each business node) from each business node in the monolithic application cluster, and collects routing information for each business node. This routing information includes the node's address, service port, and business request path.
[0055] A4. The target service gateway constructs a session mapping table based on the routing information and session credentials of each business node in the monolithic application cluster.
[0056] Specifically, a high-entropy random number generator with encrypted security can be used. This high-entropy random number is then combined with the routing information of each business node in the monolithic application cluster to determine the gateway session identifier. Based on the gateway session identifier, the session credentials of each business node, and the routing information of each business node, a session mapping table is constructed. The form of the session mapping table is described in the preceding embodiments and will not be repeated here.
[0057] A5 caches the session mapping table in the target application gateway's local cache or distributed cache to support efficient querying and routing of subsequent requests.
[0058] A6 returns the gateway session identifier included in the session mapping table to the client through the Set-Cookie header in the HTTP response header, thereby completing the session initialization and synchronization process.
[0059] In one alternative implementation, when the target service gateway receives a session request initiated by the client and determines that the session request carries the gateway session identifier and that the target service gateway does not cache the session mapping table, the target service gateway performs the operations in A2-A6 to rebuild the session mapping table.
[0060] In one alternative implementation, the target service gateway also performs the following operations: B1. Determine whether the cache duration of the session mapping table in the target service gateway exceeds the preset target duration. B2. If the cache duration of the session mapping table in the target service gateway exceeds the target duration, then the cached session mapping table in the target service gateway is cleared.
[0061] This application sets the cache lifecycle of the session mapping table and periodically cleans up expired or long-unused session state information, thereby effectively managing the gateway's cache resources and preventing memory leaks or cache pollution caused by the accumulation of invalid or outdated session data.
[0062] In one optional implementation, if a business node in a monolithic application cluster updates its session credentials in the response after processing a request (e.g., by resetting session identifiers such as JSESSIONID in the Set-Cookie header), the target service gateway will intercept the Set-Cookie header in that response. Subsequently, the target service gateway updates the corresponding node's session credentials in the cached session mapping table based on the routing information of that business node, and generates a new session mapping table based on the updated credentials to maintain consistency between the session state at the gateway level and the actual session state of the backend nodes.
[0063] Figure 5 This is an interactive diagram illustrating a session persistence method based on a target service gateway, provided as an embodiment of this application. The following will combine... Figure 5 This paper provides a detailed description of the complete session processing flow between the client, the target service gateway (including the session management module and the routing and forwarding module), and the monolithic application cluster, to further illustrate the working mechanism and beneficial effects of the session persistence method provided in this application in specific implementations. Combined with... Figure 5 As shown, the complete process of the session persistence method provided in this application is as follows: S501, Send session request.
[0064] The client sends a session request to the target service gateway.
[0065] S502, determine whether the session request carries the gateway session identifier.
[0066] Upon receiving a session request from a client, the session management module in the target service gateway parses the session request header and cookie information to determine whether the session request carries a gateway session identifier. If the session request does not carry a gateway session identifier (i.e., GS-ID), the process proceeds to step S503; otherwise, it proceeds to step S504.
[0067] S503, determine whether the session mapping table is cached in the target service gateway.
[0068] The session management module in the target service gateway determines whether the target service gateway caches the session mapping table. If the session management module determines that the target service gateway does not cache the session mapping table, it proceeds to S505; if the session management module determines that the target service gateway caches the session mapping table, it proceeds to S510.
[0069] S504 determines whether the client has passed security verification.
[0070] The session management module in the target service gateway performs security verification on the client based on the client's address. See the description of step A1 in the previous example.
[0071] If the client passes the security authentication, it proceeds to S505; if the client fails the security authentication, it proceeds to S509.
[0072] S505, logs in to each business node in a monolithic application cluster.
[0073] The session management module in the target service gateway logs into each business node in the monolithic application cluster based on the pre-stored username and password corresponding to the client. See the description of step A2 in the previous example.
[0074] S506, Feedback Session Credentials.
[0075] Each business node in a monolithic application cluster processes session requests, generates session credentials, and sends the session credentials back to the session management module in the target service gateway.
[0076] S507, build and cache session mapping table.
[0077] The session management module in the target service gateway receives session credentials from each business node in the monolithic application cluster and constructs a session mapping table based on the routing information and session credentials of each business node. The session management module in the target service gateway caches the session mapping table in the target application gateway's local cache or distributed cache to support efficient querying and routing of subsequent requests.
[0078] S508 sends the gateway session identifier to the client.
[0079] The session management module in the target service gateway returns the gateway session identifier (GS-ID) to the client via the Set-Cookie header. For example, if GS-ID=abc123, then abc123 will be returned to the client via the Set-Cookie header.
[0080] S509 sends an authentication error message to the client.
[0081] The session management module in the target service gateway sends an authentication error message to the client.
[0082] S510, retrieve the session mapping table.
[0083] The session management module in the target service gateway retrieves the session mapping table. Specifically, the session management module queries the corresponding session mapping table from the target service gateway's cache based on the GS-ID. This session mapping table includes the session credentials of all application nodes in the monolithic application cluster.
[0084] S511, obtain gateway session identifier.
[0085] The routing and forwarding module in the target service gateway parses the session request sent by the client and obtains the gateway session identifier.
[0086] S512, determine the session credentials corresponding to the target service node.
[0087] The routing and forwarding module in the target service gateway selects a backend node from the monolithic application cluster as the target application node for handling session requests, based on the configured gateway node scheduling strategy, such as a load balancing strategy. The routing and forwarding module in the target service gateway determines the session credentials corresponding to the target service node based on the gateway session identifier and the session mapping table.
[0088] For example, the gateway session identifier obtained by the routing and forwarding module is abc123; the target application node determined based on the load balancing strategy is node1; then the routing and forwarding module queries the session mapping table to obtain the session credential corresponding to the gateway session identifier abc123 as cookie1. This credential is a session identifier issued and recognized by node1 in a previous session.
[0089] S513 updates the session request based on the session credentials corresponding to the target business node to obtain the target session request.
[0090] The routing and forwarding module in the target service gateway updates the session request based on the session credentials corresponding to the target service node, thus obtaining the target session request.
[0091] S514 routes the target session request to the target business node.
[0092] The routing and forwarding module routes the target session request to the target business node.
[0093] S515: The target business node processes the target session request and generates a business response.
[0094] The target business node processes the target session request and generates a business response.
[0095] S516, obtain the business response returned by the target business node.
[0096] The routing and forwarding module in the target service gateway obtains the service response from the target service node.
[0097] S517 sends a service response carrying the gateway session identifier to the client.
[0098] The session management module in the target service gateway sends a service response carrying the gateway session identifier to the client.
[0099] S518, obtain and transmit the updated session credentials.
[0100] The routing and forwarding module in the target service gateway obtains the updated session credentials generated by each business node in the monolithic application cluster and transmits the updated session credentials to the session management module.
[0101] S519 builds and caches the updated session mapping table.
[0102] The session management module in the target service gateway constructs and caches the updated session mapping table based on the updated session credentials and routing information of each node in the monolithic application cluster; and sends the gateway session identifier to the client. This process is described in step A4 and will not be repeated here.
[0103] In summary, Figure 5 The main function of the session management module in the target service gateway is to generate and maintain a gateway session identifier (Gateway-Session-ID) and establish a cache mapping relationship between this identifier and the session credentials (Node-Session-Cookie) of all backend nodes. The main function of the routing and forwarding module in the target service gateway is to select backend nodes according to the load balancing strategy and dynamically replace or add the corresponding session credentials of the node when forwarding requests.
[0104] Based on the above description, the session maintenance method disclosed in this application has at least the following advantages: First, this application establishes an association between gateway session identifiers and session credentials of each business node by maintaining a session mapping table within the target service gateway. This ensures that even when the gateway employs stateless load balancing strategies such as random or round-robin, it can still correctly route requests for the same session to the corresponding business node. This mechanism guarantees high availability and scalability while avoiding the architectural complexity and operational burden caused by introducing additional caching components. In other words, the solution in this application can achieve highly available load balancing for a monolithic application cluster supporting session persistence within the target service gateway without relying on external caching middleware.
[0105] Second, during the session initialization phase, the target service gateway in this application completes authentication with all business nodes in the cluster at once based on the client's credentials (username and password) and obtains session credentials issued by each node. In subsequent requests, the client only needs to carry the session identifier issued by the gateway to maintain the authentication status across the cluster, significantly reducing the number of authentication interactions and network overhead. In other words, the solution in this application, based on a unified session synchronization mechanism, achieves automatic authentication of each business node within a monolithic application cluster, eliminating the need for the client to repeatedly initiate authentication requests.
[0106] Third, the client only provides authentication information on the first request. Thereafter, it only needs to include the session identifier returned by the target service gateway in the request. All session-related state synchronization, credential management, and request routing are handled transparently by the gateway, improving the simplicity and security of business calls. In other words, the client in this application is unaware of the underlying session and authentication mechanisms, achieving implicit session transmission based on cookies.
[0107] Fourth, session mapping, synchronization, and routing are implemented entirely at the target service gateway level. Existing clients and backend business nodes require almost no code or configuration adjustments to achieve session persistence and high availability load balancing without intrusion, significantly reducing the cost and risk of system upgrades and integration. In other words, clients and business nodes in the monolithic application cluster can smoothly connect to the high availability gateway load balancing architecture without major modifications.
[0108] Based on the same inventive concept, this application also provides a conversation holding device. Figure 6 This is a schematic diagram of a session holding device provided in an embodiment of this application. (In conjunction with...) Figure 6 As shown, the session holding device 600 provided in this application includes: The target service node determination module 601 is used to determine the target service node in the monolithic application cluster that is used to process the session request after the client sends a session request carrying a gateway session identifier and the target service gateway caches a session mapping table. The target session credential determination module 602 is used to determine the session credential corresponding to the target service node through the gateway session identifier and the session mapping table; the session mapping table includes the correspondence between the gateway session identifier and the session credentials of the service nodes in the monolithic application cluster. The target session request generation module 603 is used to update the session request based on the session credentials corresponding to the target service node to obtain the target session request; The target session request processing module 604 is used to route the target session request to the target business node for processing and generate a business response. The service response feedback module 605 is used to obtain the service response returned by the target service node and send the service response carrying the gateway session identifier to the client.
[0109] In one alternative implementation, the session persistence device 600 includes: The first business node login module is used to log in to each business node based on the pre-stored username and password corresponding to the client if it is determined that the session request does not carry the gateway session identifier. The first session credential receiving module is used to receive session credentials fed back by each of the service nodes; The first session mapping table construction module is used to construct the session mapping table based on the routing information of each service node and the session credentials of each service node; the routing information includes address, service port and service request path; The first information caching module is used to cache the session mapping table to the target service gateway; The first identifier sending module is used to send the gateway session identifier to the client.
[0110] In one alternative implementation, the session persistence device 600 includes: The second business node login module is used to log in to each business node based on the pre-stored username and password corresponding to the client if the session request carries the gateway session identifier and the session mapping table is not cached in the target service gateway. The second session credential receiving module is used to receive session credentials fed back by each of the service nodes; The second session mapping table construction module is used to construct the session mapping table based on the routing information of each service node and the session credentials of each service node; the routing information includes address, service port and service request path; The second information caching module is used to cache the session mapping table to the target service gateway; The second identifier sending module is used to send the gateway session identifier to the client.
[0111] In one optional implementation, the first business node login module includes: A security verification unit is used to perform security verification on the client based on the client's address; A node login unit is used to log in to each of the business nodes based on the username and the password if the client passes the security verification. An error message feedback unit is used to send an authentication error message to the client if the client fails the security verification.
[0112] In one alternative implementation, the session persistence device 600 further includes: The cache duration determination module is used to determine whether the cache duration of the session mapping table in the target service gateway exceeds the preset target duration; The gateway clearing module is used to clear the cached session mapping table in the target service gateway if the cache duration of the session mapping table in the target service gateway exceeds the target duration.
[0113] In one alternative implementation, the first session mapping table construction module and the second session mapping table construction module include: The identifier generation unit is used to determine the gateway session identifier based on a high-entropy random number and the routing information of each service node; The mapping table construction unit is used to construct the session mapping table based on the gateway session identifier, the session credentials of each service node, and the routing information of each service node.
[0114] In one alternative implementation, the session persistence device 600 further includes: The mapping table update module is used to generate an updated session mapping table based on the updated session credentials if the session credentials of the service node are updated.
[0115] Based on the session persistence method and apparatus provided in the foregoing embodiments, this application also provides a computer-readable storage medium storing a computer program thereon, which, when executed by a processor, implements some or all of the steps in the aforementioned session persistence method.
[0116] Based on the session persistence method and apparatus provided in the foregoing embodiments, this application also provides an electronic device, including: A memory on which computer programs are stored; A processor is configured to execute the computer program in the memory to implement some or all of the steps in the session persistence method provided in the foregoing embodiments.
[0117] It should be noted that the various embodiments in this specification are described in a progressive manner, and the same or similar parts between the various embodiments can be referred to mutually. Each embodiment focuses on describing the differences from other embodiments. In particular, for the device embodiments, since they are basically similar to the method embodiments, the description is relatively simple, and the relevant parts can be referred to the description of the method embodiments. The device embodiments described above are merely illustrative, and the units described as separate components may or may not be physically separate. The components indicated as units may or may not be physical units, that is, they may be located in one place or distributed across multiple network units. Some or all of the modules can be selected to achieve the purpose of this embodiment solution according to actual needs. Those skilled in the art can understand and implement this without creative effort.
[0118] The above description is merely one specific embodiment of this application, but the scope of protection of this application is not limited thereto. Any variations or substitutions that can be easily conceived by those skilled in the art within the technical scope disclosed in this application should be included within the scope of protection of this application. Therefore, the scope of protection of this application should be determined by the scope of the claims.
Claims
1. A session persistence method, characterized in that, The method is applied to a target service gateway, and the method includes: After determining that the session request sent by the client carries a gateway session identifier and that the target service gateway caches a session mapping table, the target business node in the monolithic application cluster for processing the session request is determined through the gateway node scheduling strategy. The session credentials corresponding to the target service node are determined using the gateway session identifier and the session mapping table; the session mapping table includes the correspondence between the gateway session identifier and the session credentials of the service nodes in the monolithic application cluster. The session request is updated based on the session credentials corresponding to the target service node to obtain the target session request; The target session request is routed to the target service node for processing, and a service response is generated. Obtain the service response fed back by the target service node, and send the service response carrying the gateway session identifier to the client.
2. The method according to claim 1, characterized in that, The method further includes: If it is determined that the session request does not carry the gateway session identifier, then each of the service nodes is logged in based on the pre-stored username and password corresponding to the client; Receive session credentials from each of the aforementioned service nodes; The session mapping table is constructed based on the routing information and session credentials of each service node; the routing information includes address, service port and service request path; Cache the session mapping table to the target service gateway; Send the gateway session identifier to the client.
3. The method according to claim 1, characterized in that, The method further includes: If the session request carries the gateway session identifier and the target service gateway does not cache the session mapping table, then each of the business nodes is logged in based on the pre-stored username and password corresponding to the client. Receive session credentials from each of the aforementioned service nodes; The session mapping table is constructed based on the routing information and session credentials of each service node; the routing information includes address, service port and service request path; Cache the session mapping table to the target service gateway; Send the gateway session identifier to the client.
4. The method according to claim 2, characterized in that, If it is determined that the session request does not carry the gateway session identifier, then based on the pre-stored username and password corresponding to the client, log in to each of the service nodes, including: Based on the client's address, perform security verification on the client; If the client passes the security verification, it logs into each of the business nodes based on the username and password. If the client fails the security verification, an authentication error message is sent to the client.
5. The method according to any one of claims 1-4, characterized in that, Also includes: Determine whether the cache duration of the session mapping table in the target service gateway exceeds the preset target duration; If the cache duration of the session mapping table in the target service gateway exceeds the target duration, then the cached session mapping table in the target service gateway is cleared.
6. The method according to claim 2 or 3, characterized in that, The step of constructing the session mapping table based on the routing information and session credentials of each service node includes: The gateway session identifier is determined based on high-entropy random numbers and routing information of each service node; The session mapping table is constructed based on the gateway session identifier, the session credentials of each service node, and the routing information of each service node.
7. The method according to claim 6, characterized in that, The method further includes: If the session credentials of the business node are updated, an updated session mapping table is generated based on the updated session credentials.
8. The method according to claim 1, characterized in that, The gateway node scheduling strategy is a round-robin strategy, a random strategy, or a load balancing strategy.
9. A conversation holding device, characterized in that, Applied to a target service gateway, the device includes: The target service node determination module is used to determine the target service node in the monolithic application cluster that is used to process the session request after the client sends a session request carrying a gateway session identifier and the target service gateway has a cached session mapping table. The target session credential determination module is used to determine the session credential corresponding to the target service node through the gateway session identifier and the session mapping table; the session mapping table includes the correspondence between the gateway session identifier and the session credentials of the service nodes in the monolithic application cluster. The target session request generation module is used to update the session request based on the session credentials corresponding to the target service node to obtain the target session request; The target session request processing module is used to route the target session request to the target business node for processing and generate a business response. The service response feedback module is used to obtain the service response fed back by the target service node and send the service response carrying the gateway session identifier to the client.
10. An electronic device, characterized in that, include: A memory on which computer programs are stored; A processor for executing the computer program in the memory to implement the steps of the method according to any one of claims 1-8.