Seata官方文档地址
Seata安装包下载地址
依赖版本说明地址
当前使用Spring-cloud-alibaba对应的seata版本为v1.3.0,所以需要下载v1.3.0版本安装包
/usr/local/lib/java
tar -zxvf seata-server-1.3.0.tar.gz
mv seata /usr/local/seata
在Seata的架构中,一共有三个角色:
维护全局和分支事务的状态,驱动全局事务提交和回滚
定义全局事务的范围:开始全局事务、提交或回滚全局事务
管理分支事务处理的资源,与TC交谈以注册分支事务和报告分支事务的状态,并驱动分支事务提交或回滚。
其中,TC为单独部署的server服务器,TM和RM为嵌入到应用中的Client客户端
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-uQo35f82-1665807994566)(https://note.youdao.com/yws/api/personal/file/D697E953ACEC40CB8622B61907673B21?method=download&shareKey=bf89b6bb0ec172008c89ed2c9a80a688)]
cd /seata/conf
vi file.conf
将mode="file" 修改为mode="db"
修改配置文件中db对应的连接池信息
# 注意这里需要新建一个seata的数据库
driverClassName = "com.mysql.cj.jdbc.Driver"
url = "jdbc:mysql://ip:3306/seata"
user = "xx"
password = "xx"
选择对应的数据库脚本文件,这里使用mysql
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`),
KEY `idx_xid_and_branch_id` (`xid` , `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 ('AsyncCommitting', ' ', 0);
INSERT INTO `distributed_lock` (lock_key, lock_value, expire) VALUES ('RetryCommitting', ' ', 0);
INSERT INTO `distributed_lock` (lock_key, lock_value, expire) VALUES ('RetryRollbacking', ' ', 0);
INSERT INTO `distributed_lock` (lock_key, lock_value, expire) VALUES ('TxTimeoutCheck', ' ', 0);
配置Nacos注册中心,负责事务参与者(微服务)和TC通讯。将Seata Server注册到Nacos,修改conf目录下的registry.conf配置
cd /seata/conf
vi registry.conf
# 修改注册方式
type="file" 修改为 type="nacos"
# 修改注册中心nacos配置(registry)
nacos {
application = "seata-server"
# nacos服务地址
serverAddr = "xx:xx:xx:xx:port"
# 可以设置负载均衡策略,默认轮询
# loadBalance = "RandomLoadBalance"
# loadBalanceVirtualNodes = 10
group = "SEATA_GROUP"
namespace = ""
cluster = "default"
# 用户名密码
username = "xxxx"
password = "xxxx"
}
# 修改配置中心方式
type="file" 修改为 type="nacos"
# 修改配置中心nacos配置
nacos {
# nacos服务地址
serverAddr = "xx:xx:xx:xx:port"
namespace = ""
group = "SEATA_GROUP"
username = "xxxx"
password = "xxxx"
}
tar -zxvf seata-1.3.0.tar.gz
mv seata-1.3.0 /usr/local/seata-1.3.0
cp -r /usr/local/seata-1.3.0/script/ /usr/local/seata/
cd /usr/local/seata/script/config-center
vi config.txt
# 修改模式为db,修改数据库链接配置
store.mode=db
store.db.datasource=druid
store.db.dbType=mysql
store.db.driverClassName=com.mysql.cj.jdbc.Driver
store.db.url=jdbc:mysql://ip:3306/seata?useUnicode=true
store.db.user=xx
store.db.password=xx
service.vgroupMapping.my_test_tx_group=default
my_test_tx_group需要与客户保持一致,default需要跟客户端和registry.conf中registry中的cluster保持一致
(客户端properties配置:spring.cloud.alibaba.seata.tx-service-group=my_test_tx_group)
事务分组:解决异地机房、机房断电等容错问题
my_test_tx_group可以i自定义,比如(beijing、shanghai),对应的client也要去设置
seata.service.vgroup-mapping.projectA=beijing
shell:
sh ${SEATAPATH}/script/config-center/nacos/nacos-config.sh -h 47.92.158.60 -p 8854 -g SEATA_GROUP -t xxxxxxxxx -u nacos -w nacos
参数说明:
-h:host,默认值localhost
-p:port,默认8848
-g:配置分组,默认值为‘SEATA_GROUP’
-t:租户信息,对应Nacos的命名空间ID字段,默认值为空‘’
-u:用户名
-w:密码
手动修改sh脚本文件配置:
cd /usr/local/seata/script/config-center/nacos
vi nacos-config.sh
# 修改配置
if [[ -z ${host} ]]; then
host=xx.xx.xx.xx
fi
if [[ -z ${port} ]]; then
port=xxxx
fi
if [[ -z ${group} ]]; then
group="SEATA_GROUP"
fi
if [[ -z ${tenant} ]]; then
tenant=""
fi
if [[ -z ${username} ]]; then
username="nacos"
fi
if [[ -z ${password} ]]; then
password="xxxx"
fi
# 如果sh脚本没有执行权限,先增加权限
chmod +x nacos-config.sh
# 两种方式运行
1、配置文件已修改
./nacos-config.sh
2、 配置文件无修改
sh ${SEATAPATH}/script/config-center/nacos/nacos-config.sh -h 47.92.158.60 -p 8854 -g SEATA_GROUP -t xxxxxxxxx -u nacos -w nacos
cd /usr/local/seata/bin
./seata-server.sh
启动参数:
参数 | 全写 | 作用 | 备注 |
---|---|---|---|
-h | –host | 指定在注册中心注册的IP | 不指定时获取当前的IP,外部访问部署在云环境和容器中的server建议指定 |
-p | –port | 指定server启动的端口 | 默认为8091 |
-m | –storeMode | 事务日志存储方式 | 支持file,db,redis,默认为file,注:redis需seata-server1.3版本以上 |
-n | –serverNode | 用于指定seata-server节点ID | 如1,2,3…默认为1 |
-3 | –seataEnv | 指定seata-server运行环境 | 如dev,test等,服务启动时会使用registry-dev.conf这样的配置 |
# 举例:指定端口启动
./seata-server.sh -p 8091
# 如果部署集群,指定端口和节点启动
./seata-server.sh -p 8091 -n 1
./seata-server.sh -p 8092 -n 2
./seata-server.sh -p 8093 -n 3