seata 項目結構
項目結構
seata 的代碼開源在 github https://github.com/seata 。除了主代碼 seata ,還有一些其它的項目,如 seata-samples / website 等等。git
seata 主項目使用 maven 進行管理,劃分爲多個模塊,運行 mvn compile
從編譯結果裏面可看到github
[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 模塊可分爲如下的幾類:web
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 。redis
編程模式
在開始學習 seata 的源代碼前,能夠先下載 seata-examples 裏面的示例項目看看具體的應用,挑選其中兩三個認真看看就能夠。通常來講,集成 seata 須要如下的步驟:spring
1. 配置 seata 服務器,重點是會話存儲方式、與微服務的集成方式 2. 每一個微服務都配置客戶端,在項目中用對應的方法集成 seata 3. 在全局事務的入口方法處,使用 `@GlobalTransactional` 標記一個全局事務,此事務會傳播到它調用的全部的後續服務
@GlobalTransactional
是 seata-spring 提供的註解,標註了此註冊的方法,在啓動時被 seata-spring 的 GlobalTransactionScanner
掃描到,而後進行代理。編程
在 springcloud-eureka-seata
示例,bussiness
子項目的 BusinessService
爲全局事務的入口,代碼爲:json
/** * 減庫存,下訂單 * * @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
,如倉儲控制器的入口api
@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 來協調。服務器