Method for realizing real-time distributed notification of lightweight parameters based on redis
By adopting the Redis-based event publishing and subscription mechanism in the fund trading system, the problem of database and network pressure during parameter modification is solved, and lightweight real-time distributed notification of parameters is realized, reducing system load and reducing learning costs.
Patent Information
- Application Number
- CN202411073055.5
- Authority / Receiving Office
- CN · China
- Patent Type
- Applications(China)
- Current Assignee / Owner
- Filing Date
- 2024-08-06
- Publication Date
- 2025-01-03
AI Technical Summary
In the fund trading system, the system needs to be notified quickly when the parameters are modified, resulting in excessive pressure on the database and the network. Traditional methods have the defect of frequent access to the database and the network, which is difficult to deal with when there are high transaction volumes.
Using the lightweight Redis-based real-time distributed notification method for parameters, by defining event publisher and subscriber classes, Spring's RedisTemplate uses Spring's RedisTemplate to distribute events when modifying parameters on the background management page. The parameter user receives events through MessageListenerAdapter and receives events in Save the parameter key in local memory, and if it is missing, query the latest value from the database.
It realizes distributed proactive notification, reduces the pressure on database and network, reduces the need for frequent access to database and network, and is less invasive and has low learning costs. It does not require the introduction of additional third-party jar packages when using commonly used Redis middleware.
Smart Images

