Skip to content

Commit 0f13cfb

Browse files
committed
Hystrix MDC日志链路优化
1 parent 3a023b2 commit 0f13cfb

File tree

4 files changed

+83
-1
lines changed

4 files changed

+83
-1
lines changed

pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
<parent>
1313
<groupId>org.springframework.boot</groupId>
1414
<artifactId>spring-boot-starter-parent</artifactId>
15-
<version>1.5.3.RELEASE</version>
15+
<version>2.0.6.RELEASE</version>
1616
<relativePath /> <!-- lookup parent from repository -->
1717
</parent>
1818

spring-boot-student-log/pom.xml

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,17 @@
4141
<artifactId>fastjson</artifactId>
4242
<version>1.2.47</version>
4343
</dependency>
44+
45+
<dependency>
46+
<groupId>com.netflix.hystrix</groupId>
47+
<artifactId>hystrix-core</artifactId>
48+
<version>1.5.12</version>
49+
</dependency>
50+
<dependency>
51+
<groupId>com.netflix.hystrix</groupId>
52+
<artifactId>hystrix-javanica</artifactId>
53+
<version>1.5.12</version>
54+
</dependency>
4455
</dependencies>
4556

4657
</project>
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
package com.xiaolyuh.hystrix;
2+
3+
import com.netflix.hystrix.contrib.javanica.aop.aspectj.HystrixCommandAspect;
4+
import com.netflix.hystrix.strategy.HystrixPlugins;
5+
import org.springframework.context.annotation.Bean;
6+
import org.springframework.context.annotation.Configuration;
7+
8+
import javax.annotation.PostConstruct;
9+
10+
@Configuration
11+
public class HystrixConfig {
12+
13+
//用来拦截处理HystrixCommand注解
14+
@Bean
15+
public HystrixCommandAspect hystrixAspect() {
16+
return new HystrixCommandAspect();
17+
}
18+
19+
@PostConstruct
20+
public void init() {
21+
HystrixPlugins.getInstance().registerConcurrencyStrategy(new MdcHystrixConcurrencyStrategy());
22+
}
23+
24+
}
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
package com.xiaolyuh.hystrix;
2+
3+
import com.netflix.hystrix.strategy.concurrency.HystrixConcurrencyStrategy;
4+
import org.slf4j.MDC;
5+
6+
import java.util.HashMap;
7+
import java.util.Map;
8+
import java.util.concurrent.Callable;
9+
10+
/**
11+
* Hystrix线程池隔离支持日志链路跟踪
12+
*
13+
* @author yuhao.wang3
14+
*/
15+
public class MdcHystrixConcurrencyStrategy extends HystrixConcurrencyStrategy {
16+
17+
@Override
18+
public <T> Callable<T> wrapCallable(Callable<T> callable) {
19+
return new MdcAwareCallable(callable, MDC.getCopyOfContextMap());
20+
}
21+
22+
/**
23+
* 装饰原有的Callable,用来实现MDC容器内容的拷贝
24+
* @param <T>
25+
*/
26+
private class MdcAwareCallable<T> implements Callable<T> {
27+
28+
private final Callable<T> delegate;
29+
30+
private final Map<String, String> contextMap;
31+
32+
public MdcAwareCallable(Callable<T> callable, Map<String, String> contextMap) {
33+
this.delegate = callable;
34+
this.contextMap = contextMap != null ? contextMap : new HashMap();
35+
}
36+
37+
@Override
38+
public T call() throws Exception {
39+
try {
40+
MDC.setContextMap(contextMap);
41+
return delegate.call();
42+
} finally {
43+
MDC.clear();
44+
}
45+
}
46+
}
47+
}

0 commit comments

Comments
 (0)