本文主要讨论基于spring cloud提供的hystrix组件的降级功能,示例如下:
1,引入必要的依赖
包括不限于hystrix、feign、dashboard(大盘监控)、feign客户端等的依赖
<!--hystrix-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-hystrix</artifactId>
</dependency>
<!--feign-client-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-openfeign</artifactId>
</dependency>
<dependency>
<groupId>org.example</groupId>
<artifactId>feign-client-intf</artifactId>
<version>${project.version}</version>
</dependency>
<!-- spring boot actuator-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<!--eureka-client-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
<!-- spring boot web-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
2,CODING
2.1 配置启动类
主要是叠加一些注解,使应用具有hystrix、feign等的功能
@EnableDiscoveryClient
@SpringBootApplication
@EnableFeignClients
@EnableCircuitBreaker
2.2 调用客户端的接口
@FeignClient(name = "feign-client",fallback = Fallback.class,primary = true)
public interface MyService extends IService {
}
2.3 调用feign客户端
调用fallback方法,myService会调用feign客户端的error方法,必定会异常,进而触发降级。
@RestController
public class Controller {
@Autowired
private MyService myService;
@Autowired
private RequestCacheService requestCacheService;
@GetMapping("/fallback")
public String fallback() {
return myService.error();
}
}
2.4 处理降级
当调用客户端发生异常时,这里会调用实现了MyService接口的同名降级方法。
可以通过@HystrixCommand(fallbackMethod = “XXX”)指定下一个降级方法,来实现多级降级。
如果多级降级还没有弄好,那就静默处理吧…
@Slf4j
@Component
public class Fallback implements MyService {
@Override
@HystrixCommand(fallbackMethod = "fallback2")
public String error() {
log.info("Fallback: I'm not a black sheep any more");
throw new RuntimeException("first fallback");
}
@HystrixCommand(fallbackMethod = "fallback3")
public String fallback2() {
log.info("fallback again");
throw new RuntimeException("fallback again");
}
public String fallback3() {
log.info("fallback again and again");
return "success";
}
3, 配置application.yml
当然了,启用hystrix缺少不了配置文件,一些基础的配置如下,包括启用hystrix的降级功能、超时配置等
spring:
application:
name: hystrix-consumer
main:
allow-bean-definition-overriding: true
server:
port: 50000
eureka:
client:
serviceUrl:
#注册中心地址
defaultZone: http://localhost:20000/eureka/
feign:
hystrix:
#开启feign下面的Hystrix功能
enabled: true
# 超时优先级顺序如下:feign > hystrix(全局) > hystrix(接口)
hystrix:
command:
default:
requestCache:
enabled: true
fallback:
#是否开启服务降级
enabled: true
execution:
timeout:
#超时
enabled: true
isolation:
thread:
#超时时间
timeoutInMilliseconds: 1000
#超时以后终止线程
interruptOnTimeout: true
#取消的时候终止线程
interruptOnFutureCancel: true
circuitBreaker:
#--------------打酱油的参数---------------------
#-------------默认就有的参数(值为现在标注出的值)-----------------
#开启熔断功能(熔断功能和熔断开关不一样,熔断开关开启后是直接返回静默结果,熔断功能仅仅是开启hystrix的熔断功能)
enabled: true
#强制开启熔断开关
forceOpen: false
#强制关闭熔断开关
forceClosed: false
#----------正八经的参数----------
#----------默认不开启的参数----------
#熔断的前提条件(请求的数量),在一定的时间窗口内,请求达到5个以后,才开始进行熔断的判断
#与metrics.roolingStats.timeInMilliseconds参数配合使用
requestVolumeThreshold: 5
#失败几率达到50%,熔断开启
errorThresholdPercentage: 50
#当熔断开启以后,经过多少秒再进入半开状态
sleepWindowInMilliseconds: 15000
metrics:
rollingStats:
#每20秒进行一次请求数量的统计
timeInMilliseconds: 20000
feign-client:
ribbon:
#每台机器最大的重试次数
MaxAutoRetries: 0
#可以再重试几台机器
MaxAutoRetriesNextServer: 1
#连接超时时间(ms)
ConnectTimeout: 1500
#业务处理超时(ms)
ReadTimeout: 4000
#在所有类型的HTTP Method进行重试
#默认为false,仅在不改动数据(查)的情况下进行重试
#为true时,要特别注意幂等问题
OkToRetryOnAllOperations: false
基本上hystrix通过这些配置和coding就可以启动降级和超时的功能,这里配置是有一处坑的,也就是超时优先级顺序的问题,配置代码块里有写优先级的差别,
feign > hystrix(全局) > hystrix(接口)
经笔者测试,feign中带的ribbon超时配置是优先级最高的,会最先生效,hystrix的超时时间要比ribbon的大一些才能生效。而hystrix的全局超时(default)要比为某个方法配置的超时时间优先级高。
缓存、监控等功能会在后面的文章里讲一下使用。