Method for guaranteeing cross-service database transaction
Technical Field
The invention relates to the technical field of distributed transactions, in particular to a method for guaranteeing cross-service database transactions.
Background
In the prior art, the transactional property of the database is ensured through dao native transactions by coupling all logics in the same service. However, the pressure of the flow cannot be shared, the coupling of the services is too high, the whole system has a single point problem, all the services are affected when a certain functional module has a problem, the database cannot be divided, and the data volume of the single database is too large, so that a method for guaranteeing the cross-service database transaction is provided.
Disclosure of Invention
Based on the technical problems in the background art, the invention provides a method for guaranteeing cross-service database transactions.
The invention provides a method for ensuring cross-service database transactions, which comprises the following specific implementation processes:
creating a distributed transaction controller to control the whole distributed transaction;
the front-end request is subjected to load balancing through nginx and then forwarded to the web container, and the request is executed by a single thread in the web container;
the distributed transaction controllers are distributed horizontally to form a cluster for providing services to the outside, and the down controllers are removed from cluster nodes through heartbeat detection;
and using the redis as a cache, and sharing the redis in the cluster.
Preferably, the distributed transaction controller in S1 identifies the performing of the distributed transaction by creating a record of the distributed transaction, and records the operation into the StepList field before performing the operation of the service.
Preferably, when the distributed transaction controller in S1 needs to perform rollback, the record of the current distributed transaction is put into a rollback queue, and when performing rollback, the distributed transaction controller obtains the performed service operation record according to the StepList, and sends the record to the respective service for rollback.
Preferably, the distributed transaction controller in S1 concatenates the entire distributed transaction process with the unique identifier uuid recorded by hdStatus.
Preferably, the web container is one of tomcat or resin.
The beneficial effects of the invention are as follows:
1. by creating the controller of the distributed transaction, the problem of the distributed transaction can be solved, the reconstruction of a distributed architecture of the service is realized, the performance of the system is greatly improved, the horizontal database partitioning can be performed on the database after the distributed transaction is realized, and the operation and maintenance cost can be greatly improved.
2. Only one thread is required to execute each request in the web container from initialization to completion, so that the concurrency problem caused in a multi-thread environment is avoided, and the stateless distribution of the distributed controller is realized to solve the high concurrency problem.
3. And the redis is used as a cache, and the redis is shared in the cluster, so that the problem of data consistency can be solved, and a high-availability architecture is further realized.
Drawings
FIG. 1 is a schematic diagram of a system architecture for single-thread high concurrency of a method for guaranteeing cross-service database transactions according to the present invention;
fig. 2 is a schematic diagram of a system architecture for realizing high availability of a Redis + cluster, which is a method for guaranteeing cross-service database transactions provided by the present invention.
Detailed Description
The technical solutions in the embodiments of the present invention will be clearly and completely described below with reference to the drawings in the embodiments of the present invention, and it is obvious that the described embodiments are only a part of the embodiments of the present invention, and not all of the embodiments.
Referring to fig. 1-2, a method for guaranteeing cross-service database transactions creates a central control service named hdstatuussvr to control the whole distributed transaction, and the service is mainly responsible for the following functions:
a. creating a record to identify the proceeding of this distributed transaction is called hdStatus record, and before doing any service operation, this operation must be recorded in the stepList field.
b. When rollback is required, the hdStatus record is placed in a rollback queue. And when the rollback is carried out, acquiring the operation of which services are carried out according to the stepList, and sending the operation to the respective service for rollback.
c. The unique identifiers uuid recorded by the hdStatus are connected in series in the whole distributed transaction process.
In the present invention, the high concurrency and high availability architecture of the distributed transaction controller:
(1) single thread achieves high concurrency: all the requests of the front end are subjected to load balancing through nginx and then forwarded to web containers such as tomcat, resin and the like, only one thread is required to execute each request in the web containers from initialization to termination, so that the concurrency problem caused in a multi-thread environment is avoided, the stateless distribution of the distributed controller is realized, the high concurrency problem is solved, and the system architecture diagram is shown in figure 1.
(2) Redis + clustering enables high availability: the distributed transaction controllers are horizontally distributed to form a cluster for providing services to the outside, so that even if partial controllers are down, functions cannot be influenced, the down controllers are removed from cluster nodes through heartbeat detection, the health condition in the cluster is guaranteed, a redis is used as a cache, the redis is shared in the cluster to solve the problem of data consistency, a high-availability architecture is further realized, and a system architecture diagram is shown in figure 2.
The above description is only for the preferred embodiment of the present invention, but the scope of the present invention is not limited thereto, and any person skilled in the art should be considered to be within the technical scope of the present invention, and the technical solutions and the inventive concepts thereof according to the present invention should be equivalent or changed within the scope of the present invention.