springCloud微服务系列——链路跟踪第四篇——hystrix链路跟踪器

本文详细介绍了如何在Spring Cloud中实现Hystrix的链路跟踪,通过AOP拦截和处理HTTP链路信息,特别是在新建线程中的特殊处理,以及在处理结束后销毁上下文信息。提供了一个GitHub示例代码链接供参考。
摘要由CSDN通过智能技术生成

目录

 

一、简介

二、思路

三、示例代码


一、简介

这篇文章总结hystrix链路跟踪器的实现

二、思路

上一篇文章总结了feign的链路跟踪器的实现,同样的,这里也可以用AOP来拦截,除此之外,hystrix还有一些自身的特点

1、需要从http中获取链路信息,由于是新建的线程需要特殊处理,可以参看《springCloud微服务系列——OAuth2+JWT模式下的feign+hystrix处理》

2、处理结束后,需要销毁上下文信息

三、示例代码

@Component
public class RequestAttributeHystrixConcurrencyStrategy extends HystrixConcurrencyStrategy {

	private static final Logger logger = LoggerFactory.getLogger(RequestAttributeHystrixConcurrencyStrategy.class);
	private HystrixConcurrencyStrategy delegate;
	public RequestAttributeHystrixConcurrencyStrategy() {
		try {
			this.delegate = HystrixPlugins.getInstance().getConcurrencyStrategy();
			if (this.delegate instanceof RequestAttributeHystrixConcurrencyStrategy) {
				// Welcome to singleton hell...
				return;
			}
			HystrixCommandExecutionHook commandExecutionHook = HystrixPlugins
					.getInstance().getCommandExecutionHook();
			HystrixEventNotifier eventNotifier = HystrixPlugins.getInstance()
					.getEventNotifier();
			HystrixMetricsPublisher metricsPublisher = HystrixPlugins.getInstance()
					.getMetricsPublisher();
			HystrixPropertiesStrategy propertiesStrategy = HystrixPlugins.getInstance()
					.getPropertiesStrategy();
			this.logCurrentStateOfHystrixPlugins(eventNotifier, metricsPublisher,
					propertiesStrategy);
			HystrixPlugins.reset();
			HystrixPlugins.getInstance().registerConcurrencyStrategy(this);
			HystrixPlugins.getInstance()
					.registerCommandExecutionHook(commandExecutionHook);
			HystrixPlugins.getInstance().registerEventNotifier(eventNotifier);
			HystrixPlugins.getInstance().registerMetricsPublisher(metricsPublisher);
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值