Data source read-write separation method and device
The non-intrusive data source separation method addresses hot spot data issues and supports diverse databases by routing operations efficiently, reducing development costs and business logic intrusion.
Patent Information
- Application Number
- CN202011050571.8
- Authority / Receiving Office
- CN · China
- Patent Type
- Patents(China)
- Current Assignee / Owner
- Filing Date
- 2020-09-29
- Publication Date
- 2025-07-15
- Estimated Expiration
- 2040-09-29
AI Technical Summary
In the existing technology, data sharding cannot solve the hot data problem, and the server adds a proxy layer to the server and cannot support non-mysql databases. The business-level development code invasion is serious, and the development is complex and costly.
By obtaining the original data source of the business system, copying it into a read-write data source, intercepting requests and identifying operation types, reading-write separation is achieved in a non-invasive manner, and supporting switching of different types of data sources.
It realizes non-invasive data source read and write separation, reduces development costs, and supports switching of multiple data sources.
Smart Images

Figure CN113761038B_ABST
Abstract
Description
Technical Field
[0001] The present invention relates to the field of computer technologies, and in particular, to a method, device, electronic device, and computer-readable medium for separating read and write of data sources. Background Art
[0002] Currently, in order to improve the ability of a system to concurrently access a database and reduce the impact of a large number of transactions on the overall performance of the service, some solutions can be adopted for vertical expansion of the service. Currently, commonly used methods include data sharding, read-write separation, etc. The read-write separation solution can be implemented by adding a proxy layer (Proxy) at the server side or developing code at the business level.
[0003] In the process of implementing the present invention, the inventor found that there are at least the following problems in the prior art: Data sharding cannot solve the problem of hot data; in the way of adding a proxy layer (Proxy) at the server side, the proxy layer needs to parse the syntax tree of mysql (relational database management system), intercept sql (sql is the most commonly used standardized language for accessing databases), identify read and write operations for sql forwarding, the overall development is complex, and developing a proxy at the server side can only support read-write separation of mysql at the server side and cannot implement proxy work for non-mysql, such as data stored in the ClickHouse database; in the way of developing code at the business level, it is necessary to reconstruct the business layer and develop the docking work for different data sources, and the business intrusion is serious. Summary of the Invention
[0004] In view of this, embodiments of the present invention provide a method, device, electronic device, and computer-readable medium for separating read and write of data sources, which can achieve read-write separation of data sources in a non-invasive manner, have little intrusion into business code, support switching between different types of data sources, and reduce development costs.
[0005] To achieve the above object, according to one aspect of the embodiments of the present invention, a method for separating read and write of data sources is provided, including:
[0006] Obtain the original data source of the business system, use the original data source as the first data source, copy the first data source to obtain a second data source, and use one of the first data source and the second data source as the read data source and the other as the write data source;
[0007] Intercept requests accessing the business system and identify the operation type corresponding to the requests, where the operation type includes read operations and write operations;
[0008] When it is determined that the operation type corresponding to the request is a write operation, link the request to the write data source for a write operation; when it is determined that the operation type corresponding to the request is a read operation, link the request to the read data source for a read operation.
[0009] Optionally, the method further includes: creating a proxy manager; determining the identifiers of each of the first data sources and each of the second data sources, where the identifiers include read identifiers and write identifiers; and recording the identifiers in the proxy manager.
[0010] Optionally, when it is determined that the operation type corresponding to the request is a write operation, linking the request to the write data source for a write operation includes:
[0011] If it is determined that the operation type corresponding to the request is a write operation, look up the write identifier from the proxy manager, and link the request to the first data source or the second data source corresponding to the write identifier for a write operation;
[0012] When it is determined that the operation type corresponding to the request is a read operation, linking the request to the read data source for a read operation includes: If it is determined that the operation type corresponding to the request is a read operation, look up the read identifier from the proxy manager, and link the request to the first data source or the second data source corresponding to the read identifier for a read operation.
[0013] Optionally, obtaining the original data source of the business system, using the original data source as the first data source, and copying the first data source to obtain the second data source includes:
[0014] Obtain an instance of the original data source of the business system to obtain the original data source of the business system;
[0015] When the number of instances is multiple, determine that the business system includes multiple original data sources, and use each of the multiple original data sources as the first data source;
[0016] Copy the first data source respectively to obtain multiple corresponding second data sources.
[0017] Optionally, before identifying the operation type corresponding to the request, the method further includes:
[0018] Determine the target entity class of the request;
[0019] According to a preset object-relational mapping table and the target entity class, determine the first target data source corresponding to the request from the multiple first data sources, where the object-relational mapping table is used to record the mapping relationship between the original data source of the business system and the corresponding entity class;
[0020] When it is determined that the operation type corresponding to the request is a write operation, linking the request to the write data source for the write operation includes:
[0021] When it is determined that the operation type corresponding to the request is a write operation, if the first target data source is a write data source, then link the request to the first target data source for the write operation; if the first target data source is a read data source, then link the request to the second target data source corresponding to the first target data source for the write operation;
[0022] When it is determined that the operation type corresponding to the request is a read operation, linking the request to the read data source for the read operation includes:
[0023] When it is determined that the operation type corresponding to the request is a read operation, if the first target data source is a read data source, then link the request to the first target data source for the read operation; if the first target data source is a write data source, then link the request to the second target data source corresponding to the first target data source for the read operation.
[0024] Optionally, identifying the operation type corresponding to the request includes: obtaining the annotation of the request and identifying the operation type corresponding to the annotation.
[0025] Optionally, the annotation includes one or more of the following: Mybatis annotation, JPA annotation, and custom annotation.
[0026] Optionally, identifying the operation type corresponding to the request includes: identifying the operation type corresponding to the request through an abstract syntax tree.
[0027] To achieve the above object, according to one aspect of the embodiments of the present invention, a data source read-write separation device is provided, including:
[0028] A data source initialization module, configured to determine the data source of the business system, use the data source as the first data source, copy the first data source to obtain a second data source, and use one of the first data source and the second data source as the read data source and the other as the write data source;
[0029] An identification module, configured to intercept requests accessing the business system and identify the operation type corresponding to the requests, where the operation type includes a read operation and a write operation;
[0030] A linking module, configured to, when it is determined that the operation type corresponding to the request is a write operation, link the request to the write data source for the write operation; and when it is determined that the operation type corresponding to the request is a read operation, link the request to the read data source for the read operation.
[0031] Optionally, the data source initialization module is further configured to: create a proxy manager; determine the identifiers of each of the first data sources and each of the second data sources, where the identifiers include read identifiers and write identifiers; and record the identifiers in the proxy manager.
[0032] Optionally, the linking module is further configured to: if it is determined that the operation type corresponding to the request is a write operation, look up the write identifier in the proxy manager, and link the request to the first data source or the second data source corresponding to the read identifier for a write operation; if it is determined that the operation type corresponding to the request is a read operation, look up the read identifier in the proxy manager, and link the request to the first data source or the second data source corresponding to the write identifier for a read operation.
[0033] Optionally, the data source initialization module is further configured to: obtain an instance of the original data source of the service system to obtain the original data source of the service system; when the number of instances is multiple, determine that the service system includes multiple original data sources, and use each of the multiple original data sources as a first data source; and respectively copy the first data source to obtain multiple corresponding second data sources.
[0034] Optionally, the identification module is further configured to: determine the target entity class of the request; and determine, according to a preset object relationship mapping table and the target entity class, a first target data source corresponding to the request from the multiple first data sources, where the object relationship mapping table is used to record the mapping relationship between the original data source of the service system and the corresponding entity class.
[0035] The linking module is further configured to: in the case where it is determined that the operation type corresponding to the request is a write operation, if the first target data source is a write data source, link the request to the first target data source for a write operation, and if the first target data source is a read data source, link the request to the second target data source corresponding to the first target data source for a write operation; in the case where it is determined that the operation type corresponding to the request is a read operation, if the first target data source is a read data source, link the request to the first target data source for a read operation, and if the first target data source is a write data source, link the request to the second target data source corresponding to the first target data source for a read operation.
[0036] Optionally, the identification module is further configured to: obtain the annotation of the request and identify the operation type corresponding to the annotation.
[0037] Optionally, the annotation includes one or more of the following: Mybatis annotation, JPA annotation, and custom annotation.
[0038] Optionally, the recognition module is further configured to: recognize the operation type corresponding to the request through an abstract syntax tree.
[0039] To achieve the above object, according to one aspect of the embodiments of the present invention, there is provided an electronic device, including: one or more processors; a storage device for storing one or more programs, when the one or more programs are executed by the one or more processors, enabling the one or more processors to implement the data source read-write separation method of the embodiments of the present invention.
[0040] To achieve the above object, according to one aspect of the embodiments of the present invention, there is provided a computer-readable medium, on which a computer program is stored, and when the program is executed by a processor, the data source read-write separation method of the embodiments of the present invention is implemented.
[0041] One embodiment of the above invention has the following advantages or beneficial effects: by obtaining the original data source of the service system, using the original data source as the first data source, copying the first data source to obtain a second data source, using one of the first data source and the second data source as the read data source and the other as the write data source; intercepting requests accessing the service system and recognizing the operation type corresponding to the requests, where the operation type includes read operations and write operations; when it is determined that the operation type corresponding to the request is a write operation, linking the request to the write data source for write operations; when it is determined that the operation type corresponding to the request is a read operation, linking the request to the read data source for read operations. The technical means can achieve data source read-write separation in a non-intrusive manner, has little intrusion on business code, supports switching of different types of data sources, and reduces development costs.
[0042] The further effects of the above non-conventional optional methods will be described in conjunction with the specific embodiments below. BRIEF DESCRIPTION OF THE DRAWINGS
[0043] The drawings are used to better understand the present invention and do not constitute an improper limitation to the present invention. Among them:
[0044] Figure 1 is a schematic diagram of the main process of the data source read-write separation method of the embodiments of the present invention;
[0045] Figure 2 is a schematic diagram of the main process of the data source read-write separation method of the embodiments of the present invention;
[0046] Figure 3 is a schematic diagram of the process of the data source read-write separation method of another embodiment of the present invention;
[0047] Figure 4 is a schematic diagram of the main modules of the data source read-write separation device of the embodiments of the present invention;
[0048] Figure 5 is a system schematic diagram of the data source read-write separation device applying the embodiment of the present invention;
[0049] Figure 6 is an exemplary system architecture diagram to which the embodiment of the present invention can be applied;
[0050] Figure 7 is a structural schematic diagram of a computer system of a terminal device or a server suitable for implementing the embodiment of the present invention. Detailed implementation manners
[0051] The following makes an explanation of the exemplary embodiments of the present invention in conjunction with the accompanying drawings, including various details of the embodiments of the present invention to facilitate understanding, which should be considered merely exemplary. Therefore, those of ordinary skill in the art should recognize that various changes and modifications can be made to the embodiments described herein without departing from the scope and spirit of the present invention. Similarly, for the sake of clarity and conciseness, the description of well-known functions and structures is omitted below.
[0052] Figure 1 is a schematic diagram of the main process of the data source read-write separation method according to the embodiment of the present invention, as Figure 1 shown, the method includes:
[0053] Step S101: Obtain the original data source of the service system, use the original data source as the first data source, copy the first data source to obtain a second data source, and use one of the first data source and the second data source as the read data source and the other as the write data source;
[0054] Step S102: Intercept the request accessing the service system and identify the operation type corresponding to the request, where the operation type includes a read operation and a write operation;
[0055] Step S103: When it is determined that the operation type corresponding to the request is a write operation, link the request to the write data source for a write operation; when it is determined that the operation type corresponding to the request is a read operation, link the request to the read data source for a read operation.
[0056] In this embodiment, all the information for establishing a database connection is stored in the data source, and the path to connect to the database is defined. Through the data source, the corresponding database connection can be determined.
[0057] For step S101, the original data source (datasource) of the business system can be obtained through the following process: Obtain the Java instance (i.e., bean) of the original data source of the business system in the context where the Java Spring framework is started. Obtaining the Java instance of the original data source enables obtaining the original data source. If there is only one Java instance, it means that the business system has only one original data source. If there are multiple Java instances, it means that the business system has multiple original data sources. After obtaining the original data source, use this original data source as the first data source and copy this first data source to obtain a second data source. When there are multiple first data sources, copy each of the first data sources to obtain multiple second data sources. Specifically, the process of copying the first data source to obtain the second data source includes: obtaining the configuration information of the first data source, copying this configuration information, replacing the copied configuration information with a pre-set configuration file, and generating two Java instances.
[0058] After obtaining the second data source by copying, create a proxy manager. This proxy manager is used to manage the first data source and the second data source, configure unique identifiers for the first data source and the second data source, and record the correspondence between the identifier and the first data source and the second data source, where the identifier includes a read identifier and a write identifier. If the first data source is a read data source, determine the identifier of the first data source as the read identifier. If the second data source is a write data source, determine the identifier of the second data source as the write identifier. As an example, the correspondence between the identifier and the first data source and the second data source can be recorded in the form of key-value.
[0059] In an alternative embodiment, one of the first data source and the second data source can be used as a read data source and the other as a write data source. The read data source is used to provide read data services, and the write data source is used to provide write data services. Preferably, in this embodiment, the first data source is used as the write data source and the second data source as the read data source. Then, the identifier of the first data source is the write identifier, and the identifier of the second data source is the read identifier. Therefore, when copying the first data source (i.e., copying the configuration information of the first data source), replace the copied configuration information with a pre-set read service configuration file, and use this read service configuration file as the configuration information of the second data source, so that the second data source provides read services as a read data source.
[0060] For step S102, the request for accessing the business system can be intercepted through an AOP aspect. Among them, AOP (Aspect Oriented Programming) is a technology that realizes the unified maintenance of program functions through pre-compilation and dynamic proxy during runtime. The operation type corresponding to the request can be determined by identifying the annotation of the request. Annotation is a mechanism of Spring and can be parsed and obtained through reflection. The Spring framework is an open-source Java platform that provides a comprehensive infrastructure for easily and quickly developing durable Java applications. In this embodiment, the annotation is used to distinguish read and write services. As an example, the annotation can include Mybatis annotation, JPA annotation, and custom annotation. Mybatis is a persistence framework that supports customized SQL, stored procedures, and advanced mapping, and can use annotations to configure and map native information. JPA (Java Persistence API) is used to describe the mapping relationship between objects and relational tables with JDK 5.0 annotations or XML, and persist the entity objects during runtime to the database. In addition to the existing annotations in Mybatis and JPA, this embodiment also supports custom annotations, that is, the annotations can be flexibly set according to application requirements.
[0061] For step S103, if it is identified that the operation type corresponding to the request is a read operation, the request is linked to the corresponding read data source for reading. If it is identified that the operation type corresponding to the request is a write operation, the request is linked to the corresponding write data source for writing. If it is determined that the operation type corresponding to the request is a read operation, the read identifier is searched for in the proxy manager, and the request is linked to the first data source or the second data source corresponding to the read identifier for reading. If it is determined that the operation type corresponding to the request is a write operation, the write identifier is searched for in the proxy manager, and the request is linked to the first data source or the second data source corresponding to the write identifier for writing.
[0062] In an alternative embodiment, if there are multiple first data sources and multiple second data sources, the data source corresponding to the request can be determined from the multiple first data sources or the multiple second data sources according to a preset object-relational mapping table and the target entity class in the request. Specifically, it can include:
[0063] Determine the target entity class of the request;
[0064] According to the preset object-relational mapping table and the target entity class, determine the first target data source corresponding to the request from the multiple first data sources, where the object-relational mapping table is used to record the mapping relationship between the original data source of the business system and the corresponding entity class;
[0065] When it is determined that the operation type corresponding to the request is a write operation, if the first target data source is a write data source, the request is linked to the first target data source for a write operation; if the first target data source is a read data source, the request is linked to the second target data source corresponding to the first target data source for a write operation.
[0066] When it is determined that the operation type corresponding to the request is a read operation, if the first target data source is a read data source, the request is linked to the first target data source for a read operation; if the first target data source is a write data source, the request is linked to the second target data source corresponding to the first target data source for a read operation.
[0067] Among them, the object-relational mapping table refers to the ORM mapping table. ORM (Object Relational Mapping) automatically persists the objects in the object-oriented language program into the relational database by using the metadata that describes the mapping between the objects and the database. The ORM mapping table realizes the binding of the entity class to the corresponding data source access. When the program accesses the relevant repository method of the ORM entity class, it will find its corresponding data source object. At the same time, different database connection pools maintain the data source resources, realizing the binding of the entity class and the data source. The system can obtain the data source bound to the current thread through the DataSourceUtils tool class of Spring, and further switch the data source according to the read-write determination function to achieve read-write separation. The Repository is located between the business layer and the data layer, isolating the two. The logic of data query and storage is encapsulated inside it. As an example, assume that the business system has 3 original data sources, namely the original data source A, the original data source B, and the original data source C. These 3 original data sources are used as the first data sources, and each of the 3 original data sources is copied to obtain the second data source A', the second data source B', and the third data source C'. The first data source A, the first data source B, and the first data source C are used as write data sources, and the second data source A', the second data source B', and the third data source C' are used as read data sources. The identifiers of the first data source A, the first data source B, and the first data source C are set as write identifiers, and the identifiers of the second data source A', the second data source B', and the third data source C' are set as read identifiers. The object-relational mapping table (i.e., the ORM mapping table) records the mapping relationships between the original data source A, the original data source B, the original data source C, and the corresponding entity classes. After receiving the request to access the business system, parse the request to determine the target entity class of the request, and determine the original data source corresponding to the request according to the object-relational mapping table. Assume that the original data source corresponding to the request is the first data source A, then the first data source A is the first target data source. Obtain the annotation of the request, identify the annotation to determine the operation type corresponding to the request. If the operation type corresponding to the request is a write operation and the identifier of the first data source A is a write identifier, then link the request to the first data source A. If the operation type corresponding to the request is a read operation and the identifier of the second data source A' is a read identifier, then link the request to the second data source A'.
[0068] The data source read-write separation method according to an embodiment of the present invention obtains the original data source of the service system, uses the original data source as the first data source, copies the first data source to obtain a second data source, and uses one of the first data source and the second data source as the read data source and the other as the write data source; intercepts requests accessing the service system and identifies the operation type corresponding to the request, where the operation type includes read operations and write operations; when it is determined that the operation type corresponding to the request is a write operation, links the request to the write data source for write operations; and when it is determined that the operation type corresponding to the request is a read operation, links the request to the read data source for read operations. By means of this technology, it can achieve data source read-write separation in a non-invasive manner, has little intrusion into business code, supports switching between different types of data sources, and reduces development costs.
[0069] Figure 2 is a schematic flowchart of the data source read-write separation method according to another embodiment of the present invention, as Figure 2 shown, where the merging of dataSource (data source) corresponds to Figure 1 step S101 in. Thread isolation means that in the embodiment of the present invention, the proxy manager is used to achieve separate management of multiple data sources, and each data source is an independent thread pool. In this embodiment, the request accessing the service system is linked to the read data source or the write data source corresponding to its operation type for operation, that is, the request is switched to the isolated thread pool for operation. The thread pool in this embodiment is the current data source thread pool solutions such as Druid and Hikari, where both Druid and Hikari are connection pool technologies. The read data source and the write data source in this embodiment are connected to different thread pools. The aspect rule identification corresponds to Figure 1 step S102 in, and is used to identify the operation type of the request accessing the service system. The dynamic routing corresponds to Figure 1 step S103 in, and is used to dynamically link the request to the read data source for read operations or dynamically link the request to the write data source for write operations according to the operation type.
[0070] The data source read-write separation method according to an embodiment of the present invention can achieve data source read-write separation in a non-invasive manner, has little intrusion into business code, supports switching between different types of data sources, and reduces development costs.
[0071] Figure 3 is a schematic diagram of the main process of the data source read-write separation method according to another embodiment of the present invention, as Figure 3 shown, and the method includes:
[0072] Step S301: Obtain an instance of the original data source of the business system to obtain the original data source of the business system. Use the original data source as the first data source, copy the first data source to obtain a second data source, use the first data source as the write data source, and use the second data source as the read data source. Among them, when the number of instances is multiple, it is determined that the business system includes multiple original data sources. Use each data source among the multiple original data sources as the first data source, and copy the first data source respectively to obtain multiple corresponding second data sources. When the number of instances is one, it is determined that the business system includes one original data source.
[0073] Step S302: Create a proxy manager, which is used to manage the multiple first data sources and second data sources.
[0074] Step S303: Determine the identifiers of each first data source and second data source, and record the correspondence between the identifier and the first data source or the second data source in the proxy manager. Among them, the identifier of each first data source is the write identifier, and the identifier of each second data source is the read identifier.
[0075] Step S304: Intercept the request to access the business system through the AOP aspect.
[0076] Step S305: Determine the target entity class of the request.
[0077] Step S306: According to the preset object-relational mapping table and the target entity class, determine the first target data source corresponding to the request from the multiple first data sources, where the object-relational mapping table is used to record the mapping relationship between the original data source of the business system and the corresponding entity class.
[0078] Step S307: Identify the operation type corresponding to the request, and the operation type includes read operation and write operation.
[0079] Step S308: If the identified operation type is a write operation, link the request to the first target data source. If the identified operation type is a read operation, link the request to the second target data source corresponding to the first target data source.
[0080] The data source read-write separation method according to the embodiment of the present invention obtains the original data source of the service system, uses the original data source as the first data source, copies the first data source to obtain a second data source, and uses one of the first data source and the second data source as the read data source and the other as the write data source; intercepts requests accessing the service system and identifies the operation type corresponding to the requests, where the operation type includes read operations and write operations; based on the operation type, links the requests to the read data source for read operations or links the requests to the write data source for write operations. By such technical means, it can achieve data source read-write separation in a non-invasive manner, has little intrusion into the service code, supports switching between different types of data sources, and reduces the development cost.
[0081] Figure 4 is a schematic diagram of the main modules of the data source read-write separation device 400 according to the embodiment of the present invention, as Figure 4 shown. The data source read-write separation device 400 includes:
[0082] A data source initialization module 401, configured to determine the data source of the service system, use the data source as the first data source, copy the first data source to obtain a second data source, and use one of the first data source and the second data source as the read data source and the other as the write data source;
[0083] An identification module 402, configured to intercept requests accessing the service system and identify the operation type corresponding to the requests, where the operation type includes read operations and write operations;
[0084] A linking module 403, configured to, when determining that the operation type corresponding to the request is a write operation, link the request to the write data source for a write operation; when determining that the operation type corresponding to the request is a read operation, link the request to the read data source for a read operation.
[0085] Optionally, the data source initialization module 401 is further configured to: create a proxy manager; determine the identifiers of each of the first data source and each of the second data source, where the identifiers include read identifiers and write identifiers; and record the identifiers in the proxy manager.
[0086] Optionally, the linking module 403 is further configured to: if it is determined that the operation type corresponding to the request is a write operation, look up the write identifier from the proxy manager and link the request to the first data source or the second data source corresponding to the write identifier for a write operation; if it is determined that the operation type corresponding to the request is a read operation, look up the read identifier from the proxy manager and link the request to the first data source or the second data source corresponding to the write identifier for a read operation.
[0087] Optionally, the data source initialization module 401 is further configured to: obtain an instance of the original data source of the service system; when the number of instances is multiple, determine that the service system includes multiple original data sources, and use each data source in the multiple original data sources as the first data source; copy the first data source respectively to obtain a plurality of corresponding second data sources.
[0088] Optionally, the recognition module 402 is further configured to: determine the target entity class of the request; according to a preset object relationship mapping table and the target entity class, determine a first target data source corresponding to the request from the multiple first data sources, where the object relationship mapping table is used to record the mapping relationship between the original data source of the service system and the corresponding entity class;
[0089] The linking module 403 is further configured to: in the case where it is determined that the operation type corresponding to the request is a write operation, if the first target data source is a write data source, link the request to the first target data source for a write operation, and if the first target data source is a read data source, link the request to the second target data source corresponding to the first target data source for a write operation; in the case where it is determined that the operation type corresponding to the request is a read operation, if the first target data source is a read data source, link the request to the first target data source for a read operation, and if the first target data source is a write data source, link the request to the second target data source corresponding to the first target data source for a read operation.
[0090] Optionally, the recognition module 402 is further configured to: obtain the annotation of the request and recognize the operation type corresponding to the annotation.
[0091] Optionally, the annotation includes one or more of the following: Mybatis annotation, JPA annotation, and custom annotation.
[0092] Optionally, the recognition module 402 is further configured to: recognize the operation type corresponding to the request through an abstract syntax tree.
[0093] The data source read-write separation device and method according to the embodiments of the present invention obtain the original data source of the service system, use the original data source as the first data source, copy the first data source to obtain the second data source, and use one of the first data source and the second data source as the read data source and the other as the write data source; intercept requests accessing the service system and identify the operation types corresponding to the requests, where the operation types include read operations and write operations; when it is determined that the operation type corresponding to the request is a write operation, link the request to the write data source for write operations; and when it is determined that the operation type corresponding to the request is a read operation, link the request to the read data source for read operations. By these technical means, the data source read-write separation can be achieved in a non-invasive manner, with little intrusion into the service code, support for switching different types of data sources, and reduced development costs.
[0094] The above device can execute the method provided by the embodiments of the present invention, and has the corresponding functional modules and beneficial effects for executing the method. For technical details not described in detail in this embodiment, reference can be made to the method provided by the embodiments of the present invention.
[0095] Figure 5 is a schematic diagram of a system applying the data source read-write separation device according to the embodiments of the present invention. As Figure 5 shown, the data source read-write separation device intercepts external read-write access requests, identifies the operation types of the access requests. If the operation type of the access request is a read operation, link the access request to the corresponding read data source; if the operation type of the access request is a write operation, link the access request to the corresponding write data source, thereby achieving read-write separation.
[0096] Figure 6 shows an exemplary system architecture 600 that can apply the data source read-write separation method or the data source read-write separation device according to the embodiments of the present invention.
[0097] As Figure 6 shown, the system architecture 600 may include terminal devices 601, 602, 603, a network 604, a server 605, and a database 606. The network 604 is used to provide a medium for communication links between the terminal devices 601, 602, 603 and the server 605. The network 604 may include various connection types, such as wired, wireless communication links, or fiber optic cables, etc.
[0098] Users can use the terminal devices 601, 602, 603 to interact with the server 605 through the network 604 to receive or send messages, etc. Various communication applications may be installed on the terminal devices 601, 602, 603, and requests to access the server are sent to the server through the communication application.
[0099] The server 605 may be a server that provides various services, such as a server that provides data services to users. The server 605 may link the access request to a read data source or a write data source according to the received access request, and perform data read and write services on the database 606 through the linked read data source or write data source.
[0100] It should be noted that the data source read-write separation method provided in the embodiment of the present invention is generally executed by the server 605 , and accordingly, the data source read-write separation device is generally disposed in the server 605 .
[0101] It should be understood that Figure 6 The number of terminal devices, networks and servers in the embodiment is only for illustration. Any number of terminal devices, networks and servers may be provided according to implementation requirements.
[0102] Reference below Figure 7 , which shows a schematic diagram of the structure of a computer system 700 of a server device suitable for implementing an embodiment of the present invention. Figure 7 The terminal device shown is only an example and should not bring any limitation to the functions and scope of use of the embodiments of the present invention.
[0103] like Figure 7 As shown, the computer system 700 includes a central processing unit (CPU) 701, which can perform various appropriate actions and processes according to a program stored in a read-only memory (ROM) 702 or a program loaded from a storage part 708 into a random access memory (RAM) 703. In the RAM 703, various programs and data required for the operation of the system 700 are also stored. The CPU 701, the ROM 702, and the RAM 703 are connected to each other via a bus 704. An input / output (I / O) interface 705 is also connected to the bus 704.
[0104] The following components are connected to the I / O interface 705: an input section 706 including a keyboard, a mouse, etc.; an output section 707 including a cathode ray tube (CRT), a liquid crystal display (LCD), etc., and a speaker, etc.; a storage section 708 including a hard disk, etc.; and a communication section 709 including a network interface card such as a LAN card, a modem, etc. The communication section 709 performs communication processing via a network such as the Internet. A drive 710 is also connected to the I / O interface 705 as needed. A removable medium 711, such as a magnetic disk, an optical disk, a magneto-optical disk, a semiconductor memory, etc., is installed on the drive 710 as needed, so that a computer program read therefrom is installed into the storage section 708 as needed.
[0105] In particular, according to the embodiments disclosed in the present invention, the processes described above with reference to the flowcharts can be implemented as computer software programs. For example, the embodiments disclosed in the present invention include a computer program product that includes a computer program carried on a computer-readable medium, and the computer program includes program code for performing the methods shown in the flowcharts. In such an embodiment, the computer program can be downloaded and installed from the network through the communication section 709, and / or installed from the removable medium 711. When the computer program is executed by the central processing unit (CPU) 701, the above functions defined in the system of the present invention are performed.
[0106] It should be noted that the computer-readable medium shown in the present invention can be a computer-readable signal medium, a computer-readable storage medium, or any combination of the two. The computer-readable storage medium can be, for example, but not limited to, an electrical, magnetic, optical, electromagnetic, infrared, or semiconductor system, apparatus, or device, or any combination of the above. More specific examples of the computer-readable storage medium can include, but are not limited to: an electrical connection having one or more wires, a portable computer disk, a hard disk, a random access memory (RAM), a read-only memory (ROM), an erasable programmable read-only memory (EPROM or flash memory), an optical fiber, a portable compact disk read-only memory (CD-ROM), an optical storage device, a magnetic storage device, or any suitable combination of the above. In the present invention, the computer-readable storage medium can be any tangible medium that contains or stores a program that can be used by or in conjunction with an instruction execution system, apparatus, or device. And in the present invention, the computer-readable signal medium can include a data signal propagated in a baseband or as part of a carrier wave, which carries the computer-readable program code. Such a propagated data signal can take various forms, including but not limited to electromagnetic signals, optical signals, or any suitable combination of the above. The computer-readable signal medium can also be any computer-readable medium other than the computer-readable storage medium, which can send, propagate, or transmit a program for use by or in conjunction with an instruction execution system, apparatus, or device. The program code contained on the computer-readable medium can be transmitted by any suitable medium, including but not limited to: wireless, wire, optical fiber, RF, etc., or any suitable combination of the above.
[0107] The flowcharts and block diagrams in the accompanying drawings illustrate the possible architectures, functions, and operations of systems, methods, and computer program products according to various embodiments of the present invention. In this regard, each block in the flowchart or block diagram may represent a module, a segment of a program, or a part of code, and the above-mentioned module, segment of a program, or part of code contains one or more executable instructions for implementing the specified logical function. It should also be noted that in some alternative implementations, the functions marked in the blocks may occur in a different order than that marked in the accompanying drawings. For example, two consecutive blocks shown may actually be executed substantially in parallel, and they may sometimes be executed in the reverse order, depending on the functions involved. It should also be noted that each block in the block diagram or flowchart, as well as the combination of blocks in the block diagram or flowchart, may be implemented by a dedicated hardware-based system for performing the specified functions or operations, or may be implemented by a combination of dedicated hardware and computer instructions.
[0108] The modules described in the embodiments of the present invention can be implemented in software or in hardware. The described modules can also be provided in a processor. For example, it can be described as: a processor includes a sending module, an obtaining module, a determining module, and a first processing module. Among them, the names of these modules do not constitute a limitation to the unit itself in some cases. For example, the sending module can also be described as "a module for sending a picture acquisition request to the connected server".
[0109] As another aspect, the present invention also provides a computer-readable medium, which can be included in the device described in the above embodiments; or can exist separately without being assembled into the device. The above computer-readable medium carries one or more programs, and when the above one or more programs are executed by a device, the device includes:
[0110] Determine the original data source of the service system, obtain the original data source of the service system, use the original data source as the first data source, copy the first data source to obtain a second data source, and use one of the first data source and the second data source as the read data source and the other as the write data source;
[0111] Intercept requests accessing the service system and identify the operation type corresponding to the request, where the operation type includes a read operation and a write operation;
[0112] When it is determined that the operation type corresponding to the request is a write operation, link the request to the write data source for a write operation; when it is determined that the operation type corresponding to the request is a read operation, link the request to the read data source for a read operation.
[0113] The technical solution of the embodiment of the present invention obtains the original data source of the business system, uses the original data source as the first data source, copies the first data source to obtain the second data source, uses one of the first data source and the second data source as the read data source, and the other as the write data source; intercepts the requests accessing the business system and identifies the operation types corresponding to the requests, where the operation types include read operations and write operations; when it is determined that the operation type corresponding to the request is a write operation, links the request to the write data source for write operations; when it is determined that the operation type corresponding to the request is a read operation, links the request to the read data source for read operations. By these technical means, it can achieve read-write separation of the data source in a non-invasive manner, has little intrusion into the business code, supports switching of different types of data sources, and reduces the development cost.
[0114] The above specific embodiments do not constitute a limitation on the protection scope of the present invention. Those skilled in the art should understand that various modifications, combinations, sub-combinations, and substitutions can occur depending on design requirements and other factors. Any modifications, equivalent substitutions, and improvements made within the spirit and principles of the present invention shall be included within the protection scope of the present invention.
Claims
1. A method for separating read and write of data sources, characterized in that, Including: Obtain the original data source of the business system, use the original data source as the first data source, copy the first data source to obtain a second data source, and use one of the first data source and the second data source as the read data source and the other as the write data source; wherein, replace the configuration information of the copied first data source with a pre-set read service configuration file, and use the read service configuration file as the configuration file of the first data source or the second data source; Intercept requests accessing the business system and identify the operation type corresponding to the requests, where the operation type includes read operations and write operations; wherein, identify the operation type corresponding to the requests through an abstract syntax tree; before identifying the operation type corresponding to the requests, determine the target entity class of the requests; according to a preset object-relational mapping table and the target entity class, determine a first target data source corresponding to the requests from multiple first data sources, and the object-relational mapping table is used to record the mapping relationship between the original data source of the business system and the corresponding entity class; When determining that the operation type corresponding to the requests is a write operation, link the requests to the write data source for write operations, including: if the first target data source is the write data source, link the requests to the first target data source for write operations, and if the first target data source is the read data source, link the requests to a second target data source corresponding to the first target data source for write operations; When determining that the operation type corresponding to the requests is a read operation, link the requests to the read data source for read operations, including: if the first target data source is the read data source, link the requests to the first target data source for read operations, and if the first target data source is the write data source, link the requests to a second target data source corresponding to the first target data source for read operations.
2. The method according to claim 1, characterized in that, The method further includes: Create a proxy manager; Determine the identifiers of the first data source and the second data source, where the identifiers include a read identifier and a write identifier; Record the identifiers in the proxy manager.
3. The method according to claim 2, wherein When determining that the operation type corresponding to the requests is a write operation and linking the requests to the write data source for write operations includes: If it is determined that the operation type corresponding to the requests is a write operation, look up the write identifier in the proxy manager, and link the requests to the first data source or the second data source corresponding to the write identifier for write operations; When determining that the operation type corresponding to the requests is a read operation and linking the requests to the read data source for read operations includes: If it is determined that the operation type corresponding to the requests is a read operation, look up the read identifier in the proxy manager, and link the requests to the first data source or the second data source corresponding to the read identifier for read operations.
4. The method according to claim 3, wherein Obtain the original data source of the business system, use the original data source as the first data source, and copy the first data source to obtain a second data source includes: Obtain an instance of the original data source of the business system to obtain the original data source of the business system; When the number of the instances is multiple, it is determined that the business system includes multiple original data sources, and each original data source in the multiple original data sources is used as a first data source; The first data source is copied respectively to obtain multiple corresponding second data sources.
5. The method according to claim 1, characterized in that Identifying the operation type corresponding to the request includes: obtaining the annotation of the request and identifying the operation type corresponding to the annotation.
6. The method according to claim 5, characterized in that, The annotation includes one or more of the following: Mybatis annotation, JPA annotation, and custom annotation.
7. A data source read-write separation device, characterized in that, It includes: A data source initialization module, configured to determine the data source of the business system, use the data source as the first data source, copy the first data source to obtain a second data source, and use one of the first data source and the second data source as the read data source and the other as the write data source; wherein, the configuration information of the copied first data source is replaced with a pre-set read service configuration file, and the read service configuration file is used as the configuration file of the first data source or the second data source; An identification module, configured to intercept a request for accessing the business system and identify the operation type corresponding to the request, where the operation type includes a read operation and a write operation; wherein, the operation type corresponding to the request is identified through an abstract syntax tree; before identifying the operation type corresponding to the request, the target entity class of the request is determined; according to a preset object relationship mapping table and the target entity class, a first target data source corresponding to the request is determined from multiple first data sources, and the object relationship mapping table is used to record the mapping relationship between the original data sources of the business system and the corresponding entity classes; A linking module, configured to link the request to the write data source for a write operation when it is determined that the operation type corresponding to the request is a write operation, including: if the first target data source is the write data source, linking the request to the first target data source for a write operation, and if the first target data source is the read data source, linking the request to a second target data source corresponding to the first target data source for a write operation; When it is determined that the operation type corresponding to the request is a read operation, linking the request to the read data source for a read operation, including: if the first target data source is the read data source, linking the request to the first target data source for a read operation, and if the first target data source is the write data source, linking the request to a second target data source corresponding to the first target data source for a read operation.
8. An electronic device, characterized in that, It includes: One or more processors; A storage device, configured to store one or more programs, When the one or more programs are executed by the one or more processors, the one or more processors implement the method according to any one of claims 1-6.
9. A computer-readable medium having a computer program stored thereon, characterized in that, The program, when executed by the processor, implements the method according to any one of claims 1-6.
Citation Information
Patent Citations
Method and device for reading and writing separation of database
CN107301179A
Method for realizing database dynamic switching and computer readable storage medium
CN110321337A