Dynamic data source synchronization method under cluster and micro service
The publish-subscribe mode of Redis enables synchronous updates of data sources under cluster and microservice deployment, which solves the problem of difficult data source updates in multi-application operating modes, enables synchronous updates without restarting applications, and reduces maintenance costs and impacts.
Patent Information
- Application Number
- CN202410624283.0
- Authority / Receiving Office
- CN · China
- Patent Type
- Applications(China)
- Current Assignee / Owner
- Filing Date
- 2024-05-20
- Publication Date
- 2024-08-30
AI Technical Summary
In the multi-application operating mode of cluster deployment and microservice deployment, existing technology is difficult to achieve synchronous updates of data source configuration information, causing maintenance personnel to restart the application to complete the update of the data source, affecting the normal use of the application and increasing maintenance costs.
Adopting Redis's publish-subscribe model, a connection is established with Redis through the application, the user sends an update request, and Redis publishes the message to all subscribed applications, realizing automatic updates of data sources and avoiding configuration file modifications and application restarts.
It achieves synchronous updates of data sources under cluster and microservice deployment, reducing maintenance costs and time. Applications do not need to be restarted, synchronous updates do not affect normal use, and development and learning costs are reduced.
Smart Images

Figure CN118568173A
Abstract
Description
Technical Field
[0001] The invention patent relates to the field of dynamic data source synchronization, specifically a dynamic data source synchronization method under cluster and microservices. Background Art
[0002] At present, enterprises basically develop applications based on the Java Spring framework, and multiple databases, namely multiple data sources, are often involved in the development process.
[0003] There are generally two ways to set up multiple data sources during development:
[0004] One is to configure the configuration information of multiple databases in the configuration file, and automatically read the configuration file after the application starts to create a data source Bean object for each database.
[0005] Another method is to create a data source table in the main database, which contains the configuration information of multiple databases. After the application is started, it automatically reads the configuration information in the data source table in the main database to create a data source Bean object for each database.
[0006] When the database configuration information changes, such as a password change, these two methods can update the data source by recreating the data source Bean object to replace the original data source Bean object in single application running mode.
[0007] However, in the multi-application operation mode of cluster deployment and microservice deployment, modifying the configuration information in the data source table cannot achieve the synchronous update of the data source in multiple applications. The data source can only be updated by modifying and adding configuration files and restarting the application. This method not only affects the normal use of the application, but also makes maintenance personnel time-consuming and labor-intensive.
[0008] Therefore, a convenient method is needed to update the data source without restarting the application. The maintenance personnel only need to send a request to update the data source configuration information. The application receives the request and automatically forwards the update request to other applications to complete the data source update operation synchronously.
[0009] Invention patent content
[0010] The purpose of this patent is to provide a dynamic data source synchronization method under clusters and microservices to solve the problems raised in the above background technology.
[0011] In order to solve the above technical problems, the present invention provides the following technical solutions: a dynamic data source synchronization method under cluster and microservice, comprising the following steps:
[0012] S1: Add Redis and establish a connection with the application;
[0013] S11: The application establishes a socket connection with Redis;
[0014] S12: Create a Redis message channel and a Redis message template object with a specified topic name;
[0015] The connection between the application and Redis is based on the publish-subscribe mode of Redis;
[0016] S2: User sends an update request;
[0017] S21: User creates update request information;
[0018] The format of the update request information is: data source name + driver name + URL + user name + password;
[0019] S22: The user sends the update request information to the application;
[0020] S3: The application receives the update request;
[0021] S31: The application receives update request information;
[0022] S32: querying the local data source management object according to the data source name whether the data source exists;
[0023] When the data source exists, proceed to step S4;
[0024] When the data source does not exist, an error message is returned;
[0025] S4: The application sends the request to the Redis message channel with the specified topic name;
[0026] S41: Pack the update request information into a message object;
[0027] The message object includes a data source name and data source attributes;
[0028] The format of the data source attribute is: driver name + URL + user name + password;
[0029] S42: Automatically serialize the message object into binary data format through the Redis message template object, and send it to the Redis message channel with the specified topic name;
[0030] S5: Redis receives the message and publishes it to all subscribed applications;
[0031] S51: Redis receives the message of the Redis message channel with the specified topic name;
[0032] S52: Redis sends the request to all applications subscribed to the message channel with the specified topic name;
[0033] S6: The subscribed application receives the message published by Redis and executes the update operation of the data source according to the message content;
[0034] S61: The subscribed application receives the message of the message channel with the specified topic name;
[0035] S62: Deserialize the message to obtain a message object;
[0036] S63: Create a new data source object according to the data source attribute in the message object;
[0037] S64: remove the data source object named as the data source name from the global data source management object;
[0038] S65 uses the data source name as the key and the new data source object as the value and adds it to the global data source management object.
[0039] The beneficial effects achieved by the patent of this invention are: based on the Redis publish-subscribe mode + customized update process, it perfectly solves the problem of difficult synchronous update of data sources in multi-application operation modes of cluster deployment and microservice deployment. Redis is an open source key-value-based in-memory database and is a must-have software for caching functions in enterprise application development. Therefore, the application does not need to add additional dependencies and will not increase the learning cost of developers. Developers do not need to modify configuration files or add and restart applications in traditional ways. They only need to submit an update request to achieve synchronous update of all application data sources, which saves time and effort, does not affect the normal use of the application, is non-invasive to the application, and can remove the software at any time without causing any impact on the application. BRIEF DESCRIPTION OF THE DRAWINGS
[0040] Figure 1 The present invention is a flowchart of the steps of a dynamic data source synchronization method under a cluster and microservices in an embodiment. DETAILED DESCRIPTION
[0041] The present invention is further described below in conjunction with the accompanying drawings.
[0042] like Figure 1 As shown, the present invention discloses a method for synchronizing dynamic data sources under clusters and microservices, including the following steps:
[0043] S1: Add Redis and establish a connection with the application;
[0044] S11: The application establishes a socket connection with Redis;
[0045] S12: Create a Redis message channel and a Redis message template object with a specified topic name;
[0046] The connection between the application and Redis is based on the publish-subscribe mode of Redis;
[0047] S2: User sends an update request;
[0048] S21: User creates update request information;
[0049] The format of the update request information is: data source name + driver name + URL + user name + password;
[0050] S22: The user sends the update request information to the application;
[0051] S3: The application receives the update request;
[0052] S31: The application receives update request information;
[0053] S32: querying the local data source management object according to the data source name whether the data source exists;
[0054] When the data source exists, proceed to step S4;
[0055] When the data source does not exist, an error message is returned;
[0056] S4: The application sends the request to the Redis message channel with the specified topic name;
[0057] S41: Pack the update request information into a message object;
[0058] The message object includes a data source name and data source attributes;
[0059] The format of the data source attribute is: driver name + URL + user name + password;
[0060] S42: Automatically serialize the message object into binary data format through the Redis message template object, and send it to the Redis message channel with the specified topic name;
[0061] S5: Redis receives the message and publishes it to all subscribed applications;
[0062] S51: Redis receives the message of the Redis message channel with the specified topic name;
[0063] S52: Redis sends the request to all applications subscribed to the message channel with the specified topic name;
[0064] S6: The subscribed application receives the message published by Redis and executes the update operation of the data source according to the message content;
[0065] S61: The subscribed application receives the message of the message channel with the specified topic name;
[0066] S62: Deserialize the message to obtain a message object;
[0067] S63: Create a new data source object according to the data source attribute in the message object;
[0068] S64: remove the data source object named as the data source name from the global data source management object;
[0069] S65 uses the data source name as the key and the new data source object as the value and adds it to the global data source management object.
[0070] Based on Redis's publish-subscribe model, the application establishes a connection with Redis, the user sends an update request through Redis's publish-subscribe model, the application receives the update request, the application sends the request to Redis, Redis receives the request and publishes the request to all applications, the subscribed application receives the message, and performs the data source update operation according to the message content, thereby realizing the synchronous update of the data source in the multi-application operation mode of cluster deployment and microservice deployment.
[0071] The above are only preferred specific implementation methods of the present invention, but the protection scope of the present invention is not limited thereto. Any technician familiar with the technical field can make equivalent replacements or changes according to the technical solution and invention concept of the present invention within the technical scope disclosed by the present invention, which should be covered by the protection scope of the present invention.
Claims
1. A dynamic data source synchronization method under cluster and microservice, characterized in that: The steps include: S1: Add Redis and establish a connection with the application; S11: The application establishes a socket connection with Redis; S12: Create a Redis message channel and a Redis message template object with a specified topic name; The connection between the application and Redis is based on the publish-subscribe mode of Redis; S2: User sends an update request; S3: The application receives the update request; S4: The application sends the request to the Redis message channel with the specified topic name; S5: Redis receives the message and publishes it to all subscribed applications; S6: The subscribed application receives the message published by Redis and executes the update operation of the data source according to the message content.
2. According to claim 1, a dynamic data source synchronization method under cluster and microservices is characterized in that: The step S2 includes the following sub-steps: S21: User creates update request information; The format of the update request information is: data source name + driver name + URL + user name + password; S22: The user sends update request information to the application.
3. According to claim 1, a dynamic data source synchronization method under cluster and microservices is characterized in that: The step S3 includes the following sub-steps: S31: The application receives update request information; S32: querying the global data source management object according to the data source name to see whether the data source exists; When the data source exists, proceed to step S4; When the data source does not exist, an error message is returned.
4. The method for dynamic data source synchronization under cluster and microservice according to claim 1, characterized in that: The step S4 includes the following sub-steps: S41: Pack the update request information into a message object; The message object includes a data source name and data source attributes; The format of the data source attribute is: driver name + URL + user name + password; S42: Automatically serialize the message object into binary data format through the Redis message template object, and send it to the Redis message channel with the specified topic name.
5. The method for dynamic data source synchronization under cluster and microservice according to claim 1, characterized in that: The step S5 comprises the following sub-steps: S51: Redis receives the message of the Redis message channel with the specified topic name; S52: Redis sends the request to all applications that subscribe to the message channel with the specified topic name.
6. A method for dynamic data source synchronization under cluster and microservice according to claim 1, characterized in that: The step S6 includes the following sub-steps: S61: The subscribed application receives the message of the message channel with the specified topic name; S62: Deserialize the message to obtain a message object; S63: Create a new data source object according to the data source attribute in the message object; S64: remove the data source object named as the data source name from the global data source management object; S65 uses the data source name as the key and the new data source object as the value and adds it to the global data source management object.
Citation Information
Patent Citations
Distributed data synchronization method based on push technology
CN108418894A
Message transmission method and message transmission system
CN108494815A
Method and system for pushing messages in real time
CN116248755A
Cited By
WebSoket-based login restriction method and system
CN119094510A
