SpringBoot使用GTS的示例詳解
1. 依賴類庫txc-client.jar, txt-client-spring-cloud-2.0.1.jar
2. 使用TxcDataSource代理源數(shù)據(jù)源【注意:dbcp2.BasicDataSource不支持,可以使用DruidDataSource】
3. 添加自動配置類文件
package com.bodytrack.restapi;
import com.taobao.txc.client.aop.TxcTransactionScaner;
import com.taobao.txc.client.boot.TxcSpringBootProperties;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.DependsOn;
@Configuration
@EnableConfigurationProperties({TxcSpringBootProperties.class})
public class TxcSpringBootAutoConfiguration {
@Autowired
private TxcSpringBootProperties txcSpringBootProperties;
@Autowired
private ApplicationContext applicationContext;
private static boolean isEmpty(String str) {
return str == null || str.length() == 0;
}
@Bean(name = "txcScanner")
@ConditionalOnProperty(
prefix = "spring.boot.txc",
name = {"txcServerGroup"}
)
//定義聲明式事務(wù),要想讓事務(wù)annotation感知的話,要在這里定義一下
public TxcTransactionScaner txcTransactionScaner() {
String appName = this.txcSpringBootProperties.getTxcAppName() == null ? this.applicationContext.getEnvironment().getProperty("spring.application.name") : this.txcSpringBootProperties.getTxcAppName();
String txServiceGroup = this.txcSpringBootProperties.getTxcServerGroup();
int mode = this.txcSpringBootProperties.getMode() == 0 ? 1 : this.txcSpringBootProperties.getMode();
TxcTransactionScaner txcTransactionScanner = new TxcTransactionScaner(appName, txServiceGroup, mode, this.txcSpringBootProperties.getUrl());
if (!isEmpty(this.txcSpringBootProperties.getAccessKey())) {
txcTransactionScanner.setAccessKey(this.txcSpringBootProperties.getAccessKey());
}
if (!isEmpty(this.txcSpringBootProperties.getSecretKey())) {
txcTransactionScanner.setSecretKey(this.txcSpringBootProperties.getSecretKey());
}
return txcTransactionScanner;
}
}
4. 添加GTS配置
spring:
boot:
txc:
txcAppName: demo
txcServerGroup: txc_test_public.1129361738553704.QD #公網(wǎng)測試的專用事務(wù)分組
url: https://test-cs-gts.aliyuncs.com #公網(wǎng)測試url
accessKey: xxx #非測試時需提供
secretKey: xxxx #非測試時需提供
5. 發(fā)送rest請求時,請求添加header(TXC_XID,BEGIN_COUNT,COMMIT_COUNT)
public String callTestTxc() {
HttpHeaders requestHeaders = new HttpHeaders();
requestHeaders.set("TXC_XID", String.valueOf(TxcContext.getCurrentXid()));
requestHeaders.set("BEGIN_COUNT", String.valueOf(TxcContext.getBeginCount()));
requestHeaders.set("COMMIT_COUNT", String.valueOf(TxcContext.getCommitCount()));
HttpEntity<String> entity = new HttpEntity<>("parameters", requestHeaders);
String restUrl = String.format("%s/api/scoreService/testTxc", "http://10.0.0.5:8762");
ResponseEntity<String> restData = restTemplate.exchange(restUrl, HttpMethod.GET, entity, String.class);
return restData.toString();
}
6. 發(fā)起全局事務(wù)使用注解@TxcTransaction
到此這篇關(guān)于SpringBoot使用GTS的示例詳解的文章就介紹到這了,更多相關(guān)SpringBoot使用GTS內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Spring Security 實現(xiàn)多種登錄方式(常規(guī)方式外的郵件、手機驗證碼登錄)
本文主要介紹了Spring Security 實現(xiàn)多種登錄方式(常規(guī)方式外的郵件、手機驗證碼登錄),文中通過示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下2022-01-01
Java設(shè)計模式之備忘錄模式實現(xiàn)對象狀態(tài)的保存和恢復(fù)
本文介紹Java設(shè)計模式之備忘錄模式,該模式可以實現(xiàn)對象狀態(tài)的保存和恢復(fù)。通過詳細(xì)講解備忘錄模式的原理、實現(xiàn)方法和應(yīng)用場景,幫助讀者深入理解該設(shè)計模式,并提供示例代碼和技巧,便于讀者實際應(yīng)用2023-04-04
JAVA使用爬蟲抓取網(wǎng)站網(wǎng)頁內(nèi)容的方法
這篇文章主要介紹了JAVA使用爬蟲抓取網(wǎng)站網(wǎng)頁內(nèi)容的方法,實例分析了java爬蟲的兩種實現(xiàn)技巧,具有一定參考借鑒價值,需要的朋友可以參考下2015-07-07
Apache?Commons?Imaging處理圖像實例深究
這篇文章主要為大家介紹了Apache?Commons?Imaging處理圖像的實例探索深究,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-12-12
Java C++解決在排序數(shù)組中查找數(shù)字出現(xiàn)次數(shù)問題
本文終于介紹了分別通過Java和C++實現(xiàn)統(tǒng)計一個數(shù)字在排序數(shù)組中出現(xiàn)的次數(shù)。文中詳細(xì)介紹了實現(xiàn)思路,感興趣的小伙伴可以跟隨小編學(xué)習(xí)一下2021-12-12

