Disclosure of Invention
In view of this, the embodiment of the application provides a Web application system for dynamically switching data sources, so as to solve the problem of low efficiency in the existing data source switching scheme.
The embodiment of the application provides a Web application system for dynamically switching data sources, which comprises a control layer and a service layer, wherein a service in the service layer is pre-configured with a tangent plane-oriented AOP interception tool;
the control layer is used for receiving a request for calling the service and calling a first service from the service layer to execute business operation according to the request; in the process of calling the first service to execute the business operation, the first service calls the second service to execute the intermediate business operation;
the first service is used for determining a first data source corresponding to the business operation through the AOP interception tool before the business operation is executed, and placing the first data source into a pre-constructed target stack;
and the second service is used for determining a second data source corresponding to the intermediate business operation to be executed through the AOP interception tool before the intermediate business operation is executed, and placing the second data source into the target stack.
Further, the AOP interception tool includes a first facet tool;
the determining, by the AOP intercept tool, a first data source corresponding to the executing the service operation includes:
determining, by the first facet tool, the first data source based on a structured query language, SQL, request for switching data sources in performing the business operation, and placing the first data source in the target stack;
the determining, by the AOP intercept tool, a second data source corresponding to the performing the intermediate business operation includes:
and determining, by the first facet tool, the second data source based on an SQL request for switching the data source in performing the intermediate business operation, and placing the second data source in the target stack.
Further, the first cutting tool is provided with a marking tangential point file and a head character tangential point file;
the information in the annotation tangent point file is used for describing the corresponding relation among the annotation, the data source and the database in the SQL request; wherein the annotation is used for pointing to a master database and a slave database;
the information in the header text tangential point file is used for describing the corresponding relation among the key words, the data sources and the database in the SQL request; wherein the key words are used for pointing to the master database and the slave database.
Further, the determining the first data source based on the structured query language SQL request for switching data sources in performing the business operation includes:
identifying target labels and/or target keywords in the SQL request;
if the target label and/or the target keyword points to a main database, identifying the main database as a first database, and identifying a data source associated with the main database as the first data source;
and if the target label and/or the target keyword point to a slave database, identifying the slave database as a first database, and identifying a data source associated with the slave database as the first data source.
Further, the determining the second data source based on the SQL request for switching data sources in performing the intermediate business operation includes:
identifying target labels and/or target keywords in the SQL request;
if the target label and/or the target keyword points to a main database, identifying the main database as a second database, and identifying a data source associated with the main database as the second data source;
and if the target label and/or the target keyword point to a slave database, identifying the slave database as a second database, and identifying a data source associated with the slave database as the second data source.
Further, the business operations comprise a first part of business operations and a second part of business operations;
the first service is specifically further configured to obtain the first data source from the target stack before invoking the second service to perform the intermediate service operation, and perform the first part of service operation according to the first data source;
and after the second service is called to execute the intermediate business operation, acquiring the first data source from the target stack, and executing the second part of business operation according to the first data source so as to complete the business operation.
Further, the second service is specifically configured to obtain a second data source from the target stack, and execute the intermediate service operation according to the second data source.
Further, the AOP intercept tool further includes a second tangent plane tool, where a pop tangent point file is configured in the second tangent plane tool, where the pop tangent point file is used to describe a pop policy of the data source in the target stack.
Further, the second service is specifically further configured to, after executing the intermediate service operation according to the second data source, perform a pop operation on the target stack through the second facet tool, so as to remove the second data source from the target stack.
Further, the first service is specifically further configured to, after executing the second portion of service operation according to the first data source, perform a pop operation on the target stack through the second slicing tool, so as to remove the first data source from the target stack.
The Web application system for dynamically switching the data sources has the following beneficial effects:
according to the Web application system for dynamically switching the data sources, the AOP interception tool is configured for the service in the service layer, so that the packaging operation of the service is realized, when the first service is called from the service layer through the control layer according to the request for calling the service to execute the service operation, and in the process of calling the first service to execute the service operation, the first service also needs to call the second service to execute the intermediate service operation, the first service determines the corresponding first data source in the process of executing the service operation through the AOP interception tool, and the second service determines the corresponding second data source in the process of executing the intermediate service operation through the AOP interception tool, so that the data source switching in the process of calling the service is realized under the condition that the annotation is not required, and the data source switching efficiency in the process of calling the service is improved.
Detailed Description
The present application will be described in further detail with reference to the drawings and examples, in order to make the objects, technical solutions and advantages of the present application more apparent. It should be understood that the specific embodiments described herein are for purposes of illustration only and are not intended to limit the scope of the application.
The Web application system for dynamically switching the data sources provided by all embodiments of the present application is applied to electronic devices, which may be terminal devices or server devices, such as mobile phones or computers.
Fig. 1 shows a schematic structural framework of a Web application 100 for dynamically switching data sources. As shown in fig. 1, the Web application system 100 includes a generic framework 110 of a Web application, where the generic framework 110 of the Web application includes a control layer (controller), a service layer (service), a data interaction layer (dao), and a database layer, and the database layer further includes master data and slave databases. In the general framework 110 of the Web application shown in fig. 1, the control layer serves as an input interface of a request, and calls a service in the service layer to execute corresponding business operation logic according to the received request. When a first service in the service layer is called, the first service may call another service in the process of executing the corresponding business operation, that is, call a second service to execute the intermediate business operation, thereby completing the overall business operation together. Because the method for calling the data interaction layer is also defined in the service, the data interaction with the master database or the slave database in the database layer can be realized by calling the execution logic method in the data transmission layer in the process of calling the service.
In the prior art, when implementing the mutual call of a plurality of services, how to implement the switching of the data sources needs to be considered. For example, an open-source distributed database system MyCat is used as a database agent, the core functions of the open-source distributed database system MyCat are divided into tables and libraries, and when the tool is used, the tool needs to be laid out in multiple aspects, so that the implementation cost is high. For another example, in the process of realizing the inter-call between services, the data source is switched by adopting a mode of adding comments, so that a large number of comments appear in the project, the project is unsightly and tedious, and is easy to forget, and when the added comments are forgotten, the project is loophole and even the service call error occurs.
In the Web application system for dynamically switching data sources provided by the embodiment of the application, the corresponding interception tool is configured by utilizing the tangent plane oriented technology (Aspect Oriented Programming, AOP), namely the AOP interception tool is configured for the service in the service layer, so that the data source switching function is provided for the service with the requirement of calling each other in the service layer on the basis of the original Web application framework. Because the AOP technology is a technology for realizing unified maintenance of program functions through a precompiled mode and dynamic agents in the running period, the service can be packaged by configuring corresponding AOP section classes, namely AOP interception tools, in the service, namely, unified maintenance is realized on the basis that service logic is not executed by the original service.
Fig. 2 shows a schematic flow diagram of the operation of the Web application 100 for dynamically switching data sources.
As shown in fig. 2, the Web application system 100 for dynamically switching data sources includes a control layer 10 and a service layer 20, where services in the service layer 20 are preconfigured with a facet-oriented AOP interception tool. Specifically:
a control layer 10, configured to receive a request for invoking a service, and invoke a first service 21 from the service layer 20 to perform a business operation according to the request; wherein, in the process of calling the first service 21 to execute the business operation, the first service 21 calls the second service 22 to execute the intermediate business operation;
a first service 21, configured to determine, by using an AOP interception tool, a first data source corresponding to a service operation, and put the first data source into a pre-constructed target stack before the service operation is performed;
a second service 22, configured to determine, by using an AOP interception tool, a second data source corresponding to the intermediate service operation, and put the second data source in the target stack before the intermediate service operation is performed; wherein the second data source and the first data source are respectively associated with the same database; or the second data source is associated with a different database than the first data source, respectively.
It should be noted that, when the control layer 10 serves as an input interface of a request and receives any request for calling a service to perform a business operation, the control layer directly calls a corresponding service in the service layer 20 according to the request. In this embodiment, when the control layer 10 calls the first service 21 to perform the business operation, the first service 21 also calls the second service 22 to perform the intermediate business operation, but since the process is triggered by the same request, the call of the first service 21 and the call of the second service 22 by the first service 21 belong to the same operation in one thread, so that all operations in the thread can share one stack.
In all embodiments of the application, the first data source and the second data source may be associated with the same database, i.e. the first data source corresponding to the first service 21 when performing a business operation is the same as the second data source corresponding to the second service 22 when performing an intermediate business operation. Or the first data originates from a different database associated with the second data source, respectively, i.e. the first data source corresponding to the first service 21 performing the business operation is different from the second data source corresponding to the second service 22 performing the intermediate business operation.
Taking as an example the first data source corresponding to the first service 21 when performing a business operation is the same as the second data source corresponding to the second service 22 when performing an intermediate business operation.
For example, the first service 21 selects a main database, such as an operation of writing data into main data, and the second service 22 selects a main database, such as an operation of writing data into main data, where two data sources in the target stack are identical, that is, the data sources at the top and bottom of the stack are both associated with the main database.
For another example, the first service 21 selects from the database, such as in the operation of reading data from the database, and the second service 22 selects from the database, such as in the operation of reading data from the database, where the two data sources in the target stack are identical, i.e., the data sources at the top and bottom of the stack are both associated with the database.
Taking as an example a first data source corresponding to when the first service 21 performs a business operation, a second data source corresponding to when the second service 22 performs an intermediate business operation.
For example, the first service 21 selects a master database, such as an operation to write data to the master database, and the second service 22 selects a slave database, such as an operation to read data from the database, where the two data sources within the target stack are different, wherein the data source at the top of the stack is associated with the slave database and the data source at the bottom of the stack is associated with the master database.
For another example, the first service 21 selects a slave database, such as an operation for reading data from the slave database, and the second service 22 selects a master database, such as an operation for writing data to the master data, where the two data sources within the target stack are different, wherein the data source at the top of the stack is associated with the master database and the data source at the bottom of the stack is associated with the slave database.
In practical application, the master database and the slave database are synchronized according to a certain update period, that is, the master database and the slave database are not kept consistent at any time, when data are inserted into the master database, the master database generates a corresponding log, and the slave database is updated when the next update period arrives based on the log. Therefore, when the service is invoked to write and read data to and from the database, the write operation is only directed to the master database, and the read operation can be directed to the master database or the slave database.
In this embodiment, when the invoked service performs a business operation and interacts with the database, the read-write operation separation of the database in the interaction process is implemented by switching the data source.
Taking service execution logic represented by a first service as writing data into a database, taking reading data from the database as an example, wherein the service execution logic represented by a second service is used for carrying out service operation, the first data source aimed at by the first service is associated with a main database, and the first service needs to call the second service to execute intermediate service operation, but before the first service is used for executing service operation, the content of the first service can be precompiled through an AOP interception tool in the first service, the first data source corresponding to the service operation is determined, then the first data source is put into a target stack, before the second service is called for reading data, the second service can be precompiled through an AOP interception tool in the second service, the second data source corresponding to the intermediate service operation is determined, and then the second data source is put into the target stack, at the moment, the stack top of the target stack is the second data source, so that the second service can directly acquire the second data source from the target stack when the intermediate service is executed, and the second service can directly call the second data source from the target stack, namely, and the method corresponding to the data can be directly read from the data stack. After the second service executes the intermediate business operation, the second data source is popped from the target stack, and the first service executes the business operation, and the first data source is positioned at the stack top of the target stack at this time, so that the first service can directly acquire the first data source from the target stack, that is, the first service can directly write corresponding data into the main database by calling a method in the data interaction layer.
As an embodiment of the application, the AOP intercept tool includes a first facet tool.
In this embodiment, determining, by the AOP intercept tool, a first data source corresponding to performing a service operation includes:
the method comprises the steps of determining a first data source based on a Structured Query Language (SQL) request for switching the data source in executing business operation through a first cutting tool, and placing the first data source on a target stack.
Determining, by the AOP intercept tool, a second data source corresponding to performing the intermediate business operation, including:
and determining a second data source based on the SQL request for switching the data source in the execution of the intermediate business operation by the first cutting tool, and placing the second data source in the target stack.
Note that, the class that needs to be packaged in the service is annotated in the first tangent tool, that is, the tangent point is marked. In this embodiment, a first facet tool is used to describe or specify: the switching of the data sources is performed before the service performs the method logic, i.e. before the first service performs the business operation and before the second service performs the intermediate business operation.
In this embodiment, the first cutting tool is configured with a mark tangential point file and a head text tangential point file.
Specifically:
the information in the mark tangent point file is used for describing the corresponding relation among the mark, the data source and the database in the SQL request; wherein the annotation is used to point to the master and slave databases.
The information in the header text cut point file is used for describing the corresponding relation among the key words, the data sources and the database in the SQL request; wherein the key is used to point to the master database and the slave database.
As an embodiment of the present application, determining the first data source based on a structured query language SQL request for switching data sources in performing the business operation includes:
identifying target labels and/or target keywords in the SQL request;
if the target label and/or the target keyword points to the main database, identifying the main database as a first database, and identifying a data source associated with the main database as a first data source;
if the target annotation and/or target keyword points to the secondary database, the secondary database is identified as the first database, and the data source associated with the secondary database is identified as the first data source.
Determining the second data source based on the SQL request for switching data sources in performing the intermediate business operation comprises:
identifying target labels and/or target keywords in the SQL request;
if the target label and/or the target keyword points to the main database, identifying the main database as a second database, and identifying a data source associated with the main database as a second data source;
if the target annotation and/or target keyword points to the secondary database, the secondary database is identified as a second database and the data source associated with the secondary database is identified as a second data source.
It should be noted that, although the annotation can be used to point to the master database and the slave database, unlike the annotation, the annotation can be specified in a custom manner, that is, only the master data and the slave database need to be distinguished. For example, the designation annotation @ Master points to the Master database and the designation annotation @ Slave points to the Slave database. Similarly, keywords may be custom based on the basis of programming naming specifications, e.g., insert databases typically begin with insert or save, query databases typically begin with get, find, or query, thus switching to master if the method of operating the database begins with insert, save, update, delete, remove, etc., and I switch to slave if the method of querying the database begins with query, get, find, etc.
As an embodiment of the present application, the service operation includes a first part of service operation and a second part of service operation.
The first service is specifically further configured to acquire a first data source from the target stack before invoking the second service to perform the intermediate service operation, and perform a first part of service operation according to the first data source;
after the second service is called to execute the intermediate business operation, a first data source is obtained from the target stack, and a second part of business operation is executed according to the first data source so as to complete the business operation.
In this embodiment, since the target stack has the characteristics of first-in-stack, second-out-stack, and first-in-stack, when the service operation includes the first part service operation and the second part service operation, if the first part service operation is before the second service performs the intermediate service operation, the first part service operation needs to be performed according to the first data source before the second data source is added to the target stack. When the second service is not called to execute the intermediate service operation, the second data source corresponding to the intermediate service operation is not determined through the AOP interception tool in the second service, and the first data source is put into a pre-built target stack, wherein the first data source is positioned at the stack top of the target stack.
The second service is specifically configured to obtain a second data source from the target stack, and execute the intermediate service operation according to the second data source.
It should be noted that, when the first service executes the first part of service operations, it does not represent that all the service operations have been completed, because the first service needs to call the second service to execute the intermediate service operations when executing the service operations, that is, determine, by using the AOP interception tool in the second service, a second data source corresponding to the intermediate service operations, and place the first data source in a pre-constructed target stack, where the second data source is located at the top of the target stack, after the second service obtains the second data source from the top of the target stack to execute the intermediate service operations, the second data source is popped, so that the first data source is located at the top of the stack again, and then the first service can obtain the first data source located at the top of the stack from the target stack, and execute the second part of service operations, so as to complete all the service operations.
As an embodiment of the present application, the AOP intercept tool further includes a second tangent plane tool, where a pop tangent point file is configured in the second tangent plane tool, where the pop tangent point file is used to describe a pop policy of the data source in the target stack.
As an embodiment of the present application, the second service is specifically further configured to, after performing the intermediate service operation according to the second data source, perform a pop operation on the target stack through the second facet tool, so as to remove the second data source from the target stack.
In one embodiment of the present application, the first service is further specifically configured to perform, after performing the second portion of the service operation according to the first data source, a pop operation on the target stack through the second slicing tool, so as to remove the first data source from the target stack.
In this embodiment, the first tangent plane tools defined in different services are different, but the second tangent plane tools are the same, that is, the pop strategies of the data sources in the target stack described in the configured pop tangent point file are the same, and the data source pop operation is performed on the target stack once after the service completes the service operation.
It should be noted that, by configuring the first cutting tool in the AOP interception tool and configuring the tangent point file in the first cutting tool, the service realizes data source switching before executing the service operation, and by configuring the second cutting tool and configuring the same data source pop policy in the second cutting tool, the data source recovery after switching the data source and executing the service operation can be realized, and the normal call of other services is ensured.
As can be seen from the foregoing, in the Web application system for dynamically switching data sources provided in this embodiment, by configuring an AOP interception tool for a service in a service layer, further, a service packaging operation is implemented, so that when a first service is called from the service layer by a control layer according to a request for calling the service to execute a service operation, and in a process of calling the first service to execute the service operation, the first service also needs to call a second service to execute an intermediate service operation, the first service determines, by the AOP interception tool, a first data source corresponding to the process of executing the service operation, and the second service determines, by the AOP interception tool, a second data source corresponding to the process of executing the intermediate service operation, so that, under the condition that an annotation is not required to be added, data source switching in the process of calling the service is implemented, thereby improving data source switching efficiency in the process of calling the service.
The above embodiments are only for illustrating the technical solution of the present application, and not for limiting the same; although the application has been described in detail with reference to the foregoing embodiments, it will be understood by those of ordinary skill in the art that: the technical scheme described in the foregoing embodiments can be modified or some technical features thereof can be replaced by equivalents; such modifications and substitutions do not depart from the spirit and scope of the technical solutions of the embodiments of the present application, and are intended to be included in the scope of the present application.