Spring Cloud Hystrix

Spring Cloud Hystrix简介

Spring Cloud Hystrix是Spring Cloud Netflix子项目的核心组件之一,是一个用于处理分布式系统的延迟和容错的开源库,具有服务容错和线程隔离等一系列服务保护功能。

在微服务架构中,服务与服务之间通过远程调用的方式实现通信,一旦某个被调用的服务发生故障,其依赖服务也会发生故障,此时就会发生故障的蔓延,导致分布式的系统雪崩和瘫痪。

image-20220324141706946

由于多种因素,例如网络响应、系统故障、硬件问题等,服务发生故障的情况是无法避免的。因此我们需要一个应对服务容错保护的解决方案。

Spring Cloud Hystrix实现了断路器模式,它相当于一种开关控制,通过断路器的故障监控即保险丝,当某个服务发生故障时,通过断路器监控,给服务的调用方返回一个服务于其的,可处理的备用响应响应(FallBack),而不是长时间的等待或者抛出异常,避免联级故障,这样就不会使得调用方由于长时间得不到响应而占用线程从而导致故障的蔓延,提高了分布式系统的弹性。

img

Hystrix具备服务降级、服务熔断、服务限流、线程隔离、请求缓存、请求合并及服务监控等强大的功能。

服务降级

服务降级定义

服务降级:当服务器压力剧增的情况下,根据实际业务情况及流量,对一些服务和页面有策略的不处理或作简单处理,从而释放服务器资源以保证核心业务正常运作或高效运作,即服务降级提供一个补救措施正常响应给请求者,保证服务可用的前提下,提高系统的可用性和安全性。如果在并发高峰期不做服务降级处理,不仅会影响整体服务的性能,甚至会导致宕机造成服务不可用。

服务降级应用场景

一般在高峰期,为了保证核心功能服务的可用性,要对某些服务降级处理。

当整个微服务架构整体的负载超出了预设的上限阈值或即将到来的流量预计将会超过预设的阈值时,为了保证重要或基本的服务能正常运行,可以将一些不重要或不紧急的服务或任务进行服务 延迟使用或暂停使用。

服务降级实现

导入Hystrix依赖

1
2
3
4
5
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-hystrix</artifactId>
<version>2.2.10.RELEASE</version>
</dependency>

启动类添加注解@EnableHystrix

image-20220604155829336

在服务实现层添加调用方法与服务降级方法并且添加@HystrixCommand注解

image-20220604161405440

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
@Service
public class BorrowServiceImpl implements BorrowService {
@Resource
BorrowMapper borrowMapper;
@Autowired
RestTemplate restTemplate;
@Value("${service-url.user-service}")
private String userServiceUrl;
@Value("${service-url.book-service}")
private String bookServiceUrl;

@Override
@HystrixCommand(fallbackMethod = "getDefaultUserBorrowDetail")
public UserBorrowDetail getBorrowByUser(Long uid) {
List<Borrow> borrows = borrowMapper.getBorrow(uid,null);
User user = restTemplate.getForObject(userServiceUrl + "/user/" + uid, User.class);
List<Book> books = borrows.stream().map(borrow -> restTemplate.getForObject(bookServiceUrl + "/book/" + borrow.getBid(), Book.class)).collect(Collectors.toList());
return new UserBorrowDetail(user, books);
}

public UserBorrowDetail getDefaultUserBorrowDetail(@PathVariable Long uid) {
return new UserBorrowDetail(null, Collections.emptyList());
}
}

测试结果

image-20220604161028821

image-20220604161441837

@HystrixCommand常用参数

  • fallbackMethod:指定服务降级处理方法
  • ignoreExceptions:忽略某些处理,不发生服务降级
  • commandKey:命令名称,用于区分不同的命令
  • groupKey:分组名称,Hystrix会根据不同的分组来统计命令的告警及仪表盘信息
  • threadPoolKey:线程池,名称,用于划分线程池

服务熔断

服务熔断定义

熔断机制是应对雪崩效应的一种微服务链路保护机制。Hystrix会监控微服务调用的状况,默认当5秒内20次调用失败就会开启熔断机制,当检测出服务调用链路的某个微服务不可用或者响应时间太长时,会进行服务的降级,进而熔断该节点的微服务调用,快速返回”错误”的响应信息。此外,在熔断后,Hystrix会定期检测该节点的微服务是否恢复正常响应,当检测到该节点微服务响应正常后,恢复调用链路。

Hystrix请求缓存

当系统并发量越来越大时,我们可以使用缓存来优化系统,达到减轻并发请求线程数,提供响应速度的效果。

Hystrix Dashboard

Hystrix Dashboard是Spring Cloud中查看Hystrix实例执行情况的一种仪表组件,支持查看单个实例和查看集群实例。

Hystrix提供了Hystrix Dashboard来实时监控HystrixCommand方法的执行情况。 Hystrix Dashboard可以有效地反映出每个Hystrix实例的运行情况,帮助我们快速发现系统中的问题,从而采取对应措施。

Hystrix单个实例监控

使用Hystrix Dashboard监控单个Hystrix实例

创建hystrix-dashboard模块

hystrix-dashboard用来监控hystrix实例的执行情况

image-20220605174356650

1
2
3
4
5
6
7
8
9
10
11
12
13
14
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-hystrix-dashboard</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
</dependencies>

添加配置文件

image-20220605175849005

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
server:
port: 8401
spring:
application:
name: hystrix-dashboard
eureka:
client:
register-with-eureka: true
fetch-registry: true
service-url:
defaultZone: http://root:123456@replica1:8005/eureka,
http://root:123456@replica2:8006/eureka
hystrix:
dashboard:
proxy-stream-allow-list: "localhost"

创建启动类

image-20220605175413117

1
2
3
4
5
6
7
8
@EnableHystrixDashboard
@EnableDiscoveryClient
@SpringBootApplication
public class HystrixDashboardApplication {
public static void main(String[] args) {
SpringApplication.run(HystrixDashboardApplication.class, args);
}
}

在被监控的服务添加Actuator依赖

Actuator是SpringBoot程序的监控系统,可以实现健康检查,记录信息等。

image-20220605200314158

在被监控的服务配置开启hystrix.stream监控端点

image-20220605181613411

1
2
3
4
5
management:
endpoints:
web:
exposure:
include: 'hystrix.stream'

Hystrix监控实例

访问Hystrix Dashboard:http://localhost:8401/hystrix

img

image-20220605181313785

img