在基于.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服务,再启动程序看看,会不会调用熔断机制,如果调用了则页面会显示我们设定好的提示语【服务断开,请稍后重试!】:
成功!
 

最新文章

  1. Codeforces Round #169 (Div. 2)
  2. PCIe 32GT/s 含义
  3. Ubuntu 16.04 802.1x 有线连接
  4. 服务器端解决JS跨域调用问题
  5. gif压缩
  6. C# DBNULL与NULL之间的区别【转】
  7. oc随笔五:NSArray
  8. C# Thread Lock 笔记
  9. 微信小程序wx.navigateTo页面不跳转
  10. 【Selenium】【BugList1】调用firefox浏览器,报 TypeError: &#39;module&#39; object is not callable
  11. cmd与linux使用curl差异
  12. Centos 6.5 pptpd服务端搭建过程
  13. SQL Server 存储过程 (需整理)
  14. Asp.Net分页生成页码超链接方法
  15. Hive常见问题
  16. Spring JDBC Framework
  17. WebMagic 启动例子报错
  18. POJ - 3660 Cow Contest(传递闭包)
  19. 调整多个控件的dock的顺序
  20. ios打电话发短信接口

热门文章

  1. 标准IO缓冲机制
  2. Window7安装tensorflow整套环境详细流程
  3. mysql bigint ,int , smallint,tinyint 的范围
  4. springMVC学习三 注解开发环境搭建
  5. hibernate配置文件 连接数据库
  6. 2018.12.19 atcoder Iroha and a Grid(组合数学)
  7. 2018.11.17 codechef PRIMEDST(点分治+fft)
  8. hdu-4300(字符串hash)
  9. Echarts的使用方法
  10. Docker技术入门与实战(文摘)