學(xué)習(xí)基于記錄,而不止于記錄。
希望自己能堅持下去~
0.寫在前面
spring boot版本:2.2.7.RELEASE
Seata版本:1.2.0
mysql> select version();
+-----------+
| version() |
+-----------+
| 8.0.18 |
+-----------+
其他依賴版本細節(jié)在pom.xml可以看到。
????之前有了解過分布式事務(wù)的處理方法,比如:使用消息隊列中間件實現(xiàn)事務(wù)的最終一致性
????當然只是簡單了解一下基本機制,并沒有深入的學(xué)習(xí)和使用,但是看到這種方式實現(xiàn)分布式事務(wù)的局限性,那就是雖然在一些場景:比如注冊用戶和發(fā)送驗證碼,銀行轉(zhuǎn)賬等,這種消息隊列的方式可以很好的實現(xiàn),但是在實際應(yīng)用中發(fā)生分布式事務(wù)的場景,有時候需要信息的即時性有一定保證,比如:子系統(tǒng)需要即時的向平臺系統(tǒng)獲取登錄信息,總不能像驗證碼和轉(zhuǎn)賬一樣接受那么大的延時吧。還有一點就是消息中間件號稱可以實現(xiàn)無侵入的實現(xiàn)分布式事務(wù),但是事實上,就我們公司的一些項目來說,改造起來代價還說蠻大的。
????綜上所述,我考慮學(xué)習(xí)和使用另一種分布式事務(wù)解決方案:Eureka+Seata。
1.基本概念
建議直接看Seata官網(wǎng),文檔很全面也很友好,畢竟是國內(nèi)阿里整出來的。
Seata 是一款開源的分布式事務(wù)解決方案,致力于在微服務(wù)架構(gòu)下提供高性能和簡單易用的分布式事務(wù)服務(wù)。在 Seata 開源之前,Seata 對應(yīng)的內(nèi)部版本在阿里經(jīng)濟體內(nèi)部一直扮演著分布式一致性中間件的角色,幫助經(jīng)濟體平穩(wěn)的度過歷年的雙11,對各BU業(yè)務(wù)進行了有力的支撐。2019.1 ,Seata 正式宣布對外開源。
開源萬歲!!!
2.Eureka注冊中心搭建
說白了就是一個spring boot項目,加入Eureka依賴,提供給注冊中心給其他服務(wù),進而方便實現(xiàn)負載均衡的一些集群環(huán)境下常備功能。這里不做重點,以后或會更詳細研究。
1)創(chuàng)建項目,加上依賴

2)pom.xml文件
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>com.macro.cloud</groupId>
<artifactId>springcloud-learning</artifactId>
<version>0.0.1-SNAPSHOT</version>
</parent>
<groupId>com.macro.cloud</groupId>
<artifactId>seata-storage-service</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>seata-storage-service</name>
<description>Demo project for Spring Boot</description>
<properties>
<java.version>1.8</java.version>
<spring-cloud.version>Greenwich.SR1</spring-cloud.version>
<spring-cloud-alibaba.version>2.1.1.RELEASE</spring-cloud-alibaba.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<!--eureka client-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
<!--feign-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-openfeign</artifactId>
</dependency>
<!--seata-->
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-seata</artifactId>
<version>2.2.0.RELEASE</version>
<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.2.0</version>
</dependency>
</dependencies>
<dependencyManagement>
<dependencies>
<!--Spring Cloud-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>${spring-cloud.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
3)application.yml
server:
port: 5060
spring:
application:
name: eureka-server #注冊中心服務(wù)名稱
eureka:
instance:
hostname: localhost #Eureka實例的主機名
client:
#是否啟用湖區(qū)注冊服務(wù)信息,因為這是一個單節(jié)點的EurekaServer,不需要同步其他的EurekaServer節(jié)點的數(shù)據(jù),所以設(shè)置為false;
fetch-registry: false
#表示是否向eureka注冊服務(wù),即在自己的eureka中注冊自己,默認為true,此處應(yīng)該設(shè)置為false;
register-with-eureka: true
service-url:
defaultZone: http://${eureka.instance.hostname}:5060/eureka
server:
#設(shè)為false,關(guān)閉自我保護,即Eureka server在云心光器件會去統(tǒng)計心跳失敗比例在15分鐘之內(nèi)是否低于85%,如果低于85%,EurekaServer
#會將這些事例保護起來,讓這些事例不會過期,但是在保護器內(nèi)如果剛哈這個服務(wù)提供者非正常下線了,此時服務(wù)消費者會拿到一個無效的服務(wù)
#實例,此時調(diào)用會失敗,對于這個問題需要服務(wù)消費者端有一些容錯機制,如重試、斷路器等;
enable-self-preservation: true
#掃描失效服務(wù)的間隔時間(單位是毫秒,摩恩是60*1000),即60s
eviction-interval-timer-in-ms: 10000
注意:
????eureka.client.register-with-eureka通常設(shè)置為false,但是為了演示方便,這里設(shè)置為true。這樣就可以在注冊中心看到Eureka注冊服務(wù)自己被注冊到注冊中心。
4)啟動類加注解
package com.grj.eurekaserver;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer;
@EnableEurekaServer
@SpringBootApplication
public class EurekaServerApplication {
public static void main(String[] args) {
SpringApplication.run(EurekaServerApplication.class, args);
}
}
5)啟動注冊中心,在瀏覽器打開http://localhost:5060/(端口設(shè)置不同,地址也不同)

