您好,登錄后才能下訂單哦!
這篇文章主要為大家展示了“SpringBoot如何使用GTS”,內(nèi)容簡而易懂,條理清晰,希望能夠幫助大家解決疑惑,下面讓小編帶領(lǐng)大家一起研究并學習一下“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
以上是“SpringBoot如何使用GTS”這篇文章的所有內(nèi)容,感謝各位的閱讀!相信大家都有了一定的了解,希望分享的內(nèi)容對大家有所幫助,如果還想學習更多知識,歡迎關(guān)注億速云行業(yè)資訊頻道!
免責聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。