Skip to content

Commit b5fb0e8

Browse files
committed
feat: 集成 TTL
1 parent 5030b27 commit b5fb0e8

4 files changed

Lines changed: 75 additions & 0 deletions

File tree

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,4 +33,5 @@
3333
- [x] 集成 druid 连接池
3434
- [x] 集成 pagehelper
3535
- [x] 集成 Spring Boot Admin 监控
36+
- [x] 集成 TransmittableThreadLocal
3637
- [ ] 集成 dynamicTp
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
package top.cadecode.uniboot.demo.controller;
2+
3+
import com.alibaba.ttl.TransmittableThreadLocal;
4+
import com.alibaba.ttl.threadpool.TtlExecutors;
5+
import io.swagger.annotations.Api;
6+
import io.swagger.annotations.ApiOperation;
7+
import lombok.RequiredArgsConstructor;
8+
import lombok.extern.slf4j.Slf4j;
9+
import org.springframework.web.bind.annotation.PostMapping;
10+
import org.springframework.web.bind.annotation.RequestMapping;
11+
import org.springframework.web.bind.annotation.RestController;
12+
import top.cadecode.uniboot.common.annotation.ApiFormat;
13+
14+
import java.util.concurrent.Executor;
15+
import java.util.concurrent.Executors;
16+
17+
/**
18+
* 阿里 TransmittableThreadLocal 测试
19+
*
20+
* @author Cade Li
21+
* @date 2023/3/13
22+
*/
23+
@ApiFormat
24+
@Slf4j
25+
@RequiredArgsConstructor
26+
@Api(tags = "TransmittableThreadLocal 测试")
27+
@RestController
28+
@RequestMapping("demo/ttl")
29+
public class TtlExecutorController {
30+
31+
@ApiOperation("测试普通 ThreadLocal")
32+
@PostMapping("test_thread_local")
33+
public String testThreadLocal() {
34+
ThreadLocal<String> threadLocal = new ThreadLocal<>();
35+
threadLocal.set("A");
36+
Executor executor = Executors.newFixedThreadPool(2);
37+
for (int i = 0; i < 5; i++) {
38+
executor.execute(() -> {
39+
// 前两个线程打印出 null,因为普通的 ThreadLocal 不能被子线程继承
40+
System.out.println(threadLocal.get());
41+
threadLocal.set("B");
42+
// 修改了之后,后续线程被复用时会打印出修改后的值
43+
});
44+
}
45+
return "OK";
46+
}
47+
48+
@ApiOperation("测试 TransmittableThreadLocal")
49+
@PostMapping("test_ttl")
50+
public String testTransmittableThreadLocal() {
51+
ThreadLocal<String> threadLocal = new TransmittableThreadLocal<>();
52+
threadLocal.set("A");
53+
Executor executor = TtlExecutors.getTtlExecutor(Executors.newFixedThreadPool(2));
54+
for (int i = 0; i < 5; i++) {
55+
executor.execute(() -> {
56+
System.out.println(threadLocal.get());
57+
threadLocal.set("B");
58+
// 使用 ttl 后,5 次都打印出 A,说明 ttl 可以被子线程继承,并且线程复用时没有相互影响
59+
});
60+
}
61+
return "OK";
62+
}
63+
}

common/pom.xml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -108,5 +108,10 @@
108108
<groupId>com.baomidou</groupId>
109109
<artifactId>dynamic-datasource-spring-boot-starter</artifactId>
110110
</dependency>
111+
<!--ttl-->
112+
<dependency>
113+
<groupId>com.alibaba</groupId>
114+
<artifactId>transmittable-thread-local</artifactId>
115+
</dependency>
111116
</dependencies>
112117
</project>

pom.xml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@
4343
<druid.version>1.2.15</druid.version>
4444
<dynamic-ds.version>3.6.0</dynamic-ds.version>
4545
<sba.version>2.6.11</sba.version>
46+
<ttl.version>2.12.2</ttl.version>
4647
</properties>
4748

4849
<dependencies>
@@ -153,6 +154,11 @@
153154
<artifactId>spring-boot-admin-starter-client</artifactId>
154155
<version>${sba.version}</version>
155156
</dependency>
157+
<dependency>
158+
<groupId>com.alibaba</groupId>
159+
<artifactId>transmittable-thread-local</artifactId>
160+
<version>${ttl.version}</version>
161+
</dependency>
156162
</dependencies>
157163
</dependencyManagement>
158164

0 commit comments

Comments
 (0)