Open source distributed transaction middleware Seata Guide

Introduction


Seata Alibaba open source distributed transaction middleware, a distributed transaction solution with high performance and ease of use of micro-services architecture.


Original intention

  • Business non-invasive : a reduction of micro problems distributed transaction service based on the technology architecture brings intrusion into business

  • High performance : reduce Distributed Transaction Solutions brings performance cost


Distributed transaction definition

Distributed transaction is a global transaction, the transaction composed by a group of branches, branch affairs usually just a local transaction.


640?wx_fmt=png&wxfrom=5&wx_lazy=1&wx_co=1



design


Seata distributed transaction, there are two implementations, AT and TCC.


AT

AT model is based on the XA transaction evolved, the core business is non-invasive, is a two-stage after an improved submission, need database support.


640?wx_fmt=png&wxfrom=5&wx_lazy=1&wx_co=1

The basic components:

  • Transaction Coordinator (TC) : Transaction Coordinator, maintain global transactions running, is responsible for coordinating and driving the global transaction is committed or rolled back.

  • Manager Transaction (TM) : border control global affairs, is responsible for opening a global transaction, and eventually launch a global resolution to commit or roll back global.

  • Resource Manager (RM) : Control affairs branch, the branch is responsible for registration, status reports, and receive instruction affairs coordinator, commit and rollback drive branch (local) transaction.


Process flow:

  1. TM TC required to start a new global transaction. TC generates an XID global transaction.

  2. XID micro propagation by calling chain services.

  3. RM will be registered as a local transaction XID to the appropriate branch of the global transaction TC.

  4. TM claim TC corresponding global transaction commit or rollback the XID.

  5. TC transaction driving all the branches in the respective global transaction to complete XID branch commit or rollback.


640?wx_fmt=png&wxfrom=5&wx_lazy=1&wx_co=1


TCC

Each interface requires Seata prepare, commit, rollback.


与 AT 模式一样,在运行时,该切面会拦截所有对 TCC 接口的调用。每调用一次 Try 接口,切面会先向 TC 注册一个分支事务,然后才去执行原来的 RPC 调用。当请求链路调用完成后,TC 通过分支事务的资源 ID 回调到正确的参与者去执行对应 TCC 资源的 Confirm 或 Cancel 方法。


  1. 初步操作 Try:完成所有业务检查,预留必须的业务资源。

  2. 确认操作 Confirm:真正执行的业务逻辑,不做任何业务检查,只使用 Try 阶段预留的业务资源。因此,只要 Try 操作成功,Confirm 必须能成功。另外,Confirm 操作需满足幂等性,保证一笔分布式事务能且只能成功一次。

  3. 取消操作 Cancel:释放 Try 阶段预留的业务资源。同样的,Cancel 操作也需要满足幂等性。


Seata Server安装


1.下载最新版本的 Seata Sever

https://github.com/seata/seata/releases


2. 解压并启动 Seata server


unzip seata-server-xxx.zip
cd distribution
sh ./bin/seata-server.sh 8091 file



示例

场景:

把数据库zeroa中proxy表的一条数据转移到数据库zerob中proxy表里面。


模块:

zero-discovery-server:注册中心

zero-gateway-server:服务网关

zero-consumer:服务消费者

zero-provider-a:服务提供者A

zero-provider-b:服务提供者B


架构及版本:

Spring-cloud:Finchley.BUILD-SNAPSHOT

spring-cloud-starter-netflix-eureka-server:2.0.4.BUILD-SNAPSHOT

spring-cloud-starter-netflix-eureka-client:2.0.4.BUILD-SNAPSHOT

spring-cloud-starter-gateway:2.0.4.BUILD-SNAPSHOT

spring-cloud-starter-openfeign:2.0.0.RELEASE

spring-boot:2.0.0.RELEASE

spring-boot-starter-data-jpa:2.0.0.RELEASE

spring-cloud-alibaba-seata:0.9.1.BUILD-SNAPSHOT

seata-all:0.6.1

mysql-connector-java:8.0.11

druid-spring-boot-starter:1.1.18


mysql:5.7

seata-server-0.6.1


achieve:

zero-gateway-server

  • Configuration application.yml

