下載最新版Seata Sever(window10)
官方下載地址:https://github.com/seata/seata/releases
下載完成後解壓到本地磁盤,解壓完成後有三個文件夾,bin目錄下是服務啓動的bat文件,conf目錄下是相關配置文件,lib目錄下是相關jar包
進入conf,根據自身需求可以修改file.conf以及registry.conf的內容(不建議修改,默認設置就好,因爲相關配置在項目中可以修改)
進入bin目錄,雙擊seata-server.bat即可運行
創建UNDO_LOG表
-注意此處0.3.0+增加唯一索引ux_undo_log
CREATE TABLE undo_log(
ID BIGINT( 20) NOT NULL AUTO_INCREMENT,
branch_id BIGINT( 20) NOT NULL,
XID VARCHAR( 100) NOT NULL,
上下文 VARCHAR( 128) NOT NULL,
rollback_info LONGBLOB NOT NULL,
log_status INT(11)NOT NULL,
log_created日期時間NOT NULL,
log_modified日期時間NOT NULL,
分機 VARCHAR(100)DEFAULT NULL,
PRIMARY KEY(ID),
UNIQUE KEY ux_undo_log(XID,branch_id)
)ENGINE = InnoDB AUTO_INCREMENT = 1 DEFAULT CHARSET = utf8;
創建項目
項目結構如下:
添加依賴
<properties>
<seata.version>0.8.0</seata.version>
</properties>
<dependency>
<groupId>io.seata</groupId>
<artifactId>seata-all</artifactId>
<version>${seata.version}</version>
</dependency>
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-alibaba-seata</artifactId>
<version>2.1.0.RELEASE</version>
</dependency>
2.添加Seata的配置文件file.conf,注意service中服務的地址需要跟你下載的服務地址一致,默認爲127.0.0.1:8091,不需要修改
file.conf 的 service.vgroup_mapping 配置必須和spring.application.name一致
在 org.springframework.cloud:spring-cloud-starter-alibaba-seata的org.springframework.cloud.alibaba.seata.GlobalTransactionAutoConfiguration類中,默認會使用 ${spring.application.name}-fescar-service-group作爲服務名註冊到 Seata Server上,如果和file.conf中的配置不一致,會提示 no available server to connect錯誤
也可以通過配置 spring.cloud.alibaba.seata.tx-service-group修改後綴,但是必須和file.conf中的配置保持一致
transport {
# tcp udt unix-domain-socket
type = "TCP"
#NIO NATIVE
server = "NIO"
#enable heartbeat
heartbeat = true
#thread factory for netty
thread-factory {
boss-thread-prefix = "NettyBoss"
worker-thread-prefix = "NettyServerNIOWorker"
server-executor-thread-prefix = "NettyServerBizHandler"
share-boss-worker = false
client-selector-thread-prefix = "NettyClientSelector"
client-selector-thread-size = 1
client-worker-thread-prefix = "NettyClientWorkerThread"
# netty boss thread size,will not be used for UDT
boss-thread-size = 1
#auto default pin or 8
worker-thread-size = 8
}
}
service {
#vgroup->rgroup
vgroup_mapping.這裏需要寫你spring.application.name的名字-fescar-service-group = "這裏要寫你seata服務的名字,比如我的是seata-service"
#only support single node
default.grouplist = "127.0.0.1:8091"
#degrade current not support
enableDegrade = false
#disable
disable = false
disableGlobalTransaction = false
}
client {
async.commit.buffer.limit = 10000
lock {
retry.internal = 10
retry.times = 30
}
}
registry.conf 此處的註冊中心可以根據自身需求配置,type=“eureka”,表示我的註冊中心爲eureka
registry {
# file 、nacos 、eureka、redis、zk
type = "eureka"
nacos {
serverAddr = "localhost"
namespace = "public"
cluster = "default"
}
eureka {
serviceUrl = "http://localhost:8761/eureka"
application = "seata-service"
weight = "1"
}
redis {
serverAddr = "localhost:6381"
db = "0"
}
zk {
cluster = "default"
serverAddr = "127.0.0.1:2181"
session.timeout = 6000
connect.timeout = 2000
}
file {
name = "file.conf"
}
}
config {
# file、nacos 、apollo、zk
type = "file"
nacos {
serverAddr = "localhost"
namespace = "public"
cluster = "default"
}
apollo {
app.id = "fescar-server"
apollo.meta = "http://192.168.1.204:8801"
}
zk {
serverAddr = "127.0.0.1:2181"
session.timeout = 6000
connect.timeout = 2000
}
file {
name = "file.conf"
}
}
注入 DataSourceProxy
Seata是通過代理數據源實現分佈式事務,所以需要配置io.seata.rm.datasource.DataSourceProxy的Bean,且是@Primary默認的數據源,否則事務不會回滾,無法實現分佈式事務
@Configuration
public class DataSourceConfig {
@Bean
@ConfigurationProperties(prefix = "spring.datasource")
public DruidDataSource druidDataSource() {
return new DruidDataSource();
}
/**
* 需要將 DataSourceProxy 設置爲主數據源,否則事務無法回滾
*
* @param druidDataSource The DruidDataSource
* @return The default datasource
*/
@Primary
@Bean("dataSource")
public DataSource dataSource(DruidDataSource druidDataSource) {
return new DataSourceProxy(druidDataSource);
}
}
因爲使用了mybatis的starter所以需要排除DataSourceAutoConfiguration,不然會產生循環依賴
@SpringBootApplication(exclude = {DataSourceAutoConfiguration.class})
1
在需要進行分佈式事務的方法上添加@GlobalTransactional註解即可(這裏需要注意一點,單點故障的問題,由於協調者的重要性,一旦協調者發生故障。參與者會一直阻塞下去。尤其在第二階段,協調者發生故障,那麼所有的參與者還都處於鎖定事務資源的狀態中,而無法繼續完成事務操作。(如果是協調者掛掉,可以重新選舉一個協調者,但是無法解決因爲協調者宕機導致的參與者處於阻塞狀態的問題)
代碼demo可參考官方的demo,也可參考我自己寫的demo;git:https://github.com/a870368162/seata-example