下载nacos
https://github.com/alibaba/nacos/tags
https://github.com/alibaba/nacos/releases/tag/1.4.0
下载seata
https://github.com/seata/seata/releases/tag/v1.4.0
注意:最好使用相同版本,不然出问题会找很久的
创建seata数据库
1.创建数据库导入表
导入数据库表
-- the table to store GlobalSession data
drop table if exists `global_table`;
create table `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`)
);
-- the table to store BranchSession data
drop table if exists `branch_table`;
create table `branch_table` (
`branch_id` bigint not null,
`xid` varchar(128) not null,
`transaction_id` bigint ,
`resource_group_id` varchar(32),
`resource_id` varchar(256) ,
`lock_key` varchar(128) ,
`branch_type` varchar(8) ,
`status` tinyint,
`client_id` varchar(64),
`application_data` varchar(2000),
`gmt_create` datetime,
`gmt_modified` datetime,
primary key (`branch_id`),
key `idx_xid` (`xid`)
);
-- the table to store lock data
drop table if exists `lock_table`;
create table `lock_table` (
`row_key` varchar(128) not null,
`xid` varchar(96),
`transaction_id` long ,
`branch_id` long,
`resource_id` varchar(256) ,
`table_name` varchar(32) ,
`pk` varchar(36) ,
`gmt_create` datetime ,
`gmt_modified` datetime,
primary key(`row_key`)
);
2.业务数据库执行表
-- the table to store seata xid data
-- 0.7.0+ add context
-- you must to init this sql for you business databese. the seata server not need it.
-- 此脚本必须初始化在你当前的业务数据库中,用于AT 模式XID记录。与server端无关(注:业务数据库)
-- 注意此处0.3.0+ 增加唯一索引 ux_undo_log
-- drop table `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=1 DEFAULT CHARSET=utf8;
按seata步骤
1.修改seata配置file.conf
2.修改seata配置registry.conf
3.将nacos-config.sh脚本复制到seata/conf下
在conf文件夹内,需要nacos-config.sh文件,这个文件1.4.0版本是没有的。下面有获取地址。
下载nacos-config.sh文件
4.将config.txt文件复制到seata目录下
修改文件config.txt
config.txt下载
5.在nacos创建匿名空间
6.打开git bash,进入
输入执行命令:
sh nacos-config.sh -h localhost -p 8848 -g SEATA_GROUP -t 6bae1385-3391-467c-896b-463a467d4749 -u nacos -w nacos
注:命令解析:-h -p 指定nacos的端口地址;-g 指定配置的分组,注意,是配置的分组;-t 指定命名空间id; -u -w指定nacos的用户名和密码,同样,这里开启了nacos注册和配置认证的才需要指定。
开始执行
执行成功效果
7.回到nacos控制查看配置管理
8.启动seata服务
进入seata/bin文件下,点击seata-server.bat可以启动
启动成功效果
在nacos查看seata是否注册成功
至此seata服务端搭建完成
客户端代码搭建
1.分布式项目结构
1.用户权限系统管理模块pom.xml文件引入如下依赖
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-seata</artifactId>
</dependency>
2.修改配置文件bootstrap.yml
修改内容:
seata:
enabled: true
enable-auto-data-source-proxy: true
tx-service-group: my_test_tx_group
registry:
type: nacos
nacos:
application: seata-server
server-addr: 127.0.0.1:8848
username: nacos
password: nacos
config:
type: nacos
nacos:
server-addr: 127.0.0.1:8848
group: SEATA_GROUP
username: nacos
password: nacos
namespace: 6bae1385-3391-467c-896b-463a467d4749
service:
vgroup-mapping:
my_test_tx_group: default
disable-global-transaction: false
client:
rm:
report-success-enable: false
最终修改:
server:
port: 8001
servlet:
context-path: /system
spring:
application:
name: system-server # 当前服务的应用名,与nacos中的dataid的前缀匹配
cloud:
nacos:
discovery:
server-addr: 127.0.0.1:8848 # 注册中心地址 nacos server
config:
server-addr: 127.0.0.1:8848 # 配置中心地址 nacos server
file-extension: yml # 配置中心的配置后缀
profiles:
active: dev # 指定环境为开发环境,即读取 system-server-dev.yml
seata:
enabled: true
enable-auto-data-source-proxy: true
tx-service-group: my_test_tx_group
registry:
type: nacos
nacos:
application: seata-server
server-addr: 127.0.0.1:8848
username: nacos
password: nacos
config:
type: nacos
nacos:
server-addr: 127.0.0.1:8848
group: SEATA_GROUP
username: nacos
password: nacos
namespace: 6bae1385-3391-467c-896b-463a467d4749
service:
vgroup-mapping:
my_test_tx_group: default
disable-global-transaction: false
client:
rm:
report-success-enable: false