天天看點

springcloud 內建seata1.4.2

前言

官網上的demo中的seata版本很多都不是最新的,寫得好的部落格中的版本也不是最新的,大多是1.0.0,1.1.0版本的,或者沒有內建seata-spring-boot-starter,還需要配置.conf 檔案,或者沒有內建spring-cloud-starter-alibaba-seata,還需要自己寫傳遞xid的過濾器.新的版本內建起來确實更加便捷,但是由于沒有現成的demo,是以還是費了不少勁.由于官方沒有比較新的版本的demo,是以就隻有通讀官方官方文檔,找版本的更新日志和說明,慢慢嘗試內建.

難點

關于事務分組的了解:

我開始比較疑惑,其實就是一個資源的邏輯分組,當一個組的TC失效後,可以馬上切換到另外的分組

新版本內建

官網說明.

  1. seata-spring-boot-starter:

    支援yml、properties配置(.conf可删除),内部已依賴seata-all

  2. spring-cloud-alibaba-seata:

    内部內建了seata,并實作了xid傳遞

  3. spring-cloud-starter-alibaba-seata:

    內建了seata-spring-boot-starter,是以擁有了seata-spring-boot-starter的功能,并實作了xid傳遞,自己不需要寫傳遞xid的過濾器了

  4. 是以內建後的pom為:
<!--
	spring-cloud-starter-alibaba-seata 中預設依賴的seata-spring-boot-starter 版本是1.1.0,官方說要這樣用最新的版本,關于為什麼用2.2.1.RELEASE,這個版本可以用在我的springcloud版本中 
	-->
		<dependency>
			<groupId>io.seata</groupId>
			<artifactId>seata-spring-boot-starter</artifactId>
			<version>1.4.2</version>
		</dependency>
		<dependency>
			<groupId>com.alibaba.cloud</groupId>
			<artifactId>spring-cloud-starter-alibaba-seata</artifactId>
			<version>2.2.1.RELEASE</version>
			<exclusions>
				<exclusion>
					<groupId>io.seata</groupId>
					<artifactId>seata-spring-boot-starter</artifactId>
				</exclusion>
			</exclusions>
		</dependency>
           
  1. 然後內建後的項目不需要.conf檔案.可以通過.properties或者.yml檔案配置
    springcloud 內建seata1.4.2

我的demo 說明

  1. AT 模式
  2. 庫初始化腳本在 try-business-service子產品下的db檔案夾中
  3. 服務之間的調用通過restTemplate,如果通過openFeign調用也不需要改動任何配置
  4. 為了簡單,seata-server采用預設的file模式存儲分布式事務會話資訊,如果要改成db,這個看看官網改起來比較簡單(官方demo.)
  5. 在我的demo中,seata-server 采用配置方式為file,從官網下載下傳後啥都不用改,直接輕按兩下seata-server.bat(如下截圖)啟動server就行了(seata-server下載下傳位址)
    springcloud 內建seata1.4.2
  6. 架構,down下官方demo,然後改了改
    springcloud 內建seata1.4.2
  7. 核心業務是在 try-business-service 的BusinessController 的purchaseCommit方法,對應BusinessService中的purchase方法
    springcloud 內建seata1.4.2
  8. 測試方法

    BusinessService.purchase()中 // int i=1/0;(處于注釋狀态),業務正常進行,資料庫産生業務資料

    BusinessService.purchase()中 int i=1/0;(會抛出異常),出現異常,全局復原

  9. 其他要點說明

    9.1 這個是為了生成一個固定的事務分組名稱,因為預設是每個微服務都有各自的分組(官方參考),注意需要和配置檔案中的seata.service.vgroup-mapping.xxx保持一緻

    springcloud 內建seata1.4.2
springcloud 內建seata1.4.2
  1. 從gitee上down下demo,試一下吧,很簡單的~
    springcloud 內建seata1.4.2
    springcloud 內建seata1.4.2

我的demo gitee.