1简介
1.1.Hystrix概述
Hystix是Netflix开源的一个延迟和容错库,用于隔离访问远程服务、第三方库,防止出现级联失败(雪崩)
级联失败:c挂了之后,b不知道,一直请求。直到b的资源耗尽,b挂了。然后a重复刚刚b的操作。a也挂了=》雪崩: 一个服务失败,导致整条链路的服务都失败的情形。
1.2.Hystix主要功能
1.2.1.隔离
1)原始情况:A有一百线程。C挂了,每次都有请求让A去调用C。因为C挂了每个线程都没有返回结果,每次A就用一个新的线程去调用C,最后结果A没线程了(都在调用C的途中,被占用。没有空闲线程)。A就挂了导致 A、B、C、D四个服务都挂了
2)线程池隔离(默认):将A的100个线程分成三分,分别对应服务B、C、D,调用B的线程不会去调用C、D,这样如果C挂了。只是去调用C的40个线程被占用,B、C的线程还是空闲,调用B、C还是可以正常调用
3)信号量隔离:只允许比如40个线程访问C,剩下的不被允许访问
1.2.2降级:
当出现异常、超时(默认一秒超时,一秒没有返回任何东西就算超时)的时候触发;(A消费端-考虑网络不通用这个、C服务提供端)。比如:提示当前访问过多,请等待;降级就是提供一个备选方案(参考try catch中的catch)
1.2.3熔断:
当一定时间内,一个服务的错误过多,Hystrix 就会中断对此服务的请求,直到该服务恢复正常
1.2.4限流:
限制访问线程数量
1.3细说降级
1.3.1服务提供方提供降级
1.在服务提供方,引入hystrix依赖
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-hystrix</artifactId>
</dependency>
2.定义降级方法
3.使用@HystrixCommand注解配置降级方法
/**
* 正常方法
* fallbackMethod 指定调用的降级法昂发
* @param message
* @return
* @throws InterruptedException
*/
@GetMapping("/sayhello")
@HystrixCommand(fallbackMethod = "providerSayHello_fallback"
//设置Hystrix的超时时间,默认1秒,此处设置为3秒
, commandProperties = {@HystrixProperty(name = "execution.isolation.thread.timeoutInMilliseconds", value = "4000")})
public String providerSayHello(String message) throws InterruptedException {
// int a=1/0;//错误
Thread.sleep(3000);//超时设置
log.error("provder sayhello port:{}, message:{}", port, message);
return "Provider sayhello port:" + port + " , message:" + message;
}
/**
* 定义降级方法:
* 1.方法的返回值需要和原方法一样
* 2. 方法的参数需要和原方法一样
* 3.如果原方法参数有任何限制 ,此处都不需要
*/
public String providerSayHello_fallback(String message) {
return "Provider sayhello 降级服务";
}
4.在启动类上开启Hystrix功能: @EnableCircuitBreaker
@EnableDiscoveryClient // DiscoverClient可以集成大部分注册中心
// @EnableEurekaClient 只对Eureka使用
@SpringBootApplication
@EnableCircuitBreaker //启动降级服务
public class BackendShowProviderApplication {
public static void main(String[] args) {
SpringApplication.run(BackendShowProviderApplication.class,args);
}
}
1.3.1消费服务方提供降级
基本和上面一样。
1.4细说熔断
Hystrix熔断机制,用于监控微服务调用情况,当失败的情况达到预定的阈值(5秒失败20次),会打开断路器拒绝所有请求,直到服务恢复正常为止。如下图(hystrix自动的,不需手动配置)
当然,也可以手动配置
circuitBreaker. sleepWindowInMilliseconds:监控时间
circuitBreaker.requestVolumeThreshold:失败次数
circuitBreaker. errorThresholdPercentage:失败率
下面配置就是5 秒内,在十次中 有50%出错,则就会打开熔断
@GetMapping("/sayhello")
@HystrixCommand(fallbackMethod = "providerSayHello_fallback"
//设置Hystrix的超时时间,默认1秒,此处设置为3秒
, commandProperties = {@HystrixProperty(name = "execution.isolation.thread.timeoutInMilliseconds", value = "4000")
//监控时间默认5000毫秒=5秒
, @HystrixProperty(name = "circuitBreaker.sleepWindowInMilliseconds", value = "5000")
//失败次数。默认20次
, @HystrixProperty(name = "circuitBreaker.requestVolumeThreshold", value = "10")
//失败率默认50%
, @HystrixProperty(name = "circuitBreaker.errorThresholdPercentage", value = "50")
})
public String providerSayHello(String message) throws InterruptedException {
if ("1".equals(message)) {
int a = 1 / 0;//错误
}
// int a=1/0;//错误
// Thread.sleep(3000);//超时设置
log.error("provder sayhello port:{}, message:{}", port, message);
return "Provider sayhello port:" + port + " , message:" + message;
}
待跟新