zipkin使用hystrix無法串聯起呼叫鏈的解決方法
阿新 • • 發佈:2018-11-08
在使用zipkin的過程,引用hystrix導致呼叫鏈條break,原來hystrix預設容錯方法是執行緒隔離,而在這個過程中使用了執行緒池,而執行緒池中的traceId無法重新生成,導致呼叫鏈斷掉,因此重寫HystrixConcurrencyStrategy 類的wrapCallable即可達到修復效果,程式碼如下:
@Component public class TracingHystrixConcurrencyStrategy extends HystrixConcurrencyStrategy { @Autowired private Tracing tracing; public TracingHystrixConcurrencyStrategy(){ HystrixPlugins.reset(); HystrixPlugins.getInstance().registerConcurrencyStrategy(this); } @Override public <T> Callable<T> wrapCallable(Callable<T> callable) { CurrentTraceContext context = tracing.currentTraceContext(); if(context!=null) return context.wrap(callable); else return super.wrapCallable(callable); } public void setTracing(Tracing tracing) { this.tracing = tracing; } }