SpringCloud微服务之seata篇(一)

一、 简介

Seata 是一款开源的分布式事务解决方案,致力于提供高性能和简单易用的分布式事务服务。Seata 将为用户提供了 AT、TCC、SAGA 和 XA 事务模式,为用户打造一站式的分布式解决方案。
在这里插入图片描述

二、下载

Seata官方文档:https://seata.io/zh-cn/docs/overview/what-is-seata.html
Seata-Service官方下载地址:https://seata.io/zh-cn/blog/download.html

三、安装部署

这里介绍使用的是1.4.2版本,使用的数据库为mysql,nacos作为注册中心

大体可分为两大步骤:

1、service端配置

(1) 创建名为seata的数据库,执行以下脚本创建表,其他数据库类型脚本请点击这里

-- -------------------------------- The script used when storeMode is 'db' --------------------------------

-- the table to store GlobalSession data

CREATE TABLE IF NOT EXISTS `global_table`
(
	`xid` VARCHAR(128) NOT NULL,
	`transaction_id` BIGINT,
	`status` TINYINT NOT NULL,
	`application_id` VARCHAR(32),
	`transaction_service_group` VARCHAR(32),
	`transaction_name` VARCHAR(128),
	`timeout` INT,
	`begin_time` BIGINT,
	`application_data` VARCHAR(2000),
	`gmt_create` DATETIME,
	`gmt_modified` DATETIME,
	PRIMARY KEY (`xid`),
	KEY `idx_status_gmt_modified` (`status` , `gmt_modified`),
	KEY `idx_transaction_id` (`transaction_id`)
) ENGINE = InnoDB DEFAULT CHARSET = utf8mb4;

-- the table to store BranchSession data

CREATE TABLE IF NOT EXISTS `branch_table`

(
	`branch_id` BIGINT NOT NULL,
	`xid` VARCHAR(128) NOT NULL,
	`transaction_id` BIGINT,
	`resource_group_id` VARCHAR(32),
	`resource_id` VARCHAR(256),
	`branch_type` VARCHAR(8),
	`status` TINYINT,
	`client_id` VARCHAR(64),
	`application_data` VARCHAR(2000),
	`gmt_create` DATETIME(6),
	`gmt_modified` DATETIME(6),
	PRIMARY KEY (`branch_id`),
	KEY `idx_xid` (`xid`)
) ENGINE = InnoDB DEFAULT CHARSET = utf8mb4;

-- the table to store lock data

CREATE TABLE IF NOT EXISTS `lock_table`

(
	`row_key` VARCHAR(128) NOT NULL,
	`xid` VARCHAR(128),
	`transaction_id` BIGINT,
	`branch_id` BIGINT NOT NULL,
	`resource_id` VARCHAR(256),
	`table_name` VARCHAR(32),
	`pk` VARCHAR(36),
	`status` TINYINT NOT NULL DEFAULT '0' COMMENT '0:locked ,1:rollbacking',
	`gmt_create` DATETIME,
	`gmt_modified` DATETIME,
	PRIMARY KEY (`row_key`),
	KEY `idx_status` (`status`),	
	KEY `idx_branch_id` (`branch_id`)
) ENGINE = InnoDB DEFAULT CHARSET = utf8mb4;

CREATE TABLE IF NOT EXISTS `distributed_lock`
(
	`lock_key` CHAR(20) NOT NULL,
	`lock_value` VARCHAR(20) NOT NULL,
	`expire` BIGINT,
	primary key (`lock_key`)
) ENGINE = InnoDB DEFAULT CHARSET = utf8mb4;

INSERT INTO `distributed_lock` (lock_key, lock_value, expire) VALUES ('HandleAllSession', ' ', 0);

(2)修改conf目录下的registry.conf文件,这里我的seata注册和配置都选择nacos,其他方式请查看这里

registry {
  # file 、nacos 、eureka、redis、zk、consul、etcd3、sofa
  type = "nacos"


  nacos {
    application = "seata-server"
    serverAddr = "127.0.0.1:8848"
    group = "DEFAULT_GROUP"
    namespace = "public"
    cluster = "default"
    username = "nacos"
    password = "nacos"
  }
}

