Data query method and device, electronic equipment and storage medium
By using AOP aspect-oriented programming to determine real-time requirements and dynamically matching the MongoDB master or slave database for data queries, the problem of data inconsistency in MongoDB under network anomalies is solved, improving data query consistency and response speed, and enhancing system performance.
Patent Information
- Authority / Receiving Office
- CN · China
- Patent Type
- Patents(China)
- Current Assignee / Owner
- SOUNDAI TECH CO LTD
- Filing Date
- 2022-11-18
- Publication Date
- 2026-06-02
AI Technical Summary
In the event of a network anomaly, data written to the master database in the existing MongoDB database cannot be synchronized to the slave database in a timely manner, resulting in inconsistencies between the data retrieved from the slave database and the data in the master database, which affects business query requests with high real-time requirements.
By using AOP aspect-oriented programming (AOP) technology to determine the real-time requirement weight of target business query requests, and dynamically matching the MongoDB master or slave database for data query operations, data consistency and flexibility are ensured.
It enables matching the master or slave database according to different real-time requirements, improving the consistency and response speed of data query results, alleviating database access pressure, and improving the performance of Internet systems.
Smart Images

Figure CN115827795B_ABST
Abstract
Description
Technical Field
[0001] This invention relates to the field of database technology, and in particular to a data query method, apparatus, electronic device, and storage medium. Background Technology
[0002] As we all know, internet systems are read-heavy and write-light applications. For example, in e-commerce systems, the number of times a product is viewed is usually more than the number of orders placed. Furthermore, the large number of read requests puts a heavy burden on the internet system's database. Based on this, the read-write separation architecture of databases has emerged, that is, all read requests are assigned to a dedicated database for reading, and all write requests are assigned to a dedicated database for writing.
[0003] In related technologies, MongoDB's master-slave and replica set (or copy set) architecture provides a good read-write separation environment. That is, when a client connects to a replica set (or copy set), MongoDB can identify the connection address and, based on the identified read and write connection addresses, determine whether to prioritize the master node or the slave node through five read preference modes. This achieves read-write separation by selecting either the master node or the slave node.
[0004] However, while existing read / write splitting methods for MongoDB can alleviate database access pressure and improve data response speed, the data in MongoDB slave databases is usually synchronized from the master database. Therefore, for business query requests with high real-time requirements, data written to the master database using existing MongoDB read / write splitting methods cannot be synchronized to the slave database in a timely manner under network anomalies, resulting in inconsistencies between the data queried through the slave database and the data in the master database. Summary of the Invention
[0005] This invention provides a data query method, apparatus, electronic device, and storage medium to address the shortcomings of existing MongoDB read / write separation methods, where data written to the master database cannot be synchronized to the slave database in a timely manner under network anomalies, resulting in inconsistencies between the data retrieved from the slave database and the data in the master database. It aims to match different target business query requests with varying real-time requirements to either the master or slave database of the MongoDB database. Furthermore, while alleviating database access pressure and improving data response speed, it not only ensures greater flexibility and accuracy in MongoDB read / write separation but also significantly improves the consistency of data query results, thereby greatly enhancing the system performance of the Internet.
[0006] This invention provides a data query method, comprising:
[0007] Obtain the target real-time requirement weight of the target business query request;
[0008] Determine the target query library that matches the weight of the target real-time requirement, wherein the target query library is the master or slave database of the MongoDB database;
[0009] Based on the target query database, perform a data query operation for the target business query request.
[0010] According to a data query method provided by the present invention, obtaining the target real-time requirement weight of the target business query request includes:
[0011] Using a pre-created AOP aspect, a logical judgment is made on whether the target controller interface of the target business query request carries preset annotation information, wherein the preset annotation information is the annotation information that specifies the query of the main database;
[0012] Based on the logical judgment result, the target real-time requirement weight of the target business query request is obtained.
[0013] According to a data query method provided by the present invention, obtaining the target real-time requirement weight of the target business query request based on the logical judgment result includes:
[0014] If the logical judgment result is that the target controller interface carries the preset annotation information, obtain the non-empty identifier attribute value of the target business query request, and determine that the non-empty identifier attribute value is the target real-time requirement weight of the target business query request.
[0015] Accordingly, determining the target query library that matches the target real-time requirement weight includes:
[0016] If the target real-time requirement weight is equal to the non-empty identifier attribute value, the target query database is determined to be the main database of the Mongo database.
[0017] According to a data query method provided by the present invention, the step of obtaining the target real-time demand weight of the target business query request based on the logical judgment result further includes:
[0018] If the logical judgment result is that the target controller interface does not carry the preset annotation information, obtain the empty identifier attribute value of the target business query request, and determine that the empty identifier attribute value is the target real-time requirement weight of the target business query request.
[0019] Accordingly, determining the target query library that matches the target real-time requirement weight includes:
[0020] If the target real-time requirement weight is the empty identifier attribute value, the target query database is determined to be a slave database of the MongoDB database.
[0021] According to a data query method provided by the present invention, the step of performing a data query operation based on the target query database for the target business query request includes:
[0022] For the target business query request corresponding to the target real-time requirement weight being the non-empty identifier attribute value, the slave database attribute connecting to the Mongo database is changed from the default secondaryPreferred to primaryPreferred. Then, based on the master database of the Mongo database, a data query operation is performed for the target business query request corresponding to the target real-time requirement weight being the non-empty identifier attribute value.
[0023] According to a data query method provided by the present invention, the step of performing a data query operation based on the target query database for the target business query request includes:
[0024] For the target business query request corresponding to the target real-time requirement weight being the empty identifier attribute value, based on the secondaryPreferred attribute of the Mongo database and the Mongo database slave database, a data query operation is performed for the target business query request corresponding to the target real-time requirement weight being the empty identifier attribute value.
[0025] According to a data query method provided by the present invention, the step of performing a data query operation based on the target query database for the target business query request further includes:
[0026] If the target query database is the primary database of the Mongo database, determine whether the primary database is currently experiencing an anomaly.
[0027] If the master database is currently not experiencing any abnormalities, then based on the master database, perform a data query operation for the target business query request;
[0028] If the master database is currently experiencing an anomaly, a data query operation will be performed on the slave database of the Mongo database to target the business query request.
[0029] According to a data query method provided by the present invention, the method further includes:
[0030] Predefined representations specify the preset annotation information for querying the main database;
[0031] For different controller interfaces that query the Mongo database, multiple controller interfaces that need to query the main database are pre-defined, and the preset annotation information is added to each controller interface that needs to query the main database.
[0032] According to a data query method provided by the present invention, the method further includes:
[0033] AOP aspects are pre-created for different controller interfaces that query the Mongo database. The AOP aspects are used to set an identifier attribute for each target query business request and to determine the non-empty identifier attribute value or empty identifier attribute value for each target query business request.
[0034] The present invention also provides a data query device, comprising:
[0035] The acquisition module is used to acquire the target real-time requirement weight of the target business query request;
[0036] The determination module is used to determine the target query library that matches the target real-time requirement weight, wherein the target query library is the master or slave database of the Mongo database;
[0037] The query module is used to perform data query operations for the target business query request based on the target query database.
[0038] The present invention also provides an electronic device, including a memory, a processor, and a computer program stored in the memory and executable on the processor, wherein the processor executes the program to implement the data query method as described above.
[0039] The present invention also provides a non-transitory computer-readable storage medium having a computer program stored thereon, which, when executed by a processor, implements the data query method as described above.
[0040] The present invention also provides a computer program product, including a computer program that, when executed by a processor, implements the data query method as described above.
[0041] The present invention provides a data query method, apparatus, electronic device, and storage medium. In the data query method, the server, for a target business query request, first obtains the target real-time requirement weight of the target business query request, and then determines the target query database that matches the target real-time requirement weight. Since the target query database is either the master or slave database of a MongoDB database, it can determine whether to execute the data query operation for the target business query request through the master or slave database based on the different real-time requirements of the target business query request. This achieves the goal of matching different target business query requests with different real-time requirements to the master or slave database of the MongoDB database. Furthermore, while alleviating database access pressure and improving data response speed, it not only ensures that the read / write separation of the MongoDB database is more flexible and accurate, but also significantly improves the consistency of data query results, thereby greatly improving the system performance of the Internet. Attached Figure Description
[0042] To more clearly illustrate the technical solutions in this invention or the prior art, the drawings used in the description of the embodiments or the prior art will be briefly introduced below. Obviously, the drawings described below are some embodiments of this invention. For those skilled in the art, other drawings can be obtained from these drawings without creative effort.
[0043] Figure 1 This is a flowchart illustrating the data query method provided by the present invention;
[0044] Figure 2 This is a schematic diagram illustrating an application scenario of the data query method provided by the present invention;
[0045] Figure 3 This is a schematic diagram of the data query device provided by the present invention;
[0046] Figure 4 This is a schematic diagram of the structure of the electronic device provided by the present invention. Detailed Implementation
[0047] To make the objectives, technical solutions, and advantages of this invention clearer, the technical solutions of this invention will be clearly and completely described below with reference to the accompanying drawings. Obviously, the described embodiments are only some, not all, of the embodiments of this invention. All other embodiments obtained by those skilled in the art based on the embodiments of this invention without creative effort are within the scope of protection of this invention.
[0048] The following is combined Figures 1-4This invention describes a data query method, apparatus, electronic device, and storage medium, wherein the data query method is executed by a server, which may be a MongoDB database server or a proxy server. The following method embodiments are described using a server as an example.
[0049] Reference Figure 1 This is a flowchart illustrating the data query method provided by the present invention, as shown below. Figure 1 As shown, this data query method includes the following steps:
[0050] Step 110: Obtain the target real-time requirement weight of the target business query request.
[0051] The target business query request can be a query request with high real-time requirements, such as a payment result query request for a product order or a refresh result query request after a webpage list has been modified and refreshed; the target business query request can also be a query request with low or no real-time requirements. Furthermore, the number of target business query requests can be one or multiple. No specific limitation is made here.
[0052] Specifically, the target controller interface for the server to obtain the target business query request can be a replica set (or copy set) of the MongoDB database. When a client connects to this replica set, it can submit the target business query request to the server. Based on this request, the server can further analyze its real-time requirements to determine the target real-time requirement weight. For example, regarding the completion status of a write operation for browsing, ordering, and paying for a product, the client can submit a target business query request to the server as "Return order payment result." In this case, the server's frontend calls this "Return order payment result" target business query request from the backend and can parse it to determine that the "Return order payment result" target business query request has a high real-time requirement.
[0053] Step 120: Determine the target query database that matches the weight of the target real-time requirement. The target query database is either the master database or the slave database of the MongoDB database.
[0054] Specifically, the server can determine the appropriate target query database based on the target real-time requirement weight of the target business query request. For example, for target business query requests with high real-time requirements, the target query database can be determined to be the master database of the MongoDB database; for target business query requests with low or no real-time requirements, the target query database can be determined to be the slave database of the MongoDB database.
[0055] It should be noted that the read / write separation system architecture of MongoDB can be as follows: Figure 2 As shown, in Figure 2 In MongoDB, when there are many business requests from different system applications, multiple read databases can be deployed to distribute read requests evenly across them, reducing the pressure on each read database. For example, in a scenario with one write database and multiple read databases in MongoDB, when a seller publishes product A online, the product data for product A is written to the write database in MongoDB, and the data written to the write database is simultaneously synchronized to each read database. The product data for product A that a buyer views online comes from the write database in MongoDB. If there are many read requests, and all of them default to reading data from the read database, even if there are multiple read databases, inconsistencies can arise due to the different real-time requirements of the read requests. Therefore, by determining the appropriate target query database based on the target business query requests with different real-time requirements, the drawbacks of inconsistent data query results can be effectively avoided.
[0056] Step 130: Based on the target query database, perform a data query operation for the target business query request.
[0057] Specifically, when the target query database is the master database of a MongoDB database, the data required by the target business query request can be retrieved from the master database; when the target query database is a slave database of a MongoDB database, the data required by the target business query request can be retrieved from the slave database. This completes the data query operation for the target business query request.
[0058] The data query method provided by this invention involves the server first obtaining the target real-time requirement weight of the target business query request, and then determining the target query database that matches the target real-time requirement weight. Since the target query database is either the master or slave database of a MongoDB database, it can determine whether to execute the data query operation for the target business query request through the master or slave database based on the different real-time requirements of the target business query request. This achieves the goal of matching different target business query requests with different real-time requirements to the master or slave database of a MongoDB database. Furthermore, while alleviating database access pressure and improving data response speed, it not only ensures that the read / write separation of the MongoDB database is more flexible and accurate, but also significantly improves the consistency of data query results, thereby greatly improving the system performance of the Internet.
[0059] Optionally, the specific implementation process of step 110 may include:
[0060] First, using a pre-created AOP aspect, a logical judgment is made on whether the target controller interface of the target business query request carries preset annotation information, which is the annotation information of the specified query master database; then, based on the result of the logical judgment, the target real-time requirement weight of the target business query request is obtained.
[0061] Specifically, for each target business query request obtained by the server, which is a request URL, the server cannot directly identify whether it contains preset annotation information from the target controller interface used by each request URL. Instead, it needs to determine whether it contains preset annotation information at the code level, that is, from the controller interface of each request URL code. Based on this, when the server uses a pre-created AOP aspect to logically determine whether the target controller interface of the target business query request carries preset annotation information, if the logical judgment result is that the target controller interface of the target business query request carries preset annotation information, it is determined that the real-time requirement of the target business query request is high; conversely, if the logical judgment result is that the target controller interface of the target business query request does not carry preset annotation information, it is determined that the real-time requirement of the target business query request is low. Then, based on the high or low real-time requirement of the target business query request, the target real-time requirement weight of the target business query request is obtained.
[0062] It should be noted that, in Aspect-Oriented Programming (AOP), system-level service code is called AOP aspect. AOP aspect can be used to make logical judgments on whether the target controller interface of the target business query request carries preset annotation information.
[0063] The data query method provided by this invention uses AOP aspects to logically determine whether to carry preset annotation information of the specified query master database in the target controller interface, thereby determining the target real-time requirement weight of the target business query request. By combining AOP aspect technology and custom annotation technology, it is possible to achieve the purpose of specifying the query master database of a specific controller interface. This not only has high code maintainability, strong reusability, and low code invasiveness, but also improves development efficiency and reduces labor costs.
[0064] Optionally, based on the logical judgment result, obtain the target real-time requirement weight of the target business query request, including:
[0065] If the logical judgment result is that the target controller interface carries preset annotation information, obtain the non-empty identifier attribute value of the target business query request, and determine the non-empty identifier attribute value as the target real-time requirement weight of the target business query request.
[0066] Accordingly, the specific implementation process of step 120 may include:
[0067] If the target real-time requirement weight is a non-empty identifier attribute value, the target query database is determined to be the main database of the MongoDB database.
[0068] Specifically, if the server uses an AOP aspect to perform logical judgment on the target controller interface, and the result is that the target controller interface carries preset annotation information, then the AOP aspect assigns a value to the identifier attribute of the target query request corresponding to the target controller interface carrying the preset annotation information. This determines the non-empty identifier attribute value of the target query request. For example, if the identifier attribute of the target query request is `flag`, assigning a value to `flag` will result in a non-empty identifier attribute value of `flag = 1`. Based on this, if the target real-time requirement weight of the target query request is `flag = 1`, then the target query database can be determined to be the main database of the MongoDB database.
[0069] The data query method provided by this invention determines that when the server uses AOP aspects to determine that the target real-time requirement weight of the target business query request is a non-empty identifier attribute value, the target query database is determined to be the primary database of the Mongo database. This achieves the purpose of allocating the primary database of the Mongo database for data querying of target business query requests with high real-time requirements, thus meeting the requirements of strong consistency business scenarios.
[0070] Optionally, based on the logical judgment result, obtaining the target real-time requirement weight of the target business query request also includes:
[0071] If the logical judgment result is that the target controller interface does not carry preset annotation information, obtain the empty identifier attribute value of the target business query request, and determine the empty identifier attribute value as the target real-time requirement weight of the target business query request.
[0072] Correspondingly, the specific implementation process of step 120 may also include:
[0073] If the target real-time requirement weight is an empty identifier attribute value, the target query database is determined to be a slave database of the MongoDB database.
[0074] Specifically, if the server uses an AOP aspect to perform a logical judgment on the target controller interface and the result is that the target controller interface does not carry preset annotation information, then the AOP aspect will not assign a value to the identifier attribute of the target query request corresponding to the target controller interface that does not carry preset annotation information. This determines that the target query request will have an empty identifier attribute value. For example, if the identifier attribute of the target query request is `flag`, then leaving `flag` unassigned will result in an empty identifier attribute value of `flag = 0`. Based on this, if the target real-time requirement weight of the target query request is `flag = 0`, then it can be determined that the target query database is a MongoDB slave database.
[0075] The data query method provided by this invention determines that when the target real-time requirement weight of the target business query request is an empty identifier attribute value by using AOP aspects, the target query database is determined to be a slave database of the MongoDB. This achieves the purpose of allocating a slave database of the MongoDB for data querying of target business query requests with low or no real-time requirements, which greatly improves the efficiency of master-slave separation and also significantly reduces the access pressure on the MongoDB.
[0076] Optionally, the specific implementation process of step 130 may include:
[0077] For target business query requests where the target real-time requirement weight is a non-empty identifier attribute value, the slave database attribute connecting to the Mongo database is changed from the default secondaryPreferred to primaryPreferred. Then, based on the primary database of the Mongo database, the data query operation for the target business query request where the target real-time requirement weight is a non-empty identifier attribute value is executed.
[0078] Specifically, since the default behavior for business queries on the MongoDB database is to query data from the MongoDB slave database, for the target business query request corresponding to a non-nullable identifier value for the target real-time requirement, it is necessary to change the slave database attribute of the MongoDB database connection from the default secondaryPreferred to primaryPreferred at the code level. In other words, the slave database attribute of the MongoDB database connection must be changed to the master database attribute of the connection. Only after the change operation is completed can the data query operation corresponding to the target business query request with a non-nullable identifier value for the target real-time requirement be executed through the master database of the MongoDB database.
[0079] The data query method provided by this invention improves the accuracy and reliability of data query operations by first changing the slave database attribute of the Mongo database connection to the master database attribute of the Mongo database for target business query requests with high real-time requirements, and then performing data query operations through the master database of the Mongo database.
[0080] Optionally, the specific implementation process of step 130 may also include:
[0081] For target business query requests where the target real-time requirement weight is empty, based on the secondaryPreferred attribute of the Mongo database and the Mongo database slave database, perform a data query operation for the target business query requests where the target real-time requirement weight is empty.
[0082] Specifically, for target business query requests with an empty identifier attribute value for the target real-time requirement weight, there is no need to modify the slave database attribute connecting to the Mongo database. The data query operation for the target business query request with an empty identifier attribute value for the target real-time requirement weight can be executed directly through the Mongo database slave database based on the default connection attribute secondaryPreferred.
[0083] It should be noted that MongoDB supports five read preference modes, including: (1) primary: The primary node is the default mode. Read operations are only performed on the primary node. If the primary node is unavailable, an error or exception will be thrown. The primary node is the node of the primary database. (2) primaryPreferred: The primary node is preferred. In most cases, read operations are performed on the primary node. If the primary node is unavailable, such as in failover, read operations are performed on the secondary node. (3) secondary: The secondary node is preferred. Read operations are only performed on the secondary node. If the secondary node is unavailable, an error or exception will be thrown. The secondary node is the node of the secondary database. (4) secondaryPreferred: The secondary node is preferred. In most cases, read operations are performed on the secondary node. In special cases (such as a single primary node architecture), read operations are performed on the primary node. (5) nearest: The nearest node is preferred. Read operations are performed on the nearest member, which may be the primary node or the secondary node. Furthermore, in the Spring framework, the `MongoTemplateReader` and `MongoTemplateWriter` classes correspond to the read connection address and write connection address, respectively. The `MongoTemplateReader` class is used for all read operations, and the `MongoTemplateWriter` class is used for all CRUD operations. This achieves read / write separation for the MongoDB database.
[0084] The data query method provided by this invention allows the server to directly execute data query operations through the slave database of the MongoDB for target business query requests that have low or no real-time requirements. This improves the flexibility and reliability of data query operations, ensures higher efficiency of master-slave separation of the MongoDB, and expands its applicability.
[0085] Optionally, the specific implementation process of step 130 may also include:
[0086] If the target query database is the master database of the MongoDB database, determine whether the master database is currently experiencing an error. If the master database is not experiencing an error, execute the data query operation for the target business query request based on the master database. If the master database is experiencing an error, execute the data query operation for the target business query request based on the slave database of the MongoDB database.
[0087] Specifically, when the server determines that the target query database is the MongoDB master database, it first checks if the master database is currently experiencing an anomaly. If the master database is experiencing errors or failures, it is considered to be out of service, and the query operation for the target business query request will be executed via the MongoDB slave database. Conversely, if the master database is not experiencing errors or failures, it is considered to be functioning normally, and the query operation for the target business query request will be executed via the MongoDB master database. In other words, if the MongoDB master database is unavailable due to errors or failures, regardless of whether the target query request has high real-time requirements, the query will be performed via the MongoDB slave database.
[0088] The data query method provided by this invention determines whether to perform the data query operation through the master database or the slave database when the server determines that the target query database is the master database. This is done by judging whether the master database is currently experiencing an anomaly. This ensures that the master-slave separation architecture of MongoDB is more flexible and reliable, and can also significantly improve the system performance of the Internet.
[0089] Optionally, the data query method provided by the present invention may further include:
[0090] First, predefine the default annotation information to represent the master database for querying the MongoDB database; then, for different controller interfaces that need to query the master database, pre-set multiple controller interfaces that need to query the master database, and add default annotation information to each controller interface that needs to query the master database.
[0091] Specifically, for data query scenarios with high real-time requirements, such as order payment result queries or webpage list refresh result queries, predefined annotation information can be used to represent the specified master database. Furthermore, for all controller interfaces querying the MongoDB database, multiple controller interfaces that need to query the master database can be pre-defined, and predefined annotation information can be added to each of these controller interfaces. For example, regarding the completion status of a product's browsing-ordering-payment write operation, the client can submit a target business query request to the server as "return order payment result." When the server's frontend calls this business query request from the backend, and the controller interface for this business query request is controller interface A, predefined annotation information can be added to controller interface A, resulting in a controller interface A' with the added annotation information. In other words, the frontend calls controller interface A', and all query operations in controller interface A' are queries to the master database, meaning controller interface A' connects to a write connection rather than a read connection. This achieves the goal of dividing all controller interfaces for querying the MongoDB database into controller interfaces that force queries to the master database and controller interfaces that default to querying the slave database.
[0092] It should be noted that, in existing solutions, query operations default to querying the slave database. This invention aims to force target data query requests with high real-time requirements to query the master database, avoiding inconsistencies between master and slave databases due to time delays. Target data query requests with low or no real-time requirements can continue to query the slave database. Therefore, by adding preset annotation information to the target controller interface of target business query requests with high real-time requirements, the goal of querying data with high real-time requirements through the master database instead of the slave database is achieved.
[0093] The data query method provided by this invention enables the server to force the query of the master database by pre-customizing the annotation information of the master database and adding the pre-defined annotation information to each controller interface that needs to query the master database. This achieves the purpose of forcing the query of the master database for subsequent business query requests with high real-time requirements, thus meeting the requirements of strong consistency business scenarios.
[0094] Optionally, the data query method provided by the present invention may further include:
[0095] AOP aspects are pre-created for different controller interfaces that query the Mongo database. AOP aspects are used to set identity attributes for each target query business request and determine the non-empty identity attribute value or empty identity attribute value for each target query business request.
[0096] Specifically, the server can create AOP aspects for all controller interfaces that query the Mongo database. This allows the AOP aspects to set an identifier attribute (flag) for each target query request. If the target controller interface used by the target query request carries preset annotation information, then the corresponding target query request will have a non-empty identifier attribute value, i.e., flag = 1. Conversely, if the target controller interface used by the target query request does not carry preset annotation information, then the corresponding target query request will have an empty identifier attribute value, i.e., flag = 0.
[0097] It should be noted that the created AOP aspect can be configured to perform business logic checks on all business query requests corresponding to all controller interfaces, or it can be configured to perform business logic checks on only some controller interfaces. The business logic check could be to determine whether the business query request processed by the AOP aspect carries preset annotation information. The first configuration method is preferred in this invention, but the latter is also feasible.
[0098] The data query method provided by this invention enables the server to accurately and flexibly determine whether each target query request is queried through the master database or the slave database by pre-creating and setting AOP aspects for different controller interfaces that query the Mongo database, configuring AOP aspects to set identifier attributes for target query business requests, and determining non-empty identifier attribute values or empty identifier attribute values. This also significantly improves the consistency of data query results and the efficiency of data query.
[0099] The data query device provided by the present invention is described below. The data query device described below and the data query method described above can be referred to in correspondence.
[0100] Reference Figure 3 This is a schematic diagram of the structure of the data query device provided by the present invention, as shown below. Figure 3 As shown, the data query device 300 includes:
[0101] Module 310 is used to obtain the target real-time requirement weight of the target business query request;
[0102] Module 320 is used to determine the target query library that matches the weight of the target real-time requirement. The target query library is either the master or slave database of the Mongo database.
[0103] The query module 330 is used to perform data query operations based on the target query database for the target business query request.
[0104] Optionally, module 310 can be used to use a pre-created AOP aspect to perform a logical judgment on whether the target controller interface of the target business query request carries preset annotation information, where the preset annotation information is the annotation information of the specified query master database; based on the logical judgment result, the target real-time requirement weight of the target business query request is obtained.
[0105] Optionally, the acquisition module 310 can also be used to obtain the non-empty identifier attribute value of the target business query request when the logical judgment result is that the target controller interface carries preset annotation information, and determine the non-empty identifier attribute value as the target real-time requirement weight of the target business query request.
[0106] Correspondingly, module 320 can be used to determine the target query database as the main database of the Mongo database when the target real-time requirement weight is a non-empty identifier attribute value.
[0107] Optionally, the acquisition module 310 can also be used to acquire the empty identifier attribute value of the target business query request when the logical judgment result is that the target controller interface does not carry preset annotation information, and determine the empty identifier attribute value as the target real-time requirement weight of the target business query request.
[0108] Correspondingly, module 320 can also be used to determine the target query database as a slave database of the MongoDB when the target real-time requirement weight is an empty identifier attribute value.
[0109] Optionally, the query module 330 can be used to change the slave database attribute of the Mongo database connection from the default secondaryPreferred to primaryPreferred for the target business query request when the target real-time requirement weight is a non-empty identifier attribute value. Then, based on the primary database of the Mongo database, it can perform a data query operation for the target business query request when the target real-time requirement weight is a non-empty identifier attribute value.
[0110] Optionally, the query module 330 can also be used to perform data query operations on the target business query request corresponding to the target real-time requirement weight of the null identifier attribute value, based on the secondaryPreferred attribute of the Mongo database and the Mongo database slave database.
[0111] Optionally, the query module 330 can also be used to determine whether the master database is currently experiencing an error when the target query database is the master database of the Mongo database; if the master database is not currently experiencing an error, then the data query operation for the target business query request is executed based on the master database; if the master database is currently experiencing an error, then the data query operation for the target business query request is executed based on the slave database of the Mongo database.
[0112] Optionally, the apparatus of the present invention may further include a creation module for pre-defining preset annotation information that characterizes the master database for a specified query of the Mongo database; for different controller interfaces that query the Mongo database, multiple controller interfaces that need to query the master database are pre-set, and preset annotation information is added to each controller interface that needs to query the master database.
[0113] Optionally, the creation module can also be used to pre-create AOP aspects for different controller interfaces that query the Mongo database. The AOP aspects are used to set the identifier attribute for each target query business request and determine the non-empty identifier attribute value or empty identifier attribute value for each target query business request.
[0114] Figure 4 An example is a schematic diagram of the physical structure of an electronic device, such as... Figure 4 As shown, the electronic device 400 may include: a processor 410, a communication interface 420, a memory 430, and a communication bus 440, wherein the processor 410, the communication interface 420, and the memory 430 communicate with each other through the communication bus 440. The processor 410 can call logical instructions in the memory 430 to execute a data query method, which includes:
[0115] Obtain the target real-time requirement weight of the target business query request;
[0116] Determine the target query database that matches the weight of the target real-time requirement. The target query database can be the master or slave database of the MongoDB database.
[0117] Based on the target query database, perform data query operations for the target business query request.
[0118] Furthermore, the logical instructions in the aforementioned memory 430 can be implemented as software functional units and, when sold or used as independent products, can be stored in a computer-readable storage medium. Based on this understanding, the technical solution of the present invention, in essence, or the part that contributes to the prior art, or a part of the technical solution, can be embodied in the form of a software product. This computer software product is stored in a storage medium and includes several instructions to cause a computer device (which may be a personal computer, server, or network device, etc.) to execute all or part of the steps of the methods described in the various embodiments of the present invention. The aforementioned storage medium includes various media capable of storing program code, such as USB flash drives, portable hard drives, read-only memory (ROM), random access memory (RAM), magnetic disks, or optical disks.
[0119] On the other hand, the present invention also provides a computer program product, the computer program product comprising a computer program that can be stored on a non-transitory computer-readable storage medium, wherein when the computer program is executed by a processor, the computer is able to execute the data query method provided by the above methods, the method comprising:
[0120] Obtain the target real-time requirement weight of the target business query request;
[0121] Determine the target query database that matches the weight of the target real-time requirement. The target query database can be the master or slave database of the MongoDB database.
[0122] Based on the target query database, perform data query operations for the target business query request.
[0123] In another aspect, the present invention also provides a non-transitory computer-readable storage medium having a computer program stored thereon, which, when executed by a processor, implements the data query method provided by the methods described above, the method comprising:
[0124] Obtain the target real-time requirement weight of the target business query request;
[0125] Determine the target query database that matches the weight of the target real-time requirement. The target query database can be the master or slave database of the MongoDB database.
[0126] Based on the target query database, perform data query operations for the target business query request.
[0127] The device embodiments described above are merely illustrative. The units described as separate components may or may not be physically separate. The components shown as units may or may not be physical units; that is, they may be located in one place or distributed across multiple network units. Some or all of the modules can be selected to achieve the purpose of this embodiment according to actual needs. Those skilled in the art can understand and implement this without any creative effort.
[0128] Through the above description of the embodiments, those skilled in the art can clearly understand that each embodiment can be implemented by means of software plus necessary general-purpose hardware platforms, and of course, it can also be implemented by hardware. Based on this understanding, the above technical solutions, in essence or the part that contributes to the prior art, can be embodied in the form of a software product. This computer software product can be stored in a computer-readable storage medium, such as ROM / RAM, magnetic disk, optical disk, etc., and includes several instructions to cause a computer device (which may be a personal computer, server, or network device, etc.) to execute the methods described in the various embodiments or some parts of the embodiments.
[0129] Finally, it should be noted that the above embodiments are only used to illustrate the technical solutions of the present invention, and not to limit them; although the present invention has been described in detail with reference to the foregoing embodiments, those skilled in the art should understand that modifications can still be made to the technical solutions described in the foregoing embodiments, or equivalent substitutions can be made to some of the technical features; and these modifications or substitutions do not cause the essence of the corresponding technical solutions to deviate from the spirit and scope of the technical solutions of the embodiments of the present invention.
Claims
1. A data query method, characterized by, include: Using a pre-created AOP aspect, a logical judgment is made on whether the target controller interface of the target business query request carries preset annotation information, wherein the preset annotation information is the annotation information of the specified query master database; If the logical judgment result is that the target controller interface carries the preset annotation information, obtain the non-empty identifier attribute value of the target business query request, and determine that the non-empty identifier attribute value is the target real-time requirement weight of the target business query request. The target real-time requirement weight is used to characterize the real-time requirements of the target business query request; If the logical judgment result is that the target controller interface does not carry the preset annotation information, obtain the empty identifier attribute value of the target business query request, and determine that the empty identifier attribute value is the target real-time requirement weight of the target business query request. Determine the target query library that matches the weight of the target real-time requirement, wherein the target query library is the master or slave database of the MongoDB database; For the target business query request corresponding to the target real-time requirement weight being the non-empty identifier attribute value, the slave database attribute of the Mongo database connection is changed from the default secondaryPreferred to primaryPreferred. Then, based on the primary database of the Mongo database, a data query operation is performed for the target business query request corresponding to the target real-time requirement weight being the non-empty identifier attribute value. For the target business query request corresponding to the target real-time requirement weight being the empty identifier attribute value, based on the secondaryPreferred attribute of the Mongo database and the Mongo database slave database, a data query operation is performed for the target business query request corresponding to the target real-time requirement weight being the empty identifier attribute value.
2. The data query method of claim 1, wherein, The determination of the target query library that matches the target real-time requirement weight includes: If the target real-time requirement weight is equal to the non-empty identifier attribute value, the target query database is determined to be the main database of the Mongo database.
3. The data query method of claim 1, wherein, The determination of the target query library that matches the target real-time requirement weight includes: If the target real-time requirement weight is the empty identifier attribute value, the target query database is determined to be a slave database of the MongoDB database.
4. The data query method of any one of claims 1 to 3, wherein, Based on the target query database, perform a data query operation for the target business query request, including: If the target query database is the primary database of the Mongo database, determine whether the primary database is currently experiencing an anomaly. If the master database is currently not experiencing any abnormalities, then based on the master database, perform a data query operation for the target business query request; If the master database is currently experiencing an anomaly, a data query operation will be performed on the slave database of the Mongo database to target the business query request.
5. The data query method according to any one of claims 1 to 3, characterized in that, The method further includes: Predefined representations specify the preset annotation information for querying the main database; For different controller interfaces that query the Mongo database, multiple controller interfaces that need to query the main database are pre-defined, and the preset annotation information is added to each controller interface that needs to query the main database.
6. The data query method of any one of claims 1 to 3, wherein, The method further includes: AOP aspects are pre-created for different controller interfaces that query the Mongo database. The AOP aspects are used to set an identifier attribute for each target business query request and to determine the non-empty identifier attribute value or empty identifier attribute value for each target business query request.
7. A data query apparatus, characterized by comprising: include: The acquisition module is used to perform a logical judgment on whether the target controller interface of the target business query request carries preset annotation information using a pre-created AOP aspect. The preset annotation information is the annotation information of the specified query master database. If the logical judgment result is that the target controller interface carries the preset annotation information, the module acquires the non-empty identifier attribute value of the target business query request and determines that the non-empty identifier attribute value is the target real-time requirement weight of the target business query request. The target real-time requirement weight is used to characterize the real-time requirement of the target business query request; when the logical judgment result is that the target controller interface does not carry the preset annotation information, the empty identifier attribute value of the target business query request is obtained, and the empty identifier attribute value is determined to be the target real-time requirement weight of the target business query request. The determination module is used to determine the target query library that matches the target real-time requirement weight, wherein the target query library is the master or slave database of the Mongo database; The query module is used to, for target business query requests where the target real-time requirement weight is the non-empty identifier attribute value, change the slave database attribute of the Mongo database connection from the default secondaryPreferred to primaryPreferred, and then, based on the primary database of the Mongo database, perform a data query operation for the target business query requests where the target real-time requirement weight is the non-empty identifier attribute value; and for target business query requests where the target real-time requirement weight is the empty identifier attribute value, based on the slave database attribute secondaryPreferred of the Mongo database connection and the slave database of the Mongo database, perform a data query operation for the target business query requests where the target real-time requirement weight is the empty identifier attribute value.
8. An electronic device comprising a memory, a processor, and a computer program stored on the memory and executable on the processor, characterized in that, When the processor executes the program, it implements the data query method as described in any one of claims 1 to 6.
9. A non-transitory computer-readable storage medium having a computer program stored thereon, characterized in that, When the computer program is executed by a processor, it implements the data query method as described in any one of claims 1 to 6.
10. A computer program product, comprising a computer program, characterized in that, When the computer program is executed by a processor, it implements the data query method as described in any one of claims 1 to 6.