Steeltoe之Circuit Breaker篇
在分布式系统中,服务发生异常是很正常的现象。为了处理这类“例外”,可以采取不同的应对策略,断路器模式即是其中一种方法。这个模式的主要特点是其可以阻断失败的级联影响,不会因为一个服务的失败导致其它关联服务一并失败。
在spring cloud生态系统中有hystrix类库可以提供这个模式的解决方案,而在.net世界里也有steeltoe这个开源项目能够提供助力。
package
对于asp.net core,使用steeltoe.circuitbreaker.hystrixcore类库。
对于console/asp.net 4.x,使用steeltoe.circuitbreaker.hystrixbase类库。
而如果有需求使用hystrix的dashboard,还需要增加steeltoe.circuitbreaker.hystrix.metricseventscore类库。
service
建立一个service类与接口,其中会调用试水spring cloud hystrix一文中的服务端应用程序。
public interface imessageservice { task<string> getmessage(); } public class messageservice : imessageservice { private readonly ihttpclientfactory _httpclientfactory; public messageservice(ihttpclientfactory httpclientfactory) { _httpclientfactory = httpclientfactory; } public async task<string> getmessage() { var client = _httpclientfactory.createclient(); var result = await client.getstringasync("http://localhost:8200/message"); return result; } }
command
建立一个继承hystrixcommand的command类,重写其runasync与runfallbackasync方法,分别用于正常场合下对service的调用与异常情况下的特别处理。
public class messagecommand : hystrixcommand<string> { private readonly imessageservice _messageservice; public messagecommand(ihystrixcommandoptions options, imessageservice messageservice) : base(options) { _messageservice = messageservice; } public async task<string> getmessage() { return await executeasync(); } protected override async task<string> runasync() { return await _messageservice.getmessage(); } protected override async task<string> runfallbackasync() { return await task.fromresult("woo, bad luck!"); } }
controller
controller的action方法里调用command的对应方法。
[route("api/[controller]")] [apicontroller] public class valuescontroller : controllerbase { private readonly messagecommand _command; public valuescontroller(messagecommand command) { _command = command; } // get api/values [httpget] public async task<string> get() { return await _command.getmessage(); } }
startup.cs
配置类中通过依赖注入方式注册httpclientfactory及已定义的service。如果需要使用仪表盘的话,还要追加注册addhystrixmetricsstream
。同时使用usehystrixrequestcontext
与usehystrixmetricsstream
。
public class startup { public startup(iconfiguration configuration) { configuration = configuration; } public iconfiguration configuration { get; } // this method gets called by the runtime. use this method to add services to the container. public void configureservices(iservicecollection services) { services.addhttpclient(); services.addsingleton<imessageservice, messageservice>(); services.addhystrixcommand<messagecommand>("message", configuration); services.addmvc(); services.addhystrixmetricsstream(configuration); } // this method gets called by the runtime. use this method to configure the http request pipeline. public void configure(iapplicationbuilder app, ihostingenvironment env) { if (env.isdevelopment()) { app.usedeveloperexceptionpage(); } app.usehystrixrequestcontext(); app.usemvc(); app.usehystrixmetricsstream(); } }
当应用程序启动后,如果spring cloud的服务端正常工作的话,页面会显示如下结果:
如果把服务端停止的话,页面则会显示另外的结果:
使用仪表盘的话,则可以看到更直观的数据(这里使用的也是上文中现成的客户端服务,已带有仪表盘功能):
上一篇: 堆、栈以及队列