1.简述
Hystrix在donet中的应用案例说明极少且不完整,此案例可以直接使用,基于donet4
适用点:隔离,熔断,降级,来解决web在高并发下,对远程服务的高依赖可能引起的雪崩问题
可以自己到github下载源码使用,
注意:直接下载的部分源码有问题,本案例已经修复这些bug
2.下载组件
链接: https://pan.baidu.com/s/1BM9VX4Z5Htc1Zxxvf9-u1A
3.组件结构
WEB URL:http://localhost:9000/dashboard/
STREAM URL: http://127.0.0.1:38080/Hystrix/
4.业务web中引入全部核心组件和独立组件,新建
-------AtlasPublisher.cs--------
public class AtlasPublisher : IHystrixMetricsPublisher
{
CommandMetricObserver observer;
public AtlasPublisher()
{
var atlasCfg = new AtlasConfig("http://10.0.75.2", 7101);
observer = new CommandMetricObserver(atlasCfg);
observer.Run();
}
public IHystrixMetricsPublisherCommand GetMetricsPublisherForCommand(HystrixCommandKey commandKey, HystrixCommandGroupKey commandGroupKey, HystrixCommandMetrics metrics, IHystrixCircuitBreaker circuitBreaker, IHystrixCommandProperties properties)
{
return new HystrixServoMetricsPublisherCommand(commandKey, commandGroupKey, metrics, circuitBreaker, properties);
}
public IHystrixMetricsPublisherThreadPool GetMetricsPublisherForThreadPool(HystrixThreadPoolKey threadPoolKey, HystrixThreadPoolMetrics metrics, IHystrixThreadPoolProperties properties)
{
return new Dummy1();
}
public void Dispose()
{
if (observer != null)
observer.Stop();
observer = null;
}
}
public class Dummy1 : IHystrixMetricsPublisherThreadPool
{
public void Initialize()
{
}
}
--------HystrixMonitorServer.cs-----------
public static class HystrixMonitorServer
{
private static HystrixMetricsStreamServer _metricsServer;
public static void Start(string port = "38080")
{
//if (Configration.GetInstance().IsOnline != "1")
//{
try
{
_metricsServer = new HystrixMetricsStreamServer(string.Format("http://127.0.0.1:{0}/Hystrix/", port), 10, TimeSpan.FromSeconds(2));
if (!_metricsServer.IsRunning)
{
_metricsServer.Start();
}
LogHelper.GetLoger().ErrorFormat("启动HystrixMonitor成功;时间:{0}", DateTime.Now.ToString());
}
catch (Exception e)
{
LogHelper.GetLoger().ErrorFormat("启动HystrixMonitor失败;时间:{0},详情:{1}", DateTime.Now.ToString(), e.Message + e.StackTrace);
}
//}
}
public static void Stop()
{
try
{
if (_metricsServer != null)
{
_metricsServer.Stop();
}
LogHelper.GetLoger().ErrorFormat("停止HystrixMonitor成功;时间:{0}", DateTime.Now.ToString());
}
catch (Exception e)
{
LogHelper.GetLoger().ErrorFormat("停止HystrixMonitor失败;时间:{0},详情:{1}", DateTime.Now.ToString(), e.Message + e.StackTrace);
}
}
}
5.线程隔离实例,把原来业务逻辑调用改成 new ThreadTestCommand().Run();
public class ThreadTestCommand : HystrixCommand
{
///
/// 设置隔离,熔断等策略
///
public ThreadTestCommand() : base(HystrixCommandSetter
.WithGroupKey("ThreadTestGroup")
.AndThreadPoolKey("ThreadTest")
.AndThreadPoolPropertiesDefaults(new HystrixThreadPoolPropertiesSetter()
.WithCoreSize(10)
.WithMaxQueueSize(50)
)
.AndCommandKey("ThreadTest1")
.AndCommandPropertiesDefaults(new HystrixCommandPropertiesSetter()
.WithExecutionIsolationStrategy(ExecutionIsolationStrategy.Thread) //线程隔离
.WithExecutionIsolationThreadTimeout(TimeSpan.FromSeconds(3)) //3秒超时
.WithExecutionIsolationThreadInterruptOnTimeout(true) //超时强制中断
.WithCircuitBreakerErrorThresholdPercentage(50) //超过50%自动熔断
.WithCircuitBreakerRequestVolumeThreshold(50) //熔断策略启动的最小请求量(一个周期内)
.WithCircuitBreakerSleepWindowInMilliseconds(5000) //熔断器熔断周期时间,在此时间内所有请求被拒绝
))
{
}
///
/// 执行托管逻辑
///
///
protected override string Run()
{
return ;
}
///
/// 降级逻辑
///
///
protected override string GetFallback()
{
return string.Empty;
}
}
5.信号量隔离实例,把原来业务逻辑调用改成 new SemaphoreTestCommand().Run();
public class SemaphoreTestCommand : HystrixCommand
{
///
/// 设置隔离,熔断等策略
///
public GuangDongFangWeiCommand() : base(HystrixCommandSetter
.WithGroupKey("SemaphoreTestGroup")
.AndCommandKey("SemaphoreTest1")
.AndCommandPropertiesDefaults(new HystrixCommandPropertiesSetter()
.WithExecutionIsolationStrategy(ExecutionIsolationStrategy.Semaphore) //信号量隔离
.WithExecutionIsolationSemaphoreMaxConcurrentRequests(50) //最大并发
.WithExecutionIsolationThreadTimeout(TimeSpan.FromSeconds(3)) //3秒超时
.WithExecutionIsolationThreadInterruptOnTimeout(true) //超时强制中断
.WithCircuitBreakerErrorThresholdPercentage(50) //超过50%自动熔断
.WithCircuitBreakerRequestVolumeThreshold(50) //熔断策略启动的最小请求量(一个周期内)
.WithCircuitBreakerSleepWindowInMilliseconds(5000) //熔断器熔断周期时间,在此时间内所有请求被拒绝
))
{
}
///
/// 执行托管逻辑
///
///
protected override string Run()
{
return ;
}
///
/// 降级逻辑
///
///
protected override string GetFallback()
{
return string.Empty;
}
}
6.启动Stream监听
HystrixMonitorServer.Start("38080");
7.编制执行Elders.Hystrix.NET.Dashboard.Cmd,启动统计自宿主web
8.浏览器访问:http://localhost:9000/dashboard/, 并输入流指标统计地址:http://127.0.0.1:38080/Hystrix/
9.出现类似如下的可视化统计图