文章目录
前言
Spring Cloud Netflix Hystrix 是Spring Cloud Netflix 子项目的核心组件之一,具有服务容错及线程隔离等一系列服务保护功能,本文将对其用法进行学习。
一、理论部分
1、基本的容错模式
-
主动超时:Http请求主动设置一个超时时间,超时就直接返回,不会造成服务堆积
-
限流:限制最大并发数
-
熔断:当错误数超过阈值时快速失败,不调用后端服务,同时隔一定时间放几个请求去重试后端服务是否能正常调用,如果成功则关闭熔断状态,失败则继续快速失败,直接返回。(此处有个重试,重试就是弹性恢复的能力)
-
隔离:把每个依赖或调用的服务都隔离开来,防止级联失败引起整体服务不可用
-
降级:服务失败或异常后,返回指定的默认信息
2、Hystrix 容错流程图
3、服务隔离
将请求封装在HystrixCommand中,然后这些请求在一个独立的线程中执行,每个依赖服务维护一个小的线程池(或信号量),在调用失败或超时的情况下可以断开依赖调用或者返回指定逻辑。
Hystrix提供了两种线程隔离方式:线程池和信号量。
Hystrix默认使用线程池做线程隔离,使用信号量隔离需要显示地将属性execution.isolation.strategy设置为ExecutionIsolationStrategy.SEMAPHORE,同时配置信号量个数,默认为10。客户端需向依赖服务发起请求时,首先要获取一个信号量才能真正发起调用,由于信号量的数量有限,当并发请求量超过信号量个数时,后续的请求都会直接拒绝,进入fallback流程。
线程池与信号量优缺点比较:
线程切换 | 支持异步 | 支持超时 | 支持熔断 | 限流 | 开销 | |
---|---|---|---|---|---|---|
信号量 | 否 | 否 | 否 | 是 | 是 | 小 |
线程池 | 是 | 是 | 是 | 是 | 是 | 大 |
线程池和信号量都支持熔断和限流。相比线程池,信号量不需要线程切换,因此避免了不必要的开销。但是信号量不支持异步,也不支持超时,也就是说当所请求的服务不可用时,信号量会控制超过限制的请求立即返回,但是已经持有信号量的线程只能等待服务响应或从超时中返回,即可能出现长时间等待。线程池模式下,当超过指定时间未响应的服务,Hystrix会通过响应中断的方式通知线程立即结束并返回。
4、服务熔断
当HystrixCommand请求后端服务失败数量超过一定比例(默认50%), 断路器会切换到开路状态(Open). 这时所有请求会直接失败而不会发送到后端服务,断路器保持在开路状态一段时间后(默认5秒),自动切换到半开路状态(HALF-OPEN),这时会判断下一次请求的返回情况, 如果请求成功, 断路器切回闭路状态(CLOSED),否则重新切换到开路状态(OPEN)。
熔断器配置
Circuit Breaker主要包括如下6个参数:
1、circuitBreaker.enabled
是否启用熔断器,默认是TRUE。
2 、circuitBreaker.forceOpen
熔断器强制打开,始终保持打开状态,不关注熔断开关的实际状态。默认值FLASE。
3、circuitBreaker.forceClosed
熔断器强制关闭,始终保持关闭状态,不关注熔断开关的实际状态。默认值FLASE。
4、circuitBreaker.errorThresholdPercentage
错误率,默认值50%,例如一段时间(10s)内有100个请求,其中有54个超时或者异常,那么这段时间内的错误率是54%,大于了默认值50%,这种情况下会触发熔断器打开。
5、circuitBreaker.requestVolumeThreshold
默认值20。含义是一段时间内至少有20个请求才进行errorThresholdPercentage计算。比如一段时间了有19个请求,且这些请求全部失败了,错误率是100%,但熔断器不会打开,总请求数不满足20。
6、circuitBreaker.sleepWindowInMilliseconds
半开状态试探睡眠时间,默认值5000ms。如:当熔断器开启5000ms之后,会尝试放过去一部分流量进行试探,确定依赖服务是否恢复。
5、服务降级
服务降级是指当请求后端服务出现异常的时候, 可以使用fallback方法返回的值。
Hystrix提供的降级主要是为了容错,保证当前服务不受依赖服务故障的影响,从而提高服务的健壮性。要支持回退或降级处理,可以重写HystrixCommand的getFallBack方法或HystrixObservableCommand的resumeWithFallback方法。
Hystrix在以下几种情况下会走降级逻辑:
- 执行construct()或run()抛出异常
- 熔断器打开导致命令短路
- 命令的线程池和队列或信号量的容量超额,命令被拒绝
- 命令执行超时
6、注解介绍
@EnableHystrix :启动熔断降级服务
@HystrixCommand详解
@HystrixCommand中的常用参数
- fallbackMethod:指定服务降级处理方法;
- ignoreExceptions:忽略某些异常,不发生服务降级;
- commandKey:命令名称,用于区分不同的命令;
- groupKey:分组名称,Hystrix会根据不同的分组来统计命令的告警及仪表盘信息;
- threadPoolKey:线程池名称,用于划分线程池。
示例:
@HystrixCommand(
commandProperties ={
//请求超时时间
@HystrixProperty(name = "execution.isolation.thread.timeoutInMilliseconds",value ="3000" ),
//统计窗口内的定义
@HystrixProperty(name =
"metrics.rollingStats.timeInMilliseconds",value = "8000"),
//最小请求数
@HystrixProperty(name =
"circuitBreaker.requestVolumeThreshold",value = "2"),
//错误的请求比例
@HystrixProperty(name =
"circuitBreaker.errorThresholdPercentage",value = "50"),
//自我修复的活窗口时长
@HystrixProperty(name =
"circuitBreaker.sleepWindowInMilliseconds",value = "3000")
}
,fallbackMethod ="myFallBack" //回退方法
//线程池标识,要标识为唯一
,threadPoolKey = "findRibbonResumeOpenStateTimeout"
//线程池属性
,threadPoolProperties = {
//核心纯种
@HystrixProperty(name = "coreSize",value ="1" ),
//最大等待队列长度
@HystrixProperty(name = "maxQueueSize",value ="20" )
}
)
请求缓存功能:
注解 | 描述 | 属性 |
---|---|---|
@CacheResult | 该注解用来标记请求命令返回的结果应该被缓存,它必须与@HystrixCommand注解结合使用 | cacheKeyMethod |
@CacheRemove | 该注解用来让请求命令的缓存失效,失效的缓存根据定义Key决定 | commandKey,cacheKeyMethod |
@CacheKey | 该注解用来在请求命令的参数上标记,使其作为缓存的Key值,如果没有标注则会使用所有参数。如果同事还是使用了@CacheResult和@CacheRemove注解的cacheKeyMethod方法指定缓存Key的生成,那么该注解将不会起作用value |
合并请求功能:
@HystrixCollapser的常用属性
batchMethod:用于设置请求合并的方法;
collapserProperties:请求合并属性,用于控制实例属性,有很多;
timerDelayInMilliseconds:collapserProperties中的属性,用于控制每隔多少时间合并一次请求;
7、常用配置
全局配置
hystrix:
command: #用于控制HystrixCommand的行为
default:
execution:
isolation:
strategy: THREAD #控制HystrixCommand的隔离策略,THREAD->线程池隔离策略(默认),SEMAPHORE->信号量隔离策略
thread:
timeoutInMilliseconds: 1000 #配置HystrixCommand执行的超时时间,执行超过该时间会进行服务降级处理
interruptOnTimeout: true #配置HystrixCommand执行超时的时候是否要中断
interruptOnCancel: true #配置HystrixCommand执行被取消的时候是否要中断
timeout:
enabled: true #配置HystrixCommand的执行是否启用超时时间
semaphore:
maxConcurrentRequests: 10 #当使用信号量隔离策略时,用来控制并发量的大小,超过该并发量的请求会被拒绝
fallback:
enabled: true #用于控制是否启用服务降级
circuitBreaker: #用于控制HystrixCircuitBreaker的行为
enabled: true #用于控制断路器是否跟踪健康状况以及熔断请求
requestVolumeThreshold: 20 #超过该请求数的请求会被拒绝
forceOpen: false #强制打开断路器,拒绝所有请求
forceClosed: false #强制关闭断路器,接收所有请求
requestCache:
enabled: true #用于控制是否开启请求缓存
collapser: #用于控制HystrixCollapser的执行行为
default:
maxRequestsInBatch: 100 #控制一次合并请求合并的最大请求数
timerDelayinMilliseconds: 10 #控制多少毫秒内的请求会被合并成一个
requestCache:
enabled: true #控制合并请求是否开启缓存
threadpool: #用于控制HystrixCommand执行所在线程池的行为
default:
coreSize: 10 #线程池的核心线程数
maximumSize: 10 #线程池的最大线程数,超过该线程数的请求会被拒绝
maxQueueSize: -1 #用于设置线程池的最大队列大小,-1采用SynchronousQueue,其他正数采用LinkedBlockingQueue
queueSizeRejectionThreshold: 5 #用于设置线程池队列的拒绝阀值,由于LinkedBlockingQueue不能动态改版大小,使用时需要用该参数来控制线程数
实例配置
实例配置只需要将全局配置中的default换成与之对应的key即可。
hystrix:
command:
HystrixComandKey: #将default换成HystrixComrnandKey
execution:
isolation:
strategy: THREAD
collapser:
HystrixCollapserKey: #将default换成HystrixCollapserKey
maxRequestsInBatch: 100
threadpool:
HystrixThreadPoolKey: #将default换成HystrixThreadPoolKey
coreSize: 10
配置文件中相关key的说明
HystrixComandKey对应@HystrixCommand中的commandKey属性;
HystrixCollapserKey对应@HystrixCollapser注解中的collapserKey属性;
HystrixThreadPoolKey对应@HystrixCommand中的threadPoolKey属性。
8、缓存功能
当系统并发量越来越大时,我们需要使用缓存来优化系统,达到减轻并发请求线程数,提供响应速度的效果。
9、请求合并功能
微服务系统中的服务间通信,需要通过远程调用来实现,随着调用次数越来越多,占用线程资源也会越来越多。Hystrix中提供了@HystrixCollapser用于合并请求,从而达到减少通信消耗及线程数量的效果。
10、舱壁模式
为了避免问题服务请求过多导致正常服务⽆法访问,Hystrix 不是采⽤增加线程数,⽽是单独的为每⼀个控制⽅法创建⼀个线程池的⽅式,这种模式叫做“舱壁模式",也是线程隔离的⼿段。
示例:
@HystrixCommand(
// 线程池标识保持唯一(单独使用一个线程池),和属性配置
threadPoolKey = "myThreadPool",
threadPoolProperties = {
@HystrixProperty(name = "coreSize", value = "1"),// 线程数
@HystrixProperty(name = "maxQueueSize", value = "20")// 等待队列长度
}
)
二、实战部分
1、创建一个hystrix-service模块
2、添加相关依赖
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<parent>
<artifactId>eureka</artifactId>
<groupId>com.hjl</groupId>
<version>1.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>hystrix-service</artifactId>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-hystrix</artifactId>
<version>RELEASE</version>
</dependency>
<dependency>
<groupId>cn.hutool</groupId>
<artifactId>hutool-all</artifactId>
<version>5.3.2</version>
</dependency>
</dependencies>
</project>
3、配置Ribbon负载均衡
package com.hjl.hystrix.config;
import org.springframework.cloud.client.loadbalancer.LoadBalanced;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.client.RestTemplate;
/**
* TODO:
*
* @Version 1.0
* @Author HJL
* @Date 2021/12/28 13:38
*/
@Configuration
public class RibbonConfig {
@Bean
@LoadBalanced
public RestTemplate restTemplate() {
return new RestTemplate();
}
}
4、编写控制层
package com.hjl.hystrix.controller;
import cn.hutool.core.thread.ThreadUtil;
import com.hjl.hystrix.common.Result;
import com.hjl.hystrix.entity.User;
import com.hjl.hystrix.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.Future;
/**
* TODO:
*
* @Version 1.0
* @Author HJL
* @Date 2021/12/30 20:53
*/
@RestController
@RequestMapping("/user")
public class UserHystrixController {
@Autowired
private UserService userService;
@GetMapping("/testFallback/{id}")
public Result testFallback(@PathVariable Long id) {
return userService.getUser(id);
}
@GetMapping("/testException/{id}")
public Result testException(@PathVariable Long id) {
return userService.getUserException(id);
}
@GetMapping("/testCommand/{id}")
public Result getUserCommand(@PathVariable Long id) {
return userService.getUserCommand(id);
}
@GetMapping("/testCache/{id}")
public Result testCache(@PathVariable Long id) {
userService.getUserCache(id);
userService.getUserCache(id);
userService.getUserCache(id);
return new Result("操作成功", 200);
}
@GetMapping("/testRemoveCache/{id}")
public Result testRemoveCache(@PathVariable Long id) {
userService.getUserCache(id);
userService.removeCache(id);
userService.getUserCache(id);
return new Result("操作成功", 200);
}
@GetMapping("/testCollapser")
public Result testCollapser() throws ExecutionException, InterruptedException {
Future<User> future1 = userService.getUserFuture(1L);
Future<User> future2 = userService.getUserFuture(2L);
future1.get();
future2.get();
ThreadUtil.safeSleep(200);
Future<User> future3 = userService.getUserFuture(3L);
future3.get();
return new Result("操作成功", 200);
}
}
5、编写service层
package com.hjl.hystrix.service;
import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollUtil;
import com.hjl.hystrix.common.Result;
import com.hjl.hystrix.entity.User;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixCollapser;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixProperty;
import com.netflix.hystrix.contrib.javanica.cache.annotation.CacheRemove;
import com.netflix.hystrix.contrib.javanica.cache.annotation.CacheResult;
import com.netflix.hystrix.contrib.javanica.command.AsyncResult;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.client.RestTemplate;
import java.util.List;
import java.util.Map;
import java.util.concurrent.Future;
@Service
public class UserService {
private static final Logger LOGGER = LoggerFactory.getLogger(UserService.class);
@Autowired
private RestTemplate restTemplate;
@Value("${service-url.user-service}")
private String userServiceUrl;
@HystrixCommand(fallbackMethod = "fallbackMethod1")
public Result getUser(@PathVariable Long id) {
return restTemplate.getForObject(userServiceUrl + "/user/{1}", Result.class, id);
}
/**
* 声明的参数需要包含controller的声明参数
*
* @param id
* @return
*/
public Result fallbackMethod1(@PathVariable Long id) {
return new Result("服务调用失败", 500);
}
@HystrixCommand(fallbackMethod = "fallbackMethod1",
commandKey = "getUserCommand",
groupKey = "getUserGroup",
threadPoolKey = "getUserThreadPool")
public Result getUserCommand(Long id) {
return restTemplate.getForObject(userServiceUrl + "/user/{1}", Result.class, id);
}
@HystrixCommand(fallbackMethod = "fallbackMethod2", ignoreExceptions = {NullPointerException.class})
public Result getUserException(Long id) {
if (id == 1) {
throw new IndexOutOfBoundsException();
} else if (id == 2) {
throw new NullPointerException();
}
return restTemplate.getForObject(userServiceUrl + "/user/{1}", Result.class, id);
}
public Result fallbackMethod2(@PathVariable Long id, Throwable e) {
LOGGER.error("id {},throwable class:{}", id, e.getClass());
return new Result("服务调用失败", 500);
}
@CacheResult(cacheKeyMethod = "getCacheKey")
@HystrixCommand(fallbackMethod = "fallbackMethod1", commandKey = "getUserCache")
public Result getUserCache(Long id) {
LOGGER.info("getUserCache id:{}", id);
return restTemplate.getForObject(userServiceUrl + "/user/{1}", Result.class, id);
}
/**
* 为缓存生成key的方法
*
* @return
*/
public String getCacheKey(Long id) {
return String.valueOf(id);
}
@HystrixCommand
@CacheRemove(commandKey = "getUserCache", cacheKeyMethod = "getCacheKey")
public Result removeCache(Long id) {
LOGGER.info("removeCache id:{}", id);
return restTemplate.postForObject(userServiceUrl + "/user/delete/{1}", null, Result.class, id);
}
@HystrixCollapser(batchMethod = "listUsersByIds",collapserProperties = {
@HystrixProperty(name = "timerDelayInMilliseconds",value = "100")
})
public Future<User> getUserFuture(Long id) {
return new AsyncResult<User>() {
@Override
public User invoke() {
Result result = restTemplate.getForObject(userServiceUrl + "/user/{1}", Result.class, id);
Map data = (Map) result.getData();
User user = BeanUtil.mapToBean(data, User.class,true);
LOGGER.info("getUserById username:{}",user.getUsername());
return user;
}
};
}
@HystrixCommand
public List<User> listUsersByIds(List<Long> ids) {
LOGGER.info("listUsersByIds:{}",ids);
Result result = restTemplate.getForObject(userServiceUrl + "/user/listUsersByIds?ids={1}", Result.class, CollUtil.join(ids, ","));
return (List<User>)result.getData();
}
}
6、编写启动类
启动类上添加@EnableHystrix注解,启动Hystrix功能
package com.hjl.hystrix;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.circuitbreaker.EnableCircuitBreaker;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
import org.springframework.cloud.netflix.hystrix.EnableHystrix;
/**
* TODO:
*
* @Version 1.0
* @Author HJL
* @Date 2021/12/30 19:56
*/
@EnableHystrix
@EnableEurekaClient
@SpringBootApplication
public class HystrixServiceApplication {
public static void main(String[] args) {
SpringApplication.run(HystrixServiceApplication.class,args);
}
}
7、启动服务,测试
在eureka-service服务中可以查看到hystrix-service服务,表明注册已经成功。
访问hystrix-service服务接口:http://localhost:1008/user/testFallback/1,
关闭user-service服务,再次访问,
表明已经发生了服务降级。
异常忽略测试:
@HystrixCommand(fallbackMethod = "fallbackMethod2", ignoreExceptions = {NullPointerException.class})
ignoreExceptions属性忽略异常。
@HystrixCommand(fallbackMethod = "fallbackMethod2", ignoreExceptions = {NullPointerException.class})
public Result getUserException(Long id) {
if (id == 1) {
throw new IndexOutOfBoundsException();
} else if (id == 2) {
throw new NullPointerException();
}
return restTemplate.getForObject(userServiceUrl + "/user/{1}", Result.class, id);
}
ID=1抛出IndexOutOfBoundsException异常,ID=2抛出NullPointerException异常。
测试:
降级处理IndexOutOfBoundsException异常。
被忽略处理的NullPointerException异常。
Hystrix的请求缓存测试:
编写过滤器完成每次使用缓存的请求前后对HystrixRequestContext进行初始化和关闭。
package com.hjl.hystrix.filter;
import com.netflix.hystrix.strategy.concurrency.HystrixRequestContext;
import org.springframework.stereotype.Component;
import javax.servlet.*;
import javax.servlet.annotation.WebFilter;
import java.io.IOException;
/**
* TODO:
*
* @Version 1.0
* @Author HJL
* @Date 2022/1/1 14:25
*/
@Component
@WebFilter(urlPatterns = "/*", asyncSupported = true)
public class HystrixRequestContextFilter implements Filter {
@Override
public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
HystrixRequestContext context = HystrixRequestContext.initializeContext();
try {
filterChain.doFilter(servletRequest, servletResponse);
} catch (ServletException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
context.close();
}
}
}
测试代码:
@GetMapping("/testCache/{id}")
public Result testCache(@PathVariable Long id) {
userService.getUserCache(id);
userService.getUserCache(id);
userService.getUserCache(id);
return new Result("操作成功", 200);
}
请求测试:
表明缓存已经成功使用,三次使用仅有一次请求,两次使用缓存。
请求合并测试:
@HystrixCollapser(batchMethod = "listUsersByIds",collapserProperties = {
@HystrixProperty(name = "timerDelayInMilliseconds",value = "100")
})
public Future<User> getUserFuture(Long id) {
return new AsyncResult<User>() {
@Override
public User invoke() {
Result result = restTemplate.getForObject(userServiceUrl + "/user/{1}", Result.class, id);
Map data = (Map) result.getData();
User user = BeanUtil.mapToBean(data, User.class,true);
LOGGER.info("getUserById username:{}",user.getUsername());
return user;
}
};
}
@HystrixCommand
public List<User> listUsersByIds(List<Long> ids) {
LOGGER.info("listUsersByIds:{}",ids);
Result result = restTemplate.getForObject(userServiceUrl + "/user/listUsersByIds?ids={1}", Result.class, CollUtil.join(ids, ","));
return (List<User>)result.getData();
}
总结
在微服务架构中,服务与服务之间通过远程调用的方式进行通信,一旦某个被调用的服务发生了故障,其依赖服务也会发生故障,此时就会发生故障的蔓延,最终导致系统瘫痪。Hystrix实现了断路器模式,当某个服务发生故障时,通过断路器的监控,给调用方返回一个错误响应,而不是长时间的等待,这样就不会使得调用方由于长时间得不到响应而占用线程,从而防止故障的蔓延。Hystrix具备服务降级、服务熔断、线程隔离、请求缓存、请求合并及服务监控等强大功能。
更多推荐