Skip to content

Commit

Permalink
Merge pull request #33 from dtm-labs/springcloud
Browse files Browse the repository at this point in the history
add Springcloud support
  • Loading branch information
yedf2 authored May 24, 2022
2 parents 6eb7419 + 3306371 commit 7a15656
Show file tree
Hide file tree
Showing 53 changed files with 2,838 additions and 826 deletions.
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -24,4 +24,4 @@ hs_err_pid*

.idea
*.iml
/target
/*/target/
2 changes: 1 addition & 1 deletion LICENSE
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
MIT License

Copyright (c) 2021 yedf
Copyright (c) 2022 dtm-labs

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
Expand Down
17 changes: 17 additions & 0 deletions NewVersion.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
# new version
新版本相比于旧版本Java client的的改造
## 旧版本客户端
![avatar](pic_ref/oldversion.png)
### 旧版本在设计上可以改进的一些地方
1.引入的类从引入路径看不出来自哪个jar包,比如exception.FailureException。在大型项目中可能会出现多个相同的类名,如果不明确包名可能会导致意义混乱,甚至引入冲突。
2.tcc等类直接依赖了dtmsvr信息,这样的依赖方式对后期的维护不友好,如果修改了DtmServerInfo的属性,那么后期需要修改所有支持的方式。应该做一个客户端这样的东西专门用来发送请求。
3.向dtmsvr传递参数时使用了map来传递,虽然这对用户是无感知的,但是客户在debug的时候使用map比较麻烦,感觉使用一个param对象比较好
## 新版本客户端
![avatar](pic_ref/newversion.png)
### 新版本中改进的地方
1.由于新版本需要同时支持http和feign两种交互方式,因此将请求dtmsvr相关的部分以及整个client的公共部分抽取出来形成了dtmcli-common模块,这样后续如果需要支持更多的交互方式只需要改动dtmcli-common中的代码,并在core新增代码
2.springcloud版本的client不在乎需要传入ipport这样的配置参数,一切通过feign+nacos/euraka集成起来处理。因此将java-client和spring-client分开处理了
3.对于一些非springcloud的项目我们也提供了java-client来处理,java-client支持像以前那样直接配置endpoint的方式,同时为了适配frontend服务,也支持通过配置nacos服务中心地址这样的方式来动态查找dtmsvr具体的地址。
4.优化了整个项目的包结构,让引入dtmcli-java的时候能够快速找到来自哪个包


66 changes: 37 additions & 29 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -45,50 +45,54 @@ DTM是一款跨语言的开源分布式事务管理器,优雅的解决了幂

## 使用方式

### 步骤一:JitPack 存储库添加到您的构建文件

Maven:

```bash
<repositories>
<repository>
<id>jitpack.io</id>
<url>https://jitpack.io</url>
</repository>
</repositories>
```

Gradle:

```bash
allprojects {
repositories {
...
maven { url 'https://jitpack.io' }
}
}
```
### 步骤一:确定你需要使用的版本
1. 您的项目是springcloud项目
- 您的项目中springboot版本>=2.4.0,请选择dtmcli-springcloud相应的版本直接接入即可
- 您的项目中的springboot版本<2.4.0,请选择dtmcli-java接入,dtmcli-java也提供了微服务相关的接口,请设置nacos服务中心的相关配置即可使用
2. 您的项目是普通项目/没有接入微服务的spring(boot)项目
- 请选择dtmcli-java,并设置相应的配置即可

| 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:

```bash
<dependency>
<groupId>com.github.yedf</groupId>
<artifactId>dtmcli-java</artifactId>
<version>Tag</version>
<groupId>io.github.dtm-labs</groupId>
<artifactId>dtmcli-springcloud</artifactId>
<version>${dtmcli.version}</version>
</dependency>
```

Gradle:

```bash
dependencies {
implementation 'com.github.yedf:dtmcli-java:Tag'
implementation 'io.github.dtm-labs:dtmcli-springcloud:${dtmcli.version}'
}
```

### 步骤三:设置dtmcli-java配置
如果您引入了dtmcli-java,则需要新建一个`dtm-conf.properties`配置文件
- 情形一:您引入了nacos等服务中心组件的配置文件
```
serverAddr=127.0.0.1:8848
username=nacos
password=nacos
namespace=c3dc917d-906a-429d-90a9-85012b41014e
dtm.service.name=dtmService
dtm.service.registryType=nacos
```
- 情形二:您直连dtmsvr
```
dtm.ipport=127.0.0.1:36789
```
## 示例

```bash
Expand Down Expand Up @@ -125,10 +129,14 @@ dependencies {

### 完整示例

[dtmcli-java-sample](https://github.com/dtm-labs/dtmcli-java-sample)
#### dtmcli-java使用示例
[dtmcli-java-sample](https://github.com/dtm-labs/dtmcli-java-sample)
[dtmcli-java-sample-use-configuration](https://github.com/horseLk/dtmcli-java-sample-with-conf)
#### dtmcli-springcloud使用示例
[dtmcli-java-spring-sample](https://github.com/dtm-labs/dtmcli-java-spring-sample)

### License

[MIT](https://github.com/dtm-labs/dtmcli/blob/master/LICENSE)

[license-badge]: https://img.shields.io/github/license/dtm-labs/dtmcli-py
[license-badge]: https://img.shields.io/github/license/dtm-labs/dtmcli-py
60 changes: 60 additions & 0 deletions dtmcli-common/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<parent>
<artifactId>dtmcli-java-parent</artifactId>
<groupId>io.github.dtm-labs</groupId>
<version>2.1.4</version>
</parent>
<modelVersion>4.0.0</modelVersion>

<artifactId>dtmcli-common</artifactId>
<version>2.1.4</version>
<packaging>jar</packaging>
<name>dtmcli-common</name>

<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
</properties>

<dependencies>

<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
</dependency>

<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</dependency>

<dependency>
<groupId>io.github.openfeign</groupId>
<artifactId>feign-core</artifactId>
</dependency>

<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-lang3</artifactId>
</dependency>

<dependency>
<groupId>com.squareup.okhttp3</groupId>
<artifactId>okhttp</artifactId>
</dependency>

<dependency>
<groupId>com.alibaba.nacos</groupId>
<artifactId>nacos-client</artifactId>
</dependency>

<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
</dependency>
</dependencies>
</project>
66 changes: 66 additions & 0 deletions dtmcli-common/src/main/java/pub/dtm/client/constant/Constants.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
/*
* MIT License
*
* Copyright (c) 2022 dtm-labs
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/

package pub.dtm.client.constant;

/**
* Constants
*
* @author horseLk
*/
public class Constants {
public static final String MICRO_SERVICE_NAME_KEY = "dtm.service.name";

public static final String GET_METHOD = "GET ";

public static final String POST_METHOD = "POST ";

public static final String HTTP_PREFIX = "http://";

public static final String HTTPS_PREFIX = "https://";

public static final String PING_URL = "/api/ping";

private static final String BASE_URL = "/api/dtmsvr";

public static final String NEW_GID_URL = BASE_URL + "/newGid";

public static final String PREPARE_URL = BASE_URL + "/prepare";

public static final String SUBMIT_URL = BASE_URL + "/submit";

public static final String ABORT_URL = BASE_URL + "/abort";

public static final String REGISTER_BRANCH_URL = BASE_URL + "/registerBranch";

public static final String DEFAULT_STATUS = "prepared";

public static final String EMPTY_STRING = "";

public static final String SUCCESS_RESULT = "SUCCESS";

public static final String FAILURE_RESULT = "FAILURE";

public static final int RESP_ERR_CODE = 400;
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/*
* MIT License
*
* Copyright (c) 2021 yedf
* Copyright (c) 2022 dtm-labs
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
Expand All @@ -22,13 +22,14 @@
* SOFTWARE.
*/

package common.constant;
package pub.dtm.client.constant;

/**
* @author lixiaoshuang
* Constants for dtm server parameter key
*
* @author horseLk
*/
public class ParamFieldConstant {

public class ParamFieldConstants {
public static final String GID = "gid";

public static final String TRANS_TYPE = "trans_type";
Expand Down Expand Up @@ -72,5 +73,4 @@ public class ParamFieldConstant {
public static final String PASSTHROGH_HEADERS = "passthrough_headers";

public static final String BRANCH_HEADERS = "branch_headers";

}
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/*
* MIT License
*
* Copyright (c) 2021 yedf
* Copyright (c) 2022 dtm-labs
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
Expand All @@ -22,49 +22,57 @@
* SOFTWARE.
*/

package common.model;
package pub.dtm.client.enums;

import common.enums.TransTypeEnum;
import lombok.Data;
import lombok.NoArgsConstructor;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

@Data
@NoArgsConstructor
public class TransBase {

/**
* Transtype enum
*
* @author horseLk
*/
public enum TransTypeEnum {
/**
* 全局事务id
* tcc
*/
private String gid;

TCC("tcc"),
/**
* 事务类型
* xa
*/
private TransTypeEnum transTypeEnum;

private boolean waitResult;

private long timeoutToFail;

private long retryInterval;
XA("xa"),
/**
* msg
*/
MSG("msg"),
/**
* saga
*/
SAGA("saga")
;

private Map<String, String> branchHeaders = new HashMap<>();
TransTypeEnum(String value) {
this.value = value;
}

private ArrayList<String> passthroughHeaders = new ArrayList<>();
/**
* Trans type string
*/
private final String value;

private String customData;
public String getValue() {
return this.value;
}

private ArrayList<Map<String, String>> steps = new ArrayList<>();
private static final Map<String, TransTypeEnum> EXIST = new HashMap<>();

private ArrayList<String> payloads = new ArrayList<>();
static {
for (TransTypeEnum transType : TransTypeEnum.values()) {
EXIST.put(transType.value, transType);
}
}

public TransBase(TransTypeEnum transTypeEnum, String gid, boolean waitResult) {
this.gid = gid;
this.transTypeEnum = transTypeEnum;
this.waitResult = waitResult;
public static TransTypeEnum parseString(String value) {
return EXIST.get(value);
}
}
}
Loading

0 comments on commit 7a15656

Please sign in to comment.