.net framework 4.5 +steeltoe+ springcloud(三)实现Hystrix断路器

在基于.net framework的服务客户端实现断路器功能,基本项目创建步骤可以参照我的另一篇发现和调用服务的笔记,地址:http://www.cnblogs.com/troytian/p/8621861.html

 
在客户端能实现服务调用基础上,我们首先需要在appsettings.json中添加Hystrix配置:
 "hystrix": {
"command": {
"FortuneService": {
"threadPoolKeyOverride": "FortuneServiceTPool"
}
},
"stream": {
"validate_certificates": false
}
},
 
 
 
 
在程序入口对断路器进行注册:
 
  protected void Application_Start()
{
AreaRegistration.RegisterAllAreas();
FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters);
RouteConfig.RegisterRoutes(RouteTable.Routes);
BundleConfig.RegisterBundles(BundleTable.Bundles); ApplicationConfig.RegisterConfig("development"); var builder = new ContainerBuilder(); // Add Microsoft Options to container
builder.RegisterOptions(); // Add Microsoft Logging to container
builder.RegisterLogging(ApplicationConfig.Configuration); // Add Console logger to container
builder.RegisterConsoleLogging(); // Register all the controllers with Autofac
builder.RegisterControllers(typeof(MvcApplication).Assembly); // Register IDiscoveryClient, etc.
builder.RegisterDiscoveryClient(ApplicationConfig.Configuration); // Register FortuneService Hystrix command
builder.RegisterHystrixCommand<IFetchServise, FetchServise>("fetchServise", ApplicationConfig.Configuration); // Register Hystrix Metrics/Monitoring stream
//builder.RegisterHystrixMetricsStream(ApplicationConfig.Configuration); // Create the Autofac container
var container = builder.Build();
DependencyResolver.SetResolver(new AutofacDependencyResolver(container)); // Get a logger from container
var logger = container.Resolve<ILogger<MvcApplication>>(); logger.LogInformation("Finished container build, starting background services"); // Start the Discovery client background thread
container.StartDiscoveryClient(); // Start the Hystrix Metrics stream
//container.StartHystrixMetricsStream(); logger.LogInformation("Finished starting background services");
}
 
 
然后需要修改服务调用的方式,添加断路器fallback回调功能,FetchServise.cs:
 public class FetchServise : HystrixCommand<string>, IFetchServise
{
DiscoveryHttpClientHandler _handler; private const string RANDOM_FORTUNE_URL = "http://java-service/hi?name=tian";
private ILogger<FetchServise> _logger; public FetchServise(IHystrixCommandOptions options, IDiscoveryClient client, ILoggerFactory logFactory = null) : base(options)
{
_handler = new DiscoveryHttpClientHandler(client, logFactory?.CreateLogger<DiscoveryHttpClientHandler>());
IsFallbackUserDefined = true;
_logger = logFactory?.CreateLogger<FetchServise>();
} public async Task<string> RandomFortuneAsync()
{
_logger?.LogInformation("RandomFortuneAsync");
var result = await ExecuteAsync();
_logger?.LogInformation("RandomFortuneAsync returning: " + result);
return result;
} protected override async Task<string> RunAsync()
{
_logger?.LogInformation("RunAsync");
var client = GetClient();
var result = await client.GetStringAsync(RANDOM_FORTUNE_URL);
_logger?.LogInformation("RunAsync returning: " + result);
return result;
} protected override async Task<string> RunFallbackAsync()
{
_logger?.LogInformation("RunFallbackAsync");
return await Task.FromResult("服务断开,请稍后重试!");
} private HttpClient GetClient()
{
var client = new HttpClient(_handler, false);
return client;
}
}
 
 
现在我们断开java-service服务,再启动程序看看,会不会调用熔断机制,如果调用了则页面会显示我们设定好的提示语【服务断开,请稍后重试!】:
.net framework 4.5 +steeltoe+ springcloud(三)实现Hystrix断路器
.net framework 4.5 +steeltoe+ springcloud(三)实现Hystrix断路器
成功!
 
上一篇:将内部部署网络和 Windows Azure 集成的新选项: 使用AT&T


下一篇:pyhanlp 两种依存句法分类器