The application is a divisional application of an invention patent application with the application number 201410569325.1, namely a business message sending method and device, which is filed on the 10 th and 22 th of 2014.
Detailed Description
For the purposes of making the objects, technical solutions and advantages of the embodiments of the present application more clear, the technical solutions of the embodiments of the present application will be clearly and completely described below with reference to the drawings in the embodiments of the present application, and it is apparent that the described embodiments are some embodiments of the present application, but not all embodiments. All other embodiments, which can be made by one of ordinary skill in the art without undue burden from the present disclosure, are within the scope of the present disclosure.
For the purpose of facilitating an understanding of the embodiments of the present application, reference will now be made to the following description of specific embodiments, taken in conjunction with the accompanying drawings, in which the embodiments are not intended to limit the embodiments of the present application.
Fig. 1 is a schematic diagram of a service message sending system architecture provided in the embodiment of the present application, where the system includes a database server 101, a service system 102, a service message sending device 103, and a configuration center 104, where the database server 101 provides services for applications of the service system 102, the services may specifically be insertion, update, query, or delete services, the services may be implemented by performing corresponding operations through SQL statements, the service system 102 may be one or multiple, the number of service systems 102 in fig. 1 is merely an example, and not as a limitation, and the service message sending device 103 may send corresponding service messages to the service system 102 according to the name of a class of a service message configured by the configuration center 104 for the service system 102. In this embodiment of the present application, the operation information of the database may be recorded in a database log, specifically may be a Binlog database log, and the Binlog database log is periodically parsed by a Binlog parser to generate a database change event, where the database change event includes the operation information, and then a service message may be constructed according to the operation information. The Binlog database log and the Binlog parser described above may be integrated in the service message transmitting apparatus 103.
Fig. 2 is a flowchart of a service message sending method provided in an embodiment of the present application, where an execution body of the method may be a dedicated device for executing the method, or may be an existing network device, for example, a database server, and as shown in fig. 2, the method specifically includes:
in step 201, operation information of a database is recorded in a database log, wherein the database comprises a first table.
The recording the operation information of the database in a database log, wherein the database comprises a first table, and the method specifically comprises the following steps: when a first operation is performed on a first field in a first table of a database, obtaining operation information of the database, wherein the operation information comprises a table name of the first table, an operation name of the first operation, a field name of the first field and a field value of the first field; and recording the operation information in a Binlog database log.
Step 202, analyzing the database log, and generating a database change event, wherein the database change event comprises the operation information, and the operation information comprises a table name of the first table.
The analyzing the database log to generate a database change event may specifically include: when the preset moment is reached, the database log is analyzed, and a database change event is generated.
And 203, monitoring the database change event when the table name of the first table is the same as the table name of the second table configured for the service system.
Wherein, when the name of the first table is the same as the name of the second table configured for the service system, monitoring the database change event may specifically include: and monitoring the database change event when the name of the first table is completely or partially the same as the name of a second table configured for the service system.
And 204, extracting the operation information from the database change event and constructing a service message.
The extracting the operation information from the database change event and constructing a service message may specifically include: when the operation name of the first operation is the same as the operation name of a second operation configured for a service system, extracting the operation information from the database change event; filtering out a field value of the first field from the operation information; resolving the name of the class according to the table name of the first table; filling the attribute of the class by using the filtered field value of the first field, thereby constructing a service model; and assembling the operation name of the first operation and the service model into a service message.
In this embodiment of the present application, the first operation or the second operation may specifically include: an insert operation, an update operation, a query operation, or a delete operation.
Step 205, the service message is sent to the service system, so that the service system counts service change conditions according to the service message.
Wherein, the sending the service message to the service system may specifically include: and sending the service message to the service system according to the name of the class configured for the service system.
In the service message sending method, operation information of a database is recorded in a database log, a database change event is generated by analyzing the database log, when the table name of a first table included in the database log is the same as the table name of a second table configured for a service system, the database change event is monitored, the operation information is extracted from the database change event, a service message is constructed, and the service message is sent to the service system to be used for the service system to count service change conditions according to the service message. As can be seen from the above, in the embodiment of the present application, by recording the operation information of the database, the database change is monitored, so that the occurrence of the database change can be more accurately confirmed, and since the database generally includes a plurality of tables, the database change event is monitored according to the table names, so that the method and the device can be more targeted, and accordingly, the accuracy of sending the service message can be improved.
The service message sending method is described in more detail below through the second embodiment of the present application.
Fig. 3 is a flowchart of a service message sending method provided in the second embodiment of the present application, where an execution body of the method may be a dedicated device for executing the method, or may be an existing network device, for example, a database server, and as shown in fig. 3, the method specifically includes:
step 301, when a first operation is performed on a first field in a first table of a database, operation information on the database is obtained.
The operation information includes a table name of the first table, an operation name of the first operation, a field name of the first field, and a field value of the first field.
Step 302, the operation information is recorded in a Binlog database log.
For example, when a user performs an insert operation on a database, the user needs to construct data through a service layer, then insert the data into the database, and the Binlog database log records relevant operation information of the insert operation. The specific record form can adopt the following statement:
Insert into goods(goods_type,price)values(“clothes”,50.0);
wherein Insert is the operation name of the first operation; goods is the form name of the first form; the goods_type and the price are field names of the first field; the first field is a field value of the first field.
And step 303, when the preset time is reached, analyzing the database log to generate a database change event.
The database change event comprises the operation information, and the operation information comprises a table name of the first table.
In the embodiment of the application, the database log may not be analyzed in real time, and when the preset time is reached, the database log is analyzed, so that the system processing resources are saved.
The Binlog database log can be analyzed through a Binlog analyzer so as to analyze the operation information recorded in the Binlog database log, a database change event comprising the operation information is generated, and the table name, the field name and the field value of a table changed in the database can be obtained by monitoring the database change event.
And step 304, monitoring the database change event when the table name of the first table is completely or partially the same as the table name of the second table configured for the service system.
Since a database typically includes multiple tables, database change events may be snooped by table names. In addition, the capacity of each table is limited, so that it often happens that the table is divided into a database, and monitoring the database change event according to the table name comprises: and monitoring the table corresponding to the table name and the database change event corresponding to the sub-table of the table. For example, when a table with a table name of goods has a sub-table, monitoring can be performed by using a regular expression, and a specific form can be "goods". X ", so that database change events corresponding to all the goods tables and their sub-tables can be monitored.
And step 305, when the operation name of the first operation is the same as the operation name of the second operation configured for the service system, extracting the operation information from the database change event.
Since the service system is usually not interested in the database change caused by all operations (actions) performed on the database by the user, but is only interested in the database change caused by one or more operations, the operation name of the second operation can be configured for the service system, specifically, when the service system subscribes to the service message, the user can select the operation name of the second operation, namely, which operations are interested by the user, the operation information is extracted from the database change event according to the operation name, so that the extracted operation information is more accurate, and accordingly, the service message sent to the service system is more accurate.
Step 306, filtering the field value of the first field from the operation information.
After filtering out the field values of the first field, the field values may be stored in a Map type, e.g., in the form shown in table one.
List one
Commodity type
|
Book with book cover
|
Price (Unit: yuan)
|
40 |
Step 307, resolving the name of the class according to the table name of the first table; and filling the attribute of the class by using the filtered field value of the first field, thereby constructing a service model.
Corresponding classes are configured for the service system in advance, each class corresponds to a service model, the service model is composed of attributes, and the service model can be specifically shown in a table II.
Watch II
The commodity is the name of the class, the commodity type and the price are attributes of the class, the commodity type is a String type variable, and the price is a double type variable.
When the business model is constructed, higher customization can be performed by using a script, and the following processing procedures can be realized through the script: the names of the classes (classes) are analyzed according to the table names of the first table, for example, when the table names of the first table are called as the classes, the names of the classes are analyzed according to a preset rule, then the filtered field values in the step 306 are stored in the form of maps, the attributes in the service model are filled, for example, the attributes of the goodsType in the classes can be simply filled by the field values of the goodsype, and the priority in the classes can be simply filled by the priority, so that the service information can be sent to the service system more flexibly through the realization of high compatibility and customization through the script, and the requirements of the service system can be met.
Step 308, assembling the operation name of the first operation and the service model into a service message.
In this embodiment of the present application, the first operation or the second operation may specifically include: an insert operation, an update operation, a query operation, or a delete operation.
And step 309, sending the service message to the service system according to the name of the class configured for the service system.
In this embodiment of the present application, the service message may be sent to the service system according to a name of a class configured for the service system, and further, the service message may also be sent to the service system according to a name of a class configured for the service system and an operation name configured for the service system. Wherein the names of the classes are necessary, the operation names are optional, when the service system subscribes to the service message, the service system can select which class of change is interested, and which operation is interested, and defaults to all operations.
As can be seen from the above processing procedure, in the service message sending method provided in the second embodiment of the present application, after the database change event is monitored according to the table name, the operation information can be further extracted from the database change event according to the operation name, so that the accuracy of sending the service message is further improved.
Correspondingly to the above service message sending method, the embodiment of the present application further provides a service message sending device, as shown in fig. 4, which is a structure diagram of the service message sending device provided in the embodiment of the present application, where the service message sending device includes: a recording unit 401, an parsing unit 402, a listening unit 403, a service message constructing unit 404, and a service message transmitting unit 405;
the recording unit 401 is configured to record operation information of a database in a database log, where the database includes a first table;
the parsing unit 402 is configured to parse the database log recorded by the recording unit 401, and generate a database change event, where the database change event includes the operation information, and the operation information includes a table name of the first table;
the monitoring unit 403, configured to monitor the database change event when the table name of the first table included in the database change event generated by the parsing unit 402 is the same as the table name of the second table configured for the service system;
the service message construction unit 404 is configured to extract the operation information from the database modification event monitored by the monitoring unit 403, and construct a service message;
the service message sending unit 405 is configured to send the service message constructed by the service message constructing unit 404 to the service system, so that the service system counts service change conditions according to the service message.
Preferably, the recording unit 401 specifically includes: an acquisition subunit and a recording subunit;
the obtaining subunit is configured to obtain operation information on the database when a first operation is performed on a first field in a first table of the database, where the operation information includes a table name of the first table, an operation name of the first operation, a field name of the first field, and a field value of the first field;
the recording subunit is configured to record the operation information acquired by the acquiring subunit in a Binlog database log.
Preferably, the parsing unit 402 is specifically configured to: when the preset time is reached, the database log recorded by the recording unit 401 is parsed, and a database change event is generated, where the database change event includes the operation information, and the operation information includes a table name of the first table.
Preferably, the monitoring unit 403 is specifically configured to: and monitoring the database change event when the table name of the first table included in the database change event generated by the parsing unit 402 is identical to or partially identical to the table name of the second table configured for the service system.
More preferably, the service message construction unit 404 specifically includes: an operation information extraction subunit, a field value filtering subunit, an analysis subunit, a service model construction subunit and a service message assembly subunit;
the operation information extraction subunit is configured to extract the operation information from the database change event when the operation name of the first operation is the same as the operation name of the second operation configured for the service system;
the field value filtering subunit is configured to filter the field value of the first field from the operation information extracted by the operation information extracting subunit;
the analysis subunit is configured to analyze the name of the class according to the table name of the first table;
the service model construction subunit is configured to populate the attribute of the class with the field value of the first field filtered by the field value filtering subunit, thereby constructing a service model;
the service message assembling subunit is configured to assemble the operation name of the first operation and the service model into a service message.
More preferably, the first operation or the second operation includes: an insert operation, an update operation, a query operation, or a delete operation.
More preferably, the service message sending unit 405 is specifically configured to: and sending the service message to the service system according to the name of the class configured for the service system.
Those of skill would further appreciate that the various illustrative objects and algorithm steps described in connection with the embodiments disclosed herein may be implemented as electronic hardware, computer software, or combinations of both, and that the various illustrative components and steps have been described above generally in terms of function in order to clearly illustrate the interchangeability of hardware and software. Whether such functionality is implemented as hardware or software depends upon the particular application and design constraints imposed on the solution. Skilled artisans may implement the described functionality in varying ways for each particular application, but such implementation decisions should not be interpreted as causing a departure from the scope of the present application.
The steps of a method or algorithm described in connection with the embodiments disclosed herein may be embodied in hardware, in a software module executed by a processor, or in a combination of the two. The software modules may be disposed in Random Access Memory (RAM), memory, read Only Memory (ROM), electrically programmable ROM, electrically erasable programmable ROM, registers, hard disk, a removable disk, a CD-ROM, or any other form of storage medium known in the art.
The foregoing embodiments have been provided for the purpose of illustrating the general principles of the present application, and are not meant to limit the scope of the invention, but to limit the scope of the invention.