欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

SpringBoot使用GTS的示例详解

程序员文章站 2022-06-17 22:59:31
1. 依赖类库txc-client.jar, txt-client-spring-cloud-2.0.1.jar2. 使用txcdatasource代理源数据源【注意:dbcp2.basicdatas...

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内容请搜索以前的文章或继续浏览下面的相关文章希望大家以后多多支持!

相关标签: SpringBoot GTS