1、下载seata,传送门
下载seata0.9和1.2两个版本
2、mysql中创建seata数据库,执行建表语句
/*
Navicat MySQL Data Transfer
Source Server : ym-yun
Source Server Version : 50728
Source Host : rm-bp1s43bnq4475ctu7oo.mysql.rds.aliyuncs.com:3306
Source Database : seata
Target Server Type : MYSQL
Target Server Version : 50728
File Encoding : 65001
Date: 2020-10-22 10:47:58
*/
SET FOREIGN_KEY_CHECKS=0;
-- ----------------------------
-- Table structure for branch_table
-- ----------------------------
DROP TABLE IF EXISTS `branch_table`;
CREATE TABLE `branch_table` (
`branch_id` bigint(20) NOT NULL,
`xid` varchar(128) COLLATE utf8_bin NOT NULL,
`transaction_id` bigint(20) DEFAULT NULL,
`resource_group_id` varchar(32) COLLATE utf8_bin DEFAULT NULL,
`resource_id` varchar(256) COLLATE utf8_bin DEFAULT NULL,
`lock_key` varchar(128) COLLATE utf8_bin DEFAULT NULL,
`branch_type` varchar(8) COLLATE utf8_bin DEFAULT NULL,
`status` tinyint(4) DEFAULT NULL,
`client_id` varchar(64) COLLATE utf8_bin DEFAULT NULL,
`application_data` varchar(2000) COLLATE utf8_bin DEFAULT NULL,
`gmt_create` datetime DEFAULT NULL,
`gmt_modified` datetime DEFAULT NULL,
PRIMARY KEY (`branch_id`),
KEY `idx_xid` (`xid`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_bin;
-- ----------------------------
-- Records of branch_table
-- ----------------------------
-- ----------------------------
-- Table structure for global_table
-- ----------------------------
DROP TABLE IF EXISTS `global_table`;
CREATE TABLE `global_table` (
`xid` varchar(128) COLLATE utf8_bin NOT NULL,
`transaction_id` bigint(20) DEFAULT NULL,
`status` tinyint(4) NOT NULL,
`application_id` varchar(32) COLLATE utf8_bin DEFAULT NULL,
`transaction_service_group` varchar(32) COLLATE utf8_bin DEFAULT NULL,
`transaction_name` varchar(128) COLLATE utf8_bin DEFAULT NULL,
`timeout` int(11) DEFAULT NULL,
`begin_time` bigint(20) DEFAULT NULL,
`application_data` varchar(2000) COLLATE utf8_bin 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 COLLATE=utf8_bin;
-- ----------------------------
-- Records of global_table
-- ----------------------------
-- ----------------------------
-- Table structure for lock_table
-- ----------------------------
DROP TABLE IF EXISTS `lock_table`;
CREATE TABLE `lock_table` (
`row_key` varchar(128) COLLATE utf8_bin NOT NULL,
`xid` varchar(96) COLLATE utf8_bin DEFAULT NULL,
`transaction_id` mediumtext COLLATE utf8_bin,
`branch_id` mediumtext COLLATE utf8_bin,
`resource_id` varchar(256) COLLATE utf8_bin DEFAULT NULL,
`table_name` varchar(32) COLLATE utf8_bin DEFAULT NULL,
`pk` varchar(36) COLLATE utf8_bin DEFAULT NULL,
`gmt_create` datetime DEFAULT NULL,
`gmt_modified` datetime DEFAULT NULL,
PRIMARY KEY (`row_key`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_bin;
-- ----------------------------
-- Records of lock_table
-- ----------------------------
3、在微服务的各个库中建立日志表
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 DEFAULT CHARSET=utf8;
4、解压seata,修改配置文件,将seata配置推送到nacos
1、解压seata 0.9,修改nacos-config.txt文件,因为seata 1.2 没有nacos的推送,所以需要下载0.9,我也不知道在1.2中还需不需要推送,没有试过,感兴趣的兄弟可以试一下
2、修改conf目录下的nacos-config.txt文件
3、启动 nacos-config.sh ,将配置信息推送到nacos上,使用 git 的命令框启动
运行完成后检查nacos是否多了这些配置
5、解压 seata1.2,修改配置文件
1、修改conf目录下的 file.conf文件
2、修改 conf目录下的 registry.conf 文件
3、将修改完成的 seata1.2 放到服务器中启动
进入/seata/bin/
运行 seata-server.bat 文件
nohup sh seata-server.bat -p 8091 -h 127.0.0.1 2>&1&
执行完成后进入 nacos 查看服务是否启动完成
6、服务中整合seata
1、添加依赖
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-seata</artifactId>
<version>2.1.3.RELEASE</version>
<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>1.2.0</version>
<exclusions>
<exclusion>
<groupId>io.seata</groupId>
<artifactId>seata-all</artifactId>
</exclusion>
</exclusions>
</dependency>
<!-- https://mvnrepository.com/artifact/io.seata/seata-all -->
<dependency>
<groupId>io.seata</groupId>
<artifactId>seata-all</artifactId>
<version>1.2.0</version>
</dependency>
2、将刚开始修改的 file.conf、registry.conf 文件添加到项目中,每一个项目都需要添加
3、配置seata事务组,这是seata事务组名称,在一开始向nacos推送配置的时候见过,使用的是默认的事务组名称
4、配置seata连接地址,配置seata连接的地址,马赛克地方就是服务器ip
7、启动服务,测试seata是否整合成功
@GlobalTransactional(rollbackFor = Exception.class) 在需要的方法上添加事务
调用 task 服务,故意在下面抛出异常,测试是否成功
task模块的代码很简单,就是像数据库插入一条数据
代码抛出异常,检查表中是否有新数据进入
没有新数据进入
删除 int i = 1/0;重启,再次测试是否有新数据进入
执行后查看确实有新数据进来了,自此,整合完毕