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<string>
{
/// <summary>
/// 設置隔離,熔斷等策略
/// </summary>
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) //熔斷器熔斷週期時間,在此時間內所有請求被拒絕
))
{
}
/// <summary>
/// 執行託管邏輯
/// </summary>
/// <returns></returns>
protected override string Run()
{
return ;
}
/// <summary>
/// 降級邏輯
/// </summary>
/// <returns></returns>
protected override string GetFallback()
{
return string.Empty;
}
}
5.信號量隔離實例,把原來業務邏輯調用改成 new SemaphoreTestCommand().Run();
public class SemaphoreTestCommand : HystrixCommand<string>
{
/// <summary>
/// 設置隔離,熔斷等策略
/// </summary>
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) //熔斷器熔斷週期時間,在此時間內所有請求被拒絕
))
{
}
/// <summary>
/// 執行託管邏輯
/// </summary>
/// <returns></returns>
protected override string Run()
{
return ;
}
/// <summary>
/// 降級邏輯
/// </summary>
/// <returns></returns>
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.出現類似如下的可視化統計圖