seata is an easy-to-use, high-performance, open source distributed transaction solution.seata 是乙個簡單易用的,高效能,開源的分布式事務解決方案。
at
模式是一種無侵入的分布式事務解決方案。在at
模式下,使用者只需關注自己的「業務sql
」,
使用者的 「業務sql
」 就是全域性事務一階段,seata
框架會自動生成事務的二階段提交和回滾操作。
at
模式如何做到對業務的無侵入 :
at
模式的一階段、二階段提交和回滾均由seata
框架自動生成,使用者只需編寫「業務sql
」,便能輕鬆接入分布式事務,at
模式是一種對業務無任何侵入的分布式事務解決方案。
(以上選自知乎分布式事務的4種模式)
nacos-dubbo
是乙個簡單的seata at
模式的入門專案,使用dubbo
去實現服務與服務之間的呼叫。
說明provider
專案中只有2種型別的專案一種是api
,還有一種是service
所謂api
專案是專案只有介面和domain
,沒有實現。此專案會被該api
的實現(service
)和需要呼叫service
的專案依賴。
service
專案是實現api
專案的專案一般是去運算元據庫或者其他業務的專案。
consumer
專案也只有2種型別的專案一種是api
,還有一種是service
和上面一樣,service
專案會去呼叫對應的provider
的介面,使用的是dubbo rpc
的通訊
business
專案是指業務層的**專案,他會去依賴consumer
的api
專案。然後對外部提供restful
的介面
所以在這個專案中,先啟動2個provider
中的service
,然後啟動consumer
的service
,最後啟動business
專案。
準備首先需要建立2個庫,乙個庫存放order
表,乙個庫存放order_item
。
create
table tb_order (
id bigint(20
)not
null
auto_increment
primary
key,
order_id bigint(20
)not
null
, user_id bigint(20
)not
null
);
除此之外,還需要在每個庫里都建立乙個create
table tb_order_item (
id bigint(20
)not
null
auto_increment
primary
key,
user_id bigint(20
)not
null
, order_id bigint(20
)not
null
, order_item_id bigint(20
)not
null
);
undo_log
provider`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
notnull
,`log_status`
int(11)
notnull
,`log_created`
datetime
notnull
,`log_modified`
datetime
notnull
,`ext`
varchar
(100
)default
null
,primary
key(
`id`),
unique
key`ux_undo_log`
(`xid`
,`branch_id`))
engine
=innodb
auto_increment=1
default
charset
=utf8;
主要增加了seata
依賴
配置檔案>
>
io.seatagroupid
>
>
seata-spring-boot-starterartifactid
>
>
1.0.0version
>
dependency
>
配置類,具體看專案的spring
alibaba
:seata
:# 自定義事務組名稱 tx_group,需要與服務端一致
tx-service-group
: tx_group
seataconfiguration
一定要在啟動類上加上import io.seata.rm.datasource.datasourceproxy;
@configuration
public
class
seataconfiguration
@bean
public globaltransactionscanner globaltransactionscanner()
}
@enabletransactionmanagement
transaction
這裡刪除去了mybatis mysql hikari
的依賴。
配置類
實現方法上加上註解import io.seata.spring.annotation.globaltransactionscanner;
import org.springframework.context.annotation.bean;
import org.springframework.context.annotation.configuration;
@configuration
public
class
seataconfiguration
}
@globaltransactional
business@service
(version =
"1.0.0"
)public
class
transactionserviceimpl
implements
transactionservice
}}
這裡就正常呼叫consumer
接下來就是瀏覽器訪問測試@restcontroller
("/v1/transaction"
)public
class
transactioncontroller
}
再到seata
服務上看到回滾的日誌,再檢視資料庫。成功回滾
nacos-http
是乙個使用spring cloud alibaba
實現seata at
模式的入門專案。
微服務架構分布式事務解決方案 FESCAR
fescar fast easy commit and rollback 是乙個用於微服務架構的分布式事務解決方案,它的特點是高效能且易於使用,旨在實現簡單並快速的事務提交與回滾。微服務架構中的分布式事務問題 從傳統的單體應用說起,假設乙個單體應用的業務由 3 個模組構成,三者使用單個本地資料來源。...
微服務架構及分布式事務解決方案
分布式事務場景如何設計系統架構及解決資料一致性問題,個人理解最終方案把握以下原則就可以了,那就是 大事務 小事務 原子事務 非同步 訊息通知 解決分布式事務的最好辦法其實就是不考慮分布式事務,將乙個大的業務進行拆分,整個大的業務流程,轉化成若干個小的業務流程,然後通過設計補償流程從而考慮最終一致性。...
微服務架構之分布式事務解決方案一
場景一 建立訂單 預留庫存 扣積分 鎖定優惠券 場景二 建立交易訂單 查詢賬戶 建立交易記錄 判斷賬戶餘額並扣款 增加積分 通知支付平台 場景三 收到銀行扣款結果 更改訂單狀態 給賬戶加款 增加積分 生成會計分錄 通知電商平台 場景四 收到支付平台的支付結果 更改訂單狀態,扣減庫存,扣減積分,使用優...