上一节讲述了Sentinel的使用。本节主要讲下Seata类中介绍以及安装说明。
Seata是什么?
Seata 是一款开源的分布式事务解决方案,致力于提供高性能和简单易用的分布式事务服务。Seata 将为用户提供了 AT、TCC、SAGA 和 XA 事务模式,为用户打造一站式的分布式解决方案。
详细关于seata观念、分布式事务模式以及工作原理请查看官方相关问题。
Seata 是什么Seata 是一款开源的分布式事务解决方案,致力于提供高性能和简单易用的分布式事务服务。Seata 将为用户提供了 AT、TCC、SAGA 和 XA 事务模式,为用户打造一站式的分布式解决方案。 https://seata.io/zh-cn/docs/overview/what-is-seata.html
seata安装
这里以1.3.0版本为例介绍相关安装,这边以使用nacos作为配置中心和注册中心,下载发布包,解压。更改conf目录下registry.conf文件。
registry {
# file 、nacos 、eureka、redis、zk、consul、etcd3、sofa
type = "nacos"
nacos {
application = "seata-server"
serverAddr = "192.168.43.85:8845,192.168.43.229:8846,192.168.43.251:8847"
group = "SEATA_GROUP"
namespace = "ec6004af-f122-4ed1-b6d6-5d77ea5d5c94"
cluster = "default"
username = "nacos"
password = "nacos"
}
eureka {
serviceUrl = "http://localhost:8761/eureka"
application = "default"
weight = "1"
}
redis {
serverAddr = "localhost:6379"
db = 0
password = ""
cluster = "default"
timeout = 0
}
zk {
cluster = "default"
serverAddr = "127.0.0.1:2181"
sessionTimeout = 6000
connectTimeout = 2000
username = ""
password = ""
}
consul {
cluster = "default"
serverAddr = "127.0.0.1:8500"
}
etcd3 {
cluster = "default"
serverAddr = "http://localhost:2379"
}
sofa {
serverAddr = "127.0.0.1:9603"
application = "default"
region = "DEFAULT_ZONE"
datacenter = "DefaultDataCenter"
cluster = "default"
group = "SEATA_GROUP"
addressWaitTime = "3000"
}
file {
name = "file.conf"
}
}
config {
# file、nacos 、apollo、zk、consul、etcd3
type = "nacos"
nacos {
serverAddr = "192.168.43.85:8845,192.168.43.229:8846,192.168.43.251:8847"
namespace = "ec6004af-f122-4ed1-b6d6-5d77ea5d5c94"
group = "SEATA_GROUP"
username = "nacos"
password = "nacos"
}
consul {
serverAddr = "127.0.0.1:8500"
}
apollo {
appId = "seata-server"
apolloMeta = "http://192.168.1.204:8801"
namespace = "application"
}
zk {
serverAddr = "127.0.0.1:2181"
sessionTimeout = 6000
connectTimeout = 2000
username = ""
password = ""
}
etcd3 {
serverAddr = "http://localhost:2379"
}
file {
name = "file.conf"
}
}
这里在nacos新建一个命名为seata命名空间,registry.nacos和config.nacos节点信息填写正确。
nacos上传配置
这里使用的nacos的配置信息,下一步在nacos中添加seata配置。1.4.2版本,一个dataId上添加所有的配置。1.3.0版本只能手动逐条添加容易出错,官方提供了脚步,下载解压。下载地址
https://github.com/seata/seata/tree/1.3.0
在script/config-center/nacos目录下,找到了nacos-config.sh,执行该脚本。
sh ${SEATAPATH}/script/config-center/nacos/nacos-config.sh -h localhost -p 8848 -g SEATA_GROUP -t 5a3c7d6c-f497-4d68-a71a-2e5e3340b3ca -u username -w password
根据自己情况进行修改。其中5a3c7d6c-f497-4d68-a71a-2e5e3340b3ca是命名空间的值。
执行完成后,nacos中配置如下图。
修改store.mode、store.db.url、store.db.user、store.db.password的dataId值。store.mode改成db,数据库配置根据情况更改,数据库和表初始化下面讲述。
初始化数据库
这里用到的数据库是mysql,创建库名seata,以及seata需要的三张表。
-- 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`)
);
三个表所有字段类型为datetime全部改成timestamp,为啥?
seata官方bug,不这样弄,事务回滚时会报错,序列化异常。最新的1.4.2也没解决这个问题,有兴趣的可以查看https://github.com/seata/seata/issues/3620了解相关问题。
到此关于seata-server相关配置结束了,seata/bin目录下执行nohup ./seata-server.sh -h 172.50.80.21 -p 8091 & 命令启动。
查看nacos服务列表如下
好了,seata-server启动成功,下一节让我们试下如何Java代码使用。