Distributed Transaction --- Seata

Distributed Problem

Application of the monomer is split into micro-service applications, the original three modules split into three separate applications, each using three separate data sources, three operational need to call completion services. At this time, data consistency within each service is guaranteed by the local transaction, but the global data consistency can not be guaranteed.

What Seata that?

Seata is an open source distributed transaction solutions, providing high performance and ease of use of distributed transaction services. Seata will provide users with an AT, TCC, SAGA and XA transaction model for distributed users to create a one-stop solution.

Seata can you do?

  • Distributed transaction processing of a three-component model ID +:
Transaction ID XID Globally unique transaction ID
Transaction Coordinator(TC) Transaction Coordinator: maintaining global transaction running, is responsible for coordinating and driving the transaction commit or rollback
Transaction Manager(TM ) Transaction Manager: control the border global transactions, is responsible for opening a global transaction, and eventually launch a global resolution to commit or roll back global
Resource Manager(RM) Explorer: Control affairs branch, the branch is responsible for registration, status reports, and receive instruction affairs coordinator, drive branch (local) transaction commit or rollback

AT mode

premise

  • Support for ACID transactions based on local relational database.
  • Java applications access the database via JDBC.

Whole Systems

  • The evolution of the two-phase commit protocol:
    • Stage: business data logging and rollback submitted in the same local transaction, the lock is released and the local connection resources.
    • Two stages:
      • Submit Asynchronized, very quickly completed.
      • Rollback reverse compensated by a rolling log phase.

Write isolation

  • Local transaction commits before a stage, you need to be sure to get the global lock.
  • Not get the global lock can not be submitted to local affairs.
  • Take global lock attempts is limited within a certain range, out of range will give up, and roll back the local transaction, the lock is released locally.

Read isolation

  • The default global sense has been submitted (Read Committed) or more on the basis of, Seata (AT mode) in the local database transaction isolation level isolation level is uncommitted read (Read Uncommitted).

TCC mode

  • A distributed global transaction, the overall model is two-phase commit. Global Transaction Services is composed of several branches, branch transaction model to meet the requirements of two-phase commit, which requires that each branch has its own transaction are:

    • Prepare a stage behavior
    • Two-stage commit or rollback behavior
  • Depending on the behavior patterns of two stages, we will branch transaction into Automatic (Branch) Transaction Mode and Manual (Branch) Transaction Mode.

  • AT mode supports local ACID transactions based on relational databases:

    • A prepare phase behavior: in local affairs, submit traffic data and update the corresponding rollback logging.
    • Two-phase commit acts: immediately successful conclusion, automatic asynchronous batch cleanup rollback log.
    • Two-stage behavior rollback: rollback journal by automatically generating a compensation operation, complete data rewind.
  • Appropriate, TCC mode, it is not dependent on the underlying data transaction support resources:

    • A prepare phase behavior: Calling prepare custom logic.
    • Two-phase commit acts: call commit custom logic.
    • Two-stage rollback behavior: custom calling rollback logic.
  • The so-called TCC mode refers to the custom of the support into the affairs branch of the global transaction management.

Saga mode

  • Saga is a long transaction model solutions SEATA provided in Saga mode, a business process each participant to submit local affairs, when one participant fails to compensate the participants already appeared successful, a stage and forward service two-stage compensation business development services are realized.

Applicable scene

  • Business process long, multi-business processes
  • Participants include other service companies or legacy systems can not provide TCC mode requires three interfaces

Advantage

  • Phase commit a local transaction, lock-free, high-performance
  • Event-driven architecture, participants can execute asynchronously, high throughput
  • Compensation service is easy to implement

Shortcoming

  • Does not guarantee isolation

Examples

example

  • Users buy the business logic of the commodity. The entire business logic is supported by three micro-services:
    • Warehousing services: for a given quantity of a commodity warehousing deduction.
    • Orders Services: Create orders based on procurement needs.
    • Account Services: deducting from the user's account.

Chart

Here Insert Picture Description

Warehousing Services

public interface StorageService {

    /**
     * 扣除存储数量
     */
    void deduct(String commodityCode, int count);
}

Order Services

public interface OrderService {

    /**
     * 创建订单
     */
    Order create(String userId, String commodityCode, int orderCount);
}

Account Services

public interface AccountService {

    /**
     * 从用户账户中借出
     */
    void debit(String userId, int money);
}

The main business logic

public class BusinessServiceImpl implements BusinessService {

    private StorageService storageService;

    private OrderService orderService;

    /**
     * 采购
     */
    public void purchase(String userId, String commodityCode, int orderCount) {

        storageService.deduct(commodityCode, orderCount);

        orderService.create(userId, commodityCode, orderCount);
    }
}
public class OrderServiceImpl implements OrderService {

    private OrderDAO orderDAO;

    private AccountService accountService;

    public Order create(String userId, String commodityCode, int orderCount) {

        int orderMoney = calculate(commodityCode, orderCount);

        accountService.debit(userId, orderMoney);

        Order order = new Order();
        order.userId = userId;
        order.commodityCode = commodityCode;
        order.count = orderCount;
        order.money = orderMoney;

        // INSERT INTO orders ...
        return orderDAO.insert(order);
    }
}

SEATA Distributed Transaction Solutions

Here Insert Picture Description

  • We only need to use a @GlobalTransactionalannotation on business methods
    @GlobalTransactional
    public void purchase(String userId, String commodityCode, int orderCount) {
        ......
    }

The more you know, the more you do not know.
Proper way without surgery, patients can still seek, there is no way to surgery, ending surgery.
If you have other questions, welcome message, we can discuss, learn together and progress together

He published 193 original articles · won praise 116 · views 10000 +

Guess you like

Origin blog.csdn.net/qq_40722827/article/details/105161929