當(dāng)前位置:
首頁 >
前端技术
> javascript
>内容正文
javascript
8.Spring Cloud Alibaba教程:整合Seata分布式事务
生活随笔
收集整理的這篇文章主要介紹了
8.Spring Cloud Alibaba教程:整合Seata分布式事务
小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.
概述
Seata 是一款開源的分布式事務(wù)解決方案,致力于提供高性能和簡單易用的分布式事務(wù)服務(wù)。
更多的介紹可以參考官方文檔:Seata快速入門
本篇主要是介紹Spring Cloud Alibaba + JPA 整合 Seata 的過程
安裝Seata
- 下載Seata,打開 https://github.com/seata/seata/releases,現(xiàn)在最新的是1.3.0,所以選擇seata-server-1.3.0.tar.gz 進(jìn)行下載,放到/opt
- 解壓Seata到/usr/local目錄下
- 這邊采用file單機(jī)模式(db模式相對麻煩一點,以后再另外介紹,從簡單的先開始),直接運行命令啟動即可
看到下面這個提示,說明啟動成功,默認(rèn)端口號8091
接下來介紹Spring Cloud Alibaba 接入Seata
業(yè)務(wù)場景
假設(shè)當(dāng)前存在2個微服務(wù):訂單服務(wù)、支付服務(wù)。
- 用戶下單時,訂單服務(wù)需要調(diào)用支付服務(wù)進(jìn)行扣款
- 當(dāng)余額充足時下單成功,并全局提交事務(wù)
- 當(dāng)余額不足時下單失敗,并全局回滾事務(wù)
版本說明
| Spring Boot | 2.1.13.RELEASE |
| Spring Cloud | Greenwich.SR6 |
| Spring Cloud Alibaba | 2.1.3.RELEASE |
| MySQL | 8.0.11 |
創(chuàng)建父工程
創(chuàng)建父工程 hello-alibaba-seata,并引入依賴:
<dependencyManagement><dependencies><dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-dependencies</artifactId><version>${spring-cloud-version}</version><type>pom</type><scope>import</scope></dependency><dependency><groupId>com.alibaba.cloud</groupId><artifactId>spring-cloud-alibaba-dependencies</artifactId><version>${spring-cloud-alibaba-version}</version><type>pom</type><scope>import</scope></dependency></dependencies> </dependencyManagement><dependencies><!--Spring Boot--><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-web</artifactId></dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-actuator</artifactId></dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-data-jpa</artifactId></dependency><!--Nacos--><dependency><groupId>com.alibaba.cloud</groupId><artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId></dependency><!--Seata--><dependency><groupId>com.alibaba.cloud</groupId><artifactId>spring-cloud-starter-alibaba-seata</artifactId></dependency><!--MySQL--><dependency><groupId>mysql</groupId><artifactId>mysql-connector-java</artifactId><version>${mysql.version}</version></dependency><dependency><groupId>org.projectlombok</groupId><artifactId>lombok</artifactId><optional>true</optional></dependency> </dependencies>創(chuàng)建公共服務(wù) common-server
該工程主要封裝公共的VO,由于測試場景比較簡單,目前只有一個類
@Data @Builder public class ReduceAmountDto {private Long userId;private Integer amount; }創(chuàng)建支付服務(wù) pay-order
在訂單-支付的關(guān)系中,支付服務(wù)相當(dāng)于provider,訂單服務(wù)相當(dāng)于consumer。因此,這邊我們先創(chuàng)建支付服務(wù)。
- 引入 common-server 依賴
- 創(chuàng)建賬戶 Entity
- 創(chuàng)建Dao
- 創(chuàng)建 Service
- 創(chuàng)建 controller
- application.yml
注意:如果用的不是MySQL8,就要改下 driver-class-name
創(chuàng)建訂單服務(wù) order-server
- 引入 common-server 依賴
- 創(chuàng)建訂單 Entity
- 創(chuàng)建訂單 Dao
- 創(chuàng)建 Service
@GlobalTransactional 表示開啟Seata全局事務(wù)
- 創(chuàng)建 controller
測試
總結(jié)
以上是生活随笔為你收集整理的8.Spring Cloud Alibaba教程:整合Seata分布式事务的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 7.Spring Cloud Aliba
- 下一篇: java8 时间 LocalDateTi