本文版本使用:seata 1.4.0、nacos 1.3.2、springboot 2.1.1、mysql 5.7,其中springboot、nacos和mysql是安装和使用网上一搜便是,不再赘述。
目录
一、创建数据库
1.创建数据库:seata(utf8)
2.创建表:
-- -------------------------------- 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;
二、将配置导入到nacos配置中心
1.到https://github.com/seata/seata/tree/develop/script/config-center下载配置:
- /config.txt
- /nacos/nacos-config.sh
2.将两个文件放到同一目录
3.修改config.txt配置:
server端参数配置说明:https://seata.io/zh-cn/docs/user/configurations.html
3.1.配置事务会话信息存储方式为db,并配置mysql连接信息
- store.mode=db
- store.db.url=jdbc:mysql://127.0.0.1:3306/seata?useUnicode=true
- store.db.user=test
- store.db.password=test.123
3.2.新增集成seata的client的service.vgroupMapping(有几个业务服务就增加几个mapping,请记号这几个红字,要与client端的配置一一对应的)
- (新增一行)service.vgroupMapping.demo-web-seata-service-group=default
- (新增一行)service.vgroupMapping.demo-goods-seata-service-group=default
4.上nacos创建新的命名空间:seata-server,并存好命名空间id:c38bf03a-2ee2-4ee6-834a-2db99dbec528;作为seata-server的配置中心
5.执行导入脚本:
sh nacos-config.sh -h 127.0.0.1 -p 8848 -g SEATA_GROUP -u nacos -w nacos -t c38bf03a-2ee2-4ee6-834a-2db99dbec528
参数说明:
-h nacos地址
-p nacos端口,默认 8848
-g nacos配置组
-t naocos的命名空间id
-u -w 登录nacos的账号、密码
6.检查nacos上新命名空间seata-server下,新增了起码八十个配置项,则继续往下。
三、安装seata服务端启动包
1.在服务器上下载seata服务端
# 到某个目录下
cd /opt
# 下载server压缩包
wget https://github.com/seata/seata/releases/download/v1.4.0/seata-server-1.4.0.zip
# 解压
unzip seata-server-1.4.0.zip
# 进入seata的配置conf目录
cd /opt/seata/conf
# 重命名file.conf备份
mv file.conf file.conf.bak
2.修改配置registry.conf
vim /opt/seata/conf/registry.conf
# 1.将registry下的type改为nacos
# 2.改registry下的nacos的配置
nacos {
application = "seata-server"
serverAddr = "127.0.0.1:8848"
group = "SEATA_GROUP"
namespace = "c38bf03a-2ee2-4ee6-834a-2db99dbec528"
cluster = "default"
}
# 3.将config下的type改为nacos
# 4.改config下的nacos的配置
nacos {
serverAddr = "127.0.0.1:8848"
namespace = "c38bf03a-2ee2-4ee6-834a-2db99dbec528"
group = "SEATA_GROUP"
}
3.到seata根目录启动服务
sh bin/seata-server.sh
4.查看日志是否启动成功
5.检查nacos服务列表已有新的服务“seata-server”,则服务端ok
其他:
1.生产环境部署时,一般都以集群模式部署,具体请看官网文档啦(如有需要的朋友,可以回复一下,我再抽空整理一下)。
官方部署指南:http://seata.io/zh-cn/docs/ops/deploy-guide-beginner.html
官方部署脚本:https://github.com/seata/seata/tree/1.4.0/script
参考博客:https://blog.youkuaiyun.com/calonmo/article/details/106630754