分布式事務(wù)解決方案--seata AT模式(spring cloud eureka+fegin+seata)

一.整體搭建準(zhǔn)備

? ?一.1.下載seata-server服務(wù)端(地址:https://seata.io/zh-cn/blog/download.html

? ? 一.2. 修改seata-serve里面的配置file.conf文件,file.conf.example文件耳鸯,以及registry.conf文件

? ? 一.3. 從githhub下載好準(zhǔn)備好的demo,然后配更改好demo里的一些配置文件(改成自己本地的即可)

? ? ? ? ? ? demo地址(https://github.com/chengshugithub/springcloud-eureka-feign-seata)

? ? 一.4. 運(yùn)行 eureka的服務(wù)端撒强,然后運(yùn)行seata-server锌雀,然后繼續(xù)運(yùn)行我們demo里面的三個(gè)服務(wù)蹂随;

二.先通過官網(wǎng)下載 seata-server(地址:https://seata.io/zh-cn/blog/download.html

? ?二.1.下載之后琉预,我們進(jìn)行解壓然后找到如下圖三個(gè)文件進(jìn)行修改


?我的file.conf文件配置

這里我采用的是db方式劝评,連接是mysql;自己改成自己本機(jī)的mysql的用戶名密碼和驅(qū)動(dòng)

## transaction log store, only used in seata-server

store {

? ## store mode: file倦淀、db蒋畜、redis

?#這里的話選擇是db,級(jí)尋找下面的db配置撞叽,這里我用的是mysql姻成;

? mode = "db"

? ## file store property

? file {

? ? ## store location dir

? ? dir = "sessionStore"

? ? # branch session size , if exceeded first try compress lockkey, still exceeded throws exceptions

? ? maxBranchSessionSize = 16384

? ? # globe session size , if exceeded throws exceptions

? ? maxGlobalSessionSize = 512

? ? # file buffer size , if exceeded allocate new buffer

? ? fileWriteBufferCacheSize = 16384

? ? # when recover batch read size

? ? sessionReloadReadSize = 100

? ? # async, sync

? ? flushDiskMode = async

? }

? ## database store property

? db {

? ? ## the implement of javax.sql.DataSource, such as DruidDataSource(druid)/BasicDataSource(dbcp)/HikariDataSource(hikari) etc.

? ? datasource = "dbcp"

? ? ## mysql/oracle/postgresql/h2/oceanbase etc.

? ? dbType = "mysql"

? ? driverClassName = "com.mysql.cj.jdbc.Driver"

? ? url = "jdbc:mysql://127.0.0.1:3306/seata-server?serverTimezone=GMT%2B8"

? ? user = "root"

? ? password = "root"

? ? minConn = 5

? ? maxConn = 100

? ? globalTable = "global_table"

? ? branchTable = "branch_table"

? ? lockTable = "lock_table"

? ? queryLimit = 100

? ? maxWait = 5000

? }

? ## redis store property

? redis {

? ? host = "127.0.0.1"

? ? port = "6379"

? ? password = ""

? ? database = "0"

? ? minConn = 1

? ? maxConn = 10

? ? maxTotal = 100

? ? queryLimit = 100

? }

}

## metrics configuration, only used in server side

metrics {

? enabled = false

? registryType = "compact"

? # multi exporters use comma divided

? exporterList = "prometheus"

? exporterPrometheusPort = 9898

}

? 我的file.conf.example文件配置

? ?需要注意的是 service里面的配置? vgroupMapping.my_test_tx_group = "default";

? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?default.grouplist = "127.0.0.1:8091"

transport {

? # tcp udt unix-domain-socket

? type = "TCP"

? #NIO NATIVE

? server = "NIO"

? #enable heartbeat

? heartbeat = true

? # the client batch send request enable

? enableClientBatchSendRequest = true

? #thread factory for netty

? threadFactory {

? ? bossThreadPrefix = "NettyBoss"

? ? workerThreadPrefix = "NettyServerNIOWorker"

? ? serverExecutorThread-prefix = "NettyServerBizHandler"

? ? shareBossWorker = false

? ? clientSelectorThreadPrefix = "NettyClientSelector"

? ? clientSelectorThreadSize = 1

? ? clientWorkerThreadPrefix = "NettyClientWorkerThread"

? ? # netty boss thread size,will not be used for UDT

? ? bossThreadSize = 1

? ? #auto default pin or 8

? ? workerThreadSize = "default"

? }

? shutdown {

? ? # when destroy server, wait seconds

? ? wait = 3

? }

? serialization = "seata"

? compressor = "none"

}

service {

? #transaction service group mapping

? vgroupMapping.my_test_tx_group = "default"

? #only support when registry.type=file, please don't set multiple addresses

? default.grouplist = "127.0.0.1:8091"

? #degrade, current not support

? enableDegrade = false

? #disable seata

? disableGlobalTransaction = false

}

client {

? rm {

? ? asyncCommitBufferLimit = 10000

? ? lock {

? ? ? retryInterval = 10

? ? ? retryTimes = 30

? ? ? retryPolicyBranchRollbackOnConflict = true

? ? }

? ? reportRetryCount = 5

? ? tableMetaCheckEnable = false

? ? reportSuccessEnable = false

? }

? tm {

? ? commitRetryCount = 5

? ? rollbackRetryCount = 5

? }

? undo {

? ? dataValidation = true

? ? logSerialization = "jackson"

? ? logTable = "undo_log"

? }

? log {

? ? exceptionRate = 100

? }

}


?我的registry.conf文件配置

這個(gè)注冊(cè)的話愿棋,采用的是eureka的配置科展;這里會(huì)把seata服務(wù)注冊(cè)到eureka;

registry {

? # file 糠雨、nacos 才睹、eureka、redis甘邀、zk琅攘、consul、etcd3松邪、sofa

? type = "eureka"

? loadBalance = "RandomLoadBalance"

? loadBalanceVirtualNodes = 10

? nacos {

? ? application = "seata-server"

? ? serverAddr = "127.0.0.1:8848"

? ? group = "SEATA_GROUP"

? ? namespace = ""

? ? cluster = "default"

? ? username = ""

? ? password = ""

? }

? 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 = "file"

? nacos {

? ? serverAddr = "127.0.0.1:8848"

? ? namespace = ""

? ? group = "SEATA_GROUP"

? ? username = ""

? ? password = ""

? }

? consul {

? ? serverAddr = "127.0.0.1:8500"

? }

? apollo {

? ? appId = "seata-server"

? ? apolloMeta = "http://192.168.1.204:8801"

? ? namespace = "application"

? ? apolloAccesskeySecret = ""

? }

? zk {

? ? serverAddr = "127.0.0.1:2181"

? ? sessionTimeout = 6000

? ? connectTimeout = 2000

? ? username = ""

? ? password = ""

? }

? etcd3 {

? ? serverAddr = "http://localhost:2379"

? }

? file {

? ? name = "file.conf"

? }

}

二.2.接下來創(chuàng)建seata所需要的的數(shù)據(jù)庫(kù)和表;

根據(jù) file.conf文件里面的db連接的數(shù)據(jù)庫(kù)名荧关,當(dāng)然自己可以隨便改名溉奕;

我們需要?jiǎng)?chuàng)建三個(gè)表,這個(gè)表是給seata-server用的羞酗;

需要以下三張表腐宋;建表sql如下;

CREATE TABLE `branch_table` (

? `branch_id` bigint(20) NOT NULL,

? `xid` varchar(128) CHARACTER SET utf8 COLLATE utf8_general_ci NOT NULL,

? `transaction_id` bigint(20) NULL DEFAULT NULL,

? `resource_group_id` varchar(32) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL,

? `resource_id` varchar(256) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL,

? `branch_type` varchar(8) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL,

? `status` tinyint(4) NULL DEFAULT NULL,

? `client_id` varchar(64) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL,

? `application_data` varchar(2000) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL,

? `gmt_create` datetime(6) NULL DEFAULT NULL,

? `gmt_modified` datetime(6) NULL DEFAULT NULL,

? PRIMARY KEY (`branch_id`) USING BTREE,

? INDEX `idx_xid`(`xid`) USING BTREE

) ENGINE = InnoDB CHARACTER SET = utf8 COLLATE = utf8_general_ci ROW_FORMAT = Dynamic;

-- ----------------------------

-- Table structure for global_table

-- ----------------------------

DROP TABLE IF EXISTS `global_table`;

CREATE TABLE `global_table`? (

? `xid` varchar(128) CHARACTER SET utf8 COLLATE utf8_general_ci NOT NULL,

? `transaction_id` bigint(20) NULL DEFAULT NULL,

? `status` tinyint(4) NOT NULL,

? `application_id` varchar(32) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL,

? `transaction_service_group` varchar(32) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL,

? `transaction_name` varchar(128) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL,

? `timeout` int(11) NULL DEFAULT NULL,

? `begin_time` bigint(20) NULL DEFAULT NULL,

? `application_data` varchar(2000) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL,

? `gmt_create` datetime(0) NULL DEFAULT NULL,

? `gmt_modified` datetime(0) NULL DEFAULT NULL,

? PRIMARY KEY (`xid`) USING BTREE,

? INDEX `idx_gmt_modified_status`(`gmt_modified`, `status`) USING BTREE,

? INDEX `idx_transaction_id`(`transaction_id`) USING BTREE

) ENGINE = InnoDB CHARACTER SET = utf8 COLLATE = utf8_general_ci ROW_FORMAT = Dynamic;

-- ----------------------------

-- Table structure for lock_table

-- ----------------------------

DROP TABLE IF EXISTS `lock_table`;

CREATE TABLE `lock_table`? (

? `row_key` varchar(128) CHARACTER SET utf8 COLLATE utf8_general_ci NOT NULL,

? `xid` varchar(96) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL,

? `transaction_id` bigint(20) NULL DEFAULT NULL,

? `branch_id` bigint(20) NOT NULL,

? `resource_id` varchar(256) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL,

? `table_name` varchar(32) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL,

? `pk` varchar(36) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL,

? `gmt_create` datetime(0) NULL DEFAULT NULL,

? `gmt_modified` datetime(0) NULL DEFAULT NULL,

? PRIMARY KEY (`row_key`) USING BTREE,

? INDEX `idx_branch_id`(`branch_id`) USING BTREE

) ENGINE = InnoDB CHARACTER SET = utf8 COLLATE = utf8_general_ci ROW_FORMAT = Dynamic;

SET FOREIGN_KEY_CHECKS = 1;


二.2此時(shí)去github上down我們的項(xiàng)目(地址:https://github.com/chengshugithub/springcloud-eureka-feign-seata)檀轨;

? ?@1然后每個(gè)服務(wù)下面都有創(chuàng)建自己表的sql語句胸竞;然后根據(jù)配置文件去創(chuàng)建每個(gè)服務(wù)的庫(kù);

? ?@2 我們服務(wù)的事物提交主要是通過seata服務(wù)去提交這個(gè)全局的事物参萄,每一個(gè)服務(wù)的數(shù)據(jù)庫(kù)都需要去創(chuàng)建一張這個(gè)表卫枝,sql如下;

CREATE TABLE `undo_log` (

? `id` bigint(20) NOT NULL AUTO_INCREMENT,

? `branch_id` bigint(20) NOT NULL,

? `xid` varchar(100) CHARACTER SET utf8 COLLATE utf8_general_ci NOT NULL,

? `context` varchar(128) CHARACTER SET utf8 COLLATE utf8_general_ci NOT NULL,

? `rollback_info` longblob NOT NULL,

? `log_status` int(11) NOT NULL,

? `log_created` datetime(0) NOT NULL,

? `log_modified` datetime(0) NOT NULL,

? `ext` varchar(100) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL,

? PRIMARY KEY (`id`) USING BTREE,

? UNIQUE INDEX `ux_undo_log`(`xid`, `branch_id`) USING BTREE

) ENGINE = InnoDB AUTO_INCREMENT = 1 CHARACTER SET = utf8 COLLATE = utf8_general_ci ROW_FORMAT = Dynamic;

SET FOREIGN_KEY_CHECKS = 1;

?@3然后每個(gè)服務(wù)下面都需要去增加file.conf和regist.conf文件讹挎,這個(gè)regist.conf文件其實(shí)就是seata-server下的file.conf.example的文件校赤;具體的在項(xiàng)目里面看;


@4 總結(jié)一下:如果想把自己的項(xiàng)目整合到seata-server管理筒溃。

? ? ? ?@4.1 引入setata依賴:(因?yàn)橐蕾嚪旁谶@格式會(huì)亂马篮,所以就不放了)

? ? ? @4.2? resources下配置file.conf文件和 registry.conf文件;

? ? ? @4.3 數(shù)據(jù)庫(kù)里面增加一個(gè)undo_log的表怜奖;

? ? ? @4.4 修改配置文件浑测,如下:


? ? ?@4.5 數(shù)據(jù)源交給setata代理處理;(代碼上有)

? ? @4.6? 然后代碼塊使用@GlobalTransactional注解

三.此時(shí)我們?cè)龠M(jìn)行運(yùn)行程序歪玲;

