Skip to content

Commit 1b0e391

Browse files
author
tianwu
committed
TCC实现
1 parent 1a9104f commit 1b0e391

31 files changed

+934
-395
lines changed

pom.xml

Lines changed: 50 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,10 @@
88
<artifactId>java-code</artifactId>
99
<version>1.0-SNAPSHOT</version>
1010

11+
<properties>
12+
<spring-boot.version>2.2.2.RELEASE</spring-boot.version>
13+
</properties>
14+
1115
<dependencies>
1216
<dependency>
1317
<groupId>org.javassist</groupId>
@@ -20,12 +24,28 @@
2024
<groupId>org.apache.zookeeper</groupId>
2125
<artifactId>zookeeper</artifactId>
2226
<version>3.4.8</version>
27+
<exclusions>
28+
<exclusion>
29+
<artifactId>slf4j-log4j12</artifactId>
30+
<groupId>org.slf4j</groupId>
31+
</exclusion>
32+
</exclusions>
2333
</dependency>
2434
<!-- ZkClient -->
2535
<dependency>
2636
<groupId>com.101tec</groupId>
2737
<artifactId>zkclient</artifactId>
2838
<version>0.8</version>
39+
<exclusions>
40+
<exclusion>
41+
<artifactId>log4j</artifactId>
42+
<groupId>log4j</groupId>
43+
</exclusion>
44+
<exclusion>
45+
<artifactId>slf4j-log4j12</artifactId>
46+
<groupId>org.slf4j</groupId>
47+
</exclusion>
48+
</exclusions>
2949
</dependency>
3050

3151
<dependency>
@@ -94,10 +114,25 @@
94114
<version>3.4</version>
95115
</dependency>
96116

117+
<dependency>
118+
<groupId>org.springframework.boot</groupId>
119+
<artifactId>spring-boot-starter</artifactId>
120+
<version>${spring-boot.version}</version>
121+
</dependency>
122+
<dependency>
123+
<groupId>org.springframework.boot</groupId>
124+
<artifactId>spring-boot-starter-web</artifactId>
125+
<version>${spring-boot.version}</version>
126+
</dependency>
127+
<dependency>
128+
<groupId>org.springframework.boot</groupId>
129+
<artifactId>spring-boot-test</artifactId>
130+
<version>${spring-boot.version}</version>
131+
</dependency>
97132
<dependency>
98133
<groupId>org.springframework</groupId>
99-
<artifactId>spring-core</artifactId>
100-
<version>4.3.25.RELEASE</version>
134+
<artifactId>spring-test</artifactId>
135+
<version>5.2.2.RELEASE</version>
101136
</dependency>
102137

103138
<dependency>
@@ -109,7 +144,7 @@
109144
<dependency>
110145
<groupId>junit</groupId>
111146
<artifactId>junit</artifactId>
112-
<version>4.7</version>
147+
<version>4.12</version>
113148
</dependency>
114149

115150
<dependency>
@@ -154,6 +189,18 @@
154189
<version>3.3.7</version>
155190
</dependency>
156191

192+
<dependency>
193+
<groupId>org.aspectj</groupId>
194+
<artifactId>aspectjweaver</artifactId>
195+
<version>1.9.6</version>
196+
</dependency>
197+
198+
<dependency>
199+
<groupId>aopalliance</groupId>
200+
<artifactId>aopalliance</artifactId>
201+
<version>1.0</version>
202+
</dependency>
203+
157204
</dependencies>
158205

159206
<build>
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
package code;
2+
3+
import org.springframework.boot.SpringApplication;
4+
import org.springframework.boot.autoconfigure.SpringBootApplication;
5+
import org.springframework.boot.context.properties.EnableConfigurationProperties;
6+
import org.springframework.boot.web.servlet.ServletComponentScan;
7+
8+
/**
9+
* 〈启动入口〉<p>
10+
* 〈功能详细描述〉
11+
*
12+
* @author tianwu
13+
* @date 2022/09/08
14+
*/
15+
@ServletComponentScan
16+
@EnableConfigurationProperties
17+
@SpringBootApplication
18+
public class CodeApplication {
19+
20+
public static void main(String[] args) {
21+
SpringApplication.run(CodeApplication.class);
22+
}
23+
24+
}

src/main/java/code/distribution/id/Snowflake/IdWorker.java renamed to src/main/java/code/distribution/id/snowflake/IdWorker.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package code.distribution.id.Snowflake;
1+
package code.distribution.id.snowflake;
22

33
/**
44
* 〈Snowflake算法〉<p>
@@ -141,7 +141,7 @@ public static IdWorker getInstance() {
141141
return idWorker;
142142
}
143143

144-
public static long initWorkerId() {
144+
private static long initWorkerId() {
145145
InetAddress address;
146146
try {
147147
address = InetAddress.getLocalHost();
@@ -152,7 +152,7 @@ public static long initWorkerId() {
152152
return ((ipAddressByteArray[ipAddressByteArray.length - 2] & 0B11) << Byte.SIZE) + (ipAddressByteArray[ipAddressByteArray.length - 1] & 0xFF);
153153
}
154154

155-
public static void init(Long serverNodeId) {
155+
private static void init(Long serverNodeId) {
156156
if (idWorker == null) {
157157
synchronized (IdWorker.class) {
158158
if (idWorker == null) {

src/main/java/code/distribution/tcc/common/TccAction.java renamed to src/main/java/code/distribution/tcc/annotation/TccBranch.java

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
1-
package code.distribution.tcc.common;
1+
package code.distribution.tcc.annotation;
22

33
import java.lang.annotation.*;
44

55
/**
6-
* 〈一句话功能简述〉<p>
6+
* 〈TCC分支事务〉<p>
77
* 〈功能详细描述〉
88
*
99
* @author zixiao
@@ -12,13 +12,13 @@
1212
@Target({ElementType.METHOD})
1313
@Retention(RetentionPolicy.RUNTIME)
1414
@Documented
15-
public @interface TccAction {
15+
public @interface TccBranch {
1616

1717
/**
18-
* 预占方法
18+
* 分支名称
1919
* @return
2020
*/
21-
String try1();
21+
String actionName();
2222

