Spring Cloud Hystrix:服务容错保护

SpringBoot实战电商项目mall(20k+star)地址:github.com/macrozheng/…java

摘要

Spring Cloud Hystrix 是Spring Cloud Netflix 子项目的核心组件之一,具备服务容错及线程隔离等一系列服务保护功能,本文将对其用法进行详细介绍。git

Hystrix 简介

在微服务架构中,服务与服务之间经过远程调用的方式进行通讯,一旦某个被调用的服务发生了故障,其依赖服务也会发生故障,此时就会发生故障的蔓延,最终致使系统瘫痪。Hystrix实现了断路器模式,当某个服务发生故障时,经过断路器的监控,给调用方返回一个错误响应,而不是长时间的等待,这样就不会使得调用方因为长时间得不到响应而占用线程,从而防止故障的蔓延。Hystrix具有服务降级、服务熔断、线程隔离、请求缓存、请求合并及服务监控等强大功能。github

建立一个hystrix-service模块

这里咱们建立一个hystrix-service模块来演示hystrix的经常使用功能。web

在pom.xml中添加相关依赖

<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</artifactId>
</dependency>
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-web</artifactId>
</dependency>
复制代码

在application.yml进行配置

主要是配置了端口、注册中心地址及user-service的调用路径。spring

server:
 port: 8401
spring:
 application:
 name: hystrix-service
eureka:
 client:
 register-with-eureka: true
 fetch-registry: true
 service-url:
 defaultZone: http://localhost:8001/eureka/
service-url:
 user-service: http://user-service
复制代码

在启动类上添加@EnableCircuitBreaker来开启Hystrix的断路器功能

@EnableCircuitBreaker
@EnableDiscoveryClient
@SpringBootApplication
public class HystrixServiceApplication {

    public static void main(String[] args) {
        SpringApplication.run(HystrixServiceApplication.class, args);
    }
复制代码

建立UserHystrixController接口用于调用user-service服务

服务降级演示

  • 在UserHystrixController中添加用于测试服务降级的接口:
@GetMapping("/testFallback/{id}")
public CommonResult testFallback(@PathVariable Long id) {
    return userService.getUser(id);
}
复制代码
  • 在UserService中添加调用方法与服务降级方法,方法上须要添加@HystrixCommand注解:
@HystrixCommand(fallbackMethod = "getDefaultUser")
public CommonResult getUser(Long id) {
    return restTemplate.getForObject(userServiceUrl + "/user/{1}", CommonResult.class, id);
}

public CommonResult getDefaultUser(@PathVariable Long id) {
    User defaultUser = new User(-1L, "defaultUser", "123456");
    return new CommonResult<>(defaultUser);
}
复制代码
  • 启动eureka-server、user-service、hystrix-service服务;

  • 关闭user-service服务从新测试该接口,发现已经发生了服务降级:

@HystrixCommand详解

@HystrixCommand中的经常使用参数

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

设置命令、分组及线程池名称

  • 在UserHystrixController中添加测试接口:
@GetMapping("/testCommand/{id}")
public CommonResult testCommand(@PathVariable Long id) {
    return userService.getUserCommand(id);
}
复制代码
  • 在UserService中添加方式实现功能:
@HystrixCommand(fallbackMethod = "getDefaultUser",
    commandKey = "getUserCommand",
    groupKey = "getUserGroup",
    threadPoolKey = "getUserThreadPool")
public CommonResult getUserCommand(@PathVariable Long id) {
    return restTemplate.getForObject(userServiceUrl + "/user/{1}", CommonResult.class, id);
 }
复制代码

使用ignoreExceptions忽略某些异常降级

  • 在UserHystrixController中添加测试接口:
@GetMapping("/testException/{id}")
public CommonResult testException(@PathVariable Long id) {
    return userService.getUserException(id);
}
复制代码
  • 在UserService中添加实现方法,这里忽略了NullPointerException,当id为1时抛出IndexOutOfBoundsException,id为2时抛出NullPointerException:
@HystrixCommand(fallbackMethod = "getDefaultUser2", ignoreExceptions = {NullPointerException.class})
public CommonResult getUserException(Long id) {
    if (id == 1) {
        throw new IndexOutOfBoundsException();
    } else if (id == 2) {
        throw new NullPointerException();
    }
    return restTemplate.getForObject(userServiceUrl + "/user/{1}", CommonResult.class, id);
}

public CommonResult getDefaultUser2(@PathVariable Long id, Throwable e) {
    LOGGER.error("getDefaultUser2 id:{},throwable class:{}", id, e.getClass());
    User defaultUser = new User(-2L, "defaultUser2", "123456");
    return new CommonResult<>(defaultUser);
}
复制代码

Hystrix的请求缓存

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

相关注解

  • @CacheResult:开启缓存,默认全部参数做为缓存的key,cacheKeyMethod能够经过返回String类型的方法指定key;
  • @CacheKey:指定缓存的key,能够指定参数或指定参数中的属性值为缓存key,cacheKeyMethod还能够经过返回String类型的方法指定;
  • @CacheRemove:移除缓存,须要指定commandKey。

测试使用缓存

