Nacos:1.2.0
Seata:1.2.1
框架 | 版本 |
---|---|
SpringBoot | 2.2.5 |
SpringCloud | Hoxton.SR3 |
SpringCloudAlibaba | 2.2.1 |
Sentine | 1.7.1 |
Nacos | 1.2.1 |
Seata | 1.2.0 |
https://github.com/alibaba/nacos/tree/1.2.0
拉取Nacos项目到本地,打包,启动运行
安装部署启动可以参考这篇文章:Nacos注册中心、配置中心的使用
在启动seata-server
服务以前,需要先把相关的配置先推送到nacos
上
下载Seata-Server
和源码:
https://github.com/seata/seata/releases
解压seata-1.2.0.zip
压缩包 打开源码
新建数据库名为: seata
-- 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`
(
`id` BIGINT(20) NOT NULL AUTO_INCREMENT COMMENT 'increment id',
`branch_id` BIGINT(20) NOT NULL COMMENT 'branch transaction id',
`xid` VARCHAR(100) 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 NOT NULL COMMENT 'create datetime',
`log_modified` DATETIME NOT NULL COMMENT 'modify datetime',
PRIMARY KEY (`id`),
UNIQUE KEY `ux_undo_log` (`xid`, `branch_id`)
) ENGINE = InnoDB
AUTO_INCREMENT = 1
DEFAULT CHARSET = utf8 COMMENT ='AT transaction mode undo table';
-- -------------------------------- 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_gmt_modified_status` (`gmt_modified`, `status`),
KEY `idx_transaction_id` (`transaction_id`)
) ENGINE = InnoDB
DEFAULT CHARSET = utf8;
-- 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 = utf8;
-- the table to store lock data
CREATE TABLE IF NOT EXISTS `lock_table`
(
`row_key` VARCHAR(128) NOT NULL,
`xid` VARCHAR(96),
`transaction_id` BIGINT,
`branch_id` BIGINT NOT NULL,
`resource_id` VARCHAR(256),
`table_name` VARCHAR(32),
`pk` VARCHAR(36),
`gmt_create` DATETIME,
`gmt_modified` DATETIME,
PRIMARY KEY (`row_key`),
KEY `idx_branch_id` (`branch_id`)
) ENGINE = InnoDB
DEFAULT CHARSET = utf8;
配置seata-1.2.0/script/config-center
目录下的config.txt
文件
# 添加服务组
service.vgroupMapping.storage-service-group=default
service.vgroupMapping.order-service-group=default
service.vgroupMapping.account-service-group=default
#修改为db
store.mode=db
# 数据库配置
store.db.url=jdbc:mysql://127.0.0.1:3306/seata?useUnicode=true
store.db.user=root
store.db.password=123456
注意 1
:
此处配置的 service.vgroupMapping.storage-service-group=default
中的storage-service-group
与客户端中的application.yml
中的seata.tx-service-group
属性一致。如:
否则会报no available service 'null' found, please make sure registry config correct
还需注意 2
:
1.0.0及以前的版本,像上面那样配置没错,可是从1.1.0开始,vgroup_mapping
变成 vgroupMapping
运行seata-1.2.0/script/config-center/nacos/nacos-config.sh
目录下脚本
# 后面是nacos的地址,注意先启动 nacos 服务
sh nacos-config.sh localhost
运行后刷新nacos:
seata的配置已经注册到nacos上了。
解压seata-server-1.2.0.zip
后进入conf
目录,修改registry.conf
配置文件
registry {
# file 、nacos 、eureka、redis、zk、consul、etcd3、sofa
type = "nacos"
nacos {
serverAddr = "localhost"
namespace = ""
username = "nacos"
password = "nacos"
}
}
config {
# file、nacos 、apollo、zk、consul、etcd3
type = "nacos"
nacos {
serverAddr = "localhost"
namespace = ""
group = "SEATA_GROUP"
username = "nacos"
password = "nacos"
}
}
进入bin
目录,运行启动脚本:
./seata-server.sh
<properties>
<project.build.sourceEncoding>UTF-8project.build.sourceEncoding>
<maven.compiler.source>1.8maven.compiler.source>
<maven.compiler.target>1.8maven.compiler.target>
<skipTests>trueskipTests>
<seata.version>1.2.0seata.version>
properties>
<dependency>
<groupId>com.alibaba.cloudgroupId>
<artifactId>spring-cloud-alibaba-seataartifactId>
<version>2.1.0.RELEASEversion>
<exclusions>
<exclusion>
<artifactId>seata-allartifactId>
<groupId>io.seatagroupId>
exclusion>
exclusions>
dependency>
<dependency>
<groupId>io.seatagroupId>
<artifactId>seata-allartifactId>
<version>${seata.version}version>
dependency>
registry {
# file 、nacos 、eureka、redis、zk、consul、etcd3、sofa
type = "nacos"
nacos {
serverAddr = "localhost"
namespace = ""
cluster = "default"
}
}
config {
# file、nacos 、apollo、zk、consul、etcd3、springCloudConfig
type = "nacos"
nacos {
serverAddr = "localhost"
namespace = ""
group = "SEATA_GROUP"
}
}
对于事务的处理,最重要的是要拿到数据源,因为通过数据源我们可以控制事务什么时候回滚或提交,所以数据源我们需要让seata
来代理。
事务协调器(TC):维护全局事务和分支事务的状态,驱动全局提交或回滚。
事务管理器(TM):定义全局事务的范围:开始全局事务,提交或回滚全局事务。
资源管理器(RM):管理分支事务正在处理的资源,与TC进行对话以注册分支事务并报告分支事务的状态,并驱动分支事务的提交或回滚。
Seata管理的分布式事务的典型生命周期:
TM要求TC开始一项新的全球交易。TC生成代表全局交易的XID。
XID通过微服务的调用链传播。
RM将本地事务注册为XID到TC的相应全局事务的分支。
TM要求TC提交或回退相应的XID全局事务。
TC驱动XID对应的全局事务下的所有分支事务,以完成分支的提交或回滚。
从日志可以看到 TM
和RM
已经成功连接到了TC
。
分布式事务解决方案-seata-示例