2323
/**
2424
* 确认方法
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
package code.distribution.tcc.annotation;
2+
3+
import java.lang.annotation.*;
4+
5+
/**
6+
* 〈TCC事务〉<p>
7+
* 〈功能详细描述〉
8+
*
9+
* @author tianwu
10+
* @date 2022/09/08
11+
*/
12+
@Target({ElementType.METHOD})
13+
@Retention(RetentionPolicy.RUNTIME)
14+
@Documented
15+
public @interface TccTransaction {
16+
17+
}

src/main/java/code/distribution/tcc/common/BranchTx.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,15 +12,15 @@
1212
@Data
1313
public class BranchTx {
1414

15-
private String id;
15+
private Long id;
1616

1717
private BranchState state;
1818

19-
private TxMethod methods;
19+
private TxMethod method;
2020

21-
public BranchTx(String id, BranchState state, TxMethod methods) {
21+
public BranchTx(Long id, BranchState state, TxMethod methods) {
2222
this.id = id;
2323
this.state = state;
24-
this.methods = methods;
24+
this.method = methods;
2525
}
2626
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
package code.distribution.tcc.common;
2+
3+
/**
4+
* 〈一句话功能简述〉<p>
5+
* 〈功能详细描述〉
6+
*
7+
* @author tianwu
8+
* @date 2022/09/07
9+
*/
10+
public interface Callback<T> {
11+
12+
T execute() throws Throwable;
13+
14+
}
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
package code.distribution.tcc.common;
2+
3+
import code.distribution.id.snowflake.IdWorker;
4+
5+
import java.util.HashMap;
6+
import java.util.Map;
7+
8+
/**
9+
* 〈事务容器〉<p>
10+
* 〈功能详细描述〉
11+
*
12+
* @author tianwu
13+
* @date 2022/09/07
14+
*/
15+
public class TxContainer {
16+
17+
private static final ThreadLocal<String> XID = new ThreadLocal<>();
18+
19+
private static final ThreadLocal<Map<String, Long>> BRANCH_ID_MAP = ThreadLocal.withInitial(HashMap::new);
20+
21+
public static String getXid(){
22+
return XID.get();
23+
}
24+
25+
public static void setXid(String xid) {
26+
XID.set(xid);
27+
}
28+
29+
public static Long getBranchId(String actionName){
30+
return BRANCH_ID_MAP.get().get(actionName);
31+
}
32+
33+
public static Long setBranchId(String actionName){
34+
Long branchId = IdWorker.getInstance().nextId();
35+
BRANCH_ID_MAP.get().put(actionName, branchId);
36+
return branchId;
37+
}
38+
39+
public static void remove(){
40+
XID.remove();
41+
BRANCH_ID_MAP.get().clear();
42+
}
43+
44+
}

src/main/java/code/distribution/tcc/common/TxFlowDo.java

Lines changed: 0 additions & 54 deletions
This file was deleted.
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
package code.distribution.tcc.common;
2+
3+
import lombok.Data;
4+
import java.io.Serializable;
5+
import java.util.Date;
6+
7+
/**
8+
* 〈事务控制表〉<p>
9+
`xid` VARCHAR(128) NOT NULL COMMENT 'global id',
10+
`branch_id` BIGINT NOT NULL COMMENT 'branch id',
11+
`action_name` VARCHAR(64) NOT NULL COMMENT 'action name',
12+
`status` TINYINT NOT NULL COMMENT 'status(tried:1;committed:2;rollbacked:3;suspended:4)',
13+
`gmt_create` DATETIME(3) NOT NULL COMMENT 'create time',
14+
`gmt_modified` DATETIME(3) NOT NULL COMMENT 'update time',
15+
PRIMARY KEY (`xid`, `branch_id`)
16+
*
17+
* @author zixiao
18+
* @date 2020/1/7
19+
*/
20+
@Data
21+
public class TxFlowLogDO implements Serializable {
22+
23+
/**
24+
* 全局事务id
25+
*/
26+
private String xid;
27+
28+
/**
29+
* 分支事务id
30+
*/
31+
private Long branchId;
32+
33+
/**
34+
* 已Try,已提交,已回滚
35+
*/
36+
private TxState state;
37+
38+
private Date gmtCreate;
39+
40+
private Date gmtModified;
41+
42+
public TxFlowLogDO(String xid, Long branchId, TxState state) {
43+
this.xid = xid;
44+
this.branchId = branchId;
45+
this.state = state;
46+
this.gmtCreate = new Date();
47+
this.gmtModified = new Date();
48+
}
49+
50+
public static TxFlowLogDO buildTry(String xid, Long branchId){
51+
return new TxFlowLogDO(xid, branchId, TxState.TRIED);
52+
}
53+
54+
public static TxFlowLogDO buildRollback(String xid, Long branchId){
55+
return new TxFlowLogDO(xid, branchId, TxState.ROLLBACKED);
56+
}
57+
58+
public String getUniqueKey() {
59+
return buildKey(xid, branchId);
60+
}
61+
62+
public static String buildKey(String xid, Long branchId) {
63+
return xid + "@" + branchId;
64+
}
65+
66+
}

0 commit comments

Comments
 (0)