seata-seata 项目结构
seata 项目结构
项目结构
seata 的代码开源在 github https://github.com/seata 。除了主代码 seata ,还有一些其它的项目,如 seata-samples / website 等等。
seata 主项目使用 maven 进行管理,划分为多个模块,运行 mvn compile
从编译结果里面可看到
[INFO] Seata Parent POM 1.0.0-SNAPSHOT .................... SUCCESS [ 25.687 s]
[INFO] seata-common 1.0.0-SNAPSHOT ........................ SUCCESS [ 3.319 s]
[INFO] seata-config 1.0.0-SNAPSHOT ........................ SUCCESS [ 0.238 s]
[INFO] seata-config-core 1.0.0-SNAPSHOT ................... SUCCESS [ 0.668 s]
[INFO] seata-config-custom 1.0.0-SNAPSHOT ................. SUCCESS [ 0.321 s]
[INFO] seata-config-apollo 1.0.0-SNAPSHOT ................. SUCCESS [ 0.535 s]
[INFO] seata-config-nacos 1.0.0-SNAPSHOT .................. SUCCESS [ 0.536 s]
[INFO] seata-config-zk 1.0.0-SNAPSHOT ..................... SUCCESS [ 0.426 s]
[INFO] seata-config-consul 1.0.0-SNAPSHOT ................. SUCCESS [ 0.354 s]
[INFO] seata-config-etcd3 1.0.0-SNAPSHOT .................. SUCCESS [ 1.071 s]
[INFO] seata-config-spring-cloud 1.0.0-SNAPSHOT ........... SUCCESS [ 0.346 s]
[INFO] seata-discovery 1.0.0-SNAPSHOT ..................... SUCCESS [ 0.241 s]
[INFO] seata-discovery-core 1.0.0-SNAPSHOT ................ SUCCESS [ 0.427 s]
[INFO] seata-core 1.0.0-SNAPSHOT .......................... SUCCESS [ 2.591 s]
[INFO] seata-discovery-custom 1.0.0-SNAPSHOT .............. SUCCESS [ 0.314 s]
[INFO] seata-discovery-consul 1.0.0-SNAPSHOT .............. SUCCESS [ 0.339 s]
[INFO] seata-discovery-eureka 1.0.0-SNAPSHOT .............. SUCCESS [ 0.497 s]
[INFO] seata-discovery-nacos 1.0.0-SNAPSHOT ............... SUCCESS [ 0.499 s]
[INFO] seata-discovery-redis 1.0.0-SNAPSHOT ............... SUCCESS [ 0.399 s]
[INFO] seata-discovery-sofa 1.0.0-SNAPSHOT ................ SUCCESS [ 0.677 s]
[INFO] seata-discovery-zk 1.0.0-SNAPSHOT .................. SUCCESS [ 0.400 s]
[INFO] seata-discovery-etcd3 1.0.0-SNAPSHOT ............... SUCCESS [ 0.489 s]
[INFO] seata-tm 1.0.0-SNAPSHOT ............................ SUCCESS [ 0.750 s]
[INFO] seata-dubbo 1.0.0-SNAPSHOT ......................... SUCCESS [ 0.295 s]
[INFO] seata-dubbo-alibaba 1.0.0-SNAPSHOT ................. SUCCESS [ 0.397 s]
[INFO] seata-sofa-rpc 1.0.0-SNAPSHOT ...................... SUCCESS [ 0.543 s]
[INFO] seata-motan 1.0.0-SNAPSHOT ......................... SUCCESS [ 0.333 s]
[INFO] seata-rm 1.0.0-SNAPSHOT ............................ SUCCESS [ 0.386 s]
[INFO] seata-rm-datasource 1.0.0-SNAPSHOT ................. SUCCESS [ 9.087 s]
[INFO] seata-tcc 1.0.0-SNAPSHOT ........................... SUCCESS [ 0.481 s]
[INFO] seata-codec 1.0.0-SNAPSHOT ......................... SUCCESS [ 0.249 s]
[INFO] seata-codec-seata 1.0.0-SNAPSHOT ................... SUCCESS [ 0.796 s]
[INFO] seata-codec-protobuf 1.0.0-SNAPSHOT ................ SUCCESS [ 15.332 s]
[INFO] seata-codec-kryo 1.0.0-SNAPSHOT .................... SUCCESS [ 1.254 s]
[INFO] seata-codec-all 1.0.0-SNAPSHOT ..................... SUCCESS [ 0.416 s]
[INFO] seata-spring 1.0.0-SNAPSHOT ........................ SUCCESS [ 1.515 s]
[INFO] seata-grpc 1.0.0-SNAPSHOT .......................... SUCCESS [ 1.825 s]
[INFO] seata-codec-hessian ................................ SUCCESS [ 0.546 s]
[INFO] seata-compressor 1.0.0-SNAPSHOT .................... SUCCESS [ 0.278 s]
[INFO] seata-compressor-gzip 1.0.0-SNAPSHOT ............... SUCCESS [ 0.606 s]
[INFO] seata-saga 1.0.0-SNAPSHOT .......................... SUCCESS [ 0.477 s]
[INFO] seata-saga-processctrl 1.0.0-SNAPSHOT .............. SUCCESS [ 0.804 s]
[INFO] seata-saga-statelang 1.0.0-SNAPSHOT ................ SUCCESS [ 0.797 s]
[INFO] seata-saga-engine 1.0.0-SNAPSHOT ................... SUCCESS [ 1.305 s]
[INFO] seata-saga-rm 1.0.0-SNAPSHOT ....................... SUCCESS [ 0.305 s]
[INFO] seata-saga-tm 1.0.0-SNAPSHOT ....................... SUCCESS [ 0.396 s]
[INFO] seata-saga-engine-store 1.0.0-SNAPSHOT ............. SUCCESS [ 0.616 s]
[INFO] Seata All-in-one 1.0.0-SNAPSHOT .................... SUCCESS [ 2.015 s]
[INFO] Seata bom 1.0.0-SNAPSHOT ........................... SUCCESS [ 0.001 s]
[INFO] seata-config-all 1.0.0-SNAPSHOT .................... SUCCESS [ 0.173 s]
[INFO] seata-discovery-all 1.0.0-SNAPSHOT ................. SUCCESS [ 0.175 s]
[INFO] seata-compressor-all 1.0.0-SNAPSHOT ................ SUCCESS [ 0.175 s]
[INFO] seata-metrics 1.0.0-SNAPSHOT ....................... SUCCESS [ 0.151 s]
[INFO] seata-metrics-api 1.0.0-SNAPSHOT ................... SUCCESS [ 0.260 s]
[INFO] seata-metrics-core 1.0.0-SNAPSHOT .................. SUCCESS [ 0.316 s]
[INFO] seata-metrics-registry-compact 1.0.0-SNAPSHOT ...... SUCCESS [ 0.350 s]
[INFO] seata-metrics-exporter-prometheus 1.0.0-SNAPSHOT ... SUCCESS [ 0.313 s]
[INFO] seata-metrics-all 1.0.0-SNAPSHOT ................... SUCCESS [ 0.179 s]
[INFO] seata-server 1.0.0-SNAPSHOT ........................ SUCCESS [ 1.347 s]
[INFO] seata-distribution 1.0.0-SNAPSHOT .................. SUCCESS [ 0.195 s]
[INFO] seata-test 1.0.0-SNAPSHOT .......................... SUCCESS [ 0.246 s]
[INFO] seata-spring-boot-starter 1.0.0-SNAPSHOT ........... SUCCESS [ 1.059 s]
当前的 seata 一共有 62 个模块。除了 parent、all 这几个类型为 pom 的模块,其它 jar 模块可分为以下的几类:
seata-common 包含 seata 的基本模型
seata-config seata 与其它技术集成的配置
seata-core 核心功能逻辑模块
seata-discovery seata 服务注册与发现的相关功能,支持 nacos、eureka、zookeeper 等服务发现方案
seata-tm tm 模块
seata-rm rm 模块
seata-tcc tcc 事务
seata-codec 消息编解码
seata-compressor 消息压缩
seata-saga saga 事务
seata-metrics 度量
seata-server seata 的服务器
seata-spring 与 spring 集成的模块
seata-distribution seata 发布相关
rpc 的模块 seata-grpc、seata-sofa-rpc(蚂蚁金服)、seata-motan(微博)等等
seata 提供了框架接口,非常容易与其它的微服务框架进行对接,目前已支持的 spring cloud 和 dubbo 。
编程模式
在开始学习 seata 的源代码前,可以先下载 seata-examples 里面的示例项目看看具体的应用,挑选其中两三个认真看看就可以。一般来说,集成 seata 需要以下的步骤:
1. 配置 seata 服务器,重点是会话存储方式、与微服务的集成方式
2. 每个微服务都配置客户端,在项目中用对应的方法集成 seata
3. 在全局事务的入口方法处,使用 `@GlobalTransactional` 标记一个全局事务,此事务会传播到它调用的所有的后续服务
@GlobalTransactional
是 seata-spring 提供的注解,标注了此注册的方法,在启动时被 seata-spring 的 GlobalTransactionScanner
扫描到,然后进行代理。
在 springcloud-eureka-seata
示例,bussiness
子项目的 BusinessService
为全局事务的入口,代码为:
/**
* 减库存,下订单
*
* @param userId
* @param commodityCode
* @param orderCount
*/
@GlobalTransactional
public void purchase(String userId, String commodityCode, int orderCount) {
storageFeignClient.deduct(commodityCode, orderCount);
orderFeignClient.create(userId, commodityCode, orderCount);
if (!validData()) {
throw new RuntimeException("账户或库存不足,执行回滚");
}
}
这里使用了 2 个服务的 feign 接口进行调用,如果查看各自的服务,都不需要标注 @GlobalTransactional
,如仓储控制器的入口
@RestController
public class StorageController {
@Autowired
private StorageService storageService;
@RequestMapping(value = "/deduct", produces = "application/json")
public Boolean deduct(String commodityCode, Integer count) {
storageService.deduct(commodityCode, count);
return true;
}
}
编程模式比较简单,其它都靠 seata-server 这个 TC 来协调。