File tree Expand file tree Collapse file tree 4 files changed +83
-1
lines changed
src/main/java/com/xiaolyuh/hystrix Expand file tree Collapse file tree 4 files changed +83
-1
lines changed Original file line number Diff line number Diff line change 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
Original file line number Diff line number Diff line change 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 >
Original file line number Diff line number Diff line change 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+ }
Original file line number Diff line number Diff line change 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+ }
You can’t perform that action at this time.
0 commit comments