1. 程式人生 > 程式設計 >seata-1.4.0安裝及在springcloud中使用詳解

seata-1.4.0安裝及在springcloud中使用詳解

seata-1.4.0安裝及使用

1、簡介

Seata 是一款開源的分散式事務解決方案,致力於提供高效能和簡單易用的分散式事務服務。Seata 將為使用者提供了 AT、TCC、SAGA 和 XA 事務模式,為使用者打造一站式的分散式解決方案。
詳見官方文件:https://seata.io/zh-cn/docs/overview/what-is-seata.html
網上的多是0.9.0版本的安裝方式,這裡記錄安裝seata-1.4.0版本的方式,在win10環境下安裝,centos7與此相同。

下載

需要下載seata-1.4.0.zip 和 seata-server-1.4.0.zip兩個安裝包
下載地址:https://github.com/seata/seata/releases

2.1、seata-server-1.4.0

安裝配置

解壓壓縮包,修改conf內的配置檔案

2.1.1、registry.conf修改:

我使用的nacos作為配置中心和註冊中心,使用將配置檔案改為nacos

registry {
# file 、nacos 、eureka、redis、zk、consul、etcd3、sofa
 type = "nacos"
 loadBalance = "RandomLoadBalance"
 loadBalanceVirtualNodes = 10

 nacos {
 application = "seata-server"
 serverAddr = "127.0.0.1:8848"
 group = "SEATA_GROUP"
 namespace = ""
 cluster = "default"
 username = "nacos"
 password = "nacos"
 }
}

config {
 # file、nacos 、apollo、zk、consul、etcd3
 type = "nacos"
 nacos {
 serverAddr = "127.0.0.1:8848"
 namespace = ""
 group = "SEATA_GROUP"
 username = "nacos"
 password = "nacos"
 }
}

2.1.2、file.conf修改:

修改資料庫地址,注意mysql5/mysql8驅動不同

store {
 ## store mode: file、db、redis
 mode = "db"
	## database store property
	 db {
	 ## the implement of javax.sql.DataSource,such as DruidDataSource(druid)/BasicDataSource(dbcp)/HikariDataSource(hikari) etc.
	 datasource = "druid"
	 ## mysql/oracle/postgresql/h2/oceanbase etc.
	 dbType = "mysql"
	 driverClassName = "com.mysql.cj.jdbc.Driver"
	 url = "jdbc:mysql://127.0.0.1:3307/seata?serverTimezone=Asia/Shanghai&useUnicode=true&characterEncoding=utf8&useSSL=true"
	 user = "root"
	 password = "123456"
	 minConn = 5
	 maxConn = 100
	 globalTable = "global_table"
	 branchTable = "branch_table"
	 lockTable = "lock_table"
	 queryLimit = 100
	 maxWait = 5000
	 }
	}

2.1.3、建立資料庫

	建立資料庫seata,並建立下面三張表(branch_table,global_table,lock_table),建立undo_log表放到業務庫中
-- 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,`resource_group_id` varchar(32),`resource_id` varchar(256),`lock_key` varchar(128),`branch_type` varchar(8),`status` tinyint,`client_id` varchar(64),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,`table_name` varchar(32),`pk` varchar(36),primary key(`row_key`)
);
 
-- 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;

2.1.4、啟動

seata-1.4\bin目錄下:雙擊 seata-server.bat
nacos中檢視服務是否註冊成功

在這裡插入圖片描述

2.2 seata-1.4.0.

配置

解壓安裝包,並修改配置

2.2.3 修改config.txt

路徑:script\config-center
修改service.vgroupMapping和資料庫地址
service.vgroupMapping.system-server-group=default 
service.vgroupMapping.product-server-group=default
store.mode=db
store.db.datasource=druid
store.db.dbType=mysql
store.db.driverClassName=com.mysql.cj.jdbc.Driver
store.db.url=jdbc:mysql://192.168.111.130:3307/seata?serverTimezone=Asia/Shanghai&useUnicode=true&characterEncoding=utf8&useSSL=true
store.db.user=root
store.db.password=123456
store.db.minConn=5
store.db.maxConn=30
store.db.globalTable=global_table
store.db.branchTable=branch_table
store.db.queryLimit=100
store.db.lockTable=lock_table
store.db.maxWait=5000

注:
service.vgroupMapping.system-server-group=default 
service.vgroupMapping.product-server-group=default
 #system-server-group、product-server-group 與專案模組中yml配置的seata.tx-service-group內容相同

在這裡插入圖片描述

2.2.3 啟動nacos-config.sh,

將配置注入到nacos中

1、在seata-server-1.4.0\script\config-center\nacos目錄下,右鍵 Git Bash Here

在這裡插入圖片描述

2、輸入命令:
sh nacos-config.sh -h 127.0.0.1 -p 8848 -g SEATA_GROUP -u nacos -w nacos

在這裡插入圖片描述

返回成功

2.2.3、nacos中檢視配置

在nacos配置列表中找到config.txt配置的vgroupMapping,即說明注入成功
![在這裡插入圖片描述](https://img-blog.csdnimg.cn/20201214150808987.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3dlaXhpbl8zNzY5ODMzNg==,size_16,color_FFFFFF,t_70#pic_center

3、SpringCloud 配置

3.1、pom新增依賴

<dependency>
 <groupId>com.alibaba.cloud</groupId>
 <artifactId>spring-cloud-starter-alibaba-seata</artifactId>
 <exclusions>
  <exclusion>
  <groupId>io.seata</groupId>
  <artifactId>seata-spring-boot-starter</artifactId>
  </exclusion>
 </exclusions>
 </dependency>
 <dependency>
 <groupId>io.seata</groupId>
 <artifactId>seata-spring-boot-starter</artifactId>
 <version>1.4.0</version>
 </dependency>
注:seata-spring-boot-starter的版本與安裝版本一致

3.2、修改yml配置檔案

seata:
 enabled: true
 application-id: product-server
 tx-service-group: product-server-group
 enable-auto-data-source-proxy: true
 config:
 type: nacos
 nacos:
 namespace:
 server-addr: 127.0.0.1:8848
 group: SEATA_GROUP
 userName: "nacos"
 password: "nacos"
 registry:
 type: nacos
 nacos:
 application: seata-server
 server-addr: 127.0.0.1:8848
 namespace:
 userName: "nacos"
 password: "nacos"

3.3、程式碼上添加註解:

application啟動類上新增:
@EnableAutoDataSourceProxy
service上新增:
@GlobalTransactional(rollbackFor = Exception.class)

3.4、啟動服務:

控制檯輸出:

在這裡插入圖片描述

3.5、測試

呼叫方控制檯列印

在這裡插入圖片描述

被呼叫方異常控制檯列印:

在這裡插入圖片描述

到此這篇關於seata-1.4.0安裝及在springcloud中使用的文章就介紹到這了,更多相關springcloud seata安裝使用內容請搜尋我們以前的文章或繼續瀏覽下面的相關文章希望大家以後多多支援我們!