天天看點

spring cloud整合GTS全局分布式事務服務(超簡單,一鍵解決分布式事物)——阿裡雲注意事項:一:maven包:必須引入的三方包 二:maven包:gts所需的jar包三:spring注入資料庫連接配接,并嵌套入gts四:配置檔案,定義使用gts位址五:資料庫表sql,請注意DRDS+ PolarDB,不需要,自帶!!!六:feign,rpc使用

注意事項:

1:mysql庫,必須大小寫不敏感

2:所有項目必須關閉,自定義熔斷處理。

feign: hystrix: enabled: true

3:需要的包必須引完整

4:必須要txc_undo_log表,(DRDS+ PolarDB,每個資料庫自帶,不需要建立)

5:資料庫連接配接類型,盡量選擇druid,不推薦使用别的連接配接類型

6:所有報錯日志在:使用者目錄 logs/txc/txc.pid.log (pid 為應用的程序号,按更新時間來定位也可以,一般取最新的)

7:請注意,各個版本相容問題!部落客用的

boot版本:2.2.5.RELEASE 
cloud版本:Hoxton.SR3 
nacos版本:2.2.1.RELEASE
           

8:請注意rpc調用逾時時間問題,預設rpc逾時時間必然不夠用,請自定義添加時間

9:請注意,需要開啟事物的地方添加@TxcTransaction,之後所有rpc調用禁止添加@TxcTransaction,否則轉播過去的事物id,會被重新開啟覆寫,變為新事物。

10:官方樣例:https://help.aliyun.com/document_detail/126129.html?spm=5176.8135549.1306590.btn7.37af6ddc9Ih9cm(官方樣例一堆問題,請大家自行解決)

11:正式環境:1:隻支援ecs伺服器。2:配置的分布式事物服務,區域必須和伺服器區域一緻

一:maven包:必須引入的三方包

<!--
        在應用中依賴 GTS 時, 下面的依賴是必需的.
        如果應用項目中已經包含, 不必額外引入, 版本以應用所使用的版本為準即可.
        如果項目本身不包含這些依賴, 可以按使用下面的推薦版本引入依賴.
        -->
        <dependency>
            <groupId>com.taobao.middleware</groupId>
            <artifactId>logger.api</artifactId>
            <version>0.2.7</version>
        </dependency>
        <dependency>
            <groupId>com.taobao.diamond</groupId>
            <artifactId>diamond-client</artifactId>
            <version>edas-3.7.3</version>
        </dependency>
        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>fastjson</artifactId>
            <version>1.2.58</version>
        </dependency>
        <dependency>
            <groupId>org.apache.httpcomponents</groupId>
            <artifactId>httpclient</artifactId>
            <version>4.5.2</version>
        </dependency>
        <dependency>
            <groupId>io.netty</groupId>
            <artifactId>netty-all</artifactId>
            <version>4.0.33.Final</version>
        </dependency>
        <dependency>
            <groupId>commons-lang</groupId>
            <artifactId>commons-lang</artifactId>
            <version>2.6</version>
        </dependency>
        <dependency>
            <groupId>com.google.guava</groupId>
            <artifactId>guava</artifactId>
            <version>18.0</version>
        </dependency>
        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>druid-spring-boot-starter</artifactId>
            <version>1.1.10</version>
        </dependency>
           

二:maven包:gts所需的jar包

下載下傳位址:

http://txc-console.oss-cn-beijing.aliyuncs.com/sdk/txc-client-2.9.1.jar

http://txc-console.oss-cn-beijing.aliyuncs.com/sdk/txc-client-springcloud-2.9.1.jar

jar存放位址,自定義。

<dependency>
            <groupId>com.taobao.txc</groupId>
            <artifactId>txc-client</artifactId>
            <version>${gts.sdk.version}</version>
            <scope>system</scope>
            <systemPath>${project.basedir}/lib/txc-client-${gts.sdk.version}.jar</systemPath>
        </dependency>
        <dependency>
            <groupId>com.taobao.txc</groupId>
            <artifactId>txc-client-springcloud</artifactId>
            <version>${gts.sdk.version}</version>
            <scope>system</scope>
            <systemPath>${project.basedir}/lib/txc-client-springcloud-${gts.sdk.version}.jar</systemPath>
        </dependency>
           

三:spring注入資料庫連接配接,并嵌套入gts

請自行定義,輸入的類

