博客
关于我
使用Hystrix提高系统可用性
阅读量:449 次
发布时间:2019-03-06

本文共 1909 字,大约阅读时间需要 6 分钟。

在分布式系统中,依赖关系的复杂性和服务的不可靠性是系统设计中的一个重大挑战。每个服务都可能因为依赖服务的故障或网络不稳定而无法正常运行,这种情况在单机环境中几乎不会发生。假设我们有5个依赖服务,每个服务的可用性为99.95%,即每年大约有4个多小时不可用,那么整体可用性可能会降至99.75%(接近一天)。再加上网络不稳定和依赖服务的多样性,可用性会进一步降低。那么,如何在依赖服务不可用或网络不稳定的情况下,确保自身服务的稳定性呢?

Michael T. Nygard 在他的书中提到了两种关键的提升系统可用性的模式:超时和断路器。

首先,超时机制。网络调用外部依赖服务时,必须设置超时。健康的情况下,一次远程调用通常在几十毫秒内完成,但在网络拥堵或依赖服务不可用的情况下,这个时间可能会延长到好几秒甚至永远。没有超时机制的远程调用可能会导致一个线程或进程被长时间占用资源,从而导致系统资源耗尽,进而影响自身服务的可用性。因此,非核心依赖服务的不可用性可能会拖垮整个系统。

其次,断路器。类似于家庭中的保险丝,当电路过载或短路时,保险丝会烧断切断电路,避免更大的灾难。断路器在服务间依赖中发挥着类似的作用。当大量请求由于依赖服务不可用或超时而失败时,断路器会检测到超过50%的失败率或连续20次失败后,主动打开断路器,返回失败,避免资源浪费。断路器会在5分钟后尝试关闭,以判断依赖服务是否恢复。

Hystrix 是 Netflix 开源的弹性框架,提供了对远程依赖的保护。通过Hystrix,开发者可以将远程调用包装在 Command 中,设置超时和断路器。例如,以下代码展示了如何使用Hystrix封装远程调用:

public class GetCurrentTimeCommand : HystrixCommand
{ private static long currentTimeCache; public GetCurrentTimeCommand() : base(HystrixCommandSetter.WithGroupKey("TimeGroup") .AndCommandKey("GetCurrentTime") .AndCommandPropertiesDefaults( new HystrixCommandPropertiesSetter() .WithExecutionIsolationThreadTimeout(TimeSpan.FromSeconds(1.0)) .WithExecutionIsolationThreadInterruptOnTimeout(true))) {} protected override long Run() { using (new WebClient()) { string content = wc.DownloadString("http://tycho.usno.navy.mil/cgi-bin/time.pl"); XDocument document = XDocument.Parse(content); currentTimeCache = long.Parse(document.Element("usno").Element("t").Value); } return currentTimeCache; } protected override long GetFallback() { return currentTimeCache; }}

Hystrix默认会为 Command 分配线程池,池的大小默认为10,避免了过多线程占用资源。当命令数量超过阈值时,Hystrix会走fallback或抛异常。根据需求,可以调整线程池大小和其他属性。

Hystrix 的断路器机制默认启用,会统计命令失败率。当失败率超过50%时,断路器会打开,返回失败。5秒后,Hystrix会尝试关闭断路器,观察依赖服务的状态。

通过超时和断路器机制,Hystrix能够有效保护服务不受依赖不可用或网络不稳定的影响,从而保证系统的弹性和可用性。

转载地址:http://xlsfz.baihongyu.com/

你可能感兴趣的文章
no available service ‘default‘ found, please make sure registry config corre seata
查看>>
no connection could be made because the target machine actively refused it.问题解决
查看>>
No Datastore Session bound to thread, and configuration does not allow creation of non-transactional
查看>>
No fallbackFactory instance of type class com.ruoyi---SpringCloud Alibaba_若依微服务框架改造---工作笔记005
查看>>
No Feign Client for loadBalancing defined. Did you forget to include spring-cloud-starter-loadbalanc
查看>>
No mapping found for HTTP request with URI [/...] in DispatcherServlet with name ...的解决方法
查看>>
No mapping found for HTTP request with URI [/logout.do] in DispatcherServlet with name 'springmvc'
查看>>
No module named 'crispy_forms'等使用pycharm开发
查看>>
No module named cv2
查看>>
No module named tensorboard.main在安装tensorboardX的时候遇到的问题
查看>>
No module named ‘MySQLdb‘错误解决No module named ‘MySQLdb‘错误解决
查看>>
No new migrations found. Your system is up-to-date.
查看>>
No qualifying bean of type XXX found for dependency XXX.
查看>>
No resource identifier found for attribute 'srcCompat' in package的解决办法
查看>>
no session found for current thread
查看>>
No toolchains found in the NDK toolchains folder for ABI with prefix: mips64el-linux-android
查看>>
NO.23 ZenTaoPHP目录结构
查看>>
no1
查看>>
NO32 网络层次及OSI7层模型--TCP三次握手四次断开--子网划分
查看>>
NoClassDefFoundError: org/springframework/boot/context/properties/ConfigurationBeanFactoryMetadata
查看>>