一、seata-server安装
在seata官网下载需要的seata-server版本包
1. linux环境执行 tar -zxvf seata-server-1.3.0.tar.gz命令解压
2.解压的seata文件包进入conf文件夹下 有两个配置文件需要修改 registry.conf和file.conf
3.registry.conf修改注册中心配置 ,这里注册中心用的eureka直接修改成自己的eureka配置即可
eureka.application 默认default 也可以自定义名称。
4.file.conf修改存储模式,这里用的是db模式,修改自己的数据库连接配置即可
seata默认有四种事务模式 默认使用AT模式,AT模式需要首先在file.conf配置的对应库中创建三张表global_table(全局事务表)
CREATE TABLE `global_table` (
`xid` varchar(128) NOT NULL,
`transaction_id` bigint(20) DEFAULT NULL,
`status` tinyint(4) NOT NULL,
`application_id` varchar(32) DEFAULT NULL,
`transaction_service_group` varchar(32) DEFAULT NULL,
`transaction_name` varchar(128) DEFAULT NULL,
`timeout` int(11) DEFAULT NULL,
`begin_time` bigint(20) DEFAULT NULL,
`application_data` varchar(2000) DEFAULT NULL,
`gmt_create` datetime DEFAULT NULL,
`gmt_modified` datetime DEFAULT NULL,
PRIMARY KEY (`xid`),
KEY `idx_gmt_modified_status` (`gmt_modified`,`status`),
KEY `idx_transaction_id` (`transaction_id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
branch_table(分支事务表)
CREATE TABLE `branch_table` (
`branch_id` bigint(20) NOT NULL,
`xid` varchar(128) NOT NULL,
`transaction_id` bigint(20) DEFAULT NULL,
`resource_group_id` varchar(32) DEFAULT NULL,
`resource_id` varchar(256) DEFAULT NULL,
`branch_type` varchar(8) DEFAULT NULL,
`status` tinyint(4) DEFAULT NULL,
`client_id` varchar(64) DEFAULT NULL,
`application_data` varchar(2000) DEFAULT NULL,
`gmt_create` datetime(6) DEFAULT NULL,
`gmt_modified` datetime(6) DEFAULT NULL,
PRIMARY KEY (`branch_id`),
KEY `idx_xid` (`xid`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
lock_table(全局锁表)
CREATE TABLE `lock_table` (
`row_key` varchar(128) NOT NULL,
`xid` varchar(96) DEFAULT NULL,
`transaction_id` bigint(20) DEFAULT NULL,
`branch_id` bigint(20) NOT NULL,
`resource_id` varchar(256) DEFAULT NULL,
`table_name` varchar(32) DEFAULT NULL,
`pk` varchar(36) DEFAULT NULL,
`gmt_create` datetime DEFAULT NULL,
`gmt_modified` datetime DEFAULT NULL,
PRIMARY KEY (`row_key`),
KEY `idx_branch_id` (`branch_id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
同时还需要在各个分支事务对应的数据库添加表undo_log通过保存事务的前后镜像最终完成事务的提交和回滚
CREATE TABLE `undo_log` (
`id` bigint(20) NOT NULL AUTO_INCREMENT,
`branch_id` bigint(20) NOT NULL,
`xid` varchar(100) NOT NULL,
`context` varchar(128) NOT NULL,
`rollback_info` longblob NOT NULL,
`log_status` int(11) NOT NULL,
`log_created` datetime NOT NULL,
`log_modified` datetime NOT NULL,
`ext` varchar(100) DEFAULT NULL,
PRIMARY KEY (`id`),
UNIQUE KEY `ux_undo_log` (`xid`,`branch_id`)
) ENGINE=InnoDB AUTO_INCREMENT=25 DEFAULT CHARSET=utf8;
5.在seata/bin目录下执行seata-server.sh可启动seata服务端,但这种启动离开终端后服务就会关闭,最终使用命令启动 nohup ./seata-server.sh -h ip >log.out 2>1 & ; -h 后面的ip可以是你的公网ip 因为seata启动默认是用内网ip注册到,本地启动可能会报错 ,这里可以修改未公网ip注册。
二、seata-client配置
服务端启动好后,就可以配置客户端。首先在pom文件中引入seata包
<!--seata-->
<dependency>
<groupId>io.seata</groupId>
<artifactId>seata-spring-boot-starter</artifactId>
<version>1.3.0</version>
</dependency>
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-seata</artifactId>
<version>2.1.2.RELEASE</version>
<exclusions>
<exclusion>
<groupId>io.seata</groupId>
<artifactId>seata-spring-boot-starter</artifactId>
</exclusion>
</exclusions>
</dependency>
包引入成功后修改yml配置文件
注: 目前seata-all包是需要使用conf类型配置文件。如果是依赖seata-spring-boot-starter,可直接将配置项写入到配置文件,这样可以不使用conf类型文件。
#seata配置
seata:
enabled: true
# 你的服务名称
application-id: ${spring.application.name}
tx-service-group: my_test_tx_group
# 开启数据源自动代理
enable-auto-data-source-proxy: true
# 可以指定动态代理
use-jdk-proxy: false
service:
vgroup-mapping:
# 此处配置对应Server端配置registry.eureka.application的值
my_test_tx_group: seata-server
config:
type: file
# Eureka配置
registry:
type: eureka
eureka:
serviceUrl: ${eurekaUrl}
weight: 1
注:tx-service-group名称直接对应seata.service.vgroup-mapping名;seata.service.vgroup-mapping.tx-service-group 直接对应 registry.conf配置文件中 eureka.application名称。
三、实现分布式管理
在项目中可以使用@GlobalTransactional注解实现分布式事务;但并不是所有的数据库操作都需要开启全局事务,而开启全局事务是一个比较重的操作,需要向 TC 发起开启全局事务等 RPC 过程,而@GlobalLock
注解则只会在执行过程中查询全局锁是否存在,不会去开启全局事务,因此在不需要全局事务,而又需要检查全局锁避免脏读脏写时,使用@GlobalLock
注解是一个更加轻量的操作。