HK40097161A - A method for dynamically splitting read/write database - Google Patents
A method for dynamically splitting read/write database Download PDFInfo
- Publication number
- HK40097161A HK40097161A HK42024085725.0A HK42024085725A HK40097161A HK 40097161 A HK40097161 A HK 40097161A HK 42024085725 A HK42024085725 A HK 42024085725A HK 40097161 A HK40097161 A HK 40097161A
- Authority
- HK
- Hong Kong
- Prior art keywords
- data source
- database
- read
- rule
- node
- Prior art date
Links
Description
技术领域Technical Field
本发明涉及数据库技术领域,更具体的说,本发明涉及一种数据库动态读写分离的方法。This invention relates to the field of database technology, and more specifically, to a method for dynamic read-write separation of databases.
背景技术Background Technology
现代应用系统中,作为数据存放的数据库是系统的重要支撑。应用程序(也称应用服务)需要访问数据库读取或改变数据。需要为了提升数据库灾备能力及其可靠性和可恢复性,通常同一个数据库会部署在多台服务器上,形成由多个节点组成数据库集群。In modern application systems, the database, as the storage medium for data, is a crucial support system. Applications (also known as application services) need to access the database to read or modify data. To improve the database's disaster recovery capabilities, reliability, and recoverability, the same database is typically deployed across multiple servers, forming a database cluster composed of multiple nodes.
结合图1所示,通常一个数据库集群内会部署Primary主库节点一台和Slaver从库节点一台或多台。即:As shown in Figure 1, a typical database cluster will deploy one Primary node and one or more Slave nodes. That is:
Primary主库,也称为“RW读写节点”,主库节点可以被读取访问也可以被写入访问。数据库客户端或应用程序等既可以读取访问该节点,也可以写入访问该节点;Primary主库节点通常只有一个。The primary database node, also known as the "Read-Write (RW) node," can be accessed by both read and write operations. Database clients or applications can access this node for both read and write purposes; there is typically only one primary database node.
Slaver从库,也称为“RO只读节点”,从库节点只允许被读取访问;数据库客户端或应用程序等只能读取访问该节点,不能写入访问该节点。Slaver从库节点通常有一个或者多个。A slave node, also known as a "read-only RO node," is a node that can only be accessed by reads. Database clients or applications can only read from this node and cannot write to it. There is usually one or more slave nodes.
读写访问也称为RW读写访问。包括对插入新增数据、更新数据、删除数据,也包括查询数据。只读访问也称为RO只读访问。指仅查询数据,不改变数据。Read-write access, also known as RW (Read-Write) access, includes inserting, updating, and deleting data, as well as querying data. Read-only access, also known as RO (Read-Only) access, refers to querying data without modifying it.
其中,从库为主库的镜像,从库实时或准实时从主库获取数据,实现从库数据与主库保持数据一致(因网络等原因数据一致性可能存在一定延时)。The slave database is a mirror of the master database. The slave database obtains data from the master database in real time or near real time to ensure that the data in the slave database is consistent with that in the master database (there may be some delay in data consistency due to network and other reasons).
数据库集群部署时,应用程序可按读写分离方式分别访问集群中不同的节点。若应用程序的某业务(例如改变客户余额)需要写入数据,则应连接Primary主库进行访问;若应用程序的某业务(例如查询客户余额)仅查询数据,则应连接Slaver从库进行访问。When deploying a database cluster, applications can access different nodes in the cluster using a read-write separation approach. If a certain business function of the application (such as changing a customer's balance) requires writing data, it should connect to the Primary database for access; if a certain business function of the application (such as querying a customer's balance) only requires querying data, it should connect to the Slaver database for access.
在实践情形中,应用程序对数据库的访问中,查询只读类访问往往占大多数。因此可以通过读写分离实现不同的业务访问不通的库节点,达到:In practice, read-only queries often constitute the majority of database accesses by applications. Therefore, read-write separation can be used to allow different business logic to access different database nodes, achieving the following:
由Primary主库承担写入类操作,少量或者不承担只读类操作,减轻对Primary主库负载。The primary database handles write operations and performs few or no read-only operations, thus reducing the load on the primary database.
由Slaver从库承担只读类操作,以减轻Primary主库访问负载;而且,可通过部署多个Slaver从库,并将只读类操作分散到不同Slaver从库,减轻每个Slaver从库节点的访问负载。The slave database handles read-only operations to reduce the access load on the primary database. Furthermore, by deploying multiple slave databases and distributing read-only operations across different slave databases, the access load on each slave database node can be reduced.
现有技术中关于数据库读写分离解决方案存在以下两方面的不足:Existing database read/write separation solutions have the following two shortcomings:
一方面,读写分离方式在开发阶段固定,难以在运行时动态改变。现有的读写分离解决方案无论是通过识别SQL文本中关键词识出读写;或是通过在SQL文本中增加Hint区分读写;还是通过在代码中对每支业务通过命名或其他方式区分每支业务读写等方式。这些方式都存在一个缺点,其读写分离机制在开发阶段已经固定的。应用程序部署上线后,难以在运行期间进行动态改变读写(在应用程序不重启的情况下动态改变读写分离)且实时生效。On the one hand, the read/write separation method is fixed during the development phase and is difficult to change dynamically at runtime. Existing read/write separation solutions, whether by identifying keywords in the SQL text, adding hints to the SQL text, or distinguishing read/write for each business function by naming or other means in the code, all have a drawback: their read/write separation mechanism is fixed during the development phase. After the application is deployed and online, it is difficult to dynamically change the read/write behavior during runtime (dynamically changing the read/write separation without restarting the application) and make it effective in real time.
需要动态改变读写的场景示例:对于“读取客户当前余额以判断是否足够”功能点,在开发阶段认为该功能点对数据库为RO只读访问,因此在源代码中固化使用Slaver从库作为访问数据源。但在上线运行后发现:①由于网络等因素导致Slaver从库与Primary主库的数据同步存在延时,Slaver从库与Primary主库无法保障数据强实时一致性;或者②Slaver从库节点临时故障,Primary主库正常工作。这两种情况都需要将该功能点的数据源动态从RO只读访问切换至RW读写访问。现有的读写分离解决方案难以支持动态改变,需要重新修改源代码重新部署才能部署上线。Example of a scenario requiring dynamic changes to read/write operations: For the function "reading the customer's current balance to determine sufficiency," during development, this function was assumed to have read-only (RO) access to the database. Therefore, the Slave database was permanently used as the data source in the source code. However, after deployment, it was found that: ① due to network factors, there was a delay in data synchronization between the Slave database and the Primary database, and strong real-time data consistency could not be guaranteed; or ② the Slave database node experienced a temporary failure, while the Primary database continued to function normally. In both cases, the data source for this function needs to be dynamically switched from read-only (RO) access to read-write (RW) access. Existing read/write separation solutions cannot support dynamic changes, requiring modification of the source code and redeployment before deployment.
另一方面,未考虑RO只读请求访问之间的动态分散。现有的读写分离方案虽考虑了读与写分类,但没有考虑RO只读请求如何分散。应当让数据库集群多个从节点资源均承担读请求访问负载,分散压力。而且这种分散机制应当是可动态改变的。On the other hand, the dynamic distribution of read-only request access among read-only requests is not considered. While existing read/write separation schemes consider the classification of reads and writes, they do not address how read-only requests are distributed. The database cluster should distribute the read request load across multiple slave nodes, thus allocating pressure. Furthermore, this distribution mechanism should be dynamically adaptable.
发明内容Summary of the Invention
为了克服现有技术的不足,本发明提供一种数据库动态读写分离的方法,能够支持数据库的读写分离,同时在数据库运行期间进行动态改变读写机制,且调整后立即生效。To overcome the shortcomings of existing technologies, this invention provides a method for dynamic read-write separation of databases, which can support read-write separation of databases and dynamically change the read-write mechanism during database operation, with the adjustment taking effect immediately.
本发明解决其技术问题所采用的技术方案是:一种数据库动态读写分离的方法,所述数据库由Primary主库和多个Slaver从库组成,该方法包括以下步骤:The technical solution adopted by this invention to solve its technical problem is: a method for dynamic read-write separation of a database, wherein the database consists of a primary master database and multiple slave databases, and the method includes the following steps:
建立应用程序与所述Primary主库之间读写访问的连接,同时建立应用程序与所述多个Slaver从库之间只读访问的连接;Establish a read-write access connection between the application and the Primary master database, and simultaneously establish a read-only access connection between the application and the multiple Slaver slave databases;
应用程序启动时,读取配置文件并对其进行解析,并依序对解析后的各条数据源选择规则进行存储;When the application starts, it reads and parses the configuration file, and then selects and stores the rules for each data source in sequence.
通过对外暴露接口,以接收外部调用修改数据源选择规则,并对接收的数据源选择规则进行存储,实现动态改变数据源选择规则;By exposing an interface to the outside world, the system can receive external calls to modify the data source selection rules and store the received data source selection rules, thereby enabling dynamic changes to the data source selection rules.
根据业务需求,读取动态改变后的数据源选择规则,并按规则逐条分析后选出该业务所需的数据源。Based on business requirements, read the dynamically changed data source selection rules, analyze each rule, and select the data source required for the business.
进一步的,所述的数据源选择规则为一条或多条,且当数据源选择规则为多条时,该多条数据源选择规则为有序的;Furthermore, the data source selection rule can be one or more, and when there are multiple data source selection rules, these multiple data source selection rules are ordered.
每条规则包括选库条件和选择结果,选库条件包含应用程序名、功能点、SQL中需出现的关键字、SQL中不得出现的关键字;选择结果包括库类别、库节点。Each rule includes database selection criteria and selection results. Database selection criteria include application name, function points, keywords that must appear in the SQL, and keywords that must not appear in the SQL; selection results include database category and database node.
进一步的,所述通过对外暴露接口,以接收外部调用修改数据源选择规则,并对接收的数据源选择规则进行存储,实现动态改变数据源选择规则,包括:Furthermore, the method of exposing an external interface to receive external calls to modify the data source selection rules and storing the received data source selection rules to dynamically change the data source selection rules includes:
S101、对外暴露接口;S101, Externally exposed interface;
S102、客户端按新增/修改/删除接口规范生成JOSN请求,调用对应接口;S102. The client generates a JOSN request according to the add/modify/delete interface specification and calls the corresponding interface;
S103、解析JOSN请求,并验证操作口令是否与预设一致、验证JOSN请求的内容是否合法;S103. Parse the JOSN request and verify whether the operation password is consistent with the preset password and whether the content of the JOSN request is valid.
S104、验证通过后,按根据JSON请求的内容,对数据源选择规则进行新增/修改/删除的操作,最终完成数据源选择规则的内容动态更新且立即生效。S104. After successful verification, add/modify/delete the data source selection rules according to the content of the JSON request, and finally complete the dynamic update of the data source selection rules and make them effective immediately.
进一步的,所述根据业务需求,读取动态改变后的数据源选择规则,并按规则逐条分析后选出该业务所需的数据源,还包括:Furthermore, the step of reading the dynamically changed data source selection rules according to business needs, and analyzing each rule to select the data source required for the business, also includes:
将从Slaver从库中查询得到的数据放入结果缓存中;The data retrieved from the Slaver database will be placed into the result cache;
当出现相同SQL查询请求时,直接从已有的结果缓存中获取数据。When the same SQL query request occurs, the data is retrieved directly from the existing result cache.
进一步的,所述根据业务需求,读取动态改变后的数据源选择规则,并按规则逐条分析后选出该业务所需的数据源,还包括:Furthermore, the step of reading the dynamically changed data source selection rules according to business needs, and analyzing each rule to select the data source required for the business, also includes:
S201、获取应用程序的当前信息;S201. Obtain the current information of the application;
S202、进行规则匹配,依顺序读取一条数据源选择规则的内容,并以应用程序的当前信息与选择条件进行逻辑匹配;S202. Perform rule matching, read the content of a data source selection rule in sequence, and perform logical matching with the current information of the application and the selection conditions.
S203、步骤S202的匹配中,若选择条件满足,则取得该条数据源选择规则对应的选择结果;若选择条件不满足,则重复步骤S202读取下一条数据源选择规则并进行规则匹配;In the matching of steps S203 and S202, if the selection conditions are met, the selection result corresponding to the data source selection rule is obtained; if the selection conditions are not met, step S202 is repeated to read the next data source selection rule and perform rule matching.
S204、对得到选择结果获得库类别、库节点属性,以确定最终节点;S204. Obtain the library category and library node attributes from the selected results to determine the final node;
S205、根据库类别和最终节点,获取对应库的具体连接;S205. Based on the library category and the final node, obtain the specific connection of the corresponding library;
S206、若库类别为RW读写,则获得Primary主库的数据源连接,或从已有的连接池中获取连接;通过该数据源连接,执行待执行SQL,并将执行结果返回至应用程序;S206. If the database type is RW (Read/Write), obtain a data source connection to the Primary database, or obtain a connection from an existing connection pool; through this data source connection, execute the SQL to be executed, and return the execution result to the application.
S207、若库类别为RO只读,包括:S207. If the library category is RO (Read-only), it includes:
以待执行SQL语句文本整体或者以SQL语句文本的Hash散列值作为Key,从结果缓存中获取对应缓存;若从结果缓存中能获取到Value则缓存命中;否则缓存未命中;Use either the entire text of the SQL statement to be executed or the hash value of the SQL statement text as the key to retrieve the corresponding cached value from the result cache; if the value can be retrieved from the result cache, the cache is hit; otherwise, the cache is missed.
若缓存命中,则以命中缓存的Value作为结果直接返回至应用程序,无需获取数据源连接,也无需执行SQL;该步骤中,若以序列化后的字符串作为Value则还需要反序列化;If the cache is hit, the value that was hit in the cache is returned directly to the application as the result, without needing to obtain a data source connection or execute SQL; in this step, if the serialized string is used as the value, then deserialization is also required.
若缓存未命中,则获得Slaver从库的数据源连接,或从已有的连接池中获取连接;通过该数据源连接,执行待执行SQL;若执行成功,将SQL执行结果保存至结果缓存中;无论SQL执行结果是否成功均将执行结果返回至应用程序。If the cache misses, a data source connection is obtained from the slave database, or a connection is obtained from an existing connection pool; the SQL to be executed is executed through the data source connection; if the execution is successful, the SQL execution result is saved to the result cache; regardless of whether the SQL execution result is successful or not, the execution result is returned to the application.
进一步的,步骤S201中,所述当前信息包括当前应用程序名、功能点以及待执行SQL。Furthermore, in step S201, the current information includes the current application name, function points, and SQL to be executed.
进一步的,步骤S204中,若库节点指定为“ANY”任意,则从该库类别下多个节点中,以随机方式或轮训的方式选其一个节点作为最终节点;Furthermore, in step S204, if the library node is specified as "ANY", then one node is selected from multiple nodes under that library category in a random or round-robin manner as the final node.
若库节点不是“ANY”任意而是具体指定的节点,则直接以其指定的节点作为最终节点。If the library node is not "ANY" arbitrary but a specifically designated node, then the specified node is directly used as the final node.
本发明还提供了一种数据库动态读写分离的系统,其改进之处在于,所述数据库包括Primary主库和多个Slaver从库,该系统包括:This invention also provides a system for dynamic read-write separation of a database, the improvement of which is that the database includes a primary master database and multiple slave databases, and the system includes:
加载初始规则组件,对读取的内容进行解析,并依序将解析后的各条数据源选择规则放入规则链存储组件中;Load the initial rule component, parse the read content, and sequentially put the parsed data source selection rules into the rule chain storage component;
规则链存储组件,用于实现对数据源选择规则的存储;The rule chain storage component is used to store the rules for selecting data sources;
动态调整规则组件,该动态调整规则组件通过对外暴露接口,以接收外部调用,并将接收的数据源选择规则放入规则链存储组件内,实现动态改变数据源选择规则;The dynamic adjustment rule component exposes an interface to receive external calls and puts the received data source selection rules into the rule chain storage component, thereby enabling dynamic changes to the data source selection rules.
选择引擎组件,按业务需求读取规则链存储组件中的数据源选择规则,在分析后选出该业务所需的数据源;Select the engine component, read the data source selection rules in the rule chain storage component according to business needs, and select the data source required for the business after analysis;
RW数据源管理组件,通过RW数据源管理组件实现对Primary主库的数据源连接或连接池的管理;The RW data source management component enables the management of data source connections or connection pools for the Primary database.
RO数据源管理组件,通过RO数据源管理组件实现对多个Slaver从库的数据源连接或连接池的管理。The RO data source management component enables the management of data source connections or connection pools for multiple slave databases.
进一步的,所述动态调整规则组件与中央配置单元联动同步,实现自动从中央配置单元获取最新规则;Furthermore, the dynamic adjustment rule component is linked and synchronized with the central configuration unit to automatically obtain the latest rules from the central configuration unit;
所述中央配置单元可为Nacos、Spring Config Server以及ZooKeeper中的任意一种。The central configuration unit can be any one of Nacos, Spring Config Server, and ZooKeeper.
进一步的,所述的加载初始规则组件从配置文件中读取加载规则,并在解析后形成数据源选择规则;Furthermore, the initial loading rule component reads loading rules from the configuration file and forms data source selection rules after parsing;
所述规则链存储组件在应用程序中存储数据源选择规则,当应用程序启动时,将数据源选择规则加载到应用程序的内存中。The rule chain storage component stores data source selection rules in the application and loads these rules into the application's memory when the application starts.
本发明的有益效果是:本发明的一种数据库动态读写分离的方法及系统,支持读写分离方式可以在运行时动态改变,调整后立即生效;还可以灵活地在读读之间的动态调整。The beneficial effects of the present invention are: the database dynamic read-write separation method and system of the present invention support the read-write separation mode to be dynamically changed at runtime, and the adjustment takes effect immediately; it can also flexibly adjust the dynamic between reads.
附图说明Attached Figure Description
图1为现有技术中数据库集群的结构示意图。Figure 1 is a schematic diagram of the structure of a database cluster in the prior art.
图2为本发明中一种数据库动态读写分离的系统的原理框图。Figure 2 is a schematic diagram of a database dynamic read/write separation system according to the present invention.
图3为本发明中加载初始规则组件的工作流程示意图。Figure 3 is a schematic diagram of the workflow for loading the initial rule component in this invention.
图4为本发明中动态调整规则组件的工作流程示意图。Figure 4 is a schematic diagram of the workflow of the dynamic adjustment rule component in this invention.
具体实施方式Detailed Implementation
下面结合附图和实施例对本发明进一步说明。The present invention will be further described below with reference to the accompanying drawings and embodiments.
以下将结合实施例和附图对本发明的构思、具体结构及产生的技术效果进行清楚、完整地描述,以充分地理解本发明的目的、特征和效果。显然,所描述的实施例只是本发明的一部分实施例,而不是全部实施例,基于本发明的实施例,本领域的技术人员在不付出创造性劳动的前提下所获得的其他实施例,均属于本发明保护的范围。另外,专利中涉及到的所有联接/连接关系,并非单指构件直接相接,而是指可根据具体实施情况,通过添加或减少联接辅件,来组成更优的联接结构。本发明创造中的各个技术特征,在不互相矛盾冲突的前提下可以交互组合。The following will clearly and completely describe the concept, specific structure, and technical effects of the present invention in conjunction with embodiments and accompanying drawings, so as to fully understand the purpose, features, and effects of the present invention. Obviously, the described embodiments are only a part of the embodiments of the present invention, not all of them. Other embodiments obtained by those skilled in the art based on the embodiments of the present invention without creative effort are all within the scope of protection of the present invention. Furthermore, all connections/linkages involved in the patent do not simply refer to direct contact between components, but rather to the ability to form a better connection structure by adding or reducing connecting accessories according to specific implementation conditions. The various technical features in this invention can be combined interactively without contradicting each other.
实施例1Example 1
结合图2所示,本发明公开了一种数据库动态读写分离的系统,其中数据库包括Primary主库和多个Slaver从库,本实施例中,包括有三个Slaver从库,分别为Slaver从库01、Slaver从库02、Slaver从库03。所述数据库动态读写分离的系统包括加载初始规则组件、规则链存储组件、动态调整规则组件、选择引擎组件、RW数据源管理组件以及RO数据源管理组件。Referring to Figure 2, this invention discloses a system for dynamic read-write separation of a database. The database includes a primary master database and multiple slave databases. In this embodiment, there are three slave databases: Slave Database 01, Slave Database 02, and Slave Database 03. The system includes an initial rule loading component, a rule chain storage component, a dynamic rule adjustment component, a selection engine component, an RW data source management component, and an RO data source management component.
其中,RW数据源管理组件管理RW读写访问的Primary主库的数据源连接或连接池,包括IP、端口、用户名、密码等连接配置信息,以及连接池管理等。RO数据源管理组件管理RO只读访问的Slaver从库的数据源连接或连接池,包括IP、端口、用户名、密码等连接配置信息,以及连接池管理等。可以访问多个Slaver从库,本实施例中,通过RO数据源管理组件访问三个Slaver从库。The RW (Read-Write) data source management component manages the data source connections or connection pools for the Primary master database (for RW read/write access), including connection configuration information such as IP address, port, username, and password, as well as connection pool management. The RO (Read-Only) data source management component manages the data source connections or connection pools for the Slaver database (for RO read-only access), including connection configuration information such as IP address, port, username, and password, as well as connection pool management. Multiple Slaver databases can be accessed; in this embodiment, three Slaver databases are accessed through the RO data source management component.
进一步的,规则链存储组件是在应用程序中(例如内存中)存储的数据源选择规则,数据源选择规则可有一条或者多条,多条则是有序的;数据源选择规则存储在内存中可加快规则的读取效率。加载初始规则组件对读取的内容进行解析,并依序将解析后的各条数据源选择规则放入规则链存储组件中。对于所述的动态调整规则组件,该动态调整规则组件通过对外暴露接口,以接收外部调用,并将接收的数据源选择规则放入规则链存储组件内,实现动态改变数据源选择规则;例如本实施例中,动态调整规则组件对外暴露接口为HTTP协议的接口。另外,动态调整规则组件还可以与某个中央配置单元联动同步,实现自动从中央配置单元获取最新规则;本实施例中,中央配置单元为Nacos、Spring ConfigServer、ZooKeeper中的任意一个,也可以为其他配置库。Furthermore, the rule chain storage component stores data source selection rules within the application (e.g., in memory). There can be one or more data source selection rules, which are ordered. Storing these rules in memory speeds up rule retrieval. The initial rule loading component parses the read content and sequentially places each parsed data source selection rule into the rule chain storage component. For the dynamically adjusted rule component, it exposes an interface to receive external calls and places the received data source selection rules into the rule chain storage component, thus dynamically changing the data source selection rules. For example, in this embodiment, the dynamically adjusted rule component exposes an HTTP protocol interface. Additionally, the dynamically adjusted rule component can synchronize with a central configuration unit to automatically retrieve the latest rules from the central configuration unit. In this embodiment, the central configuration unit can be any one of Nacos, Spring ConfigServer, or ZooKeeper, or other configuration libraries.
所述的选择引擎组件,按业务需求读取规则链存储组件中的数据源选择规则,在分析后选出该业务所需的数据源;对于RO数据源,还需要判断是否存在缓存,已经将从数据库查询得到的数据放入在缓存中。The selection engine component reads the data source selection rules from the rule chain storage component according to business needs, and selects the data source required for the business after analysis. For RO data sources, it is also necessary to determine whether there is a cache, and whether the data obtained from the database query has been put into the cache.
本实施例中,还包括结果缓存组件,该结果缓存组件将已经从RO数据源SQL查询得到数据作为结果缓存保存在应用程序内存中,下次相同SQL查询请求,直接从已有的结果缓存中获取,无需实际访问Slaver从库,降低从库压力。因此本发明提供了结果缓存功能,可进一步降低数据库查询压力。This embodiment also includes a result caching component. This component stores the data already retrieved from the RO data source SQL query as a result cache in the application's memory. Subsequent requests for the same SQL query will directly retrieve the data from the existing result cache, eliminating the need to actually access the Slave database and reducing the load on the Slave database. Therefore, this invention provides a result caching function, which can further reduce database query pressure.
本发明的一种数据库动态读写分离的系统,支持读写分离方式可以在运行时动态改变,调整后立即生效。还可以灵活地在读读之间的动态调整,例如可根据数据库集群中各个Slaver从库负载压力情况或者在数据库集群结构扩容/收缩时,及时动态调整各节点请求负载。This invention provides a dynamic read/write splitting system for databases, which supports dynamic changes to the read/write splitting mode at runtime, with the changes taking effect immediately. It also allows for flexible dynamic adjustments between read and write operations, for example, by dynamically adjusting the request load of each node based on the load pressure of each slave database in the database cluster or during database cluster expansion/contraction.
实施例2Example 2
继续参照图2所示,本实施例中公开了一种数据库动态读写分离的系统,其中数据库包括Primary主库和多个Slaver从库,本实施例中,包括有三个Slaver从库,分别为Slaver从库01、Slaver从库02、Slaver从库03。同样的,所述数据库动态读写分离的系统包括加载初始规则组件、规则链存储组件、动态调整规则组件、选择引擎组件、RW数据源管理组件以及RO数据源管理组件。Referring again to Figure 2, this embodiment discloses a system for dynamic read/write separation of a database. The database includes a primary master database and multiple slave databases. In this embodiment, there are three slave databases: Slave Database 01, Slave Database 02, and Slave Database 03. Similarly, the system for dynamic read/write separation of the database includes an initial rule loading component, a rule chain storage component, a dynamic rule adjustment component, a selection engine component, an RW data source management component, and an RO data source management component.
在实施例1的基础上,对于所述的规则链存储组件、加载初始规则组件、动态调整规则组件以及选择引擎组件,提供了更为具体的实施例。Based on Embodiment 1, more specific embodiments are provided for the rule chain storage component, the initial rule loading component, the dynamic rule adjustment component, and the selection engine component.
对于所述的规则链存储组件,其中的规则链即为数据源选择规则。数据源选择规则可有一条或者多条,多条是有序。应用程序启动时,会将数据源选择规则加载到内存中以加快读取规则的效率。本发明中,数据源选择规则允许动态修改,修改后立即生效。In the aforementioned rule chain storage component, the rule chain refers to the data source selection rules. There can be one or more data source selection rules, and these rules are ordered. When the application starts, it loads the data source selection rules into memory to speed up rule reading. In this invention, the data source selection rules can be dynamically modified, and the modifications take effect immediately.
本实施例中,数据源选择规则形式及内容示例如下(下表中的规则链即为数据源选择规则):In this embodiment, the data source selection rule format and content examples are as follows (the rule chain in the table below represents the data source selection rule):
以上表中示例规则链说明:Example rule chains in the table above:
规则1:若当前应用程序为AcctMgr且指定当前功能点为QueryAcctInfo时(不判断SQL语句内容),则从RO只读数据源中任选一个Slaver从库节点进行访问。Rule 1: If the current application is AcctMgr and the current function point is specified as QueryAcctInfo (without judging the SQL statement content), then any Slaver node in the RO read-only data source will be selected for access.
规则2:若当前应用程序为AcctMgr且指定当前功能点为QueryBalance时(不判断SQL语句内容),则从RO只读数据源中选择指定的Slaver01库节点进行访问。Rule 2: If the current application is AcctMgr and the current function point is specified as QueryBalance (without judging the SQL statement content), then the specified Slaver01 database node will be selected from the RO read-only data source for access.
规则3:若当前应用程序为AcctMgr且指定当前功能点为AddValue时(不判断SQL语句内容),则选择RW读写数据源的Primary主库节点进行访问。Rule 3: If the current application is AcctMgr and the current function point is specified as AddValue (without judging the SQL statement content), then the Primary master database node of the RW read/write data source will be selected for access.
规则4:若当前应用程序为AcctMgr且SQL语句包含“select”关键字且SQL语句不含“insert|update|delete”关键字中任意一个时(不判断功能点),则从RO只读数据源中任选一个Slaver从库节点进行访问。Rule 4: If the current application is AcctMgr and the SQL statement contains the "select" keyword and does not contain any of the "insert|update|delete" keywords (without judging the function point), then any Slaver node in the RO read-only data source can be selected for access.
规则5:若当前应用程序为AcctMgr且SQL语句包含“insert|update|delete” 关键字中任意一个时(不判断功能点),则选择RW读写数据源的Primary主库节点进行访问。Rule 5: If the current application is AcctMgr and the SQL statement contains any of the keywords "insert|update|delete" (regardless of the function point), then the Primary database node of the RW read/write data source will be selected for access.
规则6:若当前应用程序为RptQry(不判断功能点,不判断SQL语句内容),则从RO只读数据源中任选一个Slaver从库节点进行访问。Rule 6: If the current application is RptQry (without judging function points or SQL statement content), then any Slaver node can be selected from the RO read-only data source for access.
规则7:若当前应用程序为StoreMgr且指定当前功能点为InventoryBalance时(不判断SQL语句内容),则选择RW读写数据源的Primary主库节点进行访问。Rule 7: If the current application is StoreMgr and the current function point is specified as InventoryBalance (without judging the SQL statement content), then the Primary master database node of the RW read/write data source will be selected for access.
例如本实施例中,该功能点为“商品剩余库存查询”,虽然该功能点“商品剩余库存查询”对数据库仅涉及读取操作,没有数据变更操作。但考虑到对查询结果为强实时要求,因此选择RW读写数据源的Primary主库节点进行访问。避免因Slaver从库与Primary主库的数据同步存在延时而导致从Slaver从库查询结果不准的问题。For example, in this embodiment, the function is "Remaining Inventory Query." Although this function only involves read operations on the database and does not involve data modification, considering the strong real-time requirement for the query results, the Primary master database node of the RW read/write data source is selected for access. This avoids the problem of inaccurate query results from the Slave database due to the data synchronization delay between the Slave database and the Primary master database.
规则8:若当前应用程序为StoreMgr且SQL语句包含“select”关键字且SQL语句不含“insert|update|delete”关键字中任意一个时(不判断功能点),则从RO只读数据源中任选一个Slaver从库节点进行访问。Rule 8: If the current application is StoreMgr and the SQL statement contains the "select" keyword and does not contain any of the "insert|update|delete" keywords (without judging the function point), then any Slaver node in the RO read-only data source can be selected for access.
规则9:若当前应用程序为StoreMgr且SQL语句包含“insert|update|delete” 关键字中任意一个时(不判断功能点),则选择RW读写数据源的Primary主库节点进行访问。Rule 9: If the current application is StoreMgr and the SQL statement contains any of the keywords "insert|update|delete" (regardless of the function point), then the Primary database node of the RW read/write data source will be selected for access.
结合图3所示,对于所述的加载初始规则组件,本发明提供了一个具体实施例,本实施例中,加载初始规则组件从配置文件中读取加载规则,并在解析后形成数据源选择规则。Referring to Figure 3, the present invention provides a specific embodiment for the loading initial rule component. In this embodiment, the loading initial rule component reads loading rules from the configuration file and forms data source selection rules after parsing.
在应用程序启动时,从配置文件中加载规则的步骤入选:The step of loading rules from the configuration file when the application starts is selected:
一、配置文件内容是已按约定格式存放一条或者多条选择规则(有序存放);配置文件内容可以人工进行编辑设置;也可以是上一次应用程序运行时保存的结果。1. The configuration file contains one or more selection rules stored in an agreed format (stored in an orderly manner); the configuration file can be manually edited and set; or it can be the result saved from the last time the application was run.
二、通常在应用程序启动时,由加载初始规则组件读取配置文件,按序解析每行的内容文本;2. Typically, when the application starts, the initial rules loading component reads the configuration file and parses the text content of each line in sequence;
三、将解析得到的结果,逐条按序放入规则链存储组件中。Third, the parsed results are sequentially placed into the rule chain storage component.
进一步的,结合图4所示,对于所述的动态调整规则组件,本发明中提供了一具体实施例,所述动态调整规则组件通过对外暴露接口,以接收外部调用,并将接收的数据源选择规则放入规则链存储组件内,实现动态改变数据源选择规则,包括:Furthermore, referring to Figure 4, this invention provides a specific embodiment for the dynamically adjusting rule component. The dynamically adjusting rule component exposes an interface to receive external calls and places the received data source selection rules into a rule chain storage component, thereby dynamically changing the data source selection rules, including:
S101、动态调整规则组件对外暴露接口;S101, External interfaces exposed by the dynamic adjustment rule component;
如下所示,以HTTP+JSON协议为例,对外暴露接口规范示例如下:The following is an example of the interface specification for exposing external interfaces, using the HTTP+JSON protocol:
新增规则接口URL地址:http://IP:PORT/addOneRule;新增规则接口Body内容(JSON格式):The URL for adding a rule is: http://IP:PORT/addOneRule; the body of the rule addition interface (JSON format) is as follows:
{{
"password": "操作口令(需与预设的一致,避免误操作)","password": "Operation password (must match the preset to avoid accidental operation)",
"addBeforeRuleNo": "1","addBeforeRuleNo": "1",
"chooseCondition": {"chooseCondition": {
"applicationName": "应用程序名称(英文字母或数字)","applicationName": "Application name (letters or numbers)",
"FunctionName": "功能点名称(英文字母或数字)","FunctionName": "Function name (letters or numbers)",
"sqlContainKeywords": "SQL中需出现的关键字(多个以竖线分隔)","sqlContainKeywords": "Keywords that must appear in the SQL (multiple keywords separated by vertical bars)",
"sqlNotContainKeywords": "SQL中不得出现的关键字(多个以竖线分隔)""sqlNotContainKeywords": "Keywords that must not appear in SQL (multiple keywords separated by vertical bars)"
}}
"chooseResult": {"chooseResult": {
"type": "库类型(可取值:RW或RO)","type": "Library type (values: RW or RO)",
"NodeName": "库节点名(若不指定具体节点名,则填ANY标书任意)","NodeName": "Library node name (if no specific node name is specified, enter ANY or any name from the tender document)",
}}
}}
其中,addBeforeRuleNo字段指定将本条新规则增加在哪条规则之前。The addBeforeRuleNo field specifies which rule should be added before this new rule.
修改规则接口URL地址:http://IP:PORT/modifyOneRule修改规则接口Body内容(JSON格式):The URL for the rule modification interface is: http://IP:PORT/modifyOneRule. The body content for the rule modification interface (in JSON format) is as follows:
{{
"password": "操作口令(需与预设的一致,避免误操作)","password": "Operation password (must match the preset to avoid accidental operation)",
"RuleNo": "1","RuleNo": "1",
"chooseCondition": {"chooseCondition": {
"applicationName": "应用程序名称(英文字母或数字)","applicationName": "Application name (letters or numbers)",
"FunctionName": "功能点名称(英文字母或数字)","FunctionName": "Function name (letters or numbers)",
"sqlContainKeywords": "SQL中需出现的关键字(多个以竖线分隔)","sqlContainKeywords": "Keywords that must appear in the SQL (multiple keywords separated by vertical bars)",
"sqlNotContainKeywords": "SQL中不得出现的关键字(多个以竖线分隔)""sqlNotContainKeywords": "Keywords that must not appear in SQL (multiple keywords separated by vertical bars)"
}}
"chooseResult": {"chooseResult": {
"type": "库类型(可取值:RW或RO)","type": "Library type (values: RW or RO)",
"NodeName": "库节点名(若不指定具体节点名,则填ANY标书任意)","NodeName": "Library node name (if no specific node name is specified, enter ANY or any name from the tender document)",
}}
}}
其中,RuleNo指定对哪条规则进行修改,JSON格式内容为新规则。RuleNo specifies which rule to modify, and the JSON content represents the new rule.
删除规则接口URL地址:http://IP:PORT/deleteOneRule删除规则接口Body内容(JSON格式):The URL for the rule deletion interface is: http://IP:PORT/deleteOneRule. The body of the rule deletion interface (in JSON format) is as follows:
{{
"password": "操作口令(需与预设的一致,避免误操作)","password": "Operation password (must match the preset to avoid accidental operation)",
"RuleNo": "1","RuleNo": "1",
}}
其中,RuleNo指定对哪条规则进行删除。RuleNo specifies which rule to delete.
S102、客户端按新增/修改/删除接口规范生成JOSN请求,调用对应接口,实现JSON请求内容传输至动态调整规则组件;S102. The client generates a JSON request according to the add/modify/delete interface specification, calls the corresponding interface, and transmits the JSON request content to the dynamic adjustment rule component.
S103、动态调整规则组件接收到请求后,解析该请求,验证操作口令是否与预设一致、验证请求内容是否合法;S103. After receiving the request, the dynamic adjustment rule component parses the request, verifies whether the operation password is consistent with the preset, and verifies whether the request content is legal.
S104、动态调整规则组件验证通过后,按根据JSON请求内容,对在规则链存储组件中的数据源选择规则进行新增/修改/删除操作;最终完成对规则链存储组件中的数据源选择规则的内容动态更新且立即生效。S104. After the dynamic adjustment rule component is verified, add/modify/delete rules for the data source selection in the rule chain storage component according to the JSON request content; finally, the content of the data source selection rules in the rule chain storage component is dynamically updated and takes effect immediately.
因此,通过该步骤,本发明不仅仅支持动态读写分离,还可以灵活地在读读之间的动态调整。另外,本发明侵入性低,无需各应用程序进行大量改造。本发明具有广泛通用性,不依赖于特定数据库产品,也不依赖特定程序开发语言,也不局限在特定领域的应用系统,具有广泛的推广应用价值。Therefore, through this step, the present invention not only supports dynamic read-write separation, but also allows for flexible dynamic adjustment between reads and writes. Furthermore, the present invention is low-intrusive, requiring no extensive modifications to individual applications. The present invention has broad applicability, is not dependent on specific database products or programming languages, and is not limited to application systems in specific fields, thus possessing broad application value.
对于所述的选择引擎组件,本发明提供了一具体实施例。对于商品秒杀等业务场景,一段时间内会有大量相同的查询请求,例如高频SQL查询某个秒杀商品代码、名称、详情介绍、参数规格、价格等基本信息。这些SQL查询请求,虽可以通过以上选择引擎按规则链动态的选择数据源连接,以读写分离方式实现查询请求由Slaver从库承担,但这些高频的SQL查询仍会使Slaver从库负载较大。在Slaver从库内对每一次SQL查询请求都需要进行解析SQL文本、语法词法分析、权限校验、执行计划生成、执行计划选择、IO读取磁盘数据、数据过滤、排序、表表关联等各项操作才能最终得到结果并返回至应用程序。This invention provides a specific embodiment of the selection engine component. In business scenarios such as flash sales, a large number of identical query requests occur over a period of time. These requests might involve high-frequency SQL queries for basic information such as the code, name, details, specifications, and price of a flash sale product. While the selection engine can dynamically select data source connections according to rule chains, allowing the slave database to handle these queries in a read-write separation manner, the high frequency of these SQL queries still places a significant load on the slave database. Within the slave database, each SQL query request requires various operations, including parsing the SQL text, syntax and lexical analysis, permission verification, execution plan generation, execution plan selection, I/O to disk data, data filtering, sorting, and table joins, before the final result is obtained and returned to the application.
对此,本发明提出改进的方案,这些对于相同SQL查询请求,可在前次已从数据库查询得到数据作为结果缓存保存在应用程序内存中,下次相同SQL查询请求,直接从已有的结果缓存中获取,无需实际访问Slaver从库,降低从库压力。In response, this invention proposes an improved solution: for the same SQL query request, the data obtained from the previous database query can be cached in the application's memory as a result cache. Subsequent requests for the same SQL query can directly retrieve the data from the existing result cache without actually accessing the slave database, thus reducing the pressure on the slave database.
本实施例中,对于结果缓存的存放结构实现:由于C、C++、Java、Python、C#、Go、JavaScript等各主流程序开发语言均支持Map结构,可使用Map结构存放结果缓存;Map结构中每条数据包括Key-Value键值对;具体形式为:In this embodiment, the storage structure for the result cache is implemented as follows: Since mainstream programming languages such as C, C++, Java, Python, C#, Go, and JavaScript all support Map structures, a Map structure can be used to store the result cache; each data item in the Map structure includes a Key-Value pair; specifically, it is as follows:
Key:以SQL语句文本整体作为Key(或者以SQL语句文本的Hash散列值作为Key);Key: Use the entire SQL statement text as the key (or use the hash value of the SQL statement text as the key);
Value:成功执行SQL语句查询结果对象作为Value(或者以查询结果对象的序列化后的字符串作为Value)。Value: The result object of the successfully executed SQL statement query is used as Value (or the serialized string of the query result object is used as Value).
因此,改进后的选择引擎组件处理流程如下:Therefore, the improved selection engine component processing flow is as follows:
S201、获取应用程序的当前信息;当前信息包括当前应用程序名、功能点以及待执行SQL;S201. Obtain the current information of the application; the current information includes the current application name, function points, and SQL to be executed;
S202、进行规则匹配,从规则链存储组件中依顺序读取一条数据源选择规则的内容,并以应用程序的当前信息与选择条件进行逻辑匹配;本实施例中的依顺序为序号从小至大;S202. Perform rule matching: Read the content of a data source selection rule sequentially from the rule chain storage component, and perform logical matching with the current information of the application and the selection conditions; in this embodiment, the sequence is from smallest to largest.
S203、步骤S202的匹配中,若选择条件满足,则取得该条数据源选择规则对应的选择结果;若选择条件不满足,则重复步骤S202读取下一条数据源选择规则并进行规则匹配;In the matching of steps S203 and S202, if the selection conditions are met, the selection result corresponding to the data source selection rule is obtained; if the selection conditions are not met, step S202 is repeated to read the next data source selection rule and perform rule matching.
S204、对得到选择结果获得库类别、库节点属性,以确定最终节点;S204. Obtain the library category and library node attributes from the selected results to determine the final node;
步骤S204中,若库节点指定为“ANY”任意,则从该库类别下多个节点中,以随机方式或轮训的方式选其一个节点作为最终节点;若库节点不是“ANY”任意而是具体指定的节点,则直接以其指定的节点作为最终节点;In step S204, if the library node is specified as "ANY", then one node is selected from multiple nodes under that library category in a random or round-robin manner as the final node; if the library node is not "ANY" but a specifically specified node, then the specified node is directly used as the final node.
S205、根据库类别和最终节点,RW数据源管理或RO数据源管理获取对应库的具体连接;S205. Based on the library category and the final node, RW data source management or RO data source management obtains the specific connection of the corresponding library;
S206、若库类别为RW读写,则从RW数据源管理中获得Primary主库的数据源连接,或从已有的连接池中获取连接;通过该数据源连接,执行待执行SQL,并将执行结果返回至应用程序;S206. If the database type is RW read/write, obtain the data source connection for the Primary database from the RW data source management, or obtain a connection from an existing connection pool; execute the SQL to be executed through the data source connection, and return the execution result to the application.
S207、若库类别为RO只读,包括:S207. If the library category is RO (Read-only), it includes:
以待执行SQL语句文本整体或者以SQL语句文本的Hash散列值作为Key,从结果缓存中获取对应缓存;若从结果缓存中能获取到Value则缓存命中;否则缓存未命中;Use either the entire text of the SQL statement to be executed or the hash value of the SQL statement text as the key to retrieve the corresponding cached value from the result cache; if the value can be retrieved from the result cache, the cache is hit; otherwise, the cache is missed.
若缓存命中,则以命中缓存的Value作为结果直接返回至应用程序,无需获取数据源连接,也无需执行SQL;该步骤中,若以序列化后的字符串作为Value则还需要反序列化;If the cache is hit, the value that was hit in the cache is returned directly to the application as the result, without needing to obtain a data source connection or execute SQL; in this step, if the serialized string is used as the value, then deserialization is also required.
若缓存未命中,则从RO数据源管理中获得Slaver从库的数据源连接,或从已有的连接池中获取连接;通过该数据源连接,执行待执行SQL;若执行成功,将SQL执行结果保存至结果缓存中;无论SQL执行结果是否成功均将执行结果返回至应用程序。If the cache misses, obtain the Slaver data source connection from the RO data source management or obtain a connection from an existing connection pool; execute the SQL to be executed through the data source connection; if the execution is successful, save the SQL execution result to the result cache; regardless of whether the SQL execution result is successful or not, return the execution result to the application.
实施例3Example 3
本发明公开了一种数据库动态读写分离的方法,所述数据库包括Primary主库和多个Slaver从库,该方法包括以下的步骤:This invention discloses a method for dynamic read-write separation in a database, wherein the database includes a primary master database and multiple slave databases. The method includes the following steps:
S10、建立应用程序与所述Primary主库之间读写访问的连接,同时建立应用程序与所述多个Slaver从库之间只读访问的连接;S10. Establish a read-write access connection between the application and the Primary master database, and simultaneously establish a read-only access connection between the application and the multiple Slaver slave databases.
S20、应用程序启动时,读取配置文件并对其进行解析,并依序对解析后的各条数据源选择规则进行存储;S20. When the application starts, it reads the configuration file and parses it, and then selects and stores the rules for each data source in sequence.
S30、通过对外暴露接口,以接收外部调用,并对接收的数据源选择规则进行存储,实现动态改变数据源选择规则;S30. By exposing an interface to the outside world, external calls are received and the received data source selection rules are stored to dynamically change the data source selection rules.
S40、根据业务需求,读取动态改变后的数据源选择规则,并按规则逐条分析后选出该业务所需的数据源。S40. Based on business requirements, read the dynamically changed data source selection rules, analyze each rule, and select the data source required for the business.
其中,所述步骤S10中,所述建立应用程序与所述Primary主库之间读写访问的连接,建立应用程序与所述多个Slaver从库之间只读访问的连接时,包括:对数据源连接的IP、端口、用户名以及密码的连接配置信息的管理。所述的数据源选择规则为一条或多条,且当数据源选择规则为多条时,该多条数据源选择规则为有序的。并且,所述数据源选择规则为多条时,包括多条规则,并且该规则与实施例2中相同,本实施例中不再详细说明。In step S10, establishing a read-write access connection between the application and the Primary database, and establishing a read-only access connection between the application and the multiple Slaves, includes managing connection configuration information such as the IP address, port, username, and password for the data source connection. The data source selection rule can be one or more, and when there are multiple rules, they are ordered. Furthermore, when there are multiple data source selection rules, they include multiple rules, and these rules are the same as in Embodiment 2, and will not be described in detail in this embodiment.
进一步的,本实施例中,所述步骤S30包括:Furthermore, in this embodiment, step S30 includes:
S301、动态调整规则组件对外暴露接口;S301, External interfaces exposed by the dynamic adjustment rule component;
S302、通过客户端按新增/修改/删除接口规范,调用对应接口,实现JSON请求内容传输至动态调整规则组件;S302. By calling the corresponding interface according to the add/modify/delete interface specification through the client, the JSON request content is transmitted to the dynamic adjustment rule component;
S303、动态调整规则组件接收到请求后,解析该请求,验证操作口令是否与预设一致、验证请求内容是否合法;S303. After receiving the request, the dynamic adjustment rule component parses the request, verifies whether the operation password is consistent with the preset, and verifies whether the request content is legal.
S304、动态调整规则组件验证通过后,按根据JSON请求内容,对在规则链存储组件中的数据源选择规则进行新增/修改/删除操作;最终完成对规则链存储组件中的数据源选择规则的内容动态更新且立即生效。S304. After the dynamic adjustment rule component is verified, add/modify/delete rules for the data source selection in the rule chain storage component according to the JSON request content; finally, the content of the data source selection rules in the rule chain storage component is dynamically updated and takes effect immediately.
更进一步的,步骤S40中,对于RO数据源,判断是否存在缓存;将已经从RO数据源SQL查询得到的结果作为缓存进行保存,当出现相同SQL查询请求时,直接从已有的结果缓存中获取。具体的,步骤S40包括以下步骤:Furthermore, in step S40, for the RO data source, it is determined whether a cache exists; the results already obtained from the SQL query of the RO data source are saved as a cache, and when the same SQL query request occurs, the results are directly retrieved from the existing cache. Specifically, step S40 includes the following steps:
S401、获取应用程序的当前信息;步骤S401中,所述当前信息包括当前应用程序名、功能点以及待执行SQL;S401. Obtain the current information of the application; in step S401, the current information includes the current application name, function points, and SQL to be executed;
S402、进行规则匹配,从规则链存储组件中依顺序读取一条数据源选择规则的内容,并以应用程序的当前信息与选择条件进行逻辑匹配;S402. Perform rule matching: sequentially read the content of a data source selection rule from the rule chain storage component, and logically match it with the current information of the application and the selection conditions.
S403、步骤S402的匹配中,若选择条件满足,则取得该条数据源选择规则对应的选择结果;若选择条件不满足,则重复步骤S402读取下一条数据源选择规则并进行规则匹配;In the matching of steps S403 and S402, if the selection conditions are met, the selection result corresponding to the data source selection rule is obtained; if the selection conditions are not met, step S402 is repeated to read the next data source selection rule and perform rule matching.
S404、对得到选择结果获得库类别、库节点属性,以确定最终节点;步骤S404中,若库节点指定为“ANY”任意,则从该库类别下多个节点中,以随机方式或轮训的方式选其一个节点作为最终节点;若库节点不是“ANY”任意而是具体指定的节点,则直接以其指定的节点作为最终节点;S404. Obtain the library category and library node attributes from the selection results to determine the final node. In step S404, if the library node is specified as "ANY", then select one node from multiple nodes under the library category in a random or round-robin manner as the final node. If the library node is not "ANY" but a specifically specified node, then directly use the specified node as the final node.
S405、根据库类别和最终节点,RW数据源管理或RO数据源管理获取对应库的具体连接;S405. Based on the library category and the final node, RW data source management or RO data source management obtains the specific connection to the corresponding library.
S406、若库类别为RW读写,则从RW数据源管理中获得Primary主库的数据源连接,或从已有的连接池中获取连接;通过该数据源连接,执行待执行SQL,并将执行结果返回至应用程序;S406. If the database type is RW read/write, obtain the primary database data source connection from the RW data source management, or obtain a connection from an existing connection pool; through this data source connection, execute the SQL to be executed, and return the execution result to the application.
S407、若库类别为RO只读,包括:S407. If the library category is RO (Read-only), it includes:
以待执行SQL语句文本整体或者以SQL语句文本的Hash散列值作为Key,从结果缓存中获取对应缓存;若从结果缓存中能获取到Value则缓存命中;否则缓存未命中;Use either the entire text of the SQL statement to be executed or the hash value of the SQL statement text as the key to retrieve the corresponding cached value from the result cache; if the value can be retrieved from the result cache, the cache is hit; otherwise, the cache is missed.
若缓存命中,则以命中缓存的Value作为结果直接返回至应用程序,无需获取数据源连接,也无需执行SQL;该步骤中,若以序列化后的字符串作为Value则还需要反序列化;If the cache is hit, the value that was hit in the cache is returned directly to the application as the result, without needing to obtain a data source connection or execute SQL; in this step, if the serialized string is used as the value, then deserialization is also required.
若缓存未命中,则从RO数据源管理中获得Slaver从库的数据源连接,或从已有的连接池中获取连接;通过该数据源连接,执行待执行SQL;若执行成功,将SQL执行结果保存至结果缓存中;无论SQL执行结果是否成功均将执行结果返回至应用程序。If the cache misses, obtain the Slaver data source connection from the RO data source management or obtain a connection from an existing connection pool; execute the SQL to be executed through the data source connection; if the execution is successful, save the SQL execution result to the result cache; regardless of whether the SQL execution result is successful or not, return the execution result to the application.
基于此,本发明揭示了一种数据库动态读写分离的系统及方法,与现有技术中实现数据库读写分离的方案相比,具有以下特点:其一、支持读写分离方式可以在运行时动态改变,调整后立即生效。其二、不仅仅支持动态读写分离,还可以灵活地在读读之间的动态调整;例如可根据数据库集群中各个Slaver从库负载压力情况或者在数据库集群结构扩容/收缩时,及时动态调整各节点请求负载。其三、本发明侵入性低,无需各应用程序进行大量改造。其四、具有广泛通用性,不依赖于特定数据库产品,也不依赖特定程序开发语言,也不局限在特定领域的应用系统;具有广泛的推广应用价值。其五、提供了结果缓存功能,可进一步降低数据库查询压力。Based on this, the present invention discloses a system and method for dynamic read-write separation of databases. Compared with existing database read-write separation schemes, it has the following characteristics: First, it supports dynamic changes in the read-write separation mode at runtime, with the adjustments taking effect immediately. Second, it not only supports dynamic read-write separation but also allows for flexible dynamic adjustments between reads; for example, it can dynamically adjust the request load of each node in a timely manner based on the load pressure of each slave database in the database cluster or during the expansion/shrinkage of the database cluster structure. Third, the present invention has low invasiveness and requires no extensive modifications to applications. Fourth, it has broad applicability, is not dependent on specific database products or programming languages, and is not limited to application systems in specific fields; it has broad application value. Fifth, it provides a result caching function, which can further reduce database query pressure.
以上是对本发明的较佳实施进行了具体说明,但本发明创造并不限于所述实施例,熟悉本领域的技术人员在不违背本发明精神的前提下还可做出种种的等同变形或替换,这些等同的变形或替换均包含在本申请权利要求所限定的范围内。The above is a detailed description of the preferred embodiments of the present invention. However, the present invention is not limited to the embodiments described. Those skilled in the art can make various equivalent modifications or substitutions without departing from the spirit of the present invention. All such equivalent modifications or substitutions are included within the scope defined by the claims of this application.
Claims (5)
Publications (2)
| Publication Number | Publication Date |
|---|---|
| HK40097161A true HK40097161A (en) | 2024-03-08 |
| HK40097161B HK40097161B (en) | 2024-04-26 |
Family
ID=
Similar Documents
| Publication | Publication Date | Title |
|---|---|---|
| CN109684282B (en) | Method and device for constructing metadata cache | |
| US11030185B2 (en) | Schema-agnostic indexing of distributed databases | |
| US8392482B1 (en) | Versioning of database partition maps | |
| US8386540B1 (en) | Scalable relational database service | |
| RU2531572C2 (en) | Database replication method and table updating apparatus | |
| US9244958B1 (en) | Detecting and reconciling system resource metadata anomolies in a distributed storage system | |
| US5751962A (en) | Object-based systems management of computer networks | |
| EP2062125B1 (en) | System and method for providing high availability data | |
| US7752165B2 (en) | Persistent query system for automatic on-demand data subscriptions from mobile devices | |
| US8756196B2 (en) | Propagating tables while preserving cyclic foreign key relationships | |
| CN104615606B (en) | A kind of Hadoop distributed file systems and its management method | |
| US8620926B2 (en) | Using a hashing mechanism to select data entries in a directory for use with requested operations | |
| CN113032356B (en) | Cabin distributed file storage system and implementation method | |
| US20140101102A1 (en) | Batch processing and data synchronization in cloud-based systems | |
| TW201229795A (en) | Web service patterns for globally distributed service fabric | |
| JP2004295870A (en) | Consistency unit replication in application-defined system | |
| US20100169289A1 (en) | Two Phase Commit With Grid Elements | |
| WO2017113962A1 (en) | Method of accessing distributed database and device providing distributed data service | |
| CN104111924A (en) | Database system | |
| US8103624B2 (en) | Apparatus and method for automating the logging of table changes in a database | |
| CN117251500B (en) | Dynamic read-write separation method for database | |
| WO2023103341A1 (en) | Blockchain-based smart contract invocation method, apparatus and device | |
| CN114610680A (en) | Method, device and equipment for managing metadata of distributed file system and storage medium | |
| CN102355499A (en) | Cloud computing system | |
| US20170285951A1 (en) | Packed row representation for efficient network serialization with direct column indexing in a network switch |