The content of the invention
In order to solve the above mentioned problem of prior art presence, the invention provides distributed transaction in a kind of micro services frameworkProcessing method.
The technical solution adopted in the present invention is:The processing method of distributed transaction in a kind of micro services framework, it includesFollowing steps:Open the unit affairs of DB examples;DB requests are performed, DB is updated;Request Real-time Transaction module;To Real-time Transaction mouldWhether block asks successfully to be judged that, if not asking successfully, to trigger rollback, the affairs that throw exception or other DB drive are returnedRolling trigger event;If asking successfully, to be submitted to.
Further, in the step of request Real-time Transaction module, at most carry out a Real-time Transaction module and call, such asFruit has multiple Real-time Transaction modules, then carry out series connection and call.
Further, it is described whether to ask Real-time Transaction module the step of successfully judging, if asked successfully,Before being submitted to, also including insertion final consistency processing module the step of.
Further, it is described whether to ask Real-time Transaction module the step of successfully judging, if asked successfully,Before being submitted to, also including insertion final consistency processing module the step of.
Further, the processing method of the distributed transaction processes distributed thing using the topological structure that module is calledBusiness.
Further, the topological structure called using module processes distributed transaction core design rule bagInclude:At most once the RPC of modification type is called one module, and the RPC of modification type is called and need not be provided rollback interface.
Further, if the RPC that single process has repeatedly modification type is called, while ensureing real-time the one of entirety againCause property, need to call series connection by RPC.
Further, the topological structure called using module is also wrapped processing distributed transaction core design ruleInclude:RPC interface interchanges are encapsulated in inside DB transaction process methods, are placed on behind DB affairs.
Further, after the RPC interface interchanges failure or return fail, throw exception triggering DB transaction rollbacks.
Further, if the RPC in flow calls receiving final consistent, queue is inserted into, but must putAfter all call flows terminate.
Due to using above technical scheme, beneficial effects of the present invention to be:The present invention efficiently utilizes DB drive modulesUnit Two-phase commitment characteristic, a kind of topological structure using service call is innovatively proposed to realize distributed transactionMode, can serve as the solution of real-time consistency.Meanwhile, the program has good ductility, is tied by another topologyStructure, it is also possible to realize final consistency affairs.The present invention only can just realize transaction guarantee by an interaction, fit very muchClose the distributed module within three and realize affair mechanism.The present invention can conclusively show distributed transaction by topology designTreatment rank, series connection is realtime uniform, and parallel connection is final consistent.
Specific embodiment
To make the object, technical solutions and advantages of the present invention clearer, technical scheme will be carried out belowDetailed description.Obviously, described embodiment is only a part of embodiment of the invention, rather than whole embodiments.BaseEmbodiment in the present invention, those of ordinary skill in the art are resulting on the premise of creative work is not made to be ownedOther embodiment, belongs to the scope that the present invention is protected.
Current micro services framework has worldwide been widely used as a kind of new design.But its difficult pointOne of, it is affairs problem caused by distributed deployment.
Distributing real time system is related to multiple databases for being distributed in different places, but operation to database must be wholeIt is submitted or rollback.As long as failing when any database is operated, all databases for participating in affairs are required for rollback.It is distributedIt is consistent with final that affairs can be divided into realtime uniform.
The current general real-time consistency solution of industry has the scheme submitted to based on Two-phase commitment or three stages.TwoRealization such as Atomikos storehouses (realizing JTA), JOTM, LIXA that stage submits to.Two-phase commitment scheme is applied in practiceCompare wide.But distributed scene is different from unit scene, and process time is double, an interaction is divided into originallyPrepare and commit are interacted twice, poor performance and have shortcoming:Coordinator delays and participant can be caused synchronously to hinder in the case of machinePlug, need to increase coordinator it is hot standby and record undo and redo daily records.When coordinator and participant delay machine simultaneously, problem is without solution.
In a distributed manner as a example by Two-phase commitment, after receiving a transactions requests, coordinator initiates to coordinate request for the first timePrepare, it is desirable to the corresponding sentence of whole participant's pre-executions.When all feeding back successfully, coordinator will initiate commit pleaseAsk, it is desirable to which whole participants submit affairs to.When two steps all after the completion of, issued transaction complete.Lost when the first step has participant to feed backWhen losing, coordinator initiates rollback requests, it is desirable to whole participant's rollback affairs.
In order to solve the problems, such as Two-phase commitment, XA proposes three stages submission scheme.Wherein, XA is that X/Open DTP determineInterface specification (i.e. interface function) between the task manager and database of justice.Three stages increased once for ratificationCanPrepare, and increase the overtime obstructing problem to solve Two-phase commitment, but three stages submitted the interactivity of scheme toCan be worse.
The main flow of the solution of current final consistency is achieved in that, based on the message queue that can be retried, consumer willPeriodically retry until the system of all participants finally realizes that data are consistent.
As shown in figure 1, the invention provides a kind of processing method of distributed transaction in micro services framework, it uses moduleThe topological structure that calls processes distributed transaction, and it specifically includes following steps:
1) the unit affairs of DB (DataBase, database) example are opened.
2) DB requests are performed, DB is updated.
3) Real-time Transaction module is asked.
4) to step 3) whether ask successfully to judge, if not asking successfully, to trigger rollback, throw exception or itsThe transaction rollback trigger event that his DB drives;If asking successfully, to be submitted to.
Above-mentioned steps 3) in, at most carry out a Real-time Transaction module and call, if multiple Real-time Transaction modules, then enterRow series connection is called.
Above-mentioned steps 4) in, if asked successfully, before being submitted to, also including insertion final consistency processing moduleThe step of.
Solving distributed transaction the very corn of a subject design rule with topological mode is:
(1) module at most once modification type RPC (Remote Procedure Call Protocol, farJourney invocation of procedure agreement) call.The RPC of modification type is called and need not be provided rollback interface.
(2) if the RPC that single process has repeatedly modification type is called, while ensureing overall real-time consistency again, needRPC is called into series connection.
(3) RPC interface interchanges are encapsulated in inside DB transaction process methods, are placed on behind DB affairs.
(4) after the failure of RPC interface interchanges or return fail, throw exception triggering DB transaction rollbacks.
(5) if some of flow RPC is called can receive final consistent (such as settling accounts the background modules such as reconciliation), canTo insert queue.But must necessarily be placed in after all call flows terminate.
Illustrate below and the processing method of distributed transaction in micro services framework of the present invention is illustrated.
Call relation between micro services is organized into topological diagram, represents that affairs are real-time consistencies or final with topological diagramUniformity.As shown in figure 1, real-time consistency processing module includes distributed transaction modules A, distributed transaction module B and distributionFormula transaction module C.Final consistency processing module includes distributed transaction module D and distributed transaction module E.Each affairs mouldBlock includes that DB affairs and RPC are asked.
It is called according to mode as shown in Figure 2, the transactional guarantees driven using DB entirely process the transactional of logic.Distributed transaction modules A needs modification table a (real-time consistency), distributed transaction module B to need modification table b (realtime uniformsProperty), distributed transaction module C needs modification table c (real-time consistency), distributed transaction module D to need modification table d (finalCause property), distributed transaction module E needs modification table e (final consistency).
Distributed transaction modules A, distributed transaction module B and distributed transaction module C need to ensure realtime uniform, distributionFormula transaction module D needs to ensure final consistent with distributed transaction module E.In topology design, according to principle (1) and principle(2) order will be called to be adjusted.Distributed transaction modules A accesses distributed transaction module B, and distributed transaction module B is accessedDistributed transaction module C.According to principle (5), the access of distributed transaction module D and distributed transaction module E is placed on finally.
As shown in figure 3, each distributed transaction module execute instruction flow is:
If distributed transaction modules A, distributed transaction module B and distributed transaction module C overall operations are normal, divideCloth affairs are submitted to successfully.Then distribution of notifications formula transaction module D and distributed transaction module E, makes its final process complete i.e.Can.
Assuming that fail when distributed transaction module performs itself DB in-stockroom operation, then distributed transaction module C rollback the machineAffairs, are then back to mono- error code of distributed transaction module B.Distributed transaction module B receives distributed transaction module C'sReturn, be the discovery that mistake, also rollback the machine affairs;Distributed transaction modules A takes the return of distributed transaction module B, also returnsRolling the machine affairs, reach overall rollback.
Under extreme case, if distributed transaction module C runs succeeded, but fail when returning to distributed transaction module B(such as distributed transaction module C performs overlong time, causes TCP to connect and disconnects, or network jitter causes connection to disconnect), then canGo wrong.The database of distributed transaction module C cannot rollback.At this point it is possible to pass through to increase cache make distributed transaction mouldThe access of block C has idempotence, is that distributed transaction module B to distributed transaction module C increases time-out is retried.
The invention provides a kind of processing method of distributed transaction in micro services framework, the process efficiently utilizes DBThe unit Two-phase commitment characteristic of drive module, innovatively proposes a kind of topological structure using service call to realize distributionThe mode of formula affairs, can serve as the solution of real-time consistency.Meanwhile, the program has good ductility, by anotherPlant topological structure, it is also possible to realize final consistency affairs.The present invention only can just realize that affairs are protected by an interactionCard, is especially suitable for the distributed module within three and realizes affair mechanism.The present invention can be conclusivelyed show by topology designThe treatment rank of distributed transaction, series connection is realtime uniform, and parallel connection is final consistent.
The above, specific embodiment only of the invention, but protection scope of the present invention is not limited thereto, and it is anyThose familiar with the art the invention discloses technical scope in, change or replacement can be readily occurred in, should all containCover within protection scope of the present invention.Therefore, protection scope of the present invention should be based on the protection scope of the described claims.