微服務解決方案 分布式事務 Seata

2021-10-08 00:21:50 字數 4566 閱讀 9123

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專案是指業務層的**專案,他會去依賴consumerapi專案。然後對外部提供restful的介面

所以在這個專案中,先啟動2個provider中的service,然後啟動consumerservice,最後啟動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

`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;

provider

主要增加了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

@service

(version =

"1.0.0"

)public

class

transactionserviceimpl

implements

transactionservice

}}

business

這裡就正常呼叫consumer

@restcontroller

("/v1/transaction"

)public

class

transactioncontroller

}

接下來就是瀏覽器訪問測試

再到seata服務上看到回滾的日誌,再檢視資料庫。成功回滾

nacos-http是乙個使用spring cloud alibaba實現seata at模式的入門專案。

微服務架構分布式事務解決方案 FESCAR

fescar fast easy commit and rollback 是乙個用於微服務架構的分布式事務解決方案,它的特點是高效能且易於使用,旨在實現簡單並快速的事務提交與回滾。微服務架構中的分布式事務問題 從傳統的單體應用說起,假設乙個單體應用的業務由 3 個模組構成,三者使用單個本地資料來源。...

微服務架構及分布式事務解決方案

分布式事務場景如何設計系統架構及解決資料一致性問題,個人理解最終方案把握以下原則就可以了,那就是 大事務 小事務 原子事務 非同步 訊息通知 解決分布式事務的最好辦法其實就是不考慮分布式事務,將乙個大的業務進行拆分,整個大的業務流程,轉化成若干個小的業務流程,然後通過設計補償流程從而考慮最終一致性。...

微服務架構之分布式事務解決方案一

場景一 建立訂單 預留庫存 扣積分 鎖定優惠券 場景二 建立交易訂單 查詢賬戶 建立交易記錄 判斷賬戶餘額並扣款 增加積分 通知支付平台 場景三 收到銀行扣款結果 更改訂單狀態 給賬戶加款 增加積分 生成會計分錄 通知電商平台 場景四 收到支付平台的支付結果 更改訂單狀態,扣減庫存,扣減積分,使用優...