1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
package com.tanpu.community.service;
import com.tanpu.community.feign.community.FeignClientForCommunity;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.BeanFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.sleuth.instrument.async.LazyTraceAsyncTaskExecutor;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;
import org.springframework.stereotype.Service;
import javax.annotation.PostConstruct;
import java.util.concurrent.atomic.AtomicInteger;
@Service
@Slf4j
public class TraceTestService {
@Autowired
private BeanFactory beanFactory;
@Autowired
private FeignClientForCommunity feignClientForCommunity;
private LazyTraceAsyncTaskExecutor executor;
private AtomicInteger count;
@PostConstruct
private void init() {
ThreadPoolTaskExecutor pool = new ThreadPoolTaskExecutor();
pool.setMaxPoolSize(3);
pool.setCorePoolSize(1);
pool.setKeepAliveSeconds(3600);
pool.initialize();
executor = new LazyTraceAsyncTaskExecutor(beanFactory, pool);
count = new AtomicInteger(0);
}
public void testTraceId() {
Integer c = count.incrementAndGet();
log.info("parent count is " + c);
for (int i = 1; i <= 3; i++) {
int subCount = i;
executor.submit(new Runnable() {
@Override
public void run() {
try {
Thread.sleep(1000);
log.info("child count is " + c + "-" + subCount);
feignClientForCommunity.testTraceId("" + c + "-" + subCount);
} catch (Exception e) {
}
}
});
}
}
}