Skip to content

Commit 7a15656

Browse files
authored
Merge pull request #33 from dtm-labs/springcloud
add Springcloud support
2 parents 6eb7419 + 3306371 commit 7a15656

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

53 files changed

+2838
-826
lines changed

.gitignore

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,4 +24,4 @@ hs_err_pid*
2424

2525
.idea
2626
*.iml
27-
/target
27+
/*/target/

LICENSE

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
MIT License
22

3-
Copyright (c) 2021 yedf
3+
Copyright (c) 2022 dtm-labs
44

55
Permission is hereby granted, free of charge, to any person obtaining a copy
66
of this software and associated documentation files (the "Software"), to deal

NewVersion.md

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

README.md

Lines changed: 37 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -45,50 +45,54 @@ DTM是一款跨语言的开源分布式事务管理器,优雅的解决了幂
4545

4646
## 使用方式
4747

48-
### 步骤一:JitPack 存储库添加到您的构建文件
49-
50-
Maven:
51-
52-
```bash
53-
<repositories>
54-
<repository>
55-
<id>jitpack.io</id>
56-
<url>https://jitpack.io</url>
57-
</repository>
58-
</repositories>
59-
```
60-
61-
Gradle:
62-
63-
```bash
64-
allprojects {
65-
repositories {
66-
...
67-
maven { url 'https://jitpack.io' }
68-
}
69-
}
70-
```
48+
### 步骤一:确定你需要使用的版本
49+
1. 您的项目是springcloud项目
50+
- 您的项目中springboot版本>=2.4.0,请选择dtmcli-springcloud相应的版本直接接入即可
51+
- 您的项目中的springboot版本<2.4.0,请选择dtmcli-java接入,dtmcli-java也提供了微服务相关的接口,请设置nacos服务中心的相关配置即可使用
52+
2. 您的项目是普通项目/没有接入微服务的spring(boot)项目
53+
- 请选择dtmcli-java,并设置相应的配置即可
54+
55+
| artifact| version | 适用版本 |备注|
56+
|:-----:|:----:|:----:|:----:|
57+
|dtmcli-springcloud| 2.1.4.1| 2.4.0 <= springboot version < 2.5.13| springboot 版本>=2.5.0,需要设置spring.cloud.compatibility-verifier.enabled=false|
58+
|dtmcli-springcloud| 2.1.4.2| 2.6.0 <= springboot version < 2.6.latest| |
59+
|dtmcli-java| 2.1.4| others| |
7160

7261
### 步骤二:添加依赖项
7362

7463
Maven:
7564

7665
```bash
7766
<dependency>
78-
<groupId>com.github.yedf</groupId>
79-
<artifactId>dtmcli-java</artifactId>
80-
<version>Tag</version>
67+
<groupId>io.github.dtm-labs</groupId>
68+
<artifactId>dtmcli-springcloud</artifactId>
69+
<version>${dtmcli.version}</version>
8170
</dependency>
8271
```
8372

8473
Gradle:
8574

8675
```bash
8776
dependencies {
88-
implementation 'com.github.yedf:dtmcli-java:Tag'
77+
implementation 'io.github.dtm-labs:dtmcli-springcloud:${dtmcli.version}'
8978
}
9079
```
9180

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

9498
```bash
@@ -125,10 +129,14 @@ dependencies {
125129

126130
### 完整示例
127131

128-
[dtmcli-java-sample](https://github.com/dtm-labs/dtmcli-java-sample)
132+
#### dtmcli-java使用示例
133+
[dtmcli-java-sample](https://github.com/dtm-labs/dtmcli-java-sample)
134+
[dtmcli-java-sample-use-configuration](https://github.com/horseLk/dtmcli-java-sample-with-conf)
135+
#### dtmcli-springcloud使用示例
136+
[dtmcli-java-spring-sample](https://github.com/dtm-labs/dtmcli-java-spring-sample)
129137

130138
### License
131139

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

134-
[license-badge]: https://img.shields.io/github/license/dtm-labs/dtmcli-py
142+
[license-badge]: https://img.shields.io/github/license/dtm-labs/dtmcli-py

dtmcli-common/pom.xml

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<project xmlns="http://maven.apache.org/POM/4.0.0"
3+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
5+
<parent>
6+
<artifactId>dtmcli-java-parent</artifactId>
7+
<groupId>io.github.dtm-labs</groupId>
8+
<version>2.1.4</version>
9+
</parent>
10+
<modelVersion>4.0.0</modelVersion>
11+
12+
<artifactId>dtmcli-common</artifactId>
13+
<version>2.1.4</version>
14+
<packaging>jar</packaging>
15+
<name>dtmcli-common</name>
16+
17+
<properties>
18+
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
19+
<maven.compiler.source>1.8</maven.compiler.source>
20+
<maven.compiler.target>1.8</maven.compiler.target>
21+
</properties>
22+
23+
<dependencies>
24+
25+
<dependency>
26+
<groupId>com.fasterxml.jackson.core</groupId>
27+
<artifactId>jackson-databind</artifactId>
28+
</dependency>
29+
30+
<dependency>
31+
<groupId>org.projectlombok</groupId>
32+
<artifactId>lombok</artifactId>
33+
</dependency>
34+
35+
<dependency>
36+
<groupId>io.github.openfeign</groupId>
37+
<artifactId>feign-core</artifactId>
38+
</dependency>
39+
40+
<dependency>
41+
<groupId>org.apache.commons</groupId>
42+
<artifactId>commons-lang3</artifactId>
43+
</dependency>
44+
45+
<dependency>
46+
<groupId>com.squareup.okhttp3</groupId>
47+
<artifactId>okhttp</artifactId>
48+
</dependency>
49+
50+
<dependency>
51+
<groupId>com.alibaba.nacos</groupId>
52+
<artifactId>nacos-client</artifactId>
53+
</dependency>
54+
55+
<dependency>
56+
<groupId>org.slf4j</groupId>
57+
<artifactId>slf4j-api</artifactId>
58+
</dependency>
59+
</dependencies>
60+
</project>
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
/*
2+
* MIT License
3+
*
4+
* Copyright (c) 2022 dtm-labs
5+
*
6+
* Permission is hereby granted, free of charge, to any person obtaining a copy
7+
* of this software and associated documentation files (the "Software"), to deal
8+
* in the Software without restriction, including without limitation the rights
9+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10+
* copies of the Software, and to permit persons to whom the Software is
11+
* furnished to do so, subject to the following conditions:
12+
*
13+
* The above copyright notice and this permission notice shall be included in all
14+
* copies or substantial portions of the Software.
15+
*
16+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22+
* SOFTWARE.
23+
*/
24+
25+
package pub.dtm.client.constant;
26+
27+
/**
28+
* Constants
29+
*
30+
* @author horseLk
31+
*/
32+
public class Constants {
33+
public static final String MICRO_SERVICE_NAME_KEY = "dtm.service.name";
34+
35+
public static final String GET_METHOD = "GET ";
36+
37+
public static final String POST_METHOD = "POST ";
38+
39+
public static final String HTTP_PREFIX = "http://";
40+
41+
public static final String HTTPS_PREFIX = "https://";
42+
43+
public static final String PING_URL = "/api/ping";
44+
45+
private static final String BASE_URL = "/api/dtmsvr";
46+
47+
public static final String NEW_GID_URL = BASE_URL + "/newGid";
48+
49+
public static final String PREPARE_URL = BASE_URL + "/prepare";
50+
51+
public static final String SUBMIT_URL = BASE_URL + "/submit";
52+
53+
public static final String ABORT_URL = BASE_URL + "/abort";
54+
55+
public static final String REGISTER_BRANCH_URL = BASE_URL + "/registerBranch";
56+
57+
public static final String DEFAULT_STATUS = "prepared";
58+
59+
public static final String EMPTY_STRING = "";
60+
61+
public static final String SUCCESS_RESULT = "SUCCESS";
62+
63+
public static final String FAILURE_RESULT = "FAILURE";
64+
65+
public static final int RESP_ERR_CODE = 400;
66+
}

