最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

SpringBoot使用GTS的示例詳解

 更新時間:2021年10月11日 15:18:41   作者:jmbkeyes  
這篇文章主要介紹了SpringBoot使用GTS的示例詳解,代碼簡單易懂,對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下

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)文章

最新評論

托里县| 闻喜县| 东兰县| 金门县| 萨迦县| 云梦县| 白河县| 西充县| 武功县| 桂阳县| 汽车| 东明县| 土默特左旗| 台湾省| 南城县| 贞丰县| 旬阳县| 丹寨县| 平塘县| 基隆市| 迭部县| 阿拉善右旗| 米易县| 修水县| 榆树市| 山西省| 阿荣旗| 贵阳市| 如皋市| 亚东县| 丰城市| 巴青县| 敦化市| 合川市| 家居| 兴山县| 东城区| 邹城市| 彰化市| 湄潭县| 永寿县|