一、官方文檔網址
http://seata.io/zh-cn/docs/overview/what-is-seata.html
二、常見問題
2.1:網址:
http://seata.io/zh-cn/docs/overview/faq.html
2.2:在使用1.1.0以上版本可能遇到以下問題:
這是因為1.1.0版本以上不支持手動代理:
這個類,在1.2.0版本被刪除了,並且seata的數據源代理改成了AOP攔截代理。1.2.0及以上版本,只需創建普通的數據源bean,然后,啟用AT代理功能就行:seata.enableDataSourceProxy=true(默認就是啟用的,所以也不需要配置)
2.2使用seata-server.bat啟動seata1.3時Shell窗口出現的亂碼問題:
在seata\conf\logback.xml文件中修改:
<property name="CONSOLE_LOG_PATTERN" value="%clr(%d{yyyy-MM-dd HH:mm:ss.SSS}){faint} %clr(%5p) %clr(---){faint} %clr([%15.15t]){faint} %clr(%-40.40logger{39}){cyan} %clr(:){faint} %m%n%wEx"/>
修改為:
<property name="CONSOLE_LOG_PATTERN" value="%d{yyyy-MM-dd HH:mm:ss.SSS} %5p---[%t] %-40.40logger{39}: %m%n%wEx"/>
此問題是因為開發者為seata1.3.0添加字體顏色,而在window中的shell腳本內不顯示發生的亂碼錯誤,預計將於1.3.1進行修復。
三、入門
隨着互聯化的蔓延,各種項目都逐漸向分布式服務做轉換。如今微服務已經普遍存在,本地事務已經無法滿足分布式的要求,由此分布式事務問題誕生。 分布式事務被稱為世界性的難題,目前分布式事務存在兩大理論依據:CAP定律 BASE理論。
CAP定律
這個定理的內容是指的是在一個分布式系統中、Consistency(一致性)、 Availability(可用性)、Partition tolerance(分區容錯性),三者不可得兼。
一致性(C)
在分布式系統中的所有數據備份,在同一時刻是否同樣的值。(等同於所有節點訪問同一份最新的數據副本)
可用性(A)
在集群中一部分節點故障后,集群整體是否還能響應客戶端的讀寫請求。(對數據更新具備高可用性)
分區容錯性(P)
以實際效果而言,分區相當於對通信的時限要求。系統如果不能在時限內達成數據一致性,就意味着發生了分區的情況,必須就當前操作在C和A之間做出選擇。
BASE理論
BASE是Basically Available(基本可用)、Soft state(軟狀態)和 Eventually consistent(最終一致性)三個短語的縮寫。BASE理論是對CAP中一致性和可用性權衡的結果,其來源於對大規模互聯網系統分布式實踐的總結, 是基於CAP定理逐步演化而來的。BASE理論的核心思想是:即使無法做到強一致性,但每個應用都可以根據自身業務特點,采用適當的方式來使系統達到最終一致性。
基本可用
基本可用是指分布式系統在出現不可預知故障的時候,允許損失部分可用性—-注意,這絕不等價於系統不可用。比如:
(1)響應時間上的損失。正常情況下,一個在線搜索引擎需要在0.5秒之內返回給用戶相應的查詢結果,但由於出現故障,查詢結果的響應時間增加了1~2秒
(2)系統功能上的損失:正常情況下,在一個電子商務網站上進行購物的時候,消費者幾乎能夠順利完成每一筆訂單,但是在一些節日大促購物高峰的時候,由於消費者的購物行為激增,為了保護購物系統的穩定性,部分消費者可能會被引導到一個降級頁面
軟狀態
軟狀態指允許系統中的數據存在中間狀態,並認為該中間狀態的存在不會影響系統的整體可用性,即允許系統在不同節點的數據副本之間進行數據同步的過程存在延時
最終一致性
最終一致性強調的是所有的數據副本,在經過一段時間的同步之后,最終都能夠達到一個一致的狀態。因此,最終一致性的本質是需要系統保證最終數據能夠達到一致,而不需要實時保證系統數據的強一致性。
四、Seata術語
TC (Transaction Coordinator) - 事務協調者
維護全局和分支事務的狀態,驅動全局事務提交或回滾。
TM (Transaction Manager) - 事務管理器
定義全局事務的范圍:開始全局事務、提交或回滾全局事務。
RM (Resource Manager) - 資源管理器
管理分支事務處理的資源,與TC交談以注冊分支事務和報告分支事務的狀態,並驅動分支事務提交或回滾。
五、Seata配置
5.1:更改本地server端的file.conf和registry.conf文件:
service { #transaction service group mapping #修改,可不改,my_test_tx_group隨便起名字。 vgroup_mapping.my_test_tx_group = "default" #only support when registry.type=file, please don't set multiple addresses # 此服務的地址 default.grouplist = "127.0.0.1:8091" #disable seata disableGlobalTransaction = false }
.........
}
registry {
# file 、nacos 、eureka、redis、zk、consul、etcd3、sofa
type = "nacos"
nacos {
application = "seata-server"
serverAddr = "localhost:8848"
group = "SEATA_GROUP"
namespace = ""
cluster = "default"
username = "nacos"
password = "nacos"
}
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"
}
zk {
serverAddr = "127.0.0.1:2181"
sessionTimeout = 6000
connectTimeout = 2000
username = ""
password = ""
}
etcd3 {
serverAddr = "http://localhost:2379"
}
file {
name = "file.conf"
}
}
5.1:使用spring導入的依賴
<dependency> <groupId>com.alibaba.cloud</groupId> <artifactId>spring-cloud-starter-alibaba-seata</artifactId> <version>2.2.2.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.3.0</version> </dependency>
5.2:yml配置
#nacos注冊中心新增group的屬性配置seata:
# registry:
# nacos:
# group,如果無配置,則默認為DEFAULT_GROUP,Server和Client端需保持一致。
#seata:
# tx-service-group: gulimall-order-fescar-service-group(此處名稱可自定義,但必須與
#service:
# vgroup-mapping:
# gulimall-order-fescar-service-group相同),gulimall-order-fescar-service-group: default所代表的值與Server端中file.conf里的:
#vgroup_mapping.my_test_tx_group 的值相同
#
seata:
tx-service-group: gulimall-order-fescar-service-group
enabled: true
registry:
type: nacos
nacos:
server-addr: localhost:8848
namespace:
userName: "nacos"
password: "nacos"
group: SEATA_GROUP #group需要與server端registry.conf里的 group的值相同
application: seata-server #application需要與server端registry.conf里的application的值相同
service:
vgroup-mapping:
gulimall-order-fescar-service-group: default
grouplist:
default: 127.0.0.1:8091
config:
type: file #此處使用file類型進行存儲,可更改為db,若使用db方法可參考開頭開發組提供的文檔進行配置
file:
name: file.conf
enable-auto-data-source-proxy: true #開啟自動代理數據源
application-id: gulimall-order
5.3:開啟全局事務管理
只需要在總事務上加上@GlobalTransactional(rollbackFor = Exception.class),以及在分支事務上加入@Transactional即可