3.Seata服務(wù)
1)下載seata-server
下載中心
我使用的是1.2.0版本(可能下載速度會有些慢)
下載完成后,解壓,修改seata\conf目錄下兩個文件
2)registry.conf文件
registry {
# file 、nacos 、eureka、redis、zk、consul、etcd3、sofa
type = "eureka"
nacos {
application = "seata-server"
serverAddr = "localhost"
namespace = ""
cluster = "default"
username = ""
password = ""
}
eureka {
serviceUrl = "http://127.0.0.1:5060/eureka"
application = "seata-server"
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 = "localhost"
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"
}
zk {
serverAddr = "127.0.0.1:2181"
sessionTimeout = 6000
connectTimeout = 2000
username = ""
password = ""
}
etcd3 {
serverAddr = "http://localhost:2379"
}
file {
name = "file.conf"
}
}
3)file.conf文件
## transaction log store, only used in seata-server
store {
## store mode: file、db
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) etc.
datasource = "druid"
## mysql/oracle/postgresql/h2/oceanbase etc.
dbType = "mysql"
driverClassName = "com.mysql.cj.jdbc.Driver"
url = "jdbc:mysql://localhost:3306/seata?useUnicode=true&characterEncoding=utf-8&useSSL=false&serverTimezone=UTC&nullCatalogMeansCurrent=true"
user = "root"
password = "123456"
minConn = 5
maxConn = 30
globalTable = "global_table"
branchTable = "branch_table"
lockTable = "lock_table"
queryLimit = 100
maxWait = 5000
}
}
4)啟動Seata
點擊Seata/bin/seata-server.bat啟動Seata,你可以看到兩個服務(wù)在注冊中心,其中一個就是剛啟動的Seata

4)測試Seata分布式事務(wù)
注意這一部分Seata就提供了官方實例,而且非常清楚,而且還提供了sql建庫腳本。
簡單來講,就是訂單、賬戶和庫存三個服務(wù),下訂單時,訂單系統(tǒng)創(chuàng)建訂單,并且調(diào)用庫存服務(wù)減少庫存,然后調(diào)用賬戶系統(tǒng),削減金額,前幾部完成后再修改訂單狀態(tài)為已完成,整個事務(wù)完成。
在這里我也貼上我的代碼,只是留給自己以后查看,有路過的還是建議直接看官方實例。
4.總結(jié)
????Seata對于分布式事務(wù)的解決個人認為目前體驗良好,而且真正意義上的上手簡單,無侵入提供方案。目前公司正準備采用這種方案,部署到服務(wù)器采用k8s進行服務(wù)編排,以后或有詳細文檔繼續(xù)補充。