Read-write separation method, electronic device, and storage medium
By adding annotations to the interface and intercepting business requests to obtain data source information, the inaccuracy of read-write separation in existing technologies is solved, enabling more accurate data source selection and ensuring that SQL statements use the correct data source in the interface methods.
Patent Information
- Application Number
- CN202210181745.7
- Authority / Receiving Office
- CN · China
- Patent Type
- Patents(China)
- Current Assignee / Owner
- Filing Date
- 2022-02-25
- Publication Date
- 2026-01-02
- Estimated Expiration
- 2042-02-25
AI Technical Summary
In existing technologies, when reading and writing separation is achieved by intercepting methods of the Class class, it cannot be fully realized because the methods of the Class class contain business logic for both the primary and secondary data sources, causing the query SQL statement to still be executed from the primary data source.
By adding a first annotation to the interface in the business request to indicate the database interface, and adding a second annotation to the interface method to indicate the data source, the business request is intercepted to obtain the second annotation to select the target data source, ensuring that the SQL statement uses the correct data source when it is executed.
It enables accurate selection of the target data source even when there is business logic for both primary and secondary data sources in the interface method, thus achieving more precise read-write separation.
Smart Images

Figure CN116701416B_ABST
Abstract
Description
TECHNICAL FIELD
[0001] The present application relates to the technical field of Internet, in particular to a read-write separation method, an electronic device and a storage medium. BACKGROUND
[0002] With the rapid development of Internet technology, the application of database has become an indispensable part in Internet. With the increase of user quantity and data files, when a large amount of data concurrently accesses the database, the read-write times of the database are huge, which causes the access pressure of the database to increase exponentially. In order to alleviate the pressure of the database, the read-write separation technology appears. The principle of read-write separation is to let the master data source handle transactional increase, modification and deletion operations, and the slave data source handles query operations.
[0003] The method for realizing data separation in the prior art is generally to realize read-write separation by intercepting the method of the Class class. However, the method of intercepting the Class class needs to care about the business logic. If the business logic of the master data source and the slave data source exists in the method of the Class class at the same time, the query SQL (Structured Query Language) statement in the method of the Class class will still be executed in the master data source, so this method does not completely realize read-write separation. SUMMARY
[0004] In view of the above problems, the embodiments of the present application provide a read-write separation method, an electronic device and a storage medium, which can more accurately realize read-write separation.
[0005] According to an aspect of an embodiment of the present application, a read-write separation method is provided, and the method comprises:
[0006] In response to receiving a business request, an interface marked with a first annotation in the business request is intercepted, and a second annotation marked on an interface method of the interface is acquired; the first annotation is used to indicate that the interface is a database interface, and the second annotation is used to indicate a data source required by the interface method, and the data source comprises a master data source and a slave data source;
[0007] A target data source used when executing a SQL statement in the business request is selected based on the second annotation.
[0008] According to another aspect of an embodiment of the present application, an electronic device is provided, comprising: one or more processors; and one or more computer readable storage media having instructions stored thereon; when the instructions are executed by the one or more processors, the processor executes the read-write separation method according to any one of the above.
[0009] According to another aspect of the embodiments of the present application, there is provided a computer readable storage medium having stored thereon a computer program which, when executed by a processor, causes the processor to perform the read-write separation method according to any one of the preceding embodiments.
[0010] In the embodiments of the present application, a first annotation for indicating that the interface is a database interface is added to the interface in the service request, a second annotation for indicating a data source required by the interface method is added to the interface method of the interface, after receiving the service request, the interface in the service request marked with the first annotation is intercepted in response to receiving the service request, the second annotation marked on the interface method of the interface is acquired, and the target data source used when executing the SQL statement (specifically, the SQL statement in the interface method) in the service request is selected based on the second annotation. As can be seen, in the embodiments of the present application, the interface method in the database interface can be intercepted, since the code is simply executed as a SQL statement when executing the interface method in the database interface, and the business logic does not need to be concerned, even if the business logic of the master data source and the slave data source exists in the interface method at the same time, the data source required for use can be accurately selected based on the second annotation when executing the SQL statement in the interface method, so that the read-write separation can be more accurately implemented. BRIEF DESCRIPTION OF DRAWINGS
[0011] In order to more clearly illustrate the technical solutions of the embodiments of the present application, the drawings needed to be used in the description of the embodiments of the present application will be briefly introduced. Obviously, the drawings in the following description are only some of the drawings of the present application, and other drawings can be obtained by those skilled in the art without creative labor on the basis of these drawings.
[0012] Figure 1 is a step flow chart of a read-write separation method according to an embodiment of the present application.
[0013] Figure 2 is a data source initialization flowchart according to an embodiment of the present application.
[0014] Figure 3 is a step flow chart of another read-write separation method according to an embodiment of the present application.
[0015] Figure 4 is a structural schematic diagram of an electronic device according to an embodiment of the present application. DETAILED DESCRIPTION
[0016] The technical solutions in the embodiments of the present application will be described clearly and completely below with reference to the drawings in the embodiments of the present application. Obviously, the described embodiments are only part of the embodiments of the present application, rather than all the embodiments of the present application. Based on the embodiments in the present application, all other embodiments obtained by a person of ordinary skill in the art without creative effort belong to the scope of protection of the present application.
[0017] The read-write separation method of the embodiments of the present application can be applied to a server. A user triggers a service request by operating on a user device, the user device sends the service request to the server, the server analyzes the service request, determines the data source (i.e. the database) required by the SQL statement in the service request, and then links to the corresponding data source for processing.
[0018] Exemplarily, the user device can include but is not limited to a mobile phone, a tablet computer, a wearable device, a notebook computer, a desktop computer, a car terminal, etc.
[0019] Referring to Figure 1 , a step flowchart of a read-write separation method of the embodiments of the present application is shown.
[0020] As Figure 1 shown, the read-write separation method can include the following steps:
[0021] Step 101, in response to receiving a service request, intercepting an interface marked with a first annotation in the service request, and obtaining a second annotation marked on an interface method of the interface.
[0022] The master data source and the slave data source are initialized in advance. The master data source is used to implement the write operation of data, including but not limited to the increase, deletion, modification, etc. The slave data source is used to implement the read operation of data, including but not limited to the query, etc. The initialization process of the master data source and the slave data source will be described in detail in the following embodiments.
[0023] The service request is annotated in advance. The database interface contained in the service request is annotated with a first annotation, and the first annotation is used to indicate that the interface is a database interface. The interface method in the database interface is annotated with a second annotation, and the second annotation is used to indicate the data source required by the interface method, i.e. whether the master data source or the slave data source is required by the interface method. Specifically, if the interface method is a write operation of data, the master data source is required; if the interface method is a read operation of data, the slave data source is required.
[0024] The server responds to receiving a service request, intercepts an interface marked with a first annotation in the service request using an interception function, and obtains a second annotation marked on an interface method of the interface.
[0025] For example, the interception function can include, but is not limited to, an AOP (Aspect Oriented Programming) interception mode, etc. The server can use the acquisition annotation function to acquire the first annotation and the second annotation marked in the business request.
[0026] In step 102, the target data source used when executing the SQL statement in the business request is selected based on the second annotation.
[0027] Since the second annotation on the interface method indicates the data source required by the interface method, when the SQL statement in the business request (i.e., the SQL statement in the interface method) is executed, the target data source can be accurately selected based on the second annotation.
[0028] Specifically, if the second annotation indicates that the data source required by the interface method is the master data source, the target data source used when executing the SQL statement in the interface method is the master data source; if the second annotation indicates that the data source required by the interface method is the slave data source, the target data source used when executing the SQL statement in the interface method is the slave data source.
[0029] In the embodiment of the application, the interface method in the database interface can be intercepted. Since the code is simply executed as an SQL statement when the interface method in the database interface is executed, and the business logic does not need to be concerned, even if the business logic of the master data source and the slave data source coexist in the interface method, the data source required for execution of the SQL statement in the interface method can be accurately selected based on the second annotation, so that read-write separation can be more accurately implemented.
[0030] Referring to Figure 2 , a data source initialization flowchart of an embodiment of the application is shown.
[0031] As shown in Figure 2 , the data source initialization flowchart includes the following steps:
[0032] In step 201, a service instance is started.
[0033] In step 202, a database connection pool configuration file is read.
[0034] The database connection pool is used to manage the connection of the database (i.e., the data source) to avoid creating a connection each time an SQL statement is executed. The managed data source can include the master data source and the slave data source. For example, the database connection pool can include, but is not limited to, Druid, C3P0, Proxool, Primrose, etc.
[0035] The database connection pool configuration file is a pre-configured and stored file. For example, the information in the database connection pool configuration file can include, but is not limited to, the name of the database, the URL (Uniform Resource Locator) for connecting to the database, the username for connecting to the database, the password for connecting to the database, the maximum number of connection pools, the minimum number of connection pools, the maximum waiting time when obtaining a connection, and the like.
[0036] In step 203, the master data source and the slave data source are generated based on the configuration file.
[0037] For example, in the architecture of the Spring container family component, in general, the Druid loader encapsulated by the Spring container is used to initialize the database connection configuration and the like at the time of starting the service instance, but some configuration information about the connection pool is usually used by default and the values of these configurations cannot be customized. In the embodiment of the present application, the service instance is started, the database connection pool configuration file is read, and the master data source and the slave data source are generated based on the database connection pool configuration file, so that some configuration information in the database connection pool Druid can be further customized.
[0038] Specifically, after reading the database connection pool configuration file, the master data source therein is initialized as a Bean (instance) of the Spring container, so as to generate the master data source (SourceDataSource) in the embodiment of the present application, and the slave data source therein is initialized as a Bean (instance) of the Spring container, so as to generate the slave data source (ReplicaDataSource) in the embodiment of the present application.
[0039] In step 204, a data source routing class is customized.
[0040] In step 205, the master data source and the slave data source are set as the properties of the data source routing class.
[0041] The data source routing class A is customized and inherits the preset dynamic data source switching class (AbstractRoutingDataSource class), and the method (determineCurrentLookupKey method) for selecting a data source in the dynamic data source switching class is implemented.
[0042] The AbstractRoutingDataSource class is used to implement dynamic data source switching, and the AbstractRoutingDataSource class contains a determineCurrentLookupKey method. The specific selection of a data source during dynamic data source switching is determined by the determineCurrentLookupKey() method. This method is an abstract method, and the AbstractRoutingDataSource abstract class needs to be inherited to override this method. The method returns a Key, which is a BeanName (instance name) in the Bean, and assigns the Key to the LookupKey. The Key can be used to obtain the corresponding DataSource (data source) value, thereby achieving the function of data source switching.
[0043] The data source routing class A is then registered in the Spring container to generate a Bean (instance), and the generated master data source (SourceDataSource) and slave data source (ReplicaDataSource) are set as the properties of the data source routing class A.
[0044] For example, part of the code of the data source routing class A is as follows:
[0045] Data source routing class A +determineCurrentLookupKey(): void
[0046] Referring to Figure 3 , a step flowchart of another read-write separation method of the embodiment of the application is shown.
[0047] As shown in Figure 3 , the read-write separation method can include the following steps:
[0048] Step 301, receiving a service request.
[0049] For example, the service request in this embodiment can be implemented based on the JPA (Java Persistence Application Programming Interface, Java Persistence Application Programming Interface) framework. The JPA framework is an object-oriented query statement construction, which can quickly integrate and implement the operations of adding, deleting, modifying and querying. By customizing an interface and inheriting the JPA interface, the operations of adding, deleting, modifying and querying can be implemented. In addition, the method name can be defined according to the rules, and the SQL statement can be assembled by the JPA framework.
[0050] Step 302, in response to receiving the service request, intercepting an interface in the service request marked with a first annotation, and obtaining a second annotation marked on an interface method of the interface.
[0051] The example is that, because the AOP of spring cannot intercept the annotations on the interface method directly, a first annotation is defined to act on the interface class, specifically on the Repository (resource library, i.e. database) interface of the JPA framework; a second annotation is defined to act on the interface method, wherein the second annotation includes two kinds, one indicating that the interface method needs to use the master data source, and the other indicating that the interface method needs to use the slave data source.
[0052] In step 303, the thread variable of the current thread is set based on the second annotation.
[0053] Specifically, the process of setting the thread variable of the current thread based on the second annotation can include: in response to the second annotation indicating that the data source needed by the interface method is the master data source, setting the thread variable of the current thread to a first value, the first value indicating that the data source needed by the current thread is the master data source; in response to the second annotation indicating that the data source needed by the interface method is the slave data source, setting the thread variable of the current thread to a second value, the second value indicating that the data source needed by the current thread is the slave data source.
[0054] In an optional implementation, a setting class B is defined in advance, the thread variable of the current thread is defined in the setting class B, and the setting class B includes a setting routing data source method for setting the thread variable of the current thread defined in the setting class B.
[0055] In this case, the setting routing data source method in the pre-defined setting class B can be called to set the thread variable of the current thread defined in the setting class B based on the second annotation. If the second annotation indicates that the data source needed by the interface method is the master data source, the setting routing data source method sets the thread variable of the current thread to the first value described above, and if the second annotation indicates that the data source needed by the interface method is the slave data source, the setting routing data source method sets the thread variable of the current thread to the second value described above.
[0056] In step 304, in response to executing the SQL statement, the thread variable is read, and the target data source to be used is selected based on the thread variable.
[0057] When executing the SQL statement in the interface method described above, the thread variable of the current thread can be read, and the target data source to be used when executing the SQL statement is selected based on the thread variable.
[0058] In an alternative embodiment, the select data source method (determineCurrentLookupKey() method) in the pre-defined data source routing class A can be invoked to obtain the thread variable of the current thread.
[0059] If the thread variable of the current thread is the first value indicating that the master data source is required by the current thread, the master data source is selected as the target data source when the SQL statement in the interface method is executed; if the thread variable of the current thread is the second value indicating that the slave data source is required by the current thread, the slave data source is selected as the target data source when the SQL statement in the interface method is executed.
[0060] At step 305, the thread variable of the current thread is cleared in response to the end of the execution of the SQL statement.
[0061] After the execution of the SQL statement in the interface method, the thread variable of the current thread can be cleared so that the thread variable can be continuously used by other interface methods.
[0062] In an alternative embodiment, the pre-defined setting class B can further include a clear routing data source method for clearing the thread variable of the current thread defined in the setting class B.
[0063] In this case, the clear routing data source method in the pre-defined setting class B can be invoked to clear the thread variable of the current thread defined in the setting class B.
[0064] The following is an example.
[0065] For example, a setting class B is defined, and a ThreadLocal type variable C is defined in the setting class B. The variable C is a thread variable belonging to the current thread, and the variable C stores the value of the current thread. The setting class B further includes a set routing data source method (setRoutingDataSource() method) and a clear routing data source method (removeRoutingDataSource() method). The setRoutingDataSource() method is used to set the value of the thread variable C of the current thread, and the removeRoutingDataSource() method is used to clear the value of the thread variable C of the current thread.
[0066] For example, part of the code of the setting class B is shown as follows:
[0067]
[0068] For example, the first annotation is defined as RWInter, which acts on the interface class, specifically on the Repository interface of the JPA framework; the second annotation is defined as Source or Replica, which acts on the interface method, where Source indicates that the interface method needs to use the master data source, and Replica indicates that the interface method needs to use the slave data source.
[0069] In response to receiving the service request, the code for intercepting the RWInter annotation is as follows:
[0070] Pointcut pointcut=new AnnotationMatchingPointcut(RWInter.class,true);
[0071] Advice advice=new MethodAroundAdvice();
[0072] new DefaultPointcutAdvisor(pointcut,advice);
[0073] The MethodAroundAdvice class needs to implement MethodBeforeAdvice and AfterReturningAdvice, and override the before method and the afterReturning method.
[0074] The before method is used to determine whether the second annotation exists on the intercepted interface method before the execution of the intercepted interface method, and to set the thread variable of the current thread based on the second annotation. The afterReturning method is used to clear the thread variable of the current thread after the execution of the intercepted interface method.
[0075] After intercepting the interface marked with the first annotation RWInter annotation, the second annotation on the interface method of the interface is obtained. In the before method, if the second annotation in the interface method is the Source annotation, the before method calls the setRoutingDataSource() method in the setting class B to set the ThreadLocal type thread variable C defined in the setting class B to a first value D1, indicating that the current thread needs to use the master data source; if the second annotation in the interface method is the Replica annotation, the removeRoutingDataSource() method in the setting class B is called to set the ThreadLocal type thread variable C defined in the setting class B to a second value D2, indicating that the current thread needs to use the slave data source.
[0076] After the intercepted interface method in the SQL statement is executed, the determineCurrentLookupKey() method in the data source routing class A is called to obtain the value of the ThreadLocal type thread variable C defined in the setting class B. If the value of the thread variable C is the first value D1, the primary data source SourceDataSource is selected as the target data source required by the SQL statement; if the value of the thread variable C is the second value D2, the replica data source ReplicaDataSource is selected as the target data source required by the SQL statement.
[0077] After the execution of the SQL statement in the intercepted interface method is completed, the removeRoutingDataSource() method in the setting class B is called in the afterReturning method to clear the ThreadLocal type thread variable C defined in the setting class B.
[0078] In the embodiment of the present application, the interface method in the Repository (resource library) interface under the JPA framework is intercepted, and the read-write separation is realized by using a simple annotation method, without the need to care about the business logic. For the business logic in which the primary data source and the replica data source exist in the Class class method, the read-write separation can also be accurately realized. If it is required to add the read-write separation function in the original code, but the business logic is not familiar, the method of intercepting the interface method in the Repository interface under the JPA framework is more convenient, because the code is executed to the interface method in the Repository interface, which is simply to execute the SQL statement.
[0079] In the embodiment of the present application, an electronic device is also provided. The electronic device can include one or more processors, and one or more computer readable storage media having stored thereon instructions, such as an application. When executed by the one or more processors, the instructions cause the processor to perform the read-write separation method of any one of the above embodiments.
[0080] Figure 4 A structural schematic diagram of an electronic device 400 according to an embodiment of the present application is shown. As shown in FIG. 4, the electronic device 400 can include a processor 401, a memory 402, a storage 403, a communication interface 404, a display 405, and a power supply 406. Figure 4As shown, the electronic device 400 includes a central processing unit (CPU) 401 that can perform various appropriate actions and processes in accordance with computer program instructions stored in a read only memory (ROM) 402 or loaded from a storage unit 408 into a random access memory (RAM) 403. Various programs and data required for operation of the electronic device 400 can also be stored in the RAM 403. The CPU 401, the ROM 402, and the RAM 403 are connected to each other through a bus 404. An input / output (I / O) interface 405 is also connected to the bus 404.
[0081] Various components in the electronic device 400 are connected to the I / O interface 405, including an input unit 406 such as a keyboard, a mouse, a microphone, etc., an output unit 407 such as various types of displays, a speaker, etc., a storage unit 408 such as a magnetic disk, an optical disk, etc., and a communication unit 409 such as a network card, a modem, a wireless communication transceiver, etc. The communication unit 409 allows the electronic device 400 to exchange information / data with other devices through a computer network such as the Internet and / or various telecommunication networks.
[0082] The various processes and procedures described above can be performed by the processing unit 401. For example, the read-write separation method of any of the embodiments described above can be implemented as a computer software program that is tangibly embodied in a computer readable medium such as the storage unit 408. In some embodiments, part or all of the computer program can be loaded and / or installed on the electronic device 400 via the ROM 402 and / or the communication unit 409. When the computer program is loaded into the RAM 403 and executed by the CPU 401, one or more actions of the read-write separation method described above can be performed.
[0083] In embodiments of the present application, there is also provided a computer readable storage medium having stored thereon a computer program, the program being executable by a processor of an electronic device, and causing the processor to perform the read-write separation method as described in any of the embodiments above when the computer program is executed by the processor.
[0084] The aforementioned processor can include, but is not limited to, a CPU, a network processor (NP), a digital signal processor (DSP), an application specific integrated circuit (ASIC), a field-programmable gate array (FPGA) or other programmable logic device, a discrete gate or transistor logic, a discrete hardware component, and the like.
[0085] The aforementioned computer readable storage medium can include, but is not limited to, a ROM, a RAM, a compact disc read-only memory (CD-ROM), an electronically erasable programmable read-only memory (EEPROM), a hard disk, a floppy disk, a flash memory, and the like.
[0086] Each of the embodiments in the specification is described in a progressive manner, and each embodiment focuses on the difference from other embodiments. The same or similar parts between the embodiments can be referred to each other.
[0087] It should be noted that, in this document, the relationship terms such as first and second are only used to distinguish one entity or operation from another entity or operation, and do not necessarily require or imply that there is any such actual relationship or order between the entities or operations. Moreover, the terms "include", "contain" or any other variants thereof are intended to cover non-exclusive inclusion, so that the process, method, article or terminal device including a series of elements not only includes those elements, but also includes other elements not explicitly listed or inherent to such process, method, article or terminal device. Without more limitations, the element defined by the statement "including a" does not exclude the presence of other identical elements in the process, method, article or terminal device including the element.
[0088] Those skilled in the art can clearly understand that the above-mentioned embodiment method can be realized by means of software and necessary general hardware platform, of course, it can also be realized by hardware, but in many cases, the former is a better embodiment. Based on such understanding, the technical solutions of the present application can be embodied in the form of a software product, which is stored in a storage medium (such as ROM, RAM, magnetic disk, or optical disc) and includes a plurality of instructions for causing a terminal (which can be a mobile phone, computer, server, air conditioner, or network device) to execute the methods described in the various embodiments of the present application.
[0089] The embodiments of the present application are described above with reference to the drawings, but the present application is not limited to the specific embodiments described above, which are merely illustrative rather than restrictive, and those of ordinary skill in the art can make many other forms without departing from the purpose of the present application and the scope protected by the claims.
[0090] Those of ordinary skill in the art can realize that the units and algorithm steps of each example described in combination with the embodiments disclosed in the embodiments of the present application can be realized in the form of electronic hardware or a combination of computer software and electronic hardware. Whether the functions are executed in hardware or software depends on the specific application and design constraints of the technical solutions. Those of ordinary skill in the art can use different methods to implement the described functions for each specific application, but such implementation should not be considered beyond the scope of the present application.
[0091] Those of ordinary skill in the art can clearly understand that, for the convenience and brevity of description, the specific working processes of the above-described system, device and unit can refer to the corresponding processes in the foregoing method embodiments, which will not be described here.
[0092] In the embodiments provided in the present application, it should be understood that the disclosed devices and methods can be implemented in other ways. For example, the device embodiments described above are merely illustrative, for example, the division of the units is only a logical function division, and actual implementation can have another division manner, for example, a plurality of units or components can be combined or integrated into another system, or some features can be ignored or not executed. In addition, the coupling or direct coupling or communication connection between the displayed or discussed units can be indirect coupling or communication connection through some interfaces, devices or units, which can be electrical, mechanical or other forms.
[0093] The units described as separate components may or may not be physically separate, and the components displayed as units may or may not be physical units, that is, may be located in one place, or may be distributed on multiple network units. Part or all of the units can be selected according to actual needs to achieve the purpose of the embodiment scheme.
[0094] In addition, the functional units in each embodiment of the present application can be integrated into one processing unit, or each unit can exist physically, or two or more units can be integrated into one unit.
[0095] If the functions are realized in the form of software functional units and sold or used as independent products, they can be stored in a computer readable storage medium. Based on this understanding, the technical solutions of the present application or the part of the prior art that essentially contributes to the prior art or the part of the technical solutions can be embodied in the form of a software product. The computer software product is stored in a storage medium and includes a plurality of instructions for causing a computer device (which can be a personal computer, a server, or a network device, etc.) to execute all or part of the steps of the method described in each embodiment of the present application. The aforementioned storage medium includes: U disk, mobile hard disk, ROM, RAM, magnetic disk or optical disk, and various program code storage media.
[0096] The above is only a specific embodiment of the present application, but the protection scope of the present application is not limited thereto. Any person skilled in the art can easily think of changes or replacements within the technical scope disclosed in the present application, which should be covered within the protection scope of the present application. In summary, the content of the specification should not be understood as a limitation of the present application.
Claims
1. A read-write separation method, characterized in that, The method includes: In response to receiving a business request, the interface marked with the first annotation in the business request is intercepted, and the second annotation marked on the interface method of the interface is obtained; the first annotation is used to indicate that the interface is a database interface, and the second annotation is used to indicate the data source that the interface method needs to use, the data source includes a primary data source and a secondary data source; The target data source to be used when executing the SQL statement in the business request is selected based on the second annotation; The step of selecting the target data source to use when executing the SQL statement in the business request based on the second annotation includes: Based on the second annotation, set the thread variable for the current thread, and the thread variable is used to indicate the data source that the current thread needs to use; In response to the execution of the SQL statement, the thread variable is read, and the target data source to be used is selected based on the thread variable; The reading of the thread variable includes: The thread variable is obtained by calling the select data source method in the predefined data source routing class; The data source routing class is defined in the following way: Read the database connection pool configuration file and generate the master data source and the slave data source based on the configuration file; Customize the data source routing class and inherit the preset dynamic data source switching class, and implement the data source selection method in the dynamic data source switching class; Register the data source routing class into the Spring container, and set the main data source and the secondary data source as properties of the data source routing class.
2. The method according to claim 1, wherein setting the thread variable of the current thread based on the second annotation includes: In response to the second annotation indicating that the interface method needs to use the primary data source, the thread variable of the current thread is set to the first value, which indicates that the current thread needs to use the primary data source; In response to the second annotation indicating that the interface method needs to use a secondary data source, the thread variable of the current thread is set to a second value, which indicates that the current thread needs to use a secondary data source.
3. The method according to claim 1, wherein setting the thread variable of the current thread based on the second annotation includes: The method for setting the route data source in the predefined settings class is invoked, and the thread variable for the current thread defined in the settings class is set based on the second annotation.
4. The method according to claim 1, further comprising: In response to the completion of the SQL statement execution, clear the thread variables of the current thread.
5. The method according to claim 4, wherein clearing the thread variables of the current thread includes: Call the clear route data source method in the predefined settings class to clear the thread variable of the current thread defined in the settings class.
6. An electronic device, characterized in that, include: One or more processors; and One or more computer-readable storage media on which instructions are stored; When the instruction is executed by the one or more processors, the processors perform the read-write separation method as described in any one of claims 1 to 5.
7. A computer-readable storage medium, characterized in that, It stores a computer program that, when executed by a processor, causes the processor to perform the read-write separation method as described in any one of claims 1 to 5.
Citation Information
Patent Citations
Data reading and writing method and device and data reading and writing equipment
CN110442636A
Data source routing management method and device
CN110765187A