config {
  # file、nacos 、apollo、zk、consul、etcd3
  type = "nacos"

   nacos {
      serverAddr = "127.0.0.1:8848"
      namespace = "public"
      group = "DEFAULT_GROUP"
      username = "nacos"
      password = "nacos"
      ##if use MSE Nacos with auth, mutex with username/password attribute
      #accessKey = ""
      #secretKey = ""
      dataId = "seata.properties"
   }
}

(3)nacos新增配置seata.properties
在这里插入图片描述
配置内容,其他配置参数可查看这里

store.mode=db

#These configurations are required if the `store mode` is `db`. If `store.mode,store.lock.mode,store.session.mode` are not equal to `db`, you can remove the configuration block.
store.db.datasource=druid
store.db.dbType=mysql
store.db.driverClassName=com.mysql.jdbc.Driver
store.db.url=jdbc:mysql://127.0.0.1:3306/seata?useUnicode=true&rewriteBatchedStatements=true
store.db.user=root
store.db.password=root
store.db.minConn=5
store.db.maxConn=30
store.db.globalTable=global_table
store.db.branchTable=branch_table
store.db.distributedLockTable=distributed_lock
store.db.queryLimit=100
store.db.lockTable=lock_table
store.db.maxWait=5000

#Transaction routing rules configuration, only for the client
service.vgroupMapping.my_test_tx_group=default
#If you use a registry, you can ignore it
service.default.grouplist=127.0.0.1:8091
service.enableDegrade=false
service.disableGlobalTransaction=false

(4)使用bin目录下.sh脚本或bat脚本启动seata-service

2、Client端配置

(1)在每个涉及到分布式事务的数据库中创建undo_log表,其他数据库类型请查看这里

-- for AT mode you must to init this sql for you business database. the seata server not need it.
CREATE TABLE IF NOT EXISTS `undo_log`
(
	`branch_id` BIGINT NOT NULL COMMENT 'branch transaction id',
	`xid` VARCHAR(128) NOT NULL COMMENT 'global transaction id',
	`context` VARCHAR(128) NOT NULL COMMENT 'undo_log context,such as serialization',
	`rollback_info` LONGBLOB NOT NULL COMMENT 'rollback info',
	`log_status` INT(11) NOT NULL COMMENT '0:normal status,1:defense status',
	`log_created` DATETIME(6) NOT NULL COMMENT 'create datetime',
	`log_modified` DATETIME(6) NOT NULL COMMENT 'modify datetime',
	UNIQUE KEY `ux_undo_log` (`xid`, `branch_id`)
) ENGINE = InnoDB AUTO_INCREMENT = 1
DEFAULT CHARSET = utf8mb4 COMMENT ='AT transaction mode undo table';

(2)服务中引入依赖,依赖版本最好对应你的seata-service客户端版本

<dependency>
    <groupId>com.alibaba.cloud</groupId>
    <artifactId>spring-cloud-starter-alibaba-seata</artifactId>
    <exclusions>
        <exclusion>
            <groupId>io.seata</groupId>
            <artifactId>seata-spring-boot-starter</artifactId>
        </exclusion>
    </exclusions>
</dependency>

<dependency>
    <groupId>io.seata</groupId>
    <artifactId>seata-spring-boot-starter</artifactId>
    <version>${对应你的seata-service版本}</version>
</dependency>

(3)配置所有有关服务的application.properties

分布事务分组名称,和上面seata-service配置中的server.vgroup_mapping.my_test_tx_group = "default"中的my_test_tx_group一定要对应

#seata
seata.tx-service-group=my_test_tx_group

(4)application启动类添加@EnableAutoDataSourceProxy 注解,作用开启数据源代理

由于seata1.4.2版本可以新增了可以自动数据源代理,旧版的可能需要自己做数据源代理,这里就不细说了

(5)在分布式事务代码处使用@GlobalTransactional 注解开启全局事务

注意全局事务是靠捕捉子服务返回异常来判断是否回滚事务的,如果子服务做异常捕捉,可能会导致不能正常回滚

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值