Figure CN119248531A_ABST
Abstract
Description
Technical Field
[0001] The present invention relates to the technical field of software system development, and in particular to a lightweight method for implementing real-time distributed notification of parameters based on redis. Background Art
[0002] With the development of the financial industry, financial scenarios are constantly enriching and changing. Banks need to adapt to changes quickly and respond quickly to ensure that they can always provide the best quality services to the outside world.
[0003] In a capital transaction system, in order to respond to changes, various parameters are generally required to control switches or branch processes, etc. When a parameter is modified, the latest parameter value needs to be quickly notified to the system.
[0004] In traditional capital trading systems, the way to obtain the latest parameter values is to save the parameters in a common component, such as a MySQL / Oracle database, or update them to Redis and read them from Redis each time. However, querying the database or Redis each time you need to obtain the parameter value, although it can indeed obtain the latest parameter value, the following defects will occur when the transaction volume is large:
[0005] 1. The database pressure is high. When using Oracle / MySQL and other databases to save parameters, the database must be accessed every time the parameters are obtained, which puts a lot of pressure on the database;
[0006] 2. The network pressure is high. When using public caches such as redis to save parameters, each time you obtain parameters, you must access redis through the network, which puts a lot of pressure on redis and the network.
[0007] There are similar products in the industry such as ehcache that store parameters in server memory, but cache sharing is troublesome and cluster distributed applications are inconvenient. Summary of the invention
[0008] The purpose of the present invention is to provide a lightweight method for implementing real-time distributed notification of parameters based on redis in view of the deficiencies in the prior art.
[0009] To achieve the above object, the present invention provides a lightweight method for implementing real-time distributed notification of parameters based on redis, comprising:
[0010] A main class of notification function is defined in a public package of the system, wherein the main class of notification function includes an event, an event publisher and an event subscriber;
[0011] The parameter publisher and the parameter user both reference the public package, so that the parameter publisher can publish events normally when modifying parameters, and the parameter user can receive events normally, wherein the events contain the parameter key;
[0012] When the backend management operator modifies the parameter key on the backend management page, the event publisher sends the event in a distributed manner through Spring's RedisTemplate. The event object sent contains the modified parameter key.
[0013] As an event subscriber, the parameter user receives the event sent by the event publisher through the receiving class that inherits the MessageListenerAdapter in the spring framework in the common package. The receiving class overrides the onMessage method. After receiving the event, the parameter user calls the notifyEvent method of the event subscriber to process the event. The UasDictServiceCacheClient class that implements the event subscriber interface is used by the parameter user. The UasDictServiceCacheClient class contains a Map type domain for saving parameter keys. After obtaining the parameter key in the event in the notifyEvent method, delete the key-value pair corresponding to the parameter key in the Map in the local memory.
[0014] When the parameter user uses the UasDictServiceCacheClient class to obtain the parameter key, if it is found that the Map storing the parameter in the local memory does not contain the key, the latest value of the key is queried from the database and saved in the local Map.
[0015] Furthermore, the modification includes addition, deletion and alteration.
[0016] On this basis, this function can be used flexibly. For example, events can be divided into various types and sent to corresponding parameter users through different channels.
[0017] Beneficial effects: 1. The present invention utilizes distributed active notifications to save parameters locally, avoids frequent access to the database, and reduces the pressure on the database and the network;
[0018] 2. It is less invasive and has a low learning cost. It uses the commonly used middleware redis and does not require the introduction of more third-party jar packages. BRIEF DESCRIPTION OF THE DRAWINGS
[0019] Figure 1 This is a flowchart of a lightweight method for implementing real-time distributed notification of parameters based on redis;
[0020] Figure 2This is an implementation architecture diagram of a lightweight method for implementing real-time distributed notification of parameters based on redis;
[0021] Figure 3 It is a schematic diagram of the principle of the parameter user of the embodiment of the present invention. DETAILED DESCRIPTION
[0022] The present invention is further explained below in conjunction with the accompanying drawings and specific embodiments. The embodiments are implemented based on the technical solution of the present invention. It should be understood that these embodiments are only used to illustrate the present invention and are not used to limit the scope of the present invention.
[0023] like Figures 1 to 3 As shown, the embodiment of the present invention provides a lightweight method for implementing real-time distributed notification of parameters based on redis, including:
[0024] The main classes of notification functions are defined in the system's public package common. The main classes of notification functions include events (DistributedEvent), event publishers (DistributedEventBroadcaster) and event subscribers (DistributedEventSubscriber);
[0025] The parameter publisher (background management application that modifies parameters) and parameter users (various applications that use parameters) all reference the public package common, so that the parameter publisher can publish events normally when modifying parameters, and the parameter users can receive events normally, and the events contain the parameter key. The above modifications include addition, deletion and change.
[0026] When the backend operator modifies the parameter key on the backend management page, the event publisher sends the event in a distributed manner through Spring's RedisTemplate, and the event object sent contains the modified parameter key. Specifically, RedisTemplate is a template class provided by Spring Data Redis for operating Redis. It encapsulates various operations on Redis and provides convenient and flexible methods to access the Redis database. The convertAndSend method is a publish-subscribe function provided by redisTemplate, which is used to send messages to a specified channel. This method converts the message into a specific format (usually JSON or a serialized byte array) and sends the message to the Redis message queue. Clients subscribed to the channel will receive the message and can process it further.
[0027] The implementation steps of the convertAndSend method are as follows:
[0028] First get a Redis connection;
[0029] Then serialize the message to be sent, usually using JSON or object serialization; if using JSON, use the writeValueAsBytes method of objectMapper; objectMapper is a tool class for JSON serialization and deserialization, and can use any JSON library, such as Jackson, Gson, etc.
[0030] After the message is successfully serialized, it needs to be sent to the specified channel. Use the publish method of the redis connection, pass in the channel parameters and the serialized message as parameters, and then send it.
[0031] After sending successfully, close the redis connection and release the resources.
[0032] As an event subscriber, the parameter user receives the event sent by the event publisher through the receiving class that inherits the MessageListenerAdapter in the spring framework in the common package. The receiving class overrides the onMessage method. After receiving the event, the parameter user calls the notifyEvent method of the event subscriber to process the event. The UasDictServiceCacheClient class that implements the event subscriber interface is used by the parameter user. The UasDictServiceCacheClient class contains a Map type domain for saving parameter keys. After obtaining the parameter key in the event in the notifyEvent method, delete the key-value pair corresponding to the parameter key in the Map in the local memory.
[0033] When the parameter user uses the UasDictServiceCacheClient class to obtain the parameter key, if it finds that the Map storing the parameter in the local memory does not contain the key, it will query the latest value of the key from the database and save it to the local Map, thereby realizing distributed notification of parameters.
[0034] The above is only a preferred embodiment of the present invention. It should be noted that for ordinary technicians in this technical field, other parts not specifically described belong to the prior art or common knowledge. Several improvements and modifications can be made without departing from the principle of the present invention, and these improvements and modifications should also be considered as the protection scope of the present invention.
Claims
1. A lightweight method for implementing real-time distributed notification of parameters based on redis, characterized in that: include: A main class of notification function is defined in a public package of the system, wherein the main class of notification function includes an event, an event publisher and an event subscriber; The parameter publisher and the parameter user both reference the public package, so that the parameter publisher can publish events normally when modifying parameters, and the parameter user can receive events normally, wherein the events contain the parameter key; When the backend management operator modifies the parameter key on the backend management page, the event publisher sends the event in a distributed manner through the convertAndSend method of Spring's RedisTemplate. The event object sent contains the modified parameter key. As an event subscriber, the parameter user receives the event sent by the event publisher through the receiving class that inherits the MessageListenerAdapter in the spring framework in the common package. The receiving class overrides the onMessage method. After receiving the event, the parameter user calls the notifyEvent method of the event subscriber to process the event. The UasDictServiceCacheClient class that implements the event subscriber interface is used by the parameter user. The UasDictServiceCacheClient class contains a Map type domain for saving parameter keys. After obtaining the parameter key in the event in the notifyEvent method, delete the key-value pair corresponding to the parameter key in the Map in the local memory. When the parameter user uses the UasDictServiceCacheClient class to obtain the parameter key, if it is found that the Map storing the parameters in the local memory does not contain the key, the latest value of the key is queried from the database and saved in the local Map.
2. According to claim 1, the method for implementing lightweight real-time distributed notification of parameters based on redis is characterized in that: The modifications include additions, deletions and alterations.
