Request processing method and device, computing equipment and computer storage medium
By obtaining and filtering service instances in the gateway, using custom load balancing components to realize multi-instance sharing of WebSocketSession, it solves the problem that WebSocket connection cannot be shared, and improves load balancing effect and real-time communication efficiency.
Patent Information
- Application Number
- CN202311609347.1
- Authority / Receiving Office
- CN · China
- Patent Type
- Applications(China)
- Current Assignee / Owner
- Filing Date
- 2023-11-28
- Publication Date
- 2025-01-10
AI Technical Summary
WebSocket connections cannot be shared among service instances, resulting in poor load balancing effect and the inability to realize multiple instance sharing of WebSocketSession, which affects the scalability and efficiency of real-time communication.
By obtaining the service instance list in the gateway, filtering out service instances that match the target service instance, generating and sending requests, using custom load balancing components to realize multi-instance sharing of WebSocketSession, ensuring that requests can be routed and processed correctly.
It improves the scalability and practicality of WebSocket, solves the problem that WebSocketSession cannot be serialized, realizes the improvement of load balancing effect, and meets the needs of real-time communication.
Smart Images

Figure CN119299437A_ABST
Abstract
Description
Technical Field
[0001] The embodiments of the present application relate to the field of computer technology, and in particular to a request processing method, apparatus, computing device, and computer storage medium. Background Art
[0002] With the continuous development of Internet technology, more and more applications need to achieve real-time communication, such as online games, online chats, stock trading, etc.
[0003] The traditional polling method can no longer meet the needs of real-time communication, so WebSocket technology has emerged. WebSocket is a full-duplex communication protocol that can achieve instant two-way communication after establishing a connection, without the need to continuously send requests like the polling method.
[0004] However, when WebSocket technology is used instead of the polling method, WebSocket connections cannot be shared between service instances, resulting in poor load balancing. Summary of the invention
[0005] The embodiments of the present application provide a request processing method, apparatus, computing device and computer storage medium to solve the problem of poor load balancing effect in the prior art.
[0006] In a first aspect, an embodiment of the present application provides a request processing method, which is applied to a gateway, including:
[0007] After the second service processes the first request sent by the first service and calls back the service interface of the first service, obtaining the IP address of the first service instance carried in the first request;
[0008] Get a list of available service instances;
[0009] Filtering a second service instance matching the IP address of the first service instance from the service instance list to generate a second request;
[0010] The second request is sent to the second service instance, so that the second service instance performs corresponding processing according to the second request.
[0011] Optionally, generating the second request includes:
[0012] The resource identifier corresponding to the first service instance in the first request is replaced with the resource identifier corresponding to the second service instance to generate a second request message.
[0013] Optionally, before filtering out a second service instance matching the IP address of the first service instance from the service instance list to generate a second request, the method further includes:
[0014] Configure a custom load balancing component, and define the execution order of the custom load balancing component as the first order;
[0015] Determining whether the first service is a callback-type service, where the callback-type service refers to a service having a callback-type service interface;
[0016] If so, performing the step of filtering out a second service instance matching the IP address of the first service instance from the service instance list to generate a second request;
[0017] Among them, the filtering out a second service instance that matches the IP address of the first service instance from the service instance list to generate a second request includes: filtering out a second service instance that matches the IP address of the first service instance from the service instance list, and after obtaining the matching second service instance, modifying the execution order of the source code load balancing component to a second order, the execution priority of the second order is lower than the first order, and generating a second request according to the determined second service instance through the custom load balancing component.
[0018] Optionally, it also includes:
[0019] If not, the execution order of the source code load balancing component is modified to a second order, the execution priority of the second order is lower than the first order, and a second request is generated through the custom load balancing component.
[0020] Optionally, it also includes:
[0021] Determining whether the resource identifier in the second request includes a resource identifier of a service instance that matches the first service instance;
[0022] If yes, continue to execute the step of sending the second request to the second service instance so that the second service instance performs corresponding processing according to the second request;
[0023] If not, continue to perform the step of filtering out a second service instance that matches the IP address of the first service instance from the service instance list to generate a second request.
[0024] Optionally, it also includes:
[0025] Obtaining the uniform resource locator of the first request through the custom load balancing component;
[0026] Obtaining a corresponding protocol prefix from the uniform resource locator;
[0027] If the uniform resource locator is empty, or the protocol prefix is a non-specified string, the execution order of the source code load balancing component is modified to a second order, the execution priority of the second order is lower than the first order, and the second service instance is re-determined through the custom load balancing component to generate a second request.
[0028] Optionally, it also includes:
[0029] Determining, by the custom load balancing component, whether the determined second service instance is empty;
[0030] If so, continue to execute the step of modifying the execution order of the source code load balancing component to a second order, the execution priority of the second order is lower than the first order, and generating a second request through the custom load balancing component.
[0031] In a second aspect, an embodiment of the present application provides a request processing device, which is applied to a gateway, including:
[0032] an acquisition module, configured to acquire the IP address of the first service instance carried in the first request after the second service processes the first request sent by the first service and calls back the service interface of the first service; and acquire a list of available service instances;
[0033] A generating module, configured to filter out a second service instance matching the IP address of the first service instance from the service instance list to generate a second request;
[0034] A sending module is used to send the second request to the second service instance so that the second service instance performs corresponding processing according to the second request.
[0035] In a third aspect, an embodiment of the present application provides a computing device, comprising a processing component and a storage component; the storage component stores one or more computer instructions; the one or more computer instructions are used to be called and executed by the processing component to implement the request processing method described in the first aspect above.
[0036] In a fourth aspect, an embodiment of the present application provides a computer storage medium storing a computer program, which, when executed by a computer, implements the request processing method described in the first aspect above.
[0037] In the embodiment of the present application, after the second service processes the first request sent by the first service and calls back the service interface of the first service, the IP address of the first service instance carried in the first request is obtained; a list of available service instances is obtained; a second service instance matching the IP address of the first service instance is selected from the list of service instances to generate a second request; the second request is sent to the second service instance so that the second service instance performs corresponding processing according to the second request. The technical solution provided in the embodiment of the present application can improve the effect of load balancing.
[0038] These and other aspects of the present application will become more clearly understood in the description of the following embodiments. BRIEF DESCRIPTION OF THE DRAWINGS
[0039] In order to more clearly illustrate the embodiments of the present application or the technical solutions in the prior art, a brief introduction will be given below to the drawings required for use in the embodiments or the description of the prior art. Obviously, the drawings described below are some embodiments of the present application. For ordinary technicians in this field, other drawings can be obtained based on these drawings without paying any creative work.
[0040] Figure 1 A schematic diagram of the architecture of a request processing system provided in an embodiment of the present application;
[0041] Figure 2 A flowchart of a request processing method provided in an embodiment of the present application;
[0042] Figure 3 A schematic diagram of the structure of a request processing device provided in an embodiment of the present application;
[0043] Figure 4 A schematic diagram of the structure of a computing device provided in an embodiment of the present application. DETAILED DESCRIPTION
[0044] In order to enable those skilled in the art to better understand the solution of the present application, the technical solution in the embodiments of the present application will be clearly and completely described below in conjunction with the drawings in the embodiments of the present application.
[0045] In some of the processes described in the specification and claims of this application and the above-mentioned figures, multiple operations that appear in a specific order are included, but it should be clearly understood that these operations may not be executed in the order in which they appear in this article or executed in parallel. The serial numbers of the operations, such as 101, 102, etc., are only used to distinguish between different operations, and the serial numbers themselves do not represent any execution order. In addition, these processes may include more or fewer operations, and these operations may be executed in sequence or in parallel. It should be noted that the descriptions of "first", "second", etc. in this article are used to distinguish different messages, devices, modules, etc., do not represent the order of precedence, and do not limit the "first" and "second" to be different types.
[0046] The embodiment of the present application is mainly applied to real-time communication scenarios, and specifically can be a real-time communication function implemented based on a default load balancing algorithm and a custom load balancing algorithm. The default load balancing algorithm and the custom load balancing algorithm are used to implement multi-instance sharing of WebSocketSession.
[0047] Among them, WebSocketSession is an object that represents a WebSocket connection. It is an important concept when using WebSocket technology in Java. The WebSocketSession object is created after the WebSocket connection is established and exists throughout the life cycle of the connection. It contains information and methods related to the connection and can be used to exchange data with the client, send messages, and other operations. Through the WebSocketSession object, the server can communicate with the client in real time, send and receive messages, and realize the functions of real-time data exchange and instant communication.
[0048] In actual development, there is such a scenario: after the client (which can be HTML5, application, or other terminals that can initiate calls) calls a service A, service A calls service B, and service B takes a long time. The traditional polling method is that service A constantly queries the processing result query interface provided by service B. However, the method of reducing latency by increasing the polling frequency cannot truly achieve real-time communication. In addition, since data must be transferred for each call, the polling method may generate high network traffic and latency, and since the client continuously sends requests, the server needs to process each request, which increases the pressure on the server.
[0049] Therefore, the polling method is abandoned, and the callback method can be used for service B to actively notify service A of the processing result. In this case, a WebSocket connection is established between service A and the client, and an http connection is established between service A and service B. The callback method is used to avoid unprocessed requests occupying the Http connection between service A and service B, while avoiding the disadvantages of polling. That is, after service A routes the request to service B, service B immediately returns a successful acceptance. After service B completes the processing, it calls the processing result notification interface of service A, and service A sends the processing result to the client.
[0050] In the above scenario, the callback method and WebSocket technology are independent of each other, but the two can be used in combination to improve the efficiency of real-time communication. Specifically, WebSocket technology can establish a real-time two-way communication channel between the client and the server, so that the client and the server can exchange data at any time, thus realizing true real-time communication. In addition, the WebSocket connection is persistent, which reduces unnecessary network traffic and delays compared to the polling method, saves server resources, and reduces the burden on the server.
[0051] The callback method allows service B to actively notify service A of the processing results after the processing is completed, avoiding the situation where service A constantly polls service B, thereby improving the efficiency of real-time communication. Therefore, the callback method and WebSocket technology can be used in combination to improve the efficiency of real-time communication and solve the problems of the traditional polling method.
[0052] In addition, the above scenario can also be abstracted into the level of actively sending messages to users. This solution is applicable as long as it is necessary to actively send messages to users after the users have established a WebSocket connection.
[0053] The inventor has found that after using the combination of WebSocket technology and callback mode instead of polling mode, since the WebSocket connection cannot be shared between instances, a new problem is brought about for server cluster deployment: Assuming that the first service on the server side has service instance 1 and service instance 2, and the second service also has multiple instances, the client interacts with the first service, and the first service interacts with the second service. Assuming that the client establishes a WebSocket connection with service instance 1 in the first service, and the WebSocket connection cannot be shared between service instance 1 and service instance 2, that is, the response of the second service must be sent to service instance 1 in the first service, and then service instance 1 returns to the client through the WebSocket connection. If the response of the second service is sent to service instance 2 in the first service, and service instance 2 cannot find the WebSocket connection, it cannot send the response to the client. Therefore, the second service needs to directly send the response to the correct instance or send it to service instance 2, and then service instance 2 forwards the response to service instance 1, and then service instance 1 forwards it to the client.
[0054] Among them, the process of service instance 2 forwarding the response to service instance 1 can be implemented based on message queue (MessageQueue, MQ), open source memory database (Remote Ditionay Server, Redis) or rewriting RestTemplate (a template class for sending HTTP requests). The second service directly sends the request to the correct instance, which can be implemented using the gateway's custom load balancing algorithm.
[0055] The main reason why WebSocket cannot be shared by multiple instances is that WebSocketSession cannot be serialized. The main reason why WebSocketSession cannot be serialized is that it contains internal states related to the underlying WebSocket connection, thread context, and may depend on other external resources or states. Therefore, directly serializing and deserializing WebSocketSession may lead to problems such as failure to correctly restore the connection state, thread-related problems, or failure to correctly initialize or maintain a consistent state for external dependencies.
[0056] Therefore, in response to the problems existing in the combination of WebSocket technology and callback method, the present application provides a request method, which includes: after the second service processes the first request sent by the first service and calls back the service interface of the first service, obtaining the IP address of the first service instance carried in the first request; obtaining a list of available service instances; filtering out a second service instance that matches the IP address of the first service instance from the list of service instances to generate a second request; and sending the second request to the second service instance so that the second service instance can perform corresponding processing based on the second request.
[0057] Through this request method, multiple instances of WebSocketSession can be shared, thereby solving the problem that WebSocketSession cannot be serialized and improving the scalability and practicality of WebSocket.
[0058] The following will be combined with the drawings in the embodiments of the present application to clearly and completely describe the technical solutions in the embodiments of the present application. Obviously, the described embodiments are only part of the embodiments of the present application, not all of the embodiments. Based on the embodiments in the present application, all other embodiments obtained by those skilled in the art without creative work are within the scope of protection of this application.
[0059] Figure 1 A schematic diagram of the architecture of a request processing system provided in an embodiment of the present application is shown in FIG. Figure 1 As shown, the system includes: a first service A, a second service B, a gateway C and a client D.
[0060] The server is a computer or computer cluster that carries a service, and may be a physical server or a cloud server. The first service A and the second service B are two independent services running on the server (not shown in the figure).
[0061] There is a calling relationship between the first service A and the second service B, that is, the first service A needs to call the second service B to complete certain business logic. The first service A can communicate with the second service B through HTTP connection or other communication protocols to request the interface or function provided by the second service B.
[0062] exist Figure 1 In the example, a WebSocket connection is established between client D and first service A; an HTTP connection is established between first service A and second service B;
[0063] In the system, the client D sends a first request to the first service A, and the request message of the first request carries the IP address of the first service instance;
[0064] Through the callback method, after the gateway C forwards the first request to the second service B, the second service B returns a request acceptance success message after receiving the first request. After the second service B completes the processing, it calls the processing result notification interface of the first service A, and the first service A sends the processing result to the client D.
[0065] Furthermore, gateway C obtains a list of available service instances. Specifically, the list of available service instances can be obtained through a source code load balancing component, wherein the source code load balancing component is a filter in gateway C for implementing a load balancing function based on service registration and discovery.
[0066] Gateway C selects a second service instance matching the first service instance from the service instance list to generate a second request, and sends the second request to the second service instance so that the second service instance performs corresponding processing according to the second request.
[0067] Specifically, gateway C replaces the Uniform Resource Identifier (URI) corresponding to the first service instance in the first request with the Uniform Resource Identifier (URI) corresponding to the second service instance to generate a second request message.
[0068] Based on the above system, client D can communicate with the first service A in real time, and implement request forwarding and load balancing through gateway C, so that the first service A can call the second service B to complete business logic processing and notify the result to client D. This can improve the real-time, performance and availability of the system and meet the needs for real-time communication and request processing in actual development.
[0069] Figure 2 A flowchart of a request processing method provided in an embodiment of the present application is shown as follows: Figure 2 As shown, this method is applied to Figure 1 Gateway C of the system shown;
[0070] The method includes:
[0071] 102. After the second service processes the first request sent by the first service and calls back the service interface of the first service, obtain the IP address of the first service instance carried in the first request;
[0072] In this step, the first service is an independent module or component in the server, responsible for processing specific business logic or functions. The second service is another independent module or component in the server, corresponding to the first service, and may have different functions or processing logic. The first request refers to a request initiated by the client, which contains specific information and parameters that need to be processed. Callback refers to a mechanism that after processing the first request, the second service returns the processing result to the first service by calling the service interface of the first service.
[0073] In the embodiment of the present application, it is assumed that there is a social media platform, which includes user services and message services. The user service is the first service, which is responsible for functions such as user registration and login; the message service is the second service, which is responsible for sending messages to users and processing messages.
[0074] In an embodiment, the user service sends a first request to the message service, requesting the message service to send a message to a specified user. After processing the request, the message service calls the service interface of the user service by callback to notify the user service of the result of the message sending.
[0075] In step 102, the IP address of the first service instance carried in the first request is obtained from the gateway routing configuration. This means that the gateway is configured with routing rules, and the request is routed to the corresponding first service instance for processing according to some information in the first request (such as request path, request header, etc.). The gateway can extract the IP address of the first service instance from the request according to the routing rules to ensure that the request is correctly sent to the corresponding service instance for processing according to the IP address.
[0076] In the above steps, the first request is generated by the client in response to the user operation. To process the first request, it is necessary to send the first request to the first service (this is because a WebSocket connection is established between the client and the first service. If a WebSocket connection is established between the client and the second service, the first request will be sent to the second service), and the first request carries the IP address of the corresponding first service instance. At this time, the first service instance is the correct instance to process the first request. When the first request is forwarded to the second service, the second service processes the first request. To avoid the problem of resource occupation caused by the polling method, the second service can return a request acceptance success message after receiving the first request.
[0077] 104. Get a list of available service instances;
[0078] In this step, the gateway obtains a list of available service instances through a load balancing algorithm.
[0079] In the embodiment of the present application, as a possible implementation scheme, the process of obtaining the list of available service instances may include:
[0080] First, the gateway needs to interact with the registry to obtain a list of available service instances. The registry can be an independent component that manages and maintains information about each service instance, such as using open source registry tools such as Zookeeper or Consul. The gateway needs to query the registry for a list of available service instances. When a service instance starts, it registers its own information with the registry, including the service name, IP address, port number, etc. For example, this can be achieved using the registry client provided by microservice frameworks such as Spring Cloud.
[0081] Secondly, the gateway needs to select a suitable service instance to handle the request based on the load balancing algorithm. Common load balancing algorithms include polling, random, weighted polling, weighted random, minimum number of connections, etc. For example, use the source code load balancing component provided by SpringCloud to implement it.
[0082] Furthermore, the gateway needs to perform a health check on the service instance to determine whether it is available. For example, the health status of the service instance can be checked by sending a heartbeat message to the service instance or making an HTTP request. In addition, to avoid frequently querying the registry for a list of available service instances, the gateway can cache the query results and update the cache content regularly. For example, use the Eureka client provided by Spring Cloud to implement service instance caching.
[0083] Through the above steps, the gateway can obtain a list of available service instances and select a suitable service instance to process the request based on the load balancing algorithm. This can achieve load balancing of service instances and improve the stability and reliability of the system.
[0084] 106. Filter out a second service instance that matches the IP address of the first service instance from the service instance list to generate a second request;
[0085] In this step, when performing service instance load balancing, a second service instance that matches the IP address of the first service instance is selected from the service instance list to process the next request. This step is usually implemented in the load balancing algorithm to distribute requests to different service instances to avoid excessive load on a single service instance.
[0086] Optionally, before step 106, step 105 is further included, wherein step 105 may include the following steps:
[0087] 1051. Configure a custom load balancing component, and define the execution order of the custom load balancing component as the first order;
[0088] In this step, in order to implement a custom load balancing algorithm, a custom load balancing component can be configured in the client or server, and then its execution order is set to the first order, such as 10101 (where the smaller the value, the higher the execution order), to ensure that the component will be executed before other load balancing components (such as source code load balancing components). The specific implementation can define a class or function in the code to implement the custom load balancing algorithm. Then configure the execution order of the component to the load balancer.
[0089] For example, customize CallbackLBGatewayFilter, where CallbackLBGatewayFilter inherits from LoadBalancerClientFilter, the order value is set to 10101, and the discoveryClient is injected. The discoveryClient searches for instances in the cluster based on the IP field in the request header.
[0090] Among them, CallbackLBGatewayFilter is a filter of Spring Cloud Gateway, which is used to implement load balancing of callback-type services. After the request reaches Spring Cloud Gateway, it can find the corresponding service instance according to the service name and callback address in the request, and route the request to the instance. At the same time, it can also return the processing result to the callback address of the client after the service instance completes the processing. The order value represents its execution order, such as the execution order is set to the first order, such as the first order is 10101.
[0091] The core code is as follows:
[0092]
[0093] The above code defines a class called CallbackLBGatewayFilter, which inherits from the LoadBalancerClientFilter class. The CallbackLBGatewayFilter class contains a static log variable for logging. In addition, the class also has a discoveryClient member variable for service discovery. The CallbackLBGatewayFilter class also has a constructor that accepts three parameters: loadBalancer, properties, and discoveryClient. This constructor calls the constructor of the parent class LoadBalancerClientFilter and passes loadBalancer and properties to the parent class constructor. At the same time, it assigns the passed discoveryClient to the discoveryClient member variable of the class.
[0094] The above code overrides the getOrder() method in the parent class LoadBalancerClientFilter. The getOrder() method returns an int value, which is used to specify the execution order of the filter. In this example, the getOrder() method returns 10101, indicating that the execution order of the filter is 10101. The execution order of the filter is determined by the return value. A smaller return value indicates an earlier executed filter, while a larger return value indicates a later executed filter. By specifying different return values, you can control the execution order of the filter to meet business needs.
[0095] 1052. Determine whether the first service is a callback service. If so, execute step 106; if not, execute step 1053;
[0096] In this step, the callback service refers to a service with a callback service interface. It is determined whether the current service is a callback service. If it is a callback service, special processing is required to implement the callback function. If it is not a callback service, load balancing can be performed directly.
[0097] If it is determined that the first service is a callback service, step 106 is executed, that is, a second service instance matching the IP address of the first service instance is screened out from the service instance list to generate a second request.
[0098] At this time, step 106 may include the following specific plan: filtering out a second service instance that matches the IP address of the first service instance from the service instance list, and after obtaining the matching second service instance, modifying the execution order of the source code load balancing component to a second order, the execution priority of the second order is lower than the first order, and generating a second request through the custom load balancing component according to the determined second service instance.
[0099] In the above specific solution, in order to find a second service instance matching the first service instance from the service instance list and generate a second request, the execution order of the source code load balancing component is set to the second order, so that the execution order of the source code load balancing component is later. At this time, the execution order of the custom load balancing component takes precedence over the source code load balancing component. Therefore, the custom load balancing component generates the second request according to the determined second service instance, thereby ensuring that the second request can be processed correctly.
[0100] As a possible implementation method, in the process of generating the second request, the resource identifier corresponding to the first service instance in the first request may be replaced with the resource identifier corresponding to the second service instance to generate a second request message.
[0101] In the above process, when performing service instance load balancing, the client needs to send a request to one of the service instances. When the first service instance is selected, the gateway can filter out another service instance that matches the first service instance from the service instance list. In order to send a request to this service instance, the gateway needs to construct a new request message and replace the resource identifier in the first request with the resource identifier corresponding to the second service instance. In this way, the second request can be sent to the new service instance to achieve load balancing.
[0102] In specific implementation, the request message can be modified in the gateway code. For example, the resource identifier in the first request message can be replaced with the resource identifier corresponding to the second service instance using the string replacement method. After the replacement, the gateway can use the modified request message to send a request to the new service instance. In this way, service instance load balancing can be achieved.
[0103] The core code of this step is as follows;
[0104]
[0105]
[0106] In the above code, a service instance is selected based on the ServerWebExchange object passed in. First, the Uniform Resource Identifier (URI) of the first request is obtained through exchange.getAttribute(GATEWAY_REQUEST_URL_ATTR), and then the host name (serviceId) is obtained from the URI. Then, discoveryClient.getInstances(serviceId) is used to obtain the list of service instances from the service discovery client (discoveryClient). Next, the first value named "ip" in the request header is obtained through exchange.getRequest().getHeaders().get("ip").get(0) to obtain the IP address of the target service instance. Then, instances.stream().filter(...).findFirst().get() is used to filter out the service instances that match the IP address of the target service instance from the service instance list, and the first matching service instance is returned. Finally, the selected service instance is returned.
[0107] 1053. Modify the execution order of the source code load balancing component to a second order, where the execution priority of the second order is lower than the first order, and generate a second request through the custom load balancing component.
[0108] In this step, similar to step 106, there is no need to perform service instance matching. Instead, the execution order of the source code load balancing component is directly set to the second order, and the second request is generated through the custom load balancing component.
[0109] Further, after the second request is generated in step 106 or step 1053, the method further includes:
[0110] 1054. Determine whether the resource identifier in the second request includes a resource identifier of a service instance matching the first service instance. If so, execute step 108; if not, execute step 106.
[0111] In this step, by determining whether the resource identifier in the second request contains the resource identifier of the service instance that matches the first service instance, it is possible to further determine whether the service instance that matches the first service instance is correct, thereby improving the accuracy of the service instance that matches the first service instance. It should be noted that the service instance that matches the first service instance can be the second service instance or other service instances. The difference is that the second service instance can be understood as a service instance determined by the IP address of the first service instance, while other service instances can be understood as service instances that match the first service instance determined by other means. Whether it is the second service instance or other service instance, its essence is still the selected service instance, that is, the second request is forwarded to the selected service instance to achieve service instance load balancing.
[0112] Optionally, the method also includes: obtaining the uniform resource locator of the first request through the custom load balancing component; obtaining the corresponding protocol prefix from the uniform resource locator, if the uniform resource locator is empty, or the protocol prefix is a non-specified string, modifying the execution order of the source code load balancing component to a second order, the execution priority of the second order is lower than the first order, and re-determining the second service instance through the custom load balancing component to generate a second request.
[0113] In this step, a Uniform Resource Locator (URL) is a string sequence used to identify resources on the Internet.
[0114] The core code of this step is as follows:
[0115]
[0116] In the above code, the filter() method in the parent class LoadBalancerClientFilter is rewritten. The filter() method accepts two parameters: exchange and chain. Exchange represents the server network exchange object, and chain represents the gateway filter chain.
[0117] In this method, first get the URL of the first request through exchange.getAttribute(GATEWAY_REQUEST_URL_ATTR). Then, get the protocol prefix of the URL through exchange.getAttribute(GATEWAY_SCHEME_PREFIX_ATTR). If the URL is empty or the protocol is not Loadbalance, directly call chain.filter(exchange) to continue to execute the next filter. If the protocol of the URL is Loadbalance, call addOriginalRequestUrl(exchange,url) to add the URL of the first request.
[0118] Optionally, the method also includes: determining through the custom load balancing component whether the determined matching second service instance is empty; if so, continuing to execute step 1053 to modify the execution order of the source code load balancing component to a second order, the execution priority of the second order is lower than the first order, and generating a second request through the custom load balancing component.
[0119] The core code of this step is as follows:
[0120]
[0121] In the above code, the filter() method in the parent class LoadBalancerClientFilter is rewritten. In this method, the this.choose(exchange) method is first called to select a service instance. The selection process is determined according to the load balancing algorithm. For the specific implementation, you can refer to the specific logic of the choose() method. Then, determine whether the selected service instance is empty. If it is empty, call chain.filter(exchange) to continue executing the next filter. If the selected service instance is not empty, you can add custom logic in this method, such as modifying the request URL, request header and other information, and then call chain.filter(exchange) to pass the request to the next filter.
[0122] 108. Send the second request to a second service instance, so that the second service instance performs corresponding processing according to the second request.
[0123] In an embodiment of the present application, during the load balancing process, a step is performed in which the second request is sent to the second service instance for corresponding processing.
[0124] Specifically, after a service instance is selected according to the load balancing algorithm, the request needs to be sent to the instance for processing. The second request here refers to the request selected according to the load balancing algorithm. The method of sending the request can use protocols such as HTTP or RPC. The specific implementation method depends on the specific service architecture and technology stack.
[0125] Generally speaking, when sending a request, you need to send relevant information of the request (such as request header, request body, etc.) to the service instance so that the service instance can correctly process the request and return a response result. After processing the request, the service instance will return the response result to the client, and the client can perform subsequent business logic processing based on the response result.
[0126] In the technical solution provided by the embodiment of the present application, requests are distributed to different service instances, thereby achieving load balancing, avoiding system crash caused by overload of a service instance, improving the concurrent processing capability and response speed of the system, and improving the overall performance of the system. In addition, the failover of service instances can be realized. When a service instance fails, the request can be automatically forwarded to other normally running service instances, thereby improving the availability and fault tolerance of the system.
[0127] Figure 3 A schematic diagram of a request processing device provided in an embodiment of the present application is shown in FIG. Figure 3 As shown, the device comprises:
[0128] The acquisition module 31 is used to obtain the IP address of the first service instance carried in the first request after the second service processes the first request sent by the first service and calls back the service interface of the first service; and obtain a list of available service instances;
[0129] A generating module 32, configured to filter out a second service instance matching the IP address of the first service instance from the service instance list to generate a second request;
[0130] The sending module 33 is used to send the second request to the second service instance, so that the second service instance performs corresponding processing according to the second request.
[0131] In the embodiment of the present application, optionally, the device further includes:
[0132] The generating module 32 is specifically configured to replace the resource identifier corresponding to the first service instance in the first request with the resource identifier corresponding to the second service instance, so as to generate a second request message.
[0133] In the embodiment of the present application, optionally, the device further includes:
[0134] A configuration module 34, configured to configure a custom load balancing component and define the execution order of the custom load balancing component as the first order;
[0135] A determination module 35, configured to determine whether the first service is a callback-type service, wherein the callback-type service refers to a service having a callback-type service interface;
[0136] If the judgment module 35 determines that the first service is a callback-type service, the generation module 32 executes to filter out a second service instance that matches the IP address of the first service instance from the service instance list to generate a second request; wherein the generation module 32 is specifically used to filter out a second service instance that matches the IP address of the first service instance from the service instance list, and after obtaining the matching second service instance, modify the execution order of the source code load balancing component to a second order, the execution priority of the second order is lower than the first order, and generate a second request according to the determined second service instance through the custom load balancing component.
[0137] In the embodiment of the present application, optionally, the device further includes:
[0138] If the judgment module 35 determines that the first service is not a callback service, the generation module 32 is also used to modify the execution order of the source code load balancing component to a second order, the execution priority of the second order is lower than the first order, and generate a second request through the custom load balancing component.
[0139] In the embodiment of the present application, optionally, the device further includes:
[0140] The determination module 35 is further configured to determine whether the resource identifier in the second request includes a resource identifier of a service instance that matches the first service instance;
[0141] If the determination module 35 determines that the resource identifier in the second request includes a service instance that matches the first service instance, the sending module 33 is configured to send the second request to the second service instance so that the second service instance performs corresponding processing according to the second request;
[0142] If the judgment module 35 determines that the resource identifier in the second request does not contain a service instance that matches the first service instance, the generation module 32 is also used to filter out a second service instance that matches the IP address of the first service instance from the service instance list to generate a second request.
[0143] In the embodiment of the present application, optionally, the device further includes:
[0144] The acquisition module 31 is further configured to acquire the uniform resource locator of the first request through the custom load balancing component; and acquire the corresponding protocol prefix from the uniform resource locator;
[0145] If the judgment module 35 determines that the uniform resource locator is empty, or the protocol prefix is a non-specified string, the generation module 32 is also used to modify the execution order of the source code load balancing component to a second order, the execution priority of the second order is lower than the first order, and re-determine the second service instance through the custom load balancing component to generate a second request.
[0146] In the embodiment of the present application, optionally, the device further includes:
[0147] The determination module 35 is further configured to determine whether the determined matching second service instance is empty through the custom load balancing component;
[0148] If the judgment module 35 determines that the determined matching second service instance is empty, the generation module 32 is also used to modify the execution order of the source code load balancing component to a second order, the execution priority of the second order is lower than the first order, and generate a second request through the custom load balancing component.
[0149] Figure 3 The request processing device can execute Figure 2 The implementation principle and technical effect of the request processing method described in the illustrated embodiment will not be described in detail. The specific manner in which each module and unit performs operations in the request processing device in the above embodiment has been described in detail in the embodiment of the method, and will not be described in detail here.
[0150] In one possible design, Figure 3 The request processing device of the illustrated embodiment may be implemented as a computing device, such as Figure 4 As shown, the computing device may include a storage component 401 and a processing component 402;
[0151] The storage component 401 stores one or more computer instructions, wherein the one or more computer instructions are called and executed by the processing component 402 .
[0152] The processing component 402 is used to: after the second service processes the first request sent by the first service and calls back the service interface of the first service, obtain the IP address of the first service instance carried in the first request; obtain a list of available service instances; filter out a second service instance that matches the first service instance from the list of service instances to generate a second request; and send the second request to the second service instance so that the second service instance can perform corresponding processing according to the second request.
[0153] The processing component 402 may include one or more processors to execute computer instructions to complete all or part of the steps in the above method. Of course, the processing component may also be implemented by one or more application-specific integrated circuits (ASICs), digital signal processors (DSPs), digital signal processing devices (DSPDs), programmable logic devices (PLDs), field programmable gate arrays (FPGAs), controllers, microcontrollers, microprocessors or other electronic components to perform the above method.
[0154] The storage component 401 is configured to store various types of data to support operations at the terminal. The storage component can be implemented by any type of volatile or non-volatile storage device or a combination thereof, such as static random access memory (SRAM), electrically erasable programmable read-only memory (EEPROM), erasable programmable read-only memory (EPROM), programmable read-only memory (PROM), read-only memory (ROM), magnetic memory, flash memory, magnetic disk or optical disk.
[0155] Of course, a computing device may also include other components, such as input / output interfaces, communication components, etc.
[0156] The input / output interface provides an interface between the processing component and the peripheral interface module, which may be an output device, an input device, etc.
[0157] The communication component is configured to facilitate, among other things, wired or wireless communications between the computing device and other devices.
[0158] Among them, the computing device can be a physical device or an elastic computing host provided by a cloud computing platform, etc. In this case, the computing device can refer to a cloud server, and the above-mentioned processing components, storage components, etc. can be basic server resources rented or purchased from the cloud computing platform.
[0159] The present application also provides a computer storage medium storing a computer program, wherein the computer program can achieve the above-mentioned Figure 2 The request processing method of the illustrated embodiment.
[0160] Those skilled in the art can clearly understand that, for the convenience and brevity of description, the specific working processes of the systems, devices and units described above can refer to the corresponding processes in the aforementioned method embodiments and will not be repeated here.
[0161] The device embodiments described above are merely illustrative, wherein the units described as separate components may or may not be physically separated, and the components displayed as units may or may not be physical units, that is, they may be located in one place, or they may be distributed on multiple network units. Some or all of the modules may be selected according to actual needs to achieve the purpose of the scheme of this embodiment. Ordinary technicians in this field can understand and implement it without paying creative labor.
[0162] Through the description of the above implementation methods, those skilled in the art can clearly understand that each implementation method can be implemented by means of software plus a necessary general hardware platform, and of course, it can also be implemented by hardware. Based on this understanding, the above technical solution is essentially or the part that contributes to the prior art can be embodied in the form of a software product, and the computer software product can be stored in a computer-readable storage medium, such as ROM / RAM, a disk, an optical disk, etc., including a number of instructions for a computer device (which can be a personal computer, a server, or a network device, etc.) to execute the methods described in each embodiment or some parts of the embodiments.
[0163] Finally, it should be noted that the above embodiments are only used to illustrate the technical solutions of the present application, rather than to limit it. Although the present application has been described in detail with reference to the aforementioned embodiments, those skilled in the art should understand that they can still modify the technical solutions described in the aforementioned embodiments, or make equivalent replacements for some of the technical features therein. However, these modifications or replacements do not deviate the essence of the corresponding technical solutions from the spirit and scope of the technical solutions of the embodiments of the present application.
Claims
1. A request processing method, characterized in that: Applied to gateways, including: After the second service processes the first request sent by the first service and calls back the service interface of the first service, obtaining the IP address of the first service instance carried in the first request; Get a list of available service instances; Filtering a second service instance matching the IP address of the first service instance from the service instance list to generate a second request; The second request is sent to the second service instance, so that the second service instance performs corresponding processing according to the second request.
2. The method according to claim 1, characterized in that The generating the second request comprises: The resource identifier corresponding to the first service instance in the first request is replaced with the resource identifier corresponding to the second service instance to generate a second request message.
3. The method according to claim 2, characterized in that Before selecting a second service instance matching the IP address of the first service instance from the service instance list to generate a second request, the method further includes: Configure a custom load balancing component, and define the execution order of the custom load balancing component as the first order; Determining whether the first service is a callback-type service, where the callback-type service refers to a service having a callback-type service interface; If so, performing the step of filtering out a second service instance matching the IP address of the first service instance from the service instance list to generate a second request; Among them, the filtering out a second service instance that matches the IP address of the first service instance from the service instance list to generate a second request includes: filtering out a second service instance that matches the IP address of the first service instance from the service instance list, and after obtaining the matching second service instance, modifying the execution order of the source code load balancing component to a second order, the execution priority of the second order is lower than the first order, and generating a second request according to the determined second service instance through the custom load balancing component.
4. The method according to claim 3, characterized in that Also includes: If not, the execution order of the source code load balancing component is modified to a second order, the execution priority of the second order is lower than the first order, and a second request is generated through the custom load balancing component.
5. The method according to claim 3 or 4, characterized in that: Also includes: Determining whether the resource identifier in the second request includes a resource identifier of a service instance that matches the first service instance; If yes, continue to execute the step of sending the second request to the second service instance so that the second service instance performs corresponding processing according to the second request; If not, continue to perform the step of filtering out a second service instance that matches the IP address of the first service instance from the service instance list to generate a second request.
6. The method according to claim 3, characterized in that Also includes: Obtaining the uniform resource locator of the first request through the custom load balancing component; Obtaining a corresponding protocol prefix from the uniform resource locator; If the uniform resource locator is empty, or the protocol prefix is a non-specified string, the execution order of the source code load balancing component is modified to a second order, the execution priority of the second order is lower than the first order, and the second service instance is re-determined through the custom load balancing component to generate a second request.
7. The method according to claim 4, characterized in that Also includes: Determining, by the custom load balancing component, whether the determined second service instance is empty; If so, continue to execute the step of modifying the execution order of the source code load balancing component to a second order, the execution priority of the second order is lower than the first order, and generating a second request through the custom load balancing component.
8. A request processing device, characterized in that: Applied to gateways, including: an acquisition module, configured to acquire the IP address of the first service instance carried in the first request after the second service processes the first request sent by the first service and calls back the service interface of the first service; and acquire a list of available service instances; A generating module, configured to filter out a second service instance matching the IP address of the first service instance from the service instance list to generate a second request; A sending module is used to send the second request to the second service instance so that the second service instance performs corresponding processing according to the second request.
9. A computing device, characterized in that It comprises a processing component and a storage component; the storage component stores one or more computer instructions; the one or more computer instructions are used to be called and executed by the processing component to implement the request processing method as described in any one of claims 1 to 7.
10. A computer storage medium, characterized in that: A computer program is stored, and when the computer program is executed, the request processing method according to any one of claims 1 to 7 is implemented.