spring cloud hystrix
来源:互联网 发布:慧聪发发软件 编辑:程序博客网 时间:2024/06/01 09:43
相关文章:
Spring cloud - 概述
Spring cloud eureka - 注册中心
Spring cloud feign - RPC
Spring cloud ribbon - 负载均衡
Spring cloud hystrix - 断路器
Spring cloud hystrix dashboard - 断路器dashboard
Spring cloud zuul- 网关
关键字
断路(熔断)
背景
项目的微服务化,会导致原本一个简单的调用变成多级链式甚至网状调用。接口间相互依赖的层次越深,只要调用链底层中任何一个环节出故障,都会导致上层级联故障,而且越底层越容易放大故障,这种现象称为雪崩。
这种现象可能会拖垮整个项目,高并发依赖失败时如果没有隔离措施,就导致整个项目不可用。
现实电路中,如果电流过大就有可能引发火灾,导致整个家庭甚至整个区域无法正常供电。因此家用电路中都会使用保险丝等保险设备,如果某个电器导致电流过大,当达到危险阀值时就熔断当前电器的环路,牺牲当前电器的使用,以保全整个电网的正常使用。
如果微服务架构中也有一种如电路保险丝一样的隔离方法,可以设置在某种超时或者失败情形下断开依赖调用或者返回指定逻辑,牺牲新请求以换取项目的整体可用性,那么就可以提高应用的可用性和稳定性。
什么是hystrix
hystrix是Netflix公司实现的一个熔断库,spring 把它集成到spring cloud微服务解决方案中。当级联请求中的某个服务出现故障(如:阻塞)时可以自动切断当前服务,暂停对外提供服务。当服务恢复正常时,自动放开当前服务。
原理
原理
当调用一个特定服务,默认10(circuitBreaker.requestVolumeThreshold)秒内,此服务被调用超过20次(circuitBreaker.requestVolumeThreshold),并且错误率超过50%(circuitBreaker.errorThresholdPercentage),那么断路器就打开,阻止此服务对外提供服务,禁止外部调用,此时如果开发者提供了其它回调方法,可以调用备用的回调方法,以替代原服务。断路器保持打开状态一段时间(默认5秒),然后切换到半打开状态(HALF-OPEN),即先放进来一个请求,检查此次请求是否成功,如果本次请求仍然失败,说明服务没有恢复,断路器继续保持开路状态(OPEN)。如果本次请求成功,说明服务已恢复,则关闭断路器,把服务重新加入到整个服务链中。fallback
hystrix的断路机制,可以直接阻制新请求,也可以提供一种备选方案,即:如果断路原请求时,可以调用开发者提供的备用回调方法,以返回其它替代数据(如:表态数据,或其它失败信息),相当于降级处理。资源隔离
Semaphores
如何集成hystrix
- maven依赖
pom.xml中配置如下,引入hystrix依赖
<dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-hystrix</artifactId> </dependency>
- 代码
@SpringBootApplication@EnableCircuitBreaker //开启断路器public class Application { public static void main(String[] args) { new SpringApplicationBuilder(Application.class).web(true).run(args); }}@Componentpublic class StoreIntegration { @HystrixCommand(fallbackMethod = "defaultStores") public Object getStores(Map<String, Object> parameters) { //do stuff that might fail } //原服务被阻止时,会被调用的替代方法,参数和返回值类型需要与原方法一致 public Object defaultStores(Map<String, Object> parameters) { return /* something useful */; }}
@HystrixCommand是由Netflix贡献的注解,spring cloud会自动把带有此注解的bean封装成一个集成了Hystrix功能的代理对象。
- 配置
详情请点击这里
几个重要配置如下:circuitBreaker.enabled =true/false(断路器开关)circuitBreaker.requestVolumeThreshold=20 (断路器请求阈值:单位时间内最小请求数量,默认20.即:只有超过这个数量的请求,才会检查是否需要执行其它断路策略,如:如果默认20,那么当前请求只有19个,即使19个请求全部失败也不会断路)circuitBreaker.sleepWindowInMilliseconds(断路器休眠时间)circuitBreaker.errorThresholdPercentage(断路器错误请求百分比:单位时间内错误率)circuitBreaker.forceOpen=true/false(断路器强制开启:优先级高于forceClosed)circuitBreaker.forceClosed=true/false(断路器强制关闭:forceOpen优先级高)metrics.rollingStats.timeInMilliseconds=10000(单位时间:即断路器请求数量和失败率在计算基础,在当前指定的时间内.默认10秒)
@HystrixCommand注解的可以使用commandProperties属性做更精细化的配置,详情请点这里
@HystrixCommand(fallbackMethod = "stubMyService", commandProperties = { @HystrixProperty(name="execution.isolation.strategy", value="SEMAPHORE") })
与ribbon集成时注意事项
当使用 Hystrix commands 封装 Ribbon clients 时,要确认Hystrix的超时配置一定必须Ribbon的超时长,以及可能潜在的重复次数。例如: 如果Ribbon的超时时间是1秒,并且 Ribbon的重试次数是3次,那么Hystrix的超时时间至少是3秒。
feign如何集成hystrix
hystrix对应的类位于classpath以及feign.hystrix.enabled=true,feign会自动把使用@FeignClienti注解的类中所有方法都封装成具有hystrix功能的bean.
- maven依赖
<dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-hystrix</artifactId> </dependency>
- 代码
由于@FeignClient自动集成hystrix,所以通过@FeignClient的属性来配置故障回调类
@FeignClient(name = "hello", fallback = HystrixClientFallback.class)protected interface HystrixClient { @RequestMapping(method = RequestMethod.GET, value = "/hello") Hello iFailSometimes();}//从@HystrixCommand(fallbackMethod)中的回调方法转换成回调类static class HystrixClientFallback implements HystrixClient { @Override public Hello iFailSometimes() { return new Hello("fallback"); }}
- Feign and @Primary
当使用Feign和Hystrix的fallback时,spring 中会出现两个同类弄的bean,这会导致使用@Autowired注解进行自动装配时无法选择(@Autowired默认使用类型进行装配),因此Spring Cloud Netflix默认已经把所有@FeignClient注解的类产生的bean都标记为@Primary,而fallback类则不标记。但某些时候,如果不想让@FeignClient类的bean添加@Primary,则可以通过“@FeignClient(name = “hello”, primary = false)”取消类上的primary标记。
Hystrix Dashboard
未完待续...
接下来
本节主要了解了影响微服务稳定性的断路功能,接下将了解策服务路由zuul。
- Spring Cloud Hystrix--熔断器
- spring cloud-hystrix
- spring cloud Hystrix
- spring-cloud-hystrix(一)
- Spring Cloud Hystrix Dashboard
- spring cloud hystrix
- spring cloud hystrix dashboard
- Spring Cloud Hystrix
- Spring-Cloud 熔断器 Hystrix
- spring-cloud【Hystrix】
- spring cloud: Hystrix断路器(熔断器)
- spring cloud: Hystrix断路器(熔断器)
- Spring Cloud 断路器Hystrix实战
- Spring Cloud 中的断路器 hystrix
- spring cloud: Hystrix断路器(熔断器)
- spring-cloud-consumer-hystrix(六)
- Spring Cloud Eureka+Ribbon+Hystrix
- spring cloud: Hystrix断路器(熔断器)
- Linux用户空间与内核空间(理解高端内存)
- FSImage和EditsLog合并过程
- mini2451 BootLoader
- VIM
- 全面了解linux TCP/IP协议栈
- spring cloud hystrix
- 十万行代码的意义
- DNS查询过程
- Hadoop中的HDFS
- IOC理解
- I Hate It (线段树单点更新,求最值)
- <c语言经典100例>c8 九九乘法表
- “java.net.BindException: Address already in use”错误
- java代码注释规范