一个服务,依赖于另一个功能服务的,如果这个功能服务挂掉了,那么依赖的服务就不能再用了,这种级联的失败, 我们可以称之为雪崩
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-hystrix</artifactId>
</dependency>
@EnableHystrix
启动运行
当响应过慢(默认值是1000),访问时,也会进入到指定的降级方法当中
hystrix:
command:
default:
execution:
isolation:
thread:
timeoutInMilliseconds: 9000 # 设置hystrix的超时时间为9000ms
ribbon:
ReadTimeout: 10000
ConnectTimeout: 10000
使用Fengin调用 Feign默认也有对Hystix的集成,1默认情况下是关闭的。我们需要通过下面的参数来开启:
feign:
hystrix:
enabled: true # 开启Feign的熔断功能
运行
把goods的睡眠时间改成1000 运行:
运行:
线程隔离降级处理, 如果请求延迟过高,如果超时,返回一个异常信息:
配置:
hystrix:
command:
default:
execution:
isolation:
thread:
timeoutInMilliseconds: 9000 #设置超时时长
circuitBreaker:
requestVolumeThreshold: 5 # 10秒种访问5次都失败的话, 会断开服务,不调用方法,直接进入fallback
sleepWindowInMilliseconds : 5000 #默认是5秒 5秒后尝试再访问一次服务器
goods模拟异常
概念:
默认什么都不设置 当访问请求次数超过10的时候 , 会调用fallback方法
feign 默认是支持hystrix的, 但是在Spring - cloud Dalston 版本之后就默认关闭了, 因为不一定业务需求要用的到
feign:
hystrix:
enabled: true #开启feign当中的hystrix
第一次运行
连续刷新11次后
效果和第一种方式一样