CN115442220A - Websocket Session sharing method and system based on cluster deployment - Google Patents
Websocket Session sharing method and system based on cluster deployment Download PDFInfo
- Publication number
- CN115442220A CN115442220A CN202211016460.4A CN202211016460A CN115442220A CN 115442220 A CN115442220 A CN 115442220A CN 202211016460 A CN202211016460 A CN 202211016460A CN 115442220 A CN115442220 A CN 115442220A
- Authority
- CN
- China
- Prior art keywords
- websocket
- server
- channel
- message
- client
- Prior art date
- Legal status (The legal status is an assumption and is not a legal conclusion. Google has not performed a legal analysis and makes no representation as to the accuracy of the status listed.)
- Pending
Links
- 238000000034 method Methods 0.000 title claims abstract description 24
- 230000009286 beneficial effect Effects 0.000 abstract description 2
- 230000008878 coupling Effects 0.000 abstract description 2
- 238000010168 coupling process Methods 0.000 abstract description 2
- 238000005859 coupling reaction Methods 0.000 abstract description 2
- 230000004044 response Effects 0.000 description 3
- 238000010586 diagram Methods 0.000 description 2
- 230000004075 alteration Effects 0.000 description 1
- 238000005516 engineering process Methods 0.000 description 1
- 230000006870 function Effects 0.000 description 1
- 230000007246 mechanism Effects 0.000 description 1
- 230000004048 modification Effects 0.000 description 1
- 238000012986 modification Methods 0.000 description 1
- 238000006467 substitution reaction Methods 0.000 description 1
Images
Classifications
-
- H—ELECTRICITY
- H04—ELECTRIC COMMUNICATION TECHNIQUE
- H04L—TRANSMISSION OF DIGITAL INFORMATION, e.g. TELEGRAPHIC COMMUNICATION
- H04L41/00—Arrangements for maintenance, administration or management of data switching networks, e.g. of packet switching networks
- H04L41/06—Management of faults, events, alarms or notifications
- H04L41/0654—Management of faults, events, alarms or notifications using network fault recovery
- H04L41/0668—Management of faults, events, alarms or notifications using network fault recovery by dynamic selection of recovery network elements, e.g. replacement by the most appropriate element after failure
-
- H—ELECTRICITY
- H04—ELECTRIC COMMUNICATION TECHNIQUE
- H04L—TRANSMISSION OF DIGITAL INFORMATION, e.g. TELEGRAPHIC COMMUNICATION
- H04L47/00—Traffic control in data switching networks
- H04L47/50—Queue scheduling
- H04L47/62—Queue scheduling characterised by scheduling criteria
- H04L47/629—Ensuring fair share of resources, e.g. weighted fair queuing [WFQ]
Abstract
The invention relates to the technical field of electronic information, in particular to a WebSocketSession sharing method and a WebSocketSession sharing system based on cluster deployment, wherein a plurality of WebSocket service terminals are started to provide services for a client terminal and subscribe a public channel and a dedicated channel in Redis; when a WebSocket connection request from a client is received, a nginx random route is forwarded to a server for connection, relevant Session information is stored in a memory of the server, and corresponding identification information is stored in a Redis cache; judging after the WebSocket server receives the Http request of the push message; the beneficial effects are that: the WEBSOCKET SESSION sharing method and system based on cluster deployment effectively reduce the coupling of service codes; websocket Session sharing under cluster deployment is supported, the problem of single-point failure during single-machine deployment is solved, and the pressure of a server under the condition of high concurrency is effectively relieved; the method can effectively ensure that each user receives the message pushed by the server in real time and without errors.
Description
Technical Field
The invention relates to the technical field of electronic information, in particular to a WebSocket Session sharing method and system based on cluster deployment.
Background
With the continuous development and growth of companies, the number of served customers is more and more, the project of the whole service platform is more and more complex, and the service volume is more and more.
In the prior art, a single WebSocket server is stressed when the concurrency is large, and once the server is busy or down, the experience of the whole user is very poor.
However, a single machine version cannot meet the existing service requirements, and in the scene of deploying the WebSocket service in a cluster, although the problem that the single machine has performance bottleneck and the problem of single point failure are solved, the problem that sessions cannot be shared in the cluster mode is introduced.
Disclosure of Invention
The invention aims to provide a WebSocket Session sharing method and a WebSocket Session sharing system based on cluster deployment, so as to solve the problems in the background technology.
In order to achieve the purpose, the invention provides the following technical scheme: the WebSocket Session sharing method based on cluster deployment comprises the following steps:
starting a plurality of WebSocket service terminals to provide services for a client, and simultaneously subscribing a public channel and a dedicated channel in Redis;
when a WebSocket connection request from a client is received, a nginx random route is forwarded to a server for connection, relevant Session information is stored in a memory of the server, and corresponding identification information is stored in a Redis cache;
and the WebSocket server judges after receiving the Http request of the push message.
Preferably, when a plurality of WebSocket service terminals are started to provide services for the client, the nginx reverse proxy is configured, so that the plurality of service terminals are externally represented as the same domain name and port, and the services are uniformly provided externally.
Preferably, when the corresponding identification information is stored in the Redis cache, and when the third-party server actively pushes a message notification to the client, the corresponding forwarding rule needs to be preferentially declared; the WebSocket server sends information to a corresponding Redis channel through a forwarding rule according to a judgment result; the WebSocket server acquires the message from the channel, executes asynchronous notification logic, compares the Session connection information stored in the memory and sends the message to the corresponding client.
Preferably, when the request is received for judgment, when the specified channel is the same as the dedicated channel of the server, the asynchronous notification logic is directly executed, and the message is sent to the corresponding client connected to the server; otherwise, the message is sent to the corresponding channel of Redis, and the server subscribing the channel executes the notification logic.
Preferably, if no channel is designated, the message is transmitted to the public channel.
A sharing system based on WebSocket Session under cluster deployment is characterized in that: the system consists of a subscription module, a storage module and a judgment module;
the subscription module is used for starting a plurality of WebSocket service terminals to provide services for the client and subscribing a public channel and a dedicated channel in Redis;
the storage module is used for forwarding to a server side for connection through a nginx random route when a WebSocket connection request from a client side is received, storing relevant Session information in a memory of the server side, and storing corresponding identification information in a Redis cache;
and the judging module is used for judging after the WebSocket server receives the Http request of the push message.
Preferably, in the subscription module, when a plurality of WebSocket servers are started to provide services for the client, the nginx reverse proxy is configured, so that the plurality of servers externally represent the same domain name and port, and provide services to the outside uniformly.
Preferably, in the storage module, when the corresponding identification information is stored in the Redis cache, and when the third-party server actively pushes a message notification to the client, the corresponding forwarding rule needs to be preferentially declared; the WebSocket server sends information to a corresponding Redis channel through a forwarding rule according to a judgment result; the WebSocket server acquires the message from the channel, executes asynchronous notification logic, compares the Session connection information stored in the memory and sends the message to the corresponding client.
Preferably, in the determining module, when the specified channel is the same as the dedicated channel of the local server, the asynchronous notification logic is directly executed, and the message is sent to the corresponding client connected to the local server; otherwise, the message is sent to the corresponding channel of Redis, and the server subscribing the channel executes the notification logic.
Preferably, in the determining module, if the channel is not specified, the message is sent to the public channel.
Compared with the prior art, the invention has the beneficial effects that:
the WEBSOCKET SESSION sharing method and system based on cluster deployment effectively reduce the coupling of service codes; webSocket Session sharing under cluster deployment is supported, the problem of single-point failure during single-machine deployment is solved, and the pressure of a server under the condition of high concurrency is effectively relieved; the method can effectively ensure that each user receives the message pushed by the server in real time without errors.
Drawings
FIG. 1 is a schematic diagram of the present invention with only a single WebSocket server;
FIG. 2 is a schematic diagram illustrating that messages cannot be accurately distributed when a WebSocket service is deployed in a cluster in the present invention;
FIG. 3 is a flowchart of a WebSocket Session-based sharing method and system in the invention under cluster deployment;
fig. 4 shows the main judgment logic of the sharing method and system of WebSocket Session based on cluster deployment in the present invention.
Detailed Description
In order to make the objects, technical solutions and advantages of the present invention clear and fully described, embodiments of the present invention are further described in detail below with reference to the accompanying drawings. It should be understood that the specific embodiments described herein are illustrative of some, but not all, embodiments of the invention and are not to be construed as limiting the scope of the invention, as those skilled in the art will recognize and appreciate that many other embodiments can be made without inventive faculty.
In the description of the present invention, it should be noted that the terms "center", "middle", "upper", "lower", "left", "right", "inner", "outer", "top", "bottom", "side", "vertical", "horizontal", and the like indicate orientations or positional relationships based on those shown in the drawings, and are only for convenience of description and simplicity of description, but do not indicate or imply that the referred device or element must have a specific orientation, be constructed in a specific orientation, and be operated, and thus, should not be construed as limiting the present invention. Furthermore, the terms "first," "second," "third," "fourth," "fifth," and "sixth" are used for descriptive purposes only and are not to be construed as indicating or implying relative importance.
In the description of the present invention, it should be noted that, unless otherwise explicitly specified or limited, the terms "mounted," "connected," and "connected" are to be construed broadly, e.g., as meaning either a fixed connection, a removable connection, or an integral connection; can be mechanically or electrically connected; they may be connected directly or indirectly through intervening media, or they may be interconnected between two elements. The specific meanings of the above terms in the present invention can be understood in specific cases to those skilled in the art.
For the purposes of simplicity and explanation, the principles of the embodiments are described by referring mainly to examples. In the following description, numerous specific details are set forth in order to provide a thorough understanding of the embodiments. It will be apparent, however, to one skilled in the art that the embodiments may be practiced without these specific details. In some instances, well-known methods and structures have not been described in detail so as not to unnecessarily obscure the embodiments. In addition, all embodiments may be used in combination with each other.
Example one
The invention provides a technical scheme that: the sharing method of the WebSocket Session based on cluster deployment comprises the following steps:
starting a plurality of WebSocket service terminals to provide services for a client, and simultaneously subscribing a public channel and a dedicated channel in Redis; configuring the nginx reverse proxy to enable a plurality of service terminals to be presented with the same domain name and port to the outside, and uniformly providing service to the outside;
when a WebSocket connection request from a client is received, a nginx random route is forwarded to a server for connection, relevant Session information is stored in a memory of the server, and corresponding identification information is stored in a Redis cache; when a third-party server actively pushes a message notification to a client, a corresponding forwarding rule needs to be declared preferentially; the WebSocket server sends information to a corresponding Redis channel according to a forwarding rule and a judgment result; the WebSocket server acquires the message from the channel, executes asynchronous notification logic, compares Session connection information stored in a memory and sends the message to a corresponding client;
the WebSocket server receives the Http request of the push message, then judges, directly executes asynchronous notification logic when the specified channel is the same as the exclusive channel of the server, and sends the message to a corresponding client connected to the server; otherwise, the message is sent to a corresponding channel of Redis, and a server subscribing the channel executes a notification logic; if no channel is designated, the message is sent to the public channel.
Example two
A WebSocket Session sharing system based on cluster deployment is composed of a subscription module, a storage module and a judgment module;
the subscription module is used for starting a plurality of WebSocket service terminals to provide services for the client and subscribing a public channel and a dedicated channel in Redis; when a plurality of WebSocket service terminals are started to provide services for the client, a nginx reverse proxy is configured, so that the plurality of service terminals are externally presented as the same domain name and port, and the services are uniformly provided for the outside;
the storage module is used for forwarding the WebSocket connection request from the client to a server by the nginx random route for connection when the WebSocket connection request from the client is received, storing relevant Session information in a memory of the server, and storing corresponding identification information into a Redis cache; when the corresponding identification information is stored in a Redis cache, when a third-party server actively pushes a message notification to a client, a corresponding forwarding rule needs to be preferentially declared; the WebSocket server sends information to a corresponding Redis channel according to a forwarding rule and a judgment result; the WebSocket server acquires the message from the channel, executes asynchronous notification logic, compares Session connection information stored in a memory and sends the message to a corresponding client;
the judging module is used for judging after the WebSocket server receives the Http request of the push message; when the appointed channel is the same as the exclusive channel of the server, the asynchronous notification logic is directly executed, and the message is sent to the corresponding client connected to the server; otherwise, the message is sent to a corresponding channel of Redis, and a server subscribing the channel executes a notification logic; if no channel is designated, the message is sent to the public channel.
EXAMPLE III
Considering that the number of served customers is more and more along with the continuous development and growth of companies, the project of the whole service platform is more and more complex, and the service volume is more and more. When a single WebSocket server is in a condition of a large concurrency amount, the pressure is large, referring to fig. 1, once the server is busy or down, all users are forced to disconnect the WebSocket connection, and the experience of the whole user is very poor. The single-machine version can not meet the existing service requirements, and under the scene of cluster deployment of WebSocket service, although the problem that a single machine has performance bottleneck and the problem of single-point failure are solved, the problem that sessions can not be shared under the cluster mode is introduced. Referring to fig. 2, it is assumed that there are 2 servers of asynchronous response, namely a server 1 and a server 2, and a code connecting a WebSocket is written in a response project, at this time, when a client B connects the WebSocket, the code is randomly forwarded to a certain server through nginx, and it is assumed that Session information is connected to the server 2 and is stored in a memory of the server 2, because the Session information cannot be stored in Redis after serialization, then Session deserialization is obtained from the Redis to rebuild connection operation to realize sharing, so that the server 2 can only send a message to the client B. And when a third-party asynchronous notification is also transmitted to the service terminals 1 and 2 randomly, if the fortune is good, the third-party asynchronous notification is transmitted to the service terminal 2, and at the moment, the asynchronous response can reach the Session value, and the third-party asynchronous notification can be normally notified to the client B. If the message is not forwarded to the server 1, the message is lost because the client B has not received the message although the asynchronous notification logic is executed.
In order to effectively ensure that each user receives a message pushed by a server in real time and without errors, the present application provides an embodiment of a WebSocket Session sharing method and system based on cluster deployment, where reference is made to fig. 3, and the method specifically includes the following contents:
the method comprises the following steps: and starting a plurality of WebSocket servers to provide services for the client, and simultaneously subscribing a public channel and a dedicated channel in Redis. And configuring the nginx reverse proxy to enable a plurality of service terminals to be presented to the outside as the same domain name and port, and uniformly providing services to the outside.
Step two: when a WebSocket connection request from a client is received, the WebSocket connection request is forwarded to a server by a nginx random route for connection, relevant Session information is stored in a memory of the server, and corresponding identification information is stored in a Redis cache. When the third-party server actively pushes the message notification to the client, the corresponding forwarding rule needs to be declared preferentially. And the WebSocket server sends the information to the corresponding Redis channel according to the judgment result through the forwarding rule. The WebSocket server acquires the message from the channel, executes asynchronous notification logic, compares Session connection information stored in the memory, and sends the message to the corresponding client.
In order to realize Session sharing of WebSocket under clustered deployment by using Session state management based on the WebSocket of the SpringBoot WebSocket by using a publish-subscribe mechanism and a storage function of the Redis, in an embodiment of the sharing method and system of the WebSocket Session under clustered deployment, the following contents may be specifically included:
and step two, the identification information comprises a user ID, a user name and a dedicated channel name.
When the third-party server side sends the message, a forwarding rule needs to be declared preferentially, and whether the message is forwarded to a specified channel or a public channel is defined according to business requirements. And when the channel needs to be forwarded to a specified channel, acquiring the exclusive channel name stored by the identification information from the Redis cache.
After receiving the Http request of the push message, the WebSocket server judges that:
if forwarding to the designated channel: when the appointed channel is the same as the exclusive channel of the server, the asynchronous notification logic is directly executed, and the message is sent to the corresponding client connected to the server; otherwise, the message is sent to the corresponding channel of Redis, and the server subscribing the channel executes the notification logic. If the channel is not appointed, the message is sent to the public channel, all the service terminals subscribing the public channel can receive the message at the moment, then the asynchronous notification logic is executed to judge whether the corresponding client terminal exists at the service terminal, if so, the message is sent, otherwise, the message is directly discarded. See fig. 4.
Although embodiments of the present invention have been shown and described, it will be appreciated by those skilled in the art that various changes, modifications, substitutions and alterations can be made in these embodiments without departing from the principles and spirit of the invention, the scope of which is defined in the appended claims and their equivalents.
Claims (10)
1. A WebSocket Session sharing method based on cluster deployment is characterized in that: the WEBSOCKET SESSION sharing method based on cluster deployment comprises the following steps:
starting a plurality of WebSocket service terminals to provide services for a client, and simultaneously subscribing a public channel and a dedicated channel in Redis;
when a WebSocket connection request from a client is received, a nginx random route is forwarded to a server for connection, relevant Session information is stored in a memory of the server, and corresponding identification information is stored in a Redis cache;
and the WebSocket server judges after receiving the Http request of the push message.
2. The WebSocket Session-based sharing method under cluster deployment as claimed in claim 1, wherein: when a plurality of WebSocket service terminals are started to provide services for the client, the nginx reverse proxy is configured, so that the plurality of service terminals are externally represented as the same domain name and port, and the services are uniformly provided for the outside.
3. The WebSocket Session-based sharing method under cluster deployment as claimed in claim 2, wherein: when the corresponding identification information is stored in a Redis cache, when a third-party server actively pushes a message notification to a client, a corresponding forwarding rule needs to be preferentially declared; the WebSocket server sends information to a corresponding Redis channel according to a forwarding rule and a judgment result; the WebSocket server acquires the message from the channel, executes asynchronous notification logic, compares Session connection information stored in the memory, and sends the message to the corresponding client.
4. The WebSocket Session-based sharing method under cluster deployment as claimed in claim 3, wherein: when receiving a request for judgment, when the appointed channel is the same as the exclusive channel of the server, directly executing asynchronous notification logic, and sending a message to a corresponding client connected to the server; otherwise, the message is sent to the corresponding channel of Redis, and the server subscribing the channel executes the notification logic.
5. The sharing method based on the WebSocket Session under cluster deployment according to claim 4, characterized in that: if no channel is designated, the message is sent to the public channel.
6. A sharing system based on WebSocket Session under cluster deployment as claimed in any of the above claims 1-5, characterized in that: the system consists of a subscription module, a storage module and a judgment module;
the subscription module is used for starting a plurality of WebSocket service terminals to provide services for the client and subscribing a public channel and a dedicated channel in Redis;
the storage module is used for forwarding the WebSocket connection request from the client to a server by the nginx random route for connection when the WebSocket connection request from the client is received, storing relevant Session information in a memory of the server, and storing corresponding identification information into a Redis cache;
and the judging module is used for judging after the WebSocket server receives the Http request of the push message.
7. The WebSocket Session-based sharing system under cluster deployment as claimed in claim 6, wherein: in the subscription module, when a plurality of WebSocket service terminals are started to provide services for the client, the nginx reverse proxy is configured, so that the plurality of service terminals are externally represented as the same domain name and port, and the services are uniformly provided for the outside.
8. The WebSocket Session-based sharing system under cluster deployment as claimed in claim 7, wherein: in the storage module, when corresponding identification information is stored in a Redis cache and a third-party server actively pushes a message notification to a client, a corresponding forwarding rule needs to be preferentially declared; the WebSocket server sends information to a corresponding Redis channel according to a forwarding rule and a judgment result; the WebSocket server acquires the message from the channel, executes asynchronous notification logic, compares Session connection information stored in the memory, and sends the message to the corresponding client.
9. The sharing system based on WebSocket Session under cluster deployment according to claim 8, characterized in that: in the judging module, when the appointed channel is the same as the exclusive channel of the server, the asynchronous notification logic is directly executed, and the message is sent to the corresponding client connected to the server; otherwise, the message is sent to the corresponding channel of Redis, and the server subscribing the channel executes the notification logic.
10. The WebSocket Session-based sharing system under cluster deployment as claimed in claim 9, wherein: and in the judging module, if the channel is not specified, the message is sent to the public channel.
Priority Applications (1)
Application Number | Priority Date | Filing Date | Title |
---|---|---|---|
CN202211016460.4A CN115442220A (en) | 2022-08-24 | 2022-08-24 | Websocket Session sharing method and system based on cluster deployment |
Applications Claiming Priority (1)
Application Number | Priority Date | Filing Date | Title |
---|---|---|---|
CN202211016460.4A CN115442220A (en) | 2022-08-24 | 2022-08-24 | Websocket Session sharing method and system based on cluster deployment |
Publications (1)
Publication Number | Publication Date |
---|---|
CN115442220A true CN115442220A (en) | 2022-12-06 |
Family
ID=84245437
Family Applications (1)
Application Number | Title | Priority Date | Filing Date |
---|---|---|---|
CN202211016460.4A Pending CN115442220A (en) | 2022-08-24 | 2022-08-24 | Websocket Session sharing method and system based on cluster deployment |
Country Status (1)
Country | Link |
---|---|
CN (1) | CN115442220A (en) |
Citations (4)
Publication number | Priority date | Publication date | Assignee | Title |
---|---|---|---|---|
US9325790B1 (en) * | 2009-02-17 | 2016-04-26 | Netapp, Inc. | Servicing of network software components of nodes of a cluster storage system |
CN108540556A (en) * | 2018-04-13 | 2018-09-14 | 南京新贝金服科技有限公司 | A kind of fining Session clusters shared system and method based on cache |
CN111031058A (en) * | 2019-12-23 | 2020-04-17 | 集奥聚合(北京)人工智能科技有限公司 | Websocket-based distributed server cluster interaction method and device |
CN113157466A (en) * | 2021-04-27 | 2021-07-23 | 上海销氪信息科技有限公司 | Message pushing method, device, system, electronic equipment and storage medium |
-
2022
- 2022-08-24 CN CN202211016460.4A patent/CN115442220A/en active Pending
Patent Citations (4)
Publication number | Priority date | Publication date | Assignee | Title |
---|---|---|---|---|
US9325790B1 (en) * | 2009-02-17 | 2016-04-26 | Netapp, Inc. | Servicing of network software components of nodes of a cluster storage system |
CN108540556A (en) * | 2018-04-13 | 2018-09-14 | 南京新贝金服科技有限公司 | A kind of fining Session clusters shared system and method based on cache |
CN111031058A (en) * | 2019-12-23 | 2020-04-17 | 集奥聚合(北京)人工智能科技有限公司 | Websocket-based distributed server cluster interaction method and device |
CN113157466A (en) * | 2021-04-27 | 2021-07-23 | 上海销氪信息科技有限公司 | Message pushing method, device, system, electronic equipment and storage medium |
Similar Documents
Publication | Publication Date | Title |
---|---|---|
CN104144098B (en) | Information push method, system and push server equipment | |
CN104412640B (en) | High layer compression is carried out using low layer signaling | |
US7512408B2 (en) | Scalable wireless messaging system | |
RU2608469C2 (en) | Method and apparatus for high performance low latency real time notification delivery | |
CN110808948B (en) | Remote procedure calling method, device and system | |
CN106293887A (en) | Data base processing method and device | |
CN102223381A (en) | Message pushing method, server, client terminal and system | |
CN110545327A (en) | information pushing method and system | |
CN112218342A (en) | Method, device and system for realizing core network sub-slice disaster tolerance | |
CN102118325B (en) | Method and device for configuring chat client terminal, as well as chat client terminal | |
CN111541555A (en) | Group chat optimization method and related product | |
CN102065099B (en) | Signaling and bearing separated communication system | |
CN101110789A (en) | Method for sending instant message in instant message system | |
CN105515936A (en) | Method, server and system for message communication | |
CN113329447B (en) | Communication method and system acting on IP multimedia subsystem IMS slice network | |
WO2022126837A1 (en) | Intelligent scheduling system for video monitoring service | |
CN114338063B (en) | Message queue system, service processing method and computer readable storage medium | |
US20130188559A1 (en) | Method for Establishing a Communication Connection over the Internet Between Mobile Terminals, Computer Program, and Storage Medium | |
CN115442220A (en) | Websocket Session sharing method and system based on cluster deployment | |
CN112613919A (en) | Information processing method and related device | |
CN111737029A (en) | Server, data pushing method and data pushing system | |
CN110572350B (en) | Method and equipment for carrying out IMS service registration | |
WO2022267688A1 (en) | Method and apparatus for discovering standby smf, and electronic device and medium | |
CN115499501A (en) | Message pushing method, system, service gateway and storage medium | |
CN102611639A (en) | System for sending instant message report in instant message system |
Legal Events
Date | Code | Title | Description |
---|---|---|---|
PB01 | Publication | ||
PB01 | Publication | ||
SE01 | Entry into force of request for substantive examination | ||
SE01 | Entry into force of request for substantive examination |