SpringBoot使用GTS的示例詳解
1. 依賴類庫txc-client.jar, txt-client-spring-cloud-2.0.1.jar
2. 使用TxcDataSource代理源數(shù)據(jù)源【注意:dbcp2.BasicDataSource不支持,可以使用DruidDataSource】
3. 添加自動(dòng)配置類文件
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)測(cè)試的專用事務(wù)分組
url: https://test-cs-gts.aliyuncs.com #公網(wǎng)測(cè)試url
accessKey: xxx #非測(cè)試時(shí)需提供
secretKey: xxxx #非測(cè)試時(shí)需提供
5. 發(fā)送rest請(qǐng)求時(shí),請(qǐng)求添加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)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
SSH框架網(wǎng)上商城項(xiàng)目第25戰(zhàn)之使用java email給用戶發(fā)送郵件
這篇文章主要為大家詳細(xì)介紹了SSH框架網(wǎng)上商城項(xiàng)目第25戰(zhàn)之使用java email給用戶發(fā)送郵件,感興趣的小伙伴們可以參考一下2016-06-06
Java 滑動(dòng)窗口最大值的實(shí)現(xiàn)
這篇文章主要介紹了Java 滑動(dòng)窗口最大值,給定一個(gè)數(shù)組 nums,有一個(gè)大小為 k 的滑動(dòng)窗口從數(shù)組的最左側(cè)移動(dòng)到數(shù)組的最右側(cè)。感興趣的可以了解一下2021-05-05
Spring的@PreAuthorize注解自定義權(quán)限校驗(yàn)詳解
這篇文章主要介紹了Spring的@PreAuthorize注解自定義權(quán)限校驗(yàn)詳解,由于項(xiàng)目中,需要對(duì)外開放接口,要求做請(qǐng)求頭校驗(yàn),不做其他權(quán)限控制,所以準(zhǔn)備對(duì)開放的接口全部放行,不做登錄校驗(yàn),需要的朋友可以參考下2023-11-11
Javaweb動(dòng)態(tài)開發(fā)最重要的Servlet詳解
動(dòng)態(tài)web的核心是Servlet,由tomcat解析并執(zhí)行,本質(zhì)是Java中的一個(gè)類(面向?qū)ο螅┻@個(gè)類的功能十分強(qiáng)大幾乎可以完成全部功能,在Java規(guī)范中只有Servlet實(shí)現(xiàn)類實(shí)例化的對(duì)象才能被瀏覽器訪問,所以掌握Servlet具有重要意義2022-08-08