src/main/java/common/constant/ParamFieldConstant.java renamed to dtmcli-common/src/main/java/pub/dtm/client/constant/ParamFieldConstants.java

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
/*
22
* MIT License
33
*
4-
* Copyright (c) 2021 yedf
4+
* Copyright (c) 2022 dtm-labs
55
*
66
* Permission is hereby granted, free of charge, to any person obtaining a copy
77
* of this software and associated documentation files (the "Software"), to deal
@@ -22,13 +22,14 @@
2222
* SOFTWARE.
2323
*/
2424

25-
package common.constant;
25+
package pub.dtm.client.constant;
2626

2727
/**
28-
* @author lixiaoshuang
28+
* Constants for dtm server parameter key
29+
*
30+
* @author horseLk
2931
*/
30-
public class ParamFieldConstant {
31-
32+
public class ParamFieldConstants {
3233
public static final String GID = "gid";
3334

3435
public static final String TRANS_TYPE = "trans_type";
@@ -72,5 +73,4 @@ public class ParamFieldConstant {
7273
public static final String PASSTHROGH_HEADERS = "passthrough_headers";
7374

7475
public static final String BRANCH_HEADERS = "branch_headers";
75-
7676
}
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
/*
22
* MIT License
33
*
4-
* Copyright (c) 2021 yedf
4+
* Copyright (c) 2022 dtm-labs
55
*
66
* Permission is hereby granted, free of charge, to any person obtaining a copy
77
* of this software and associated documentation files (the "Software"), to deal
@@ -22,49 +22,57 @@
2222
* SOFTWARE.
2323
*/
2424

25-
package common.model;
25+
package pub.dtm.client.enums;
2626

27-
import common.enums.TransTypeEnum;
28-
import lombok.Data;
29-
import lombok.NoArgsConstructor;
30-
31-
import java.util.ArrayList;
3227
import java.util.HashMap;
3328
import java.util.Map;
3429

35-
@Data
36-
@NoArgsConstructor
37-
public class TransBase {
38-
30+
/**
31+
* Transtype enum
32+
*
33+
* @author horseLk
34+
*/
35+
public enum TransTypeEnum {
3936
/**
40-
* 全局事务id
37+
* tcc
4138
*/
42-
private String gid;
43-
39+
TCC("tcc"),
4440
/**
45-
* 事务类型
41+
* xa
4642
*/
47-
private TransTypeEnum transTypeEnum;
48-
49-
private boolean waitResult;
50-
51-
private long timeoutToFail;
52-
53-
private long retryInterval;
43+
XA("xa"),
44+
/**
45+
* msg
46+
*/
47+
MSG("msg"),
48+
/**
49+
* saga
50+
*/
51+
SAGA("saga")
52+
;
5453

55-
private Map<String, String> branchHeaders = new HashMap<>();
54+
TransTypeEnum(String value) {
55+
this.value = value;
56+
}
5657

57-
private ArrayList<String> passthroughHeaders = new ArrayList<>();
58+
/**
59+
* Trans type string
60+
*/
61+
private final String value;
5862

59-
private String customData;
63+
public String getValue() {
64+
return this.value;
65+
}
6066

61-
private ArrayList<Map<String, String>> steps = new ArrayList<>();
67+
private static final Map<String, TransTypeEnum> EXIST = new HashMap<>();
6268

63-
private ArrayList<String> payloads = new ArrayList<>();
69+
static {
70+
for (TransTypeEnum transType : TransTypeEnum.values()) {
71+
EXIST.put(transType.value, transType);
72+
}
73+
}
6474

65-
public TransBase(TransTypeEnum transTypeEnum, String gid, boolean waitResult) {
66-
this.gid = gid;
67-
this.transTypeEnum = transTypeEnum;
68-
this.waitResult = waitResult;
75+
public static TransTypeEnum parseString(String value) {
76+
return EXIST.get(value);
6977
}
70-
}
78+
}

0 commit comments

Comments
 (0)