.net framework 4.5 +steeltoe+ springcloud(三)实现Hystrix断路器
2024-08-24 04:22:29
在基于.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服务,再启动程序看看,会不会调用熔断机制,如果调用了则页面会显示我们设定好的提示语【服务断开,请稍后重试!】:
成功!
最新文章
- Codeforces Round #169 (Div. 2)
- PCIe 32GT/s 含义
- Ubuntu 16.04 802.1x 有线连接
- 服务器端解决JS跨域调用问题
- gif压缩
- C# DBNULL与NULL之间的区别【转】
- oc随笔五:NSArray
- C# Thread Lock 笔记
- 微信小程序wx.navigateTo页面不跳转
- 【Selenium】【BugList1】调用firefox浏览器,报 TypeError: &#39;module&#39; object is not callable
- cmd与linux使用curl差异
- Centos 6.5 pptpd服务端搭建过程
- SQL Server 存储过程 (需整理)
- Asp.Net分页生成页码超链接方法
- Hive常见问题
- Spring JDBC Framework
- WebMagic 启动例子报错
- POJ - 3660 Cow Contest(传递闭包)
- 调整多个控件的dock的顺序
- ios打电话发短信接口