1.依賴
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-alibaba-seata</artifactId>
<version>2.0.0.RELEASE</version>
</dependency>
<dependency>
<groupId>io.seata</groupId>
<artifactId>seata-all</artifactId>
<version>0.8.0</version>
</dependency>
2.配置
- registry.conf:配置注冊中心和配置中心,默認是file。
- file.conf:seata工作規則信息
- DataSourceConfig:配置代理數據源實現分支事務,如果沒有注入,事務無法成功回滾
2.1 registry.conf
該文件包含兩部分配置:
- 注冊中心
- 配置中心
注冊中心:
registry { # 注冊中心配置
# 可選項:file 、nacos 、eureka、redis、zk
type = "nacos" # 指定nacos注冊中心,默認是file。由於項目整體使用nacos,所以后續選擇nacos
nacos {
serverAddr = "127.0.0.1:8848"
namespace = "public"
cluster = "default"
}
eureka {
serviceUrl = "http://localhost:1001/eureka"
application = "default"
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" # 指向file配置中心,也可以指向nacos等其他注冊中心
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" # 通過file.conf配置seata參數,指向第二個配置文件
}
}
2.2 file.conf
該文件的命名取決於registry.conf配置中心的配置
由於registry.conf中配置的是
也就是說:file.conf文件名取決於registry的配置中心(config{...})配置,如果registry配置的配置中心不是file,可以沒有改文件。
例如:如果配置中心是nacos,這是file.conf文件就不需要了,把file.conf文件內容交給nacos就可
*事務日志存儲配置:
store {
## store mode: file、db
mode = "file" # 存儲方式file、db
## file store
file {
dir = "sessionStore"
# branch session size , if exceeded first try compress lockkey, still exceeded throws exceptions
max-branch-session-size = 16384
# globe session size , if exceeded throws exceptions
max-global-session-size = 512
# file buffer size , if exceeded allocate new buffer
file-write-buffer-cache-size = 16384
# when recover batch read size
session.reload.read_size = 100
# async, sync
flush-disk-mode = async
}
## database store
db {
driver_class = ""
url = ""
user = ""
password = ""
}
}
*當前微服務在seata服務器中注冊的信息配置:
service {
#vgroup->rgroup
#必須和服務名一致:${spring.applicaiton.name}
vgroup_mapping.${spring.application.name}-fescar-service-group = "default"
#only support single node
default.grouplist = "127.0.0.1:8091" #seata-server服務器地址,默認是8091
#degrade current not support
enableDegrade = false
#disable
disable = false
}
客戶端相關工作的機制
client {
async.commit.buffer.limit = 10000
lock {
retry.internal = 10
retry.times = 30
}
}
2.3 DataSourceConfig
每一個微服務原來自己的數據源都必須使用DataSourceProxy代理,這樣seata才能掌控所有事務。
@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);
}
}
3.注解
主業務方法添加全局事務:@GlobalTransactional
分支業務方法添加本地事務注解:@Transactional