什么是 "Circuit Breaker is open. Execution was short circuited." 以及如何解决这个问题?
What is "Circuit Breaker is open. Execution was short circuited." And How to fix this?
我现在做一个简单的负载均衡测试,代码如下:
public static class APIStoreLoadBalancerManager
{
private static readonly ConcurrentDictionary<string, ILoadBalancer> _balancers
= new ConcurrentDictionary<string, ILoadBalancer>();
private static volatile ManualResetEventSlim _switch = new ManualResetEventSlim(true);
static APIStoreLoadBalancerManager()
{
AutoUpdateUrlMappings();
}
public static string ChooseServer(string key)
{
string choosenServerHost = null;
_switch.Wait();
ILoadBalancer bl = _balancers.GetOrAdd(key, (k) =>
{
return new BaseLoadBalancer(ConfigServiceInteraction.GetServiceURLs(k).Split(','), key, null);
});
choosenServerHost = bl.ChooseServer(key);
return choosenServerHost;
}
public static void UpdateServerList(string key, IEnumerable<string> serverList)
{
ILoadBalancer old = null;
if (_balancers.TryGetValue(key, out old))
{
_balancers[key] = new BaseLoadBalancer(serverList, key, old.Rule);
}
}
private static void UpdateAll()
{
IEnumerable<string> list = null;
foreach (string key in _balancers.Keys)
{
try
{
list = ConfigServiceInteraction.GetServiceURLs(key).Split(',');
UpdateServerList(key, list);
}
catch (System.Exception ex)
{
using (FileStream fs = new FileStream("d:\Error.txt", FileMode.OpenOrCreate, FileAccess.Write, FileShare.Write))
{
StreamWriter sw = new StreamWriter(fs);
sw.WriteLine(ex.StackTrace);
sw.WriteLine(ex.Message);
sw.WriteLine("================");
}
}
}
}
private static void AutoUpdateUrlMappings()
{
Thread thAutoRefresh = new Thread(() =>
{
while (true)
{
Thread.Sleep((int)(AppSettings.AutoRefreshMinutes * 60 * 1000));
_switch.Reset();
UpdateAll();
_switch.Set();
}
});
thAutoRefresh.IsBackground = true;
thAutoRefresh.Start();
}
}
现在我们可以像这样简单地调用:
context.RealUrl = APIStoreLoadBalancerManager.ChooseServer("xxx"); //this is a whole global WebApi.
现在我用fiddler手动测试了,结果是:
现在看来是 1 分钟后(自动更新 LOAD BALANCER 的服务 IP 地址...等)。它告诉我:
为什么以及如何修复此类错误?我在 Global 的 Application_Error(写在文件中)中一无所获。
注意:请忽略200和502(它们交替出现,根据我的负载均衡器的算法,这是正确的)。
好吧,这是通过类似 Hystrix 的东西使用的。
我现在做一个简单的负载均衡测试,代码如下:
public static class APIStoreLoadBalancerManager
{
private static readonly ConcurrentDictionary<string, ILoadBalancer> _balancers
= new ConcurrentDictionary<string, ILoadBalancer>();
private static volatile ManualResetEventSlim _switch = new ManualResetEventSlim(true);
static APIStoreLoadBalancerManager()
{
AutoUpdateUrlMappings();
}
public static string ChooseServer(string key)
{
string choosenServerHost = null;
_switch.Wait();
ILoadBalancer bl = _balancers.GetOrAdd(key, (k) =>
{
return new BaseLoadBalancer(ConfigServiceInteraction.GetServiceURLs(k).Split(','), key, null);
});
choosenServerHost = bl.ChooseServer(key);
return choosenServerHost;
}
public static void UpdateServerList(string key, IEnumerable<string> serverList)
{
ILoadBalancer old = null;
if (_balancers.TryGetValue(key, out old))
{
_balancers[key] = new BaseLoadBalancer(serverList, key, old.Rule);
}
}
private static void UpdateAll()
{
IEnumerable<string> list = null;
foreach (string key in _balancers.Keys)
{
try
{
list = ConfigServiceInteraction.GetServiceURLs(key).Split(',');
UpdateServerList(key, list);
}
catch (System.Exception ex)
{
using (FileStream fs = new FileStream("d:\Error.txt", FileMode.OpenOrCreate, FileAccess.Write, FileShare.Write))
{
StreamWriter sw = new StreamWriter(fs);
sw.WriteLine(ex.StackTrace);
sw.WriteLine(ex.Message);
sw.WriteLine("================");
}
}
}
}
private static void AutoUpdateUrlMappings()
{
Thread thAutoRefresh = new Thread(() =>
{
while (true)
{
Thread.Sleep((int)(AppSettings.AutoRefreshMinutes * 60 * 1000));
_switch.Reset();
UpdateAll();
_switch.Set();
}
});
thAutoRefresh.IsBackground = true;
thAutoRefresh.Start();
}
}
现在我们可以像这样简单地调用:
context.RealUrl = APIStoreLoadBalancerManager.ChooseServer("xxx"); //this is a whole global WebApi.
现在我用fiddler手动测试了,结果是:
现在看来是 1 分钟后(自动更新 LOAD BALANCER 的服务 IP 地址...等)。它告诉我:
为什么以及如何修复此类错误?我在 Global 的 Application_Error(写在文件中)中一无所获。
注意:请忽略200和502(它们交替出现,根据我的负载均衡器的算法,这是正确的)。
好吧,这是通过类似 Hystrix 的东西使用的。