640?wx_fmt=png&wxfrom=5&wx_lazy=1&wx_co=1



zero-provider-a:

  • Configuration application.yml

640?wx_fmt=png&wxfrom=5&wx_lazy=1&wx_co=1


  • File.conf

The main configuration application name and address seata server

vgroup_mapping.${spring.application.name}-fescar-service-group="default"

default.grouplist = "127.0.0.1:8091"


640?wx_fmt=png&wxfrom=5&wx_lazy=1&wx_co=1


  • Registry.conf

640?wx_fmt=png&wxfrom=5&wx_lazy=1&wx_co=1


  • Written Entity

640?wx_fmt=png&wxfrom=5&wx_lazy=1&wx_co=1


  • Written Repository

640?wx_fmt=png&wxfrom=5&wx_lazy=1&wx_co=1


  • Write Service

640?wx_fmt=png&wxfrom=5&wx_lazy=1&wx_co=1


  • Write code Controller

640?wx_fmt=png&wxfrom=5&wx_lazy=1&wx_co=1


  • DataSource

640?wx_fmt=png&wxfrom=5&wx_lazy=1&wx_co=1


zero-provider-b

Configuration with zero-provider-a project, write the corresponding business logic.

Add in dealing with business, an exception is thrown.

640?wx_fmt=png&wxfrom=5&wx_lazy=1&wx_co=1



zero-consumer

Configuration with zero-provider-a project, write the corresponding business logic.


  • feignClient

640?wx_fmt=png&wxfrom=5&wx_lazy=1&wx_co=1


  • feignclient

640?wx_fmt=png&wxfrom=5&wx_lazy=1&wx_co=1


  • Service

640?wx_fmt=png&wxfrom=5&wx_lazy=1&wx_co=1


  • Controller

640?wx_fmt=png&wxfrom=5&wx_lazy=1&wx_co=1


test

Start Seata Server

Start Mysql, and initialize (each bank to be created undo_log table)


DROP SCHEMA IF EXISTS zeroa;

CREATE SCHEMA zeroa;

USE zeroa;



CREATE TABLE `undo_log` (

  `id` bigint(20NOT NULL AUTO_INCREMENT,

  `branch_id` bigint(20NOT NULL,

  `xid` varchar(100NOT NULL,

  `context` varchar(128NOT NULL,

  `rollback_info` longblob NOT NULL,

  `log_status` int(11NOT NULL,

  `log_created` datetime NOT NULL,

  `log_modified` datetime NOT NULL,

  `ext` varchar(100DEFAULT NULL,

  PRIMARY KEY (`id`),

  UNIQUE KEY `ux_undo_log` (`xid`,`branch_id`)

ENGINE=InnoDB AUTO_INCREMENT=1 DEFAULT CHARSET=utf8;



DROP SCHEMA IF EXISTS zerob;

CREATE SCHEMA zerob;

USE zerob;



CREATE TABLE `undo_log` (

  `id` bigint(20NOT NULL AUTO_INCREMENT,

  `branch_id` bigint(20NOT NULL,

  `xid` varchar(100NOT NULL,

  `context` varchar(128NOT NULL,

  `rollback_info` longblob NOT NULL,

  `log_status` int(11NOT NULL,

  `log_created` datetime NOT NULL,

  `log_modified` datetime NOT NULL,

  `ext` varchar(100DEFAULT NULL,

  PRIMARY KEY (`id`),

  UNIQUE KEY `ux_undo_log` (`xid`,`branch_id`)

ENGINE=InnoDB AUTO_INCREMENT=1 DEFAULT CHARSET=utf8;


start up

zero-discovery-server

zero-gateway-server

zero-provider-a

zero-provider-b

zero-consumer


adding data

640?wx_fmt=png&wxfrom=5&wx_lazy=1&wx_co=1


Normal execution of the transaction

640?wx_fmt=png&wxfrom=5&wx_lazy=1&wx_co=1


You can view the data (a database to delete the data id = 2, b library to add a data) in two tables.


Perform transaction rollback

640?wx_fmt=png&wxfrom=5&wx_lazy=1&wx_co=1

Check database data (no change).


Guess you like

Origin blog.51cto.com/11976981/2423730