iis服务器助手广告广告
返回顶部
首页 > 资讯 > 后端开发 > Python >SpringBoot使用GTS的示例详解
  • 330
分享到

SpringBoot使用GTS的示例详解

2024-04-02 19:04:59 330人浏览 泡泡鱼

Python 官方文档:入门教程 => 点击学习

摘要

1. 依赖类库txc-client.jar, txt-client-spring-cloud-2.0.1.jar 2. 使用TxcDataSource代理源数据源【注意:dbcp2.

1. 依赖类库txc-client.jar, txt-client-spring-cloud-2.0.1.jar

2. 使用TxcDataSource代理源数据源【注意: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"}
    )
    //定义声明式事务,要想让事务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 #公网测试的专用事务分组
           url: https://test-cs-gts.aliyuncs.com #公网测试url
           accessKey: xxx #非测试时需提供
           secretKey: xxxx #非测试时需提供

5. 发送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. 发起全局事务使用注解@TxcTransaction

到此这篇关于SpringBoot使用GTS的示例详解的文章就介绍到这了,更多相关SpringBoot使用GTS内容请搜索编程网以前的文章或继续浏览下面的相关文章希望大家以后多多支持编程网!

--结束END--

本文标题: SpringBoot使用GTS的示例详解

本文链接: https://www.lsjlt.com/news/137771.html(转载时请注明来源链接)

有问题或投稿请发送至: 邮箱/279061341@qq.com    QQ/279061341

本篇文章演示代码以及资料文档资料下载

下载Word文档到电脑,方便收藏和打印~

下载Word文档
猜你喜欢
软考高级职称资格查询
编程网,编程工程师的家园,是目前国内优秀的开源技术社区之一,形成了由开源软件库、代码分享、资讯、协作翻译、讨论区和博客等几大频道内容,为IT开发者提供了一个发现、使用、并交流开源技术的平台。
  • 官方手机版

  • 微信公众号

  • 商务合作