seata-1.4.0如何在springcloud中使用-創(chuàng)新互聯(lián)

seata-1.4.0如何在springcloud中使用?很多新手對(duì)此不是很清楚,為了幫助大家解決這個(gè)難題,下面小編將為大家詳細(xì)講解,有這方面需求的人可以來(lái)學(xué)習(xí)下,希望你能有所收獲。

10多年的叢臺(tái)網(wǎng)站建設(shè)經(jīng)驗(yàn),針對(duì)設(shè)計(jì)、前端、開(kāi)發(fā)、售后、文案、推廣等六對(duì)一服務(wù),響應(yīng)快,48小時(shí)及時(shí)工作處理。成都營(yíng)銷網(wǎng)站建設(shè)的優(yōu)勢(shì)是能夠根據(jù)用戶設(shè)備顯示端的尺寸不同,自動(dòng)調(diào)整叢臺(tái)建站的顯示方式,使網(wǎng)站能夠適用不同顯示終端,在瀏覽器中調(diào)整網(wǎng)站的寬度,無(wú)論在任何一種瀏覽器上瀏覽網(wǎng)站,都能展現(xiàn)優(yōu)雅布局與設(shè)計(jì),從而大程度地提升瀏覽體驗(yàn)。創(chuàng)新互聯(lián)建站從事“叢臺(tái)網(wǎng)站設(shè)計(jì)”,“叢臺(tái)網(wǎng)站推廣”以來(lái),每個(gè)客戶項(xiàng)目都認(rèn)真落實(shí)執(zhí)行。

 1、簡(jiǎn)介


Seata 是一款開(kāi)源的分布式事務(wù)解決方案,致力于提供高性能和簡(jiǎn)單易用的分布式事務(wù)服務(wù)。Seata 將為用戶提供了 AT、TCC、SAGA 和 XA 事務(wù)模式,為用戶打造一站式的分布式解決方案。
詳見(jiàn)官方文檔:https://seata.io/zh-cn/docs/overview/what-is-seata.html
網(wǎng)上的多是0.9.0版本的安裝方式,這里記錄安裝seata-1.4.0版本的方式,在win10環(huán)境下安裝,centos7與此相同。

下載

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

2.1、seata-server-1.4.0

安裝配置

解壓壓縮包,修改conf內(nèi)的配置文件

2.1.1、registry.conf修改:

我使用的nacos作為配置中心和注冊(cè)中心,使用將配置文件改為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修改:

修改數(shù)據(jù)庫(kù)地址,注意mysql5/mysql8驅(qū)動(dòng)不同

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/h3/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、創(chuàng)建數(shù)據(jù)庫(kù)

創(chuàng)建數(shù)據(jù)庫(kù)seata,并建立下面三張表(branch_table, global_table, lock_table),創(chuàng)建undo_log表放到業(yè)務(wù)庫(kù)中
-- 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`)
);
 
-- 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.
-- 此腳本必須初始化在你當(dāng)前的業(yè)務(wù)數(shù)據(jù)庫(kù)中,用于AT 模式XID記錄。與server端無(wú)關(guān)(注:業(yè)務(wù)數(shù)據(jù)庫(kù))
-- 注意此處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、啟動(dòng)

seata-1.4\bin目錄下:雙擊 seata-server.bat
nacos中查看服務(wù)是否注冊(cè)成功

seata-1.4.0如何在springcloud中使用

2.2 seata-1.4.0.

配置

解壓安裝包,并修改配置

2.2.3 修改config.txt

路徑:script\config-center
修改service.vgroupMapping和數(shù)據(jù)庫(kù)地址
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 與項(xiàng)目模塊中yml配置的seata.tx-service-group內(nèi)容相同

seata-1.4.0如何在springcloud中使用

2.2.3 啟動(dòng)nacos-config.sh,

將配置注入到nacos中

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

seata-1.4.0如何在springcloud中使用

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

seata-1.4.0如何在springcloud中使用

返回成功

2.2.3、nacos中查看配置

在nacos配置列表中找到config.txt配置的vgroupMapping,即說(shuō)明注入成功
![在這里插入圖片描述](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啟動(dòng)類上添加:
@EnableAutoDataSourceProxy
service上添加:
@GlobalTransactional(rollbackFor = Exception.class)

3.4、啟動(dòng)服務(wù):

控制臺(tái)輸出:

seata-1.4.0如何在springcloud中使用

3.5、測(cè)試

調(diào)用方控制臺(tái)打印

seata-1.4.0如何在springcloud中使用

被調(diào)用方異??刂婆_(tái)打印:

seata-1.4.0如何在springcloud中使用

看完上述內(nèi)容是否對(duì)您有幫助呢?如果還想對(duì)相關(guān)知識(shí)有進(jìn)一步的了解或閱讀更多相關(guān)文章,請(qǐng)關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道,感謝您對(duì)創(chuàng)新互聯(lián)網(wǎng)站建設(shè)公司,的支持。

本文標(biāo)題:seata-1.4.0如何在springcloud中使用-創(chuàng)新互聯(lián)
分享鏈接:http://muchs.cn/article2/dspcoc.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供全網(wǎng)營(yíng)銷推廣、品牌網(wǎng)站設(shè)計(jì)、微信公眾號(hào)、用戶體驗(yàn)軟件開(kāi)發(fā)、自適應(yīng)網(wǎng)站

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來(lái)源: 創(chuàng)新互聯(lián)

微信小程序開(kāi)發(fā)