Long connection message pushing method, device and system in double-site environment
By using message middleware and Redis in a dual-site environment, the timeliness and accuracy of long-connected message push in a dual-site environment is solved, and efficient message push and simplified process is achieved.
Patent Information
- Application Number
- CN202411830578.X
- Authority / Receiving Office
- CN · China
- Patent Type
- Applications(China)
- Current Assignee / Owner
- Filing Date
- 2024-12-12
- Publication Date
- 2025-03-21
- Estimated Expiration
- 2044-12-12
AI Technical Summary
In a dual-site environment, long-connect message push is difficult to ensure success rate and timeliness, and the existing technology lacks effective solutions.
By introducing a combination of message middleware and Redis in a dual-site environment, users add long connection instances when they go online. When message producers send messages, they query the user's associated long connection instances through message middleware and Redis to ensure that messages are correctly identified and pushed.
It realizes the timeliness and accuracy of long-connected message push in a dual-site environment, improves the success rate and efficiency of message push, simplifies the message push process, and improves the efficiency of problem investigation.
Smart Images

Figure CN119676292A_ABST
Abstract
Description
Technical Field
[0001] The present invention relates to the field of Internet technology, and in particular to a method, device and system for pushing long connection messages in a dual-site environment. Background Art
[0002] With the development of Internet technology, especially the rise of mobile Internet and real-time applications, users have increasing requirements for the real-time and interactivity of information. The traditional client polling request method has poor real-time performance and can no longer meet the needs of modern applications. Frequent requests will lead to unnecessary resource consumption. Especially in the scenario of multi-environment deployment, that is, there are two or more independent environments that need to work together (such as development environment, test environment, production environment, etc.), ensuring the consistency and accuracy of cross-environment message push becomes a major challenge. Especially in the long connection push service, since the generation and consumption of messages may occur in different environments, the routing and user end can be easily misidentified, resulting in missed or mis-sent messages.
[0003] Many solutions have been proposed to address the above issues, such as:
[0004] WebSocket protocol: As the basis of real-time communication, WebSocket provides a low-latency data transmission channel and is the key technology for implementing long-connection message push;
[0005] Message queues: such as RabbitMQ and Kafka, which are used to asynchronously transmit messages between producers and consumers to achieve message storage and distribution;
[0006] Event-driven architecture: Through the event triggering mechanism, system components can respond to specific events without continuous querying or polling, which improves the system's response speed and efficiency;
[0007] Load balancing technology: Distribute client requests through load balancers (such as Nginx and HAProxy) to ensure high availability and stability of services;
[0008] Distributed system and microservice architecture: decompose the system into multiple independent services, each of which is responsible for a part of the function and communicates through API, which enhances the scalability and flexibility of the system;
[0009] Security mechanisms: including but not limited to TLS / SSL encryption, OAuth authentication, JWT (JSON Web Tokens), etc., to ensure secure data transmission and user identity verification.
[0010] The long connection service adopts the technical strategy of dual-site maintenance. The long connection service in the dual-site environment cannot intelligently perceive the user's geographical site, resulting in unnecessary repeated message pushes, which consumes resources and affects efficiency. The above solutions can improve the success rate and efficiency of message sending in specific environments, but it is difficult to achieve the expected effect in a dual-site environment, and is more suitable for short-connection message push methods.
[0011] In short, it is difficult to guarantee the success rate and timeliness of long-connection message push in a dual-site environment, and the existing technology lacks a solution. Summary of the invention
[0012] In order to overcome the defect that the above-mentioned prior art has no effective solution for long connection message push in a dual-site environment, the present invention proposes a long connection message push method in a dual-site environment, which is scalable and robust, can ensure that messages can be correctly identified and pushed, and ensures the timeliness and accuracy of long connection message push in the dual-site environment.
[0013] The present invention proposes a method for pushing long connection messages in a dual-site environment:
[0014] When a user goes online, he joins any persistent connection instance, and the user's connection information is recorded in Redis;
[0015] When a message producer sends a message associated with the user, the consumer's persistent connection instance is defined as the party involved;
[0016] Determine whether the party is a persistent connection instance associated with the user;
[0017] If yes, the party sends the message to the user;
[0018] If not, the party queries Redis to obtain the persistent connection instance information of the user connection, and sends the message to the persistent connection instance associated with the user through the message middleware; the persistent connection instance associated with the user sends the message to the user.
[0019] Preferably, the method comprises the following steps:
[0020] S1. The service registration center configures the message middleware and the persistent connection instance, and configures the binding relationship between the message middleware and the persistent connection instance;
[0021] S2: The message middleware monitors the message producer and receives the message; the persistent connection instance randomly consumes messages from the message middleware and extracts the specified user from the message;
[0022] S3. The persistent connection instance determines whether the specified user exists in the user set bound to it.
[0023] If yes, the persistent connection instance sends the message to the specified user;
[0024] If no, proceed to step S4;
[0025] S4. The persistent connection instance queries the persistent connection instance bound to the specified user, and then sends the message and the persistent connection instance bound to the specified user to the message middleware;
[0026] S5. The message middleware sends the message to the persistent connection instance bound to the specified user, and the persistent connection instance bound to the specified user sends the message to the specified user.
[0027] Preferably, the service registration center uses Nacos, Zookeeper or Consul.
[0028] Preferably, the message middleware adopts RabbitMQ, RocketMQ or Kafka.
[0029] Preferably, Redis is set to active-active synchronization.
[0030] Preferably, the user connection information includes user identity information session.
[0031] Preferably, the message producer steals the SDK interface box message middleware to send the message.
[0032] The present invention also proposes a long connection message push device in a dual-site environment, which is applied to a load balancing module and includes: a request monitoring unit, a user monitoring unit and a message distribution unit;
[0033] The request monitoring unit is used to monitor the message push request of the message producer and notify the message distribution unit; it is also used to monitor the user connection request; the message push request contains the identity information of the specified user, and the user connection request contains the user identity information;
[0034] The user monitoring unit is used to obtain the user connection request and the user online status, and allocate a persistent connection instance to the user for binding when the user connection request is obtained;
[0035] The message distribution unit is used to send messages and message push requests to the message middleware and notify the persistent connection instance for consumption.
[0036] The present invention also proposes a long connection message push system in a dual-site environment, comprising: a message push device, a message consumption module, a message sending module and a database module;
[0037] The message push device as described above includes a request monitoring unit, a user monitoring unit and a message sending unit;
[0038] The database module is used to obtain the binding information between the user and the persistent connection instance generated by the user monitoring unit, and store the user identity information and the binding information; when the user monitoring unit learns that the user is offline, it notifies the database module to delete the user identity information and the binding information, so as to ensure that the database module only records online users;
[0039] The message consumption module is used to manage the persistent connection instances to consume information from the message middleware. For each message, only one persistent connection instance consumes it to avoid congestion caused by multiple persistent connection instances consuming at the same time.
[0040] The message sending module is used to send the messages consumed by the persistent connection instance to the users specified by the messages.
[0041] Preferably, a log monitoring management platform is also included for recording log information.
[0042] The advantages of the present invention are:
[0043] (1) The present invention proposes a method for long connection message push in a dual-site environment, which regards the long connection push service as a unified whole, combines the message middleware decoupling with the message redelivery mechanism, and effectively solves the problem of failure of dual-site load balancing under high concurrency conditions through the message redelivery method. With the cooperation of the message middleware and message redelivery, it can ensure the consistency of the user's connection service and message push service, thereby realizing efficient long connection push in the dual-site environment and ensuring the accuracy of message push in the dual-site environment.
[0044] (2) In the present invention, the message middleware redirects the message and regards the persistent connection instance as a whole. The message producer only needs to send the message, and the message transmission is completed by the persistent connection instance, which simplifies the process of message push producers and consumers and facilitates subsequent problem troubleshooting. The message producer pushes the message to the message middleware normally, and in the subsequent problem troubleshooting, it can be directly checked in the persistent connection service.
[0045] (3) In the persistent connection service, logs are collected through the monitoring platform at key nodes of the message consumption process. Through log monitoring and management, the entire process of message consumption is displayed on the monitoring and management, effectively improving the efficiency of troubleshooting abnormal message push problems.
[0046] (4) By using the dual-site environment long connection service, the real-time and accuracy of user message push can be effectively improved. The dual-site environment long connection services do not affect each other, which effectively improves the availability and stability of the entire system. In the online production environment, the remote active-active service can play a protective role for the overall system service. Remote active-active can ensure that if the service in one area is abnormal, the service in another area can operate normally, thus achieving high availability of the system.
[0047] (5) By adding message re-delivery to the long connection server, the service instance where the user establishes a connection can be accurately found, saving the maintenance cost of the overall system. If you want to accurately locate the service instance where the user establishes a connection through system load balancing, you need to improve it in two aspects. On the one hand, you can improve it through hardware, that is, using routing, and on the other hand, you can improve it at the software level, that is, load balancing on the server side. At present, the most commonly used method for long connection push messages is to improve the system load balancing ability at the software level, mainly by using nginx's ip-hash method to ensure that the long connection instance of each user connection is fixed. This method can achieve accurate message push to a certain extent. However, in the case of an abnormality in the current service instance, it is easy to fail to push messages. The present invention uses message re-delivery decoupled from the message middleware, optimizes the message consumption logic, avoids repeated cache or database operations as much as possible, and re-delivers messages by combining the service container ip and environment variables as the routingKey of the message queue, perfectly avoiding the abnormal problem of the service instance using the nginx+ip-hash method, so that the overall long connection instance can push messages efficiently.
[0048] (6) Through the log monitoring and management platform, the entire process of message consumption is displayed on the monitoring and management platform, which effectively improves the efficiency of troubleshooting abnormal message push issues. BRIEF DESCRIPTION OF THE DRAWINGS
[0049] Figure 1 It is a flow chart of a method for pushing long connection messages in a dual-site environment;
[0050] Figure 2 This is a flow chart of a method for pushing long connection messages in a dual-site environment in an embodiment. DETAILED DESCRIPTION
[0051] The following will be combined with the drawings in the embodiments of the present invention to clearly and completely describe the technical solutions in the embodiments of the present invention. Obviously, the described embodiments are only part of the embodiments of the present invention, not all of the embodiments. Based on the embodiments of the present invention, all other embodiments obtained by ordinary technicians in this field without creative work are within the scope of protection of the present invention.
[0052] Reference Figure 1 The present invention proposes a method for pushing persistent connection messages in a dual-site environment. When a user goes online, he joins any persistent connection instance, and the user connection information session is recorded by the Redis synchronized by the dual active-active. When the message producer sends a message associated with the user, the persistent connection instance consumed is defined as the party concerned. It is determined whether the party concerned is the persistent connection instance associated with the user.
[0053] If yes, the party sends the message to the user;
[0054] If not, the party queries Redis to obtain the persistent connection instance information of the user connection, and sends the message to the persistent connection instance associated with the user through the message middleware; the persistent connection instance associated with the user sends the message to the user.
[0055] Compared with the prior art method of making all long connection instances consume information and then sending it to the designated user, in the present invention, the long connection instance that first consumes the message is responsible to the end. In this way, a message only needs to pass through two long connection instances at most to be pushed to the designated user, which greatly saves communication resources, reduces the occupancy of middleware and long connection instances, and improves the efficiency of message push.
[0056] like Figure 2 As shown, in a specific embodiment, the method includes the following steps:
[0057] S1. Nacos, the service registration center, configures the message middleware RabbitMQ and persistent connection instances. Multiple persistent connection instances are configured. When a user goes online, the connection request obtains the service allocation information through the load balancing nginx, which is used to bind to the persistent connection instance specified by the service allocation information. The bound persistent connection instance stores the user identity information session in its own user collection, and the user binding information with the persistent connection instance is stored in the active-active Redis. When the user goes offline, the user identity information is deleted from the user collection of the corresponding persistent connection instance, and Redis deletes the binding information corresponding to the user.
[0058] In specific implementation, the service registration center can also use Zookeeper (ZK) or Consul; the message middleware can also use RocketMQ or Kafka.
[0059] S2: The message middleware monitors the message producer and receives the message. The persistent connection instance randomly consumes messages from the message middleware and extracts the specified user from the message, that is, identifies the specified user based on the session.
[0060] S3. The persistent connection instance determines whether the specified user exists in the user set bound to it. When the user goes online, the user information is recorded by the persistent connection instance bound to it. Here, the persistent connection instance only needs to perform identity comparison to determine whether the specified user is bound to the persistent connection instance.
[0061] If yes, the persistent connection instance sends the message to the specified user;
[0062] If no, proceed to step S4;
[0063] S4. The persistent connection instance queries Redis for the persistent connection instance bound to the specified user, and then sends the message and the persistent connection instance bound to the specified user to the message middleware.
[0064] S5. The message middleware sends the message to the persistent connection instance bound to the specified user, and the persistent connection instance bound to the specified user sends the message to the specified user.
[0065] In this embodiment, only one persistent connection instance consumes a message at a time, avoiding redundant pressure caused by multiple persistent connection instances consuming messages at the same time; at the same time, the persistent connection instance is responsible for the message to the end. For messages that it is not responsible for, it queries the binding relationship of the specified user and pushes them to the message middleware together with the message, which facilitates the message middleware to implement targeted message redelivery and improves the efficiency of message transmission.
[0066] After the method of this embodiment is actually applied, the data statistics are shown in Table 1.
[0067] Table 1: Message transmission in this embodiment
[0068]
[0069] In the existing message push, all persistent connection instances consume messages, and then each determines whether the specified user is bound to itself. The persistent connection instance bound to the specified user pushes the message to the specified user, and other persistent connection instances give up the message push. The existing message push effect is shown in Table 2.
[0070] Table 2: Traditional message push
[0071]
[0072]
[0073] The data in Table 1 and Table 2 above are obtained through log sorting. By comparing Table 1 and Table 2, it can be seen that the method of the present invention greatly reduces the omission of message push and increases the message sending success rate by about 10%.
[0074] This implementation also proposes a long connection message push system in a dual-site environment, which includes: a message push device, a message consumption module, a message sending module and a database module.
[0075] The message push device is used to monitor the message producer to generate the message and send the message to the persistent connection instance. Specifically, the message push device is applied to the load balancing module, and the message push device includes a request monitoring unit, a user monitoring unit and a message distribution unit.
[0076] The request monitoring unit is used to monitor the message push request of the message producer and notify the message distribution unit; the message push request contains the identity information of the specified user.
[0077] The user monitoring unit is used to obtain the user connection request and the user's online status. When the user connection request is obtained, a persistent connection instance is allocated to the user for binding; the user connection request contains the user's identity information. Specifically, after the user monitoring unit monitors the user's connection request, a persistent connection instance is allocated to the user, and then the database module records the user and the binding of the user to the persistent connection instance; until the user goes offline, the database module deletes the records related to the user.
[0078] The message distribution unit is used to send messages and message push requests to the message middleware and notify the persistent connection instance for consumption.
[0079] The database module is used to obtain the binding information between the user and the persistent connection instance generated by the user monitoring unit, and store the user identity information and the binding information; when the user monitoring unit learns that the user is offline, it notifies the database module to delete the user identity information and the binding information; to ensure that the database module only records online users.
[0080] The message consumption module is used to manage the persistent connection instances to consume information from the message middleware. For each message, only one persistent connection instance consumes it to avoid congestion caused by multiple persistent connection instances consuming at the same time.
[0081] The message sending module is used to send the messages consumed by the persistent connection instance to the users specified by the messages.
[0082] Specifically, when the message middleware receives a message sent by a message producer, the message consumption module randomly notifies a persistent connection instance to consume the message.
[0083] If the persistent connection instance happens to correspond to the designated user of the message, the persistent connection instance pushes the message to the designated user through the message sending module.
[0084] If the persistent connection instance subsequently finds that the designated user of the message is not managed by itself, the persistent connection instance executes a query to the database module to obtain the binding status of the designated user, and then sends the message and the designated user binding status to the message middleware. The message middleware obtains the persistent connection instance bound to the designated user from the designated user binding status, and pushes the message to the persistent connection instance bound to the designated user. The persistent connection instance pushes the message to the designated user through the message sending module, thereby realizing secondary targeted push of the message.
[0085] Of course, it is obvious to those skilled in the art that the present invention is not limited to the details of the exemplary embodiments described above, but also includes the same or similar structures that can be implemented in other specific forms without departing from the spirit or essential features of the present invention. Therefore, no matter from which point of view, the embodiments should be regarded as exemplary and non-limiting, and the scope of the present invention is defined by the appended claims rather than the above description, and it is intended that all changes falling within the meaning and scope of the equivalent elements of the claims are included in the present invention. Any reference numerals in the claims should not be regarded as limiting the claims involved.
[0086] In addition, it should be understood that although the present specification is described according to implementation modes, not every implementation mode contains only one independent technical solution. This description of the specification is only for the sake of clarity. Those skilled in the art should regard the specification as a whole. The technical solutions in each embodiment may also be appropriately combined to form other implementation modes that can be understood by those skilled in the art.
[0087] The techniques, shapes, and structural parts not described in detail in the present invention are all well-known techniques.
Claims
1. A method for pushing long connection messages in a dual-site environment, characterized in that: When a user goes online, he joins any persistent connection instance, and the user's connection information is recorded in Redis; When a message producer sends a message associated with the user, the consumer's persistent connection instance is defined as the party involved; Determine whether the party is a persistent connection instance associated with the user; If yes, the party sends the message to the user; If not, the party queries Redis to obtain the persistent connection instance information of the user connection, and sends the message to the persistent connection instance associated with the user through the message middleware; the persistent connection instance associated with the user sends the message to the user.
2. The method for pushing long connection messages in a dual-site environment as claimed in claim 1, characterized in that: The following steps are involved: S1. The service registration center configures the message middleware and the persistent connection instance, and configures the binding relationship between the message middleware and the persistent connection instance; S2: The message middleware monitors the message producer and receives the message; the persistent connection instance randomly consumes messages from the message middleware and extracts the specified user from the message; S3. The persistent connection instance determines whether the specified user exists in the user set bound to it. If yes, the persistent connection instance sends the message to the specified user; If no, proceed to step S4; S4. The persistent connection instance queries the persistent connection instance bound to the specified user, and then sends the message and the persistent connection instance bound to the specified user to the message middleware; S5. The message middleware sends the message to the persistent connection instance bound to the specified user, and the persistent connection instance bound to the specified user sends the message to the specified user.
3. The method for pushing long connection messages in a dual-site environment as claimed in claim 2, characterized in that: The service registration center uses Nacos, Zookeeper or Consul.
4. The method for pushing persistent connection messages in a dual-site environment as claimed in claim 2, characterized in that: The message middleware uses RabbitMQ, RocketMQ or Kafka.
5. The method for pushing long connection messages in a dual-site environment as claimed in claim 1, characterized in that: Redis is set to active-active synchronization.
6. The method for pushing persistent connection messages in a dual-site environment as claimed in claim 1, characterized in that: User connection information includes user identity information session.
7. The method for pushing persistent connection messages in a dual-site environment according to any one of claims 1 to 6, characterized in that: The message producer steals the SDK interface box and the message middleware to send messages.
8. A long connection message push device in a dual-site environment, applied to a load balancing module, characterized in that: include: Request monitoring unit, user monitoring unit and message distribution unit; A request monitoring unit is used to monitor the message push request of the message producer and notify the message distribution unit; It is also used to monitor user connection requests; the message push request contains the identity information of the specified user, and the user connection request contains the user identity information; The user monitoring unit is used to obtain the user connection request and the user online status, and allocate a persistent connection instance to the user for binding when the user connection request is obtained; The message distribution unit is used to send messages and message push requests to the message middleware and notify the persistent connection instance for consumption.
9. A long connection message push system in a dual-site environment, characterized in that: include: Message push device, message consumption module, message sending module and database module; The message push device as described above includes a request monitoring unit, a user monitoring unit and a message sending unit; The database module is used to obtain the binding information between the user and the persistent connection instance generated by the user monitoring unit, and store the user identity information and the binding information; When the user monitoring unit learns that the user is offline, it notifies the database module to delete the user identity information and binding information; To ensure that the database module only records online users; The message consumption module is used to manage the persistent connection instances to consume information from the message middleware. For each message, only one persistent connection instance consumes it to avoid congestion caused by multiple persistent connection instances consuming at the same time. The message sending module is used to send the messages consumed by the persistent connection instance to the users specified by the messages.
10. The long connection message push system in a dual-site environment as claimed in claim 9, characterized in that: It also includes a log monitoring management platform for recording log information.
Citation Information
Patent Citations
Information synchronization method, system, apparatus, computer device and storage medium
CA3131954A1
Distributed message push system and method
CN104717133A
Message pushing method and device, computer equipment and storage medium
CN110300050A
Distributed server cluster interaction method and device based on WebSocket
CN111031058A
Message pushing system and method based on MQTT protocol
CN111131501A