https://github.com/dtm-labs/dtm is a revolutionary distributed transaction framework that provides a fool-like way of using it, which greatly reduces the threshold for using distributed transactions The industry status quo of "do not use the type transaction" elegantly solves the problem of data consistency between services.
characteristic
- Supports multiple languages: SDKs that support Go, Java, PHP, C#, Python, Nodejs in various languages
- Support multiple transaction modes: SAGA, TCC, XA, two-phase message (local message table, transaction message)
- Support multiple database transactions: Mysql, Redis, MongoDB, Postgres, TDSQL, etc.
- Support multiple storage engines: Mysql (commonly used), Redis (high performance), MongoDB (planning)
- Supports multiple microservice architectures: go-zero, go-kratos/kratos, polarismesh/polaris
- Support high availability and easy horizontal expansion
Major improvements in the new version of the Java SDK
- Support spring cloud microservice series components
- Introduce pluggable microservice registry dependencies, users can introduce different plugins according to the actual situation
- Optimized the overall package structure
Thanks to the contribution of https://github.com/horseLk , this version update is mainly completed by him
dtmcli-java new version implementation
github warehouse address: https://github.com/dtm-labs/dtmcli-java
Project structure
|--dtmcli-java-parent # parent module
|--dtmcli-common # dtmcli common module
|--dtmcli-core # dtmcli core module
|--dtmcli-java # dtmcli java client
|--dtmcli-springcloud # dtmcli springcloud client
|--*-plugin # dtmcli plugin
dtmcli-java new version quick start
The new version of dtmcli-java provides a variety of different implementation methods, improves and optimizes on the basis of being as compatible as possible with the original Java client, and supports microservices. The new version of the java client provides a variety of flexible implementations that can be selected according to business scenarios.
Usage example
Use the HTTP interface to start a TCC transaction that automatically generates a gid
DtmClient dtmClient;
// 根据dtmsvr自动生成gid
dtmClient.tccGlobalTransaction(new DtmConsumer<Tcc>() {
@Override
public void accept(Tcc tcc) throws Exception {
String svc = "http://127.0.0.1:8888";
Response outResponse = tcc.callBranch( "", svc + "/TransOutTry", svc + "/TransOutConfirm",
svc + "/TransOutCancel");
Response inResponse = tcc.callBranch("", svc + "/TransInTry", svc + "/TransInConfirm",
svc + "/TransInCancel");
}
});
// 使用自定义的gid
dtmClient.tccGlobalTransaction("custom_gid_123456", new DtmConsumer<Tcc>() {
@Override
public void accept(Tcc tcc) throws Exception {
String svc = "http://127.0.0.1:8888";
Response outResponse = tcc.callBranch( "", svc + "/TransOutTry", svc + "/TransOutConfirm",
svc + "/TransOutCancel");
Response inResponse = tcc.callBranch("", svc + "/TransInTry", svc + "/TransInConfirm",
svc + "/TransInCancel");
}
});
Start a TCC transaction using the microservice interface
DtmClient dtmClient;
// 根据dtmsvr自动生成gid
dtmClient.tccGlobalTransaction(new DtmConsumer<Tcc>() {
@Override
public void accept(Tcc tcc) throws Exception {
String appName = "serviceName";
tcc.callBranch(new HashMap<>(), new ServiceMessage(appName, "/TransOutTry"),
new ServiceMessage(appName, "/TransOutConfirm"), new ServiceMessage(appName, "/TransOutCancel"));
tcc.callBranch(new HashMap<>(), new ServiceMessage(appName, "/TransInTry"),
new ServiceMessage(appName, "/TransInConfirm"), new ServiceMessage(appName, "/TransInCancel"));
}
});
// 使用自定义的gid
dtmClient.tccGlobalTransaction("custom_gid_123456", new DtmConsumer<Tcc>() {
@Override
public void accept(Tcc tcc) throws Exception {
String appName = "serviceName";
tcc.callBranch(new HashMap<>(), new ServiceMessage(appName, "/TransOutTry"),
new ServiceMessage(appName, "/TransOutConfirm"), new ServiceMessage(appName, "/TransOutCancel"));
tcc.callBranch(new HashMap<>(), new ServiceMessage(appName, "/TransInTry"),
new ServiceMessage(appName, "/TransInConfirm"), new ServiceMessage(appName, "/TransInCancel"));
}
});
Start a SAGA transaction using the HTTP interface
DtmClient dtmClient;
String svc = "127.0.0.1:8888";
// 从dtmsvr获取gid
Saga saga = dtmClient
.newSaga()
.add(svc + "/TransOut", svc + "/TransOutCompensate", "")
.add(svc + "/TransIn", svc + "/TransInCompensate", "")
.enableWaitResult()
.submit();
// 自定义gid
Saga saga = dtmClient
.newSaga("custom_gid_123456")
.add(svc + "/TransOut", svc + "/TransOutCompensate", "")
.add(svc + "/TransIn", svc + "/TransInCompensate", "")
.enableWaitResult()
.submit();
Start a SAGA transaction using the microservice interface
DtmClient dtmClient;
String serviceName = "serviceName";
// 从dtmsvr获取gid
Saga saga = dtmClient
.newSaga()
.add(new ServiceMessage(serviceName, "/TransOut"), new ServiceMessage(serviceName, "/TransOutCompensate"), "")
.add(new ServiceMessage(serviceName, "/TransIn"), new ServiceMessage(serviceName, "/TransInCompensate"), "")
.enableWaitResult()
.submit();
// 自定义gid
Saga saga = dtmClient
.newSaga("custom_gid_123456")
.add(new ServiceMessage(serviceName, "/TransOut"), new ServiceMessage(serviceName, "/TransOutCompensate"), "")
.add(new ServiceMessage(serviceName, "/TransIn"), new ServiceMessage(serviceName, "/TransInCompensate"), "")
.enableWaitResult()
.submit();
How to get DtmClient object
spring-cloud client
The DtmClient object has been injected into the container, just need to be imported through the @Autowired annotation in the class you need to use.
@Autowired
private DtmClient dtmClient;
dtmcli-java client
Direct connection to dtmsvr
Just pass the service address of dtmsvr to the constructor.
DtmClient dtmClient = new DtmClient("127.0.0.1:36789");
Of course, if you want to set the address of the direct connection to the configuration file, you only need to add a configuration file named dtm-conf.properties. The format of the configuration file is as follows:
# dtmsvr地址
dtm.ipport=121.4.131.37:36789
Use service discovery
If you use service discovery and the currently supported springcloud version is not compatible with your project version, you can use the dtmcli-java version, just add a configuration file named dtm-conf.properties, the configuration file format is as follows:
# 服务中心地址
serverAddr=127.0.0.1:8848
# 服务中心登录username
username=nacos
# 服务中心登录密码
password=nacos
# namespace
namespace=c3dc917d-906a-429d-90a9-85012b41014e
# dtmsvr注册到服务中心的服务名
dtm.service.name=dtmService
# 服务中心类型
dtm.service.registryType=nacos
Note: We have two different configuration methods for a configuration file. If both are used at the same time, the direct connection method will be preferred.
pom introduces dtmcli java client
springboot version >= 2.6.0
# dtmcli依赖
<dependency>
<groupId>io.github.dtm-labs</groupId>
<artifactId>dtmcli-springcloud</artifactId>
<version>2.1.4.2</version>
</dependency>
# 使用的服务中心插件,如果您的项目中已经引入了相关依赖,则可以忽略
<dependency>
<groupId>io.github.dtm-labs</groupId>
<artifactId>nacos-plugin</artifactId>
<version>2.1.4.2</version>
</dependency>
2.4.0 <= springboot version < 2.6.0
# dtmcli依赖
<dependency>
<groupId>io.github.dtm-labs</groupId>
<artifactId>dtmcli-springcloud</artifactId>
<version>2.1.4.1</version>
</dependency>
# 使用的服务中心插件,如果您的项目中已经引入了相关依赖,则可以忽略
<dependency>
<groupId>io.github.dtm-labs</groupId>
<artifactId>nacos-plugin</artifactId>
<version>2.1.4.1</version>
</dependency>
Other cases
# dtmcli依赖
<dependency>
<groupId>io.github.dtm-labs</groupId>
<artifactId>dtmcli-java</artifactId>
<version>2.1.4</version>
</dependency>
# 如果有需要也可以引入相应的服务中心插件
Rapid expansion of the new version of dtmcli-java
At present, dtmcli-java has supported some open source communication service frameworks and service center frameworks, and will support as many communication service frameworks and service center frameworks as possible according to your demands. But if your project uses the company's internal communication service frame and you want to access dtmsvr, you need to complete simple operations to adapt your framework.
The new version of the client provides the IDtmServerStub interface to expand different communication frameworks. You only need to use the communication service framework used in your project to implement the IDtmServerStub interface, and use it to construct a DtmClient client. Considering that projects that need to use self-developed frameworks generally do not use the open source springcloud, and in order to prevent problems caused by the incompatibility of your framework and springcloud, this constructor can only be used in the dtmcli-java version.
public DtmClient(IDtmServerStub dtmServerStub) {
this.dtmServerStub = dtmServerStub;
}
sample code
dtm-springcloud sample code
https://github.com/dtm-labs/dtmcli-java-spring-sample
dtm-java is used in the spring project
https://github.com/dtm-labs/dtmcli-java-sample
**粗体** _斜体_ [链接](http://example.com) `代码` - 列表 > 引用
。你还可以使用@
来通知其他用户。