  • 在UserHystrixController中添加使用缓存的测试接口,直接调用三次getUserCache方法:
@GetMapping("/testCache/{id}")
public CommonResult testCache(@PathVariable Long id) {
    userService.getUserCache(id);
    userService.getUserCache(id);
    userService.getUserCache(id);
    return new CommonResult("操做成功", 200);
}
复制代码
  • 在UserService中添加具备缓存功能的getUserCache方法:
@CacheResult(cacheKeyMethod = "getCacheKey")
@HystrixCommand(fallbackMethod = "getDefaultUser", commandKey = "getUserCache")
    public CommonResult getUserCache(Long id) {
    LOGGER.info("getUserCache id:{}", id);
    return restTemplate.getForObject(userServiceUrl + "/user/{1}", CommonResult.class, id);
}

/** * 为缓存生成key的方法 */
public String getCacheKey(Long id) {
    return String.valueOf(id);
}
复制代码

测试移除缓存

  • 在UserHystrixController中添加移除缓存的测试接口,调用一次removeCache方法:
@GetMapping("/testRemoveCache/{id}")
public CommonResult testRemoveCache(@PathVariable Long id) {
    userService.getUserCache(id);
    userService.removeCache(id);
    userService.getUserCache(id);
    return new CommonResult("操做成功", 200);
}
复制代码
  • 在UserService中添加具备移除缓存功能的removeCache方法:
@CacheRemove(commandKey = "getUserCache", cacheKeyMethod = "getCacheKey")
@HystrixCommand
public CommonResult removeCache(Long id) {
    LOGGER.info("removeCache id:{}", id);
    return restTemplate.postForObject(userServiceUrl + "/user/delete/{1}", null, CommonResult.class, id);
}
复制代码

缓存使用过程当中的问题

  • 在缓存使用过程当中,咱们须要在每次使用缓存的请求先后对HystrixRequestContext进行初始化和关闭,不然会出现以下异常:
java.lang.IllegalStateException: Request caching is not available. Maybe you need to initialize the HystrixRequestContext?
	at com.netflix.hystrix.HystrixRequestCache.get(HystrixRequestCache.java:104) ~[hystrix-core-1.5.18.jar:1.5.18]
	at com.netflix.hystrix.AbstractCommand$7.call(AbstractCommand.java:478) ~[hystrix-core-1.5.18.jar:1.5.18]
	at com.netflix.hystrix.AbstractCommand$7.call(AbstractCommand.java:454) ~[hystrix-core-1.5.18.jar:1.5.18]
复制代码
  • 这里咱们经过使用过滤器,在每一个请求先后初始化和关闭HystrixRequestContext来解决该问题:
/** * Created by macro on 2019/9/4. */
@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);
        } finally {
            context.close();
        }
    }
}
复制代码

请求合并

微服务系统中的服务间通讯,须要经过远程调用来实现,随着调用次数愈来愈多,占用线程资源也会愈来愈多。Hystrix中提供了@HystrixCollapser用于合并请求,从而达到减小通讯消耗及线程数量的效果。架构

@HystrixCollapser的经常使用属性

  • batchMethod:用于设置请求合并的方法;
  • collapserProperties:请求合并属性,用于控制实例属性,有不少;
  • timerDelayInMilliseconds:collapserProperties中的属性,用于控制每隔多少时间合并一次请求;

功能演示

  • 在UserHystrixController中添加testCollapser方法,这里咱们先进行两次服务调用,再间隔200ms之后进行第三次服务调用:
@GetMapping("/testCollapser")
public CommonResult 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 CommonResult("操做成功", 200);
}
复制代码
  • 使用@HystrixCollapser实现请求合并,全部对getUserFuture的的屡次调用都会转化为对getUserByIds的单次调用:
@HystrixCollapser(batchMethod = "getUserByIds",collapserProperties = {
    @HystrixProperty(name = "timerDelayInMilliseconds", value = "100")
})
public Future<User> getUserFuture(Long id) {
    return new AsyncResult<User>(){
    @Override
    public User invoke() {
        CommonResult commonResult = restTemplate.getForObject(userServiceUrl + "/user/{1}", CommonResult.class, id);
        Map data = (Map) commonResult.getData();
        User user = BeanUtil.mapToBean(data,User.class,true);
        LOGGER.info("getUserById username:{}", user.getUsername());
        return user;
        }
    };
}

@HystrixCommand
public List<User> getUserByIds(List<Long> ids) {
    LOGGER.info("getUserByIds:{}", ids);
    CommonResult commonResult = restTemplate.getForObject(userServiceUrl + "/user/getUserByIds?ids={1}", CommonResult.class, CollUtil.join(ids,","));
    return (List<User>) commonResult.getData();
}
复制代码

Hystrix的经常使用配置

全局配置

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属性。

使用到的模块

springcloud-learning
├── eureka-server -- eureka注册中心
├── user-service -- 提供User对象CRUD接口的服务
└── hystrix-service -- hystrix服务调用测试服务
复制代码

项目源码地址

github.com/macrozheng/…app

公众号

mall项目全套学习教程连载中,关注公众号第一时间获取。async

公众号图片
相关文章
相关标签/搜索