上一節講述了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代碼使用。