Disclosure of Invention
The present invention provides a distributed transaction manager and a management method under a microservice architecture, aiming at the above-mentioned defects in the prior art.
The technical scheme adopted by the invention for solving the technical problems is as follows: a distributed transaction management method under a micro-service architecture is constructed, and the method comprises the following steps:
judging the type of the called service according to the service calling method type, if the called service is a slave service needing remote calling, obtaining a transaction context from a cache and identifying a JVM environment, if the slave service and the master service are located in the same JVM, carrying out local calling, executing preprocessing, storing compensation operation, updating the transaction context, executing the slave service, and carrying out transaction compensation according to the transaction context when a service exception occurs.
In the distributed transaction management method under the microservice architecture of the present invention, the method further includes:
judging the type of the called service according to the service calling method type, if the called service is a slave service needing remote calling, obtaining a transaction context from a cache and identifying a JVM environment, if the slave service and a master service are located in different JVMs, entering a remote branch according to a remote interceptor, executing preprocessing, storing compensation operation, updating the transaction context, then executing the slave service, and when a service abnormality occurs, performing transaction compensation according to the transaction context.
In the distributed transaction management method under the microservice architecture of the present invention, the method further includes:
judging the type of the called service according to the service calling method type, if the called service is a main service, creating a transaction, taking the action of calling the main service as the root of a service calling tree, updating the transaction context in a cache, calling the main service, and when a service is abnormal, calling a service logic to execute transaction compensation according to the historical state in the transaction context and submit the transaction, and directly submitting the transaction if no abnormality occurs.
In the distributed transaction management method under the microservice architecture of the present invention, the method further includes: when the business activity starts, a business interceptor is used for intercepting, generating a transaction context and storing the transaction context in a cache.
The invention also discloses a distributed transaction manager under the micro-service architecture, which comprises a remote interceptor, a service interceptor and a cache, wherein the remote interceptor is started once when the service is called remotely each time and stores the intercepted information into the cache, and the service interceptor is started once when the service is called each time and stores the intercepted information into the cache;
the transaction manager is used for judging the type of the called service according to the type of a service calling method when the service is called, obtaining a transaction context from a cache and identifying a JVM (JVM) environment if the called service is a slave service needing remote calling, carrying out local calling, executing preprocessing, storing compensation operation, updating the transaction context, then executing the slave service, and carrying out transaction compensation according to the transaction context when service abnormity occurs.
In the distributed transaction manager under the micro-service architecture, the transaction manager is further configured to, when a service is called, determine a type of the called service according to a service calling method type, obtain a transaction context from the cache and identify a JVM environment if the called service is a slave service that needs to be remotely called, enter a remote branch according to a remote interceptor if the slave service and a master service are located in different JVMs, obtain the transaction context from the cache, perform preprocessing and store a compensation operation, update the transaction context, then execute the slave service, and perform transaction compensation according to the transaction context when a service exception occurs.
In the distributed transaction manager under the micro-service architecture, the transaction manager is also used for judging the type of the called service according to the type of the service calling method when the service is called, if the called service is the main service, creating a transaction, taking the action of calling the main service as the root of a service calling tree, updating the transaction context in a cache, calling the main service, and when the service is abnormal, calling the service logic to execute transaction compensation according to the historical state in the transaction context, submitting the transaction, and directly submitting the transaction if the abnormal condition does not occur.
In the distributed transaction manager under the micro-service architecture, the transaction manager is further configured to intercept, generate a transaction context, and store the transaction context in a cache by using a service interceptor when a service activity starts.
The distributed transaction manager and the management method under the micro-service architecture have the following beneficial effects: the invention can identify the JVM environment, obtain the transaction context from the cache for the calling of the same JVM environment, and carry out the calling through the local calling, thereby facilitating the use of the service developer, leading the service developer to not need to distinguish the local calling from the remote calling, and avoiding unnecessary resource waste and performance loss.
Detailed Description
To facilitate an understanding of the invention, the invention will now be described more fully with reference to the accompanying drawings. Exemplary embodiments of the invention are shown in the drawings. This invention may, however, be embodied in many different forms and should not be construed as limited to the embodiments set forth herein. Rather, these embodiments are provided so that this disclosure will be thorough and complete.
Unless defined otherwise, all technical and scientific terms used herein have the same meaning as commonly understood by one of ordinary skill in the art to which this invention belongs. The terminology used in the description of the invention herein is for the purpose of describing particular embodiments only and is not intended to be limiting of the invention.
The general idea of the invention is as follows: the JVM environment is automatically identified, the transaction context is obtained from the cache for the calling of the same JVM environment, and the calling is carried out through local calling, so that the use of service developers is facilitated, the service developers do not need to distinguish local calling from remote calling, and unnecessary resource waste and performance loss are avoided.
In order to better understand the technical solutions, the technical solutions will be described in detail below with reference to the drawings and the specific embodiments of the specification, and it should be understood that the embodiments and specific features of the embodiments of the present invention are detailed descriptions of the technical solutions of the present application, and are not limited to the technical solutions of the present application, and the technical features of the embodiments and examples of the present invention may be combined with each other without conflict.
A complete application business activity consists of a main business service and a plurality of auxiliary business services, and the whole business activity is generally initiated and ended by the main business service; providing a compensation operation from a business operation provided by the business service, the compensation operation being capable of offsetting (or partially offsetting) a business result of the forward business operation; the business activity manager controls the consistency of business activities, is responsible for registering operations in the business activity, and invokes compensation operations when the business activity is cancelled.
The complete business processing process comprises a plurality of service operations, and the service operations form a call tree. The master action (i.e., the action that invokes the portal/service) is the initiator of the overall business activity and is also the root of the service operation invocation tree. The submission and rollback of the master action itself can be made to reasonably determine the submission and rollback of the entire business activity. Corresponding to a service operation call; each atomic action serves as a basic unit of business activity, satisfying ACID by local transactions. The service operation corresponding to the atomic action is of two types: request-response, asynchronous call. There are many consequences of service operation, including successful business operation, abnormal business operation, network exception/timeout, etc. The service invocation and the corresponding transaction management in the invention are mainly divided into the following two parts:
1) all services of business activities are called locally, and if the service calling is abnormal, the transaction is guaranteed by the existing database transaction manager;
2) for a (springclosed or Dubbo) business activity with a remote service invocation, if a local service invocation in the business activity is abnormal, compensation needs to be performed on the remote service and all the branches of the business activity related to the remote service invocation success. The first embodiment and the second embodiment are specific examples for implementing the contents in this section.
Example one
Referring to fig. 1, an embodiment of the invention discloses a distributed transaction manager under a micro-service architecture, and the transaction manager of this embodiment may integrate a springclosed and a Dubbo micro-service framework, so as to provide distributed transaction management for application development under a cloud environment.
Specifically, the transaction manager of this embodiment includes a remote interceptor, a service interceptor, and a cache. The remote interceptor is started once each time the service is remotely called and stores the intercepted information into the cache. The service interceptor is started once when calling service each time, and stores the intercepted information into the cache. The cache can use a cache database such as redis, memcached or mongodb.
Firstly, when the business activity starts, the transaction manager uses the internal business interceptor to intercept, generate the transaction context, and store the transaction context in the cache. The transaction context includes a global transaction id, a JVM identification, a persisted historical state, a current transaction state, and the like.
Then, when the transaction manager calls the service, the transaction manager judges the type of the called service according to the type of the service calling method, if the called service is the main service, the transaction manager creates a transaction, updates the transaction context in the cache by taking the action of calling the main service as the root of a service calling tree, and calls the main service. When the business is abnormal, the business logic is called to execute the business compensation according to the historical state in the context of the business and submit the business, and the business is directly submitted without the abnormal situation.
The transaction manager judges the type of the called service according to the type of a service calling method when calling the service, obtains a transaction context from a cache if the called service is a slave service needing remote calling, identifies a JVM environment according to a JVM identifier, obtains the transaction context from the cache according to a remote branch entered by a remote interceptor if the slave service and a master service are located in different JVMs, executes preprocessing and stores compensation operation, updates the transaction context, then executes the slave service, and performs transaction compensation according to the transaction context when a service abnormality occurs.
The transaction manager judges the type of the called service according to the service calling method type when calling the service, obtains a transaction context from a cache if the called service is a slave service needing remote calling, identifies a JVM environment according to a JVM identifier, carries out local calling if the slave service and a master service are located in the same JVM, carries out preprocessing and stores compensation operation, updates the transaction context, then executes the slave service, and carries out transaction compensation according to the transaction context when service abnormity occurs.
Example two
Referring to fig. 2, the transaction management method provided by the embodiment mainly includes:
and S100, when the business activity starts, intercepting by using a business interceptor, generating a transaction context, and storing the transaction context in a cache.
The cache can use a cache database such as redis, memcached or mongodb. The transaction context includes a global transaction id, a JVM identification, a persisted historical data state, a current transaction state, and the like.
S200, judging the type of the called service according to the type of the service calling method, if the called service is a main service, creating a transaction, taking the action of calling the main service as the root of a service calling tree, updating the transaction context in a cache, calling the main service, and when a service is abnormal, calling a service logic to execute transaction compensation according to the historical state in the transaction context, submitting the transaction, and directly submitting the transaction if no abnormality occurs.
S300, judging the type of the called service according to the type of the service calling method, if the called service is a slave service needing remote calling, obtaining a transaction context from a cache, identifying a JVM (virtual machine manager) environment according to a JVM identifier, if the slave service and the master service are located in different JVMs, entering a remote branch according to a remote interceptor, executing preprocessing, storing compensation operation, updating the transaction context, executing the slave service, and performing transaction compensation according to the transaction context when service abnormality occurs.
S400, judging the type of the called service according to the type of the service calling method, if the called service is a slave service needing remote calling, obtaining a transaction context from a cache, identifying a JVM environment according to a JVM identifier, if the slave service and the master service are located in the same JVM, carrying out local calling, executing preprocessing, storing compensation operation, updating the transaction context, then executing the slave service, and carrying out transaction compensation according to the transaction context when service abnormity occurs.
In summary, the distributed transaction manager and the management method under the micro-service architecture of the present invention have the following advantages: the invention can identify the JVM environment, obtain the transaction context from the cache for the calling of the same JVM environment, and carry out the calling through the local calling, thereby facilitating the use of the service developer, leading the service developer to not need to distinguish the local calling from the remote calling, and avoiding unnecessary resource waste and performance loss.
While the present invention has been described with reference to the embodiments shown in the drawings, the present invention is not limited to the embodiments, which are illustrative and not restrictive, and it will be apparent to those skilled in the art that various changes and modifications can be made therein without departing from the spirit and scope of the invention as defined in the appended claims.