Official java client for distributed transaction framework dtm
简体中文 | English
dtmcli-java
是分布式事务管理器 dtm 的Java客户端sdk
DTM是一款变革性的分布式事务框架,提供了傻瓜式的使用方式,极大的降低了分布式事务的使用门槛,改变了“能不用分布式事务就不用”的行业现状,优雅的解决了服务间的数据一致性问题。
artifact | version | 适用版本 | 备注 |
---|---|---|---|
dtmcli-springcloud | 2.1.4.1 | 2.4.0 <= springboot version < 2.5.13 | springboot 版本>=2.5.0,需要设置spring.cloud.compatibility-verifier.enabled=false |
dtmcli-springcloud | 2.1.4.2 | 2.6.0 <= springboot version < 2.6.latest | |
dtmcli-java | 2.1.4 | others |
Maven:
<dependency>
<groupId>io.github.dtm-labs</groupId>
<artifactId>dtmcli-springcloud</artifactId>
<version>${dtmcli.version}</version>
</dependency>
Gradle:
dependencies {
implementation 'io.github.dtm-labs:dtmcli-springcloud:${dtmcli.version}'
}
如果您引入了dtmcli-java,则需要新建一个dtm-conf.properties
配置文件
serverAddr=127.0.0.1:8848
username=nacos
password=nacos
namespace=c3dc917d-906a-429d-90a9-85012b41014e
dtm.service.name=dtmService
dtm.service.registryType=nacos
dtm.ipport=127.0.0.1:36789
@RequestMapping("testTcc")
public String testTcc() {
//创建dtm clinet
DtmClient dtmClient = new DtmClient(ipPort);
//创建tcc事务
try {
dtmClient.tccGlobalTransaction(dtmClient.genGid(), TccTestController::tccTrans);
} catch (Exception e) {
log.error("tccGlobalTransaction error", e);
return "fail";
}
return "success";
}
/**
* 定义tcc事务函数,内部需要通过callBranch注册事务子分支
*
* @param tcc
* @return
* @see TransController
*/
public static void tccTrans(Tcc tcc) throws Exception {
Response outResponse = tcc
.callBranch("", svc + "/TransOutTry", svc + "/TransOutConfirm", svc + "/TransOutCancel");
log.info("outResponse:{}", outResponse);
Response inResponse = tcc.callBranch("", svc + "/TransInTry", svc + "/TransInConfirm", svc + "/TransInCancel");
log.info("inResponse:{}", inResponse);
}
dtmcli-java-sample
dtmcli-java-sample-use-configuration