Seata 是一款开源的分布式事务解决方案,致力于在微服务架构下提供高性能和简单易用的分布式事务服务
内部组成
-
Transaction Coordinator (TC): 事务协调器,它是独立的中间件,需要独立部署运行,它维护全局事务的运
行状态,接收TM指令发起全局事务的提交与回滚,负责与RM通信协调各各分支事务的提交或回滚 -
Transaction Manager ™: 事务管理器,TM 需要嵌入应用程序中工作,它负责开启一个全局事务,并最终
向TC发起全局提交或全局回滚的指令 -
Resource Manager (RM): 控制分支事务,负责分支注册、状态汇报,并接收事务协调器TC的指令,驱动分
支(本地)事务的提交和回滚
deploy
通过Docker Compose部署Seata 下面是我的docker-compose.yml
文件,根据我自己的需要,进行了一点点修改
version: "3"
services:
seata-server:
image: seataio/seata-server
hostname: seata-server
restart: always
container_name: seata
ports:
- "8091:8091"
environment:
- SEATA_PORT=8091
- STORE_MODE=file
执行命令 docker-compose up -d
启动项目,可以通过docker ps
查看是否成功
项目实践
项目路径: https://github.com/lucasIsfyf/seata-demo
maven依赖
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-alibaba-seata</artifactId>
<version>2.1.0.RELEASE</version>
</dependency>
并且在项目文件中配置组名:
spring.cloud.alibaba.seata.tx-service-group=#{组名}
,该组名会在file.conf中用到
配置seata文件
在src/main/resource中,新增 registry.conf、file.conf 两个文件,内容可拷贝项目中的配置文件docker exec -it seata bash
registry.conf
registry {
type = "file"
file {
name = "file.conf"
}
}
config {
type = "file"
file {
name = "file.conf"
}
}
file.conf
transport {
type = "TCP"
server = "NIO"
heartbeat = true
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"
boss-thread-size = 1
worker-thread-size = 8
}
}
service {
#vgroup->rgroup
vgroup_mapping.order-service-seata-service-group = "default"
default.grouplist = "47.92.86.79:8091"
enableDegrade = false
disable = false
}
client {
async.commit.buffer.limit = 10000
lock {
retry.internal = 10
retry.times = 30
}
}
注意 vgroup_mapping.order-service-seata-service-group 中 order-service-seata-service-group是我们在application.properties中配置的服务组名
default.grouplist = “47.92.86.79:8091” // default是根据vgroup_mapping.#{服务组名得到}
上面是我在实践时,遇见的问题的解决方案,配置成功后就可以运行了(大多数问题是服务端版本和客户端版本不对导致的)
可以看到我们成功了,创建了事务管理器™
后面的测试内容请参考官方的测试案例:Seata快速启动案例
参考资料
Seata 分布式事务实践和开源详解 | GIAC 实录
解决Seata(Fescar) Server无法连接的Bug,no available server to connect,netty TooLongFrameException