? ?1.啟動(dòng) eureka-server

? ?2.啟動(dòng) seatea-serer

? ?3.陸續(xù)啟動(dòng)自己demo的三個(gè)服務(wù)迁央;

? 4.http://localhost:8180/order/create?userId=1&productId=1&count=10&money=100;

( 邏輯的話自己看就好了滥崩,基本上是訂單調(diào)用庫(kù)存岖圈,庫(kù)存減少,賬戶減少)

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
  • 序言:七十年代末钙皮,一起剝皮案震驚了整個(gè)濱河市蜂科,隨后出現(xiàn)的幾起案子,更是在濱河造成了極大的恐慌株灸,老刑警劉巖崇摄,帶你破解...
    沈念sama閱讀 206,482評(píng)論 6 481
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件,死亡現(xiàn)場(chǎng)離奇詭異慌烧,居然都是意外死亡逐抑,警方通過查閱死者的電腦和手機(jī),發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 88,377評(píng)論 2 382
  • 文/潘曉璐 我一進(jìn)店門屹蚊,熙熙樓的掌柜王于貴愁眉苦臉地迎上來厕氨,“玉大人进每,你說我怎么就攤上這事∶” “怎么了田晚?”我有些...
    開封第一講書人閱讀 152,762評(píng)論 0 342
  • 文/不壞的土叔 我叫張陵,是天一觀的道長(zhǎng)国葬。 經(jīng)常有香客問我贤徒,道長(zhǎng),這世上最難降的妖魔是什么汇四? 我笑而不...
    開封第一講書人閱讀 55,273評(píng)論 1 279
  • 正文 為了忘掉前任接奈,我火速辦了婚禮,結(jié)果婚禮上通孽,老公的妹妹穿的比我還像新娘序宦。我一直安慰自己,他們只是感情好背苦,可當(dāng)我...
    茶點(diǎn)故事閱讀 64,289評(píng)論 5 373
  • 文/花漫 我一把揭開白布互捌。 她就那樣靜靜地躺著,像睡著了一般行剂。 火紅的嫁衣襯著肌膚如雪秕噪。 梳的紋絲不亂的頭發(fā)上,一...
    開封第一講書人閱讀 49,046評(píng)論 1 285
  • 那天厚宰,我揣著相機(jī)與錄音巢价,去河邊找鬼。 笑死固阁,一個(gè)胖子當(dāng)著我的面吹牛,可吹牛的內(nèi)容都是我干的城菊。 我是一名探鬼主播备燃,決...
    沈念sama閱讀 38,351評(píng)論 3 400
  • 文/蒼蘭香墨 我猛地睜開眼,長(zhǎng)吁一口氣:“原來是場(chǎng)噩夢(mèng)啊……” “哼凌唬!你這毒婦竟也來了并齐?” 一聲冷哼從身側(cè)響起,我...
    開封第一講書人閱讀 36,988評(píng)論 0 259
  • 序言:老撾萬榮一對(duì)情侶失蹤客税,失蹤者是張志新(化名)和其女友劉穎况褪,沒想到半個(gè)月后,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體更耻,經(jīng)...
    沈念sama閱讀 43,476評(píng)論 1 300
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡测垛,尸身上長(zhǎng)有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 35,948評(píng)論 2 324
  • 正文 我和宋清朗相戀三年,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了秧均。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片食侮。...
    茶點(diǎn)故事閱讀 38,064評(píng)論 1 333
  • 序言:一個(gè)原本活蹦亂跳的男人離奇死亡号涯,死狀恐怖,靈堂內(nèi)的尸體忽然破棺而出锯七,到底是詐尸還是另有隱情链快,我是刑警寧澤,帶...
    沈念sama閱讀 33,712評(píng)論 4 323
  • 正文 年R本政府宣布眉尸,位于F島的核電站域蜗,受9級(jí)特大地震影響,放射性物質(zhì)發(fā)生泄漏噪猾。R本人自食惡果不足惜霉祸,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 39,261評(píng)論 3 307
  • 文/蒙蒙 一、第九天 我趴在偏房一處隱蔽的房頂上張望畏妖。 院中可真熱鬧脉执,春花似錦、人聲如沸戒劫。這莊子的主人今日做“春日...
    開封第一講書人閱讀 30,264評(píng)論 0 19
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽迅细。三九已至巫橄,卻和暖如春,著一層夾襖步出監(jiān)牢的瞬間茵典,已是汗流浹背湘换。 一陣腳步聲響...
    開封第一講書人閱讀 31,486評(píng)論 1 262
  • 我被黑心中介騙來泰國(guó)打工, 沒想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留统阿,地道東北人彩倚。 一個(gè)月前我還...
    沈念sama閱讀 45,511評(píng)論 2 354
  • 正文 我出身青樓,卻偏偏與公主長(zhǎng)得像扶平,于是被迫代替她去往敵國(guó)和親帆离。 傳聞我的和親對(duì)象是個(gè)殘疾皇子,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 42,802評(píng)論 2 345

推薦閱讀更多精彩內(nèi)容