@Bean
    public DruidDataSource dataSource() {
        DruidDataSource druidDataSource = new DruidDataSource();
        druidDataSource.setUrl(mysqlUrl);
        druidDataSource.setDriverClassName(mysqlDriverClassName);
        druidDataSource.setUsername(mysqlUsername);
        druidDataSource.setPassword(mysqlPassword);
        return druidDataSource;
    }

    @Primary
    @DependsOn({"dataSource"})
    @Bean("dataSourceProxy")
    public TxcDataSource dataSourceProxy(DruidDataSource dataSource) {
        TxcDataSource dataSourceProxy = new TxcDataSource(dataSource);
        return dataSourceProxy;
    }
           

四:配置檔案,定義使用gts位址

本地環境使用:

yml:

spring:
  application:
    name: 自定義
    txc:
      txcAppName: 自定義
      txcServerGroup: txc_test_public.1129361738553704.QD
      url: https://test-cs-gts.aliyuncs.com
           

properties:

spring.application.name=自定義應用名

# - txcAppName: spring.cloud.txc.txcAppName(自定義應用名)
spring.cloud.txc.txcAppName=自定義應用名

# - txcServerGroup: spring.cloud.txc.txcServerGroup(事務分組)
# txc_test_public.1129361738553704.QD 是公網測試的專用事務分組
# 在阿裡雲環境運作時,請使用自己在官網開通的事務分組(GTS執行個體)
spring.cloud.txc.txcServerGroup=txc_test_public.1129361738553704.QD


# - url: spring.cloud.txc.url(服務發現位址)
# 本地公網測試的服務發現位址, 配置如下
# 在阿裡雲環境運作時, 有預設位址, 請删除該配置
spring.cloud.txc.url=https://test-cs-gts.aliyuncs.com
           

生成環境使用:

yml:

spring:
  application:
    name: 自定義
    txc:
      txcAppName: 自定義
      txcServerGroup: 購買的阿裡雲事務分組全稱
      accessKey: XXXX
      secretKey: XXXX
           

properties:

spring.application.name=自定義應用名

# - txcAppName: spring.cloud.txc.txcAppName(自定義應用名)
spring.cloud.txc.txcAppName=自定義應用名

# - txcServerGroup: spring.cloud.txc.txcServerGroup(事務分組)
# txc_test_public.1129361738553704.QD 是公網測試的專用事務分組
# 在阿裡雲環境運作時,請使用自己在官網開通的事務分組(GTS執行個體)
spring.cloud.txc.txcServerGroup=在阿裡雲環境運作時,請使用自己在官網開通的事務分組(GTS執行個體)


# 下面兩個配置對應 TxcTransactionScaner 的 accessKey/secretKey 兩個屬性
# 在阿裡雲環境運作時, 請配置擁有 spring.cloud.txc.txcServerGroup 指定的事務分組(GTS執行個體) 運作權限的使用者的 AK/SK
# 本地公網測試不需要該配置
spring.cloud.txc.accessKey=xxx
spring.cloud.txc.secretKey=xxx
           

五:資料庫表sql,請注意DRDS+ PolarDB,不需要,自帶!!!

DROP TABLE IF EXISTS `txc_undo_log`;
CREATE TABLE `txc_undo_log` (
  `id` bigint(20) NOT NULL AUTO_INCREMENT COMMENT '主鍵',
  `gmt_create` datetime NOT NULL COMMENT '建立時間',
  `gmt_modified` datetime NOT NULL COMMENT '修改時間',
  `xid` varchar(100) NOT NULL COMMENT '全局事務ID',
  `branch_id` bigint(20) NOT NULL COMMENT '分支事務ID',
  `rollback_info` longblob NOT NULL COMMENT 'LOG',
  `status` int(11) NOT NULL COMMENT '狀态',
  `server` varchar(32) NOT NULL COMMENT '伺服器',
  PRIMARY KEY (`id`),
  KEY `idx_unionkey` (`xid`,`branch_id`)
) ENGINE=InnoDB AUTO_INCREMENT=1 DEFAULT CHARSET=utf8 COMMENT='事務日志表';
           

六:feign,rpc使用

1:每個事物開啟使用,方法前打開注解:

import com.taobao.txc.client.aop.annotation.TxcTransaction;
@TxcTransaction
           

2:請注意,每個事物使用前,請先列印,看分布式事物xid是否一緻或者為空!!!!!!!!!!!

String xid = TxcContext.getCurrentXid();
log.info("xid: " + xid);
           

3:之後就正常的feign,rpc調用,隻要異常,gts分布式事物,就會進行統一復原。

繼續閱讀