4 Hystrix 服务容错处理

1、前言

  在微服务架构中,根据业务来拆分成一个个的服务,服务与服务之间可以通过 RPC 相互调用,在 Spring Cloud 中可以用 RestTemplate + Ribbon 和 Feign 来调用。为了保证其高可用,单个服务通常会集群部署。由于网络原因或者自身的原因,服务并不能保证 100% 可用,如果单个服务出现问题,调用这个服务就会出现线程阻塞,此时若有大量的请求涌入,Servlet 容器的线程资源会被消耗完毕,导致服务瘫痪。服务与服务之间的依赖性,故障会传播,会对整个微服务系统造成灾难性的严重后果,这种情况称之为服务雪崩效应
  如何避免服务雪崩效应?通过 Hystrix 就能够解决,下面学习如何用 Hystrix 实现服务容错处理。

2 Hystrix

2.1 Spring Cloud 整合 Hystrix
  • 在 spring-cloud-web-admin-feign 项目中添加如下依赖:
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-hystrix</artifactId>
        </dependency>
  • 在启动类中添加 @EnableHystrix 或者 @EnableCircuitBreaker。注意,@EnableHystrix 中包含了 @EnableCircuitBreaker。
package com.pky.spring.cloud.web.admin.feign;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
import org.springframework.cloud.netflix.hystrix.EnableHystrix;
import org.springframework.cloud.openfeign.EnableFeignClients;

@SpringBootApplication
@EnableEurekaClient
@EnableFeignClients(basePackages = "com.pky.spring.cloud")
@EnableHystrix
public class WebAdminFeignApplication {
    public static void main(String[] args) {
        SpringApplication.run(WebAdminFeignApplication.class);
    }
}
  • Feign 是自带熔断器的,但默认是关闭的。需要在配置文件中配置打开它,在配置文件增加以下代码:
# 服务熔断
feign:
  hystrix:
    enabled: true
  • 创建熔断器类并实现对应的 Feign 接口
package com.pky.spring.cloud.web.admin.feign.service.hystrix;

import com.pky.spring.cloud.web.admin.feign.service.AdminService;
import org.springframework.stereotype.Component;

@Component
public class AdminServiceHystrix implements AdminService {
    @Override
    public String testFeign() {
        return "网络错误,请稍后再试!";
    }

    @Override
    public String testFeignPost() {
        return "网络错误,请稍后再试!";
    }
}
  • 在 Service 中增加 fallback 指定类
package com.pky.spring.cloud.web.admin.feign.service;

import com.pky.spring.cloud.web.admin.feign.config.FeignConfiguration;
import com.pky.spring.cloud.web.admin.feign.service.hystrix.AdminServiceHystrix;
import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;

@FeignClient(value = "admin-provider", path = "/admin", configuration = FeignConfiguration.class, fallback = AdminServiceHystrix.class)
public interface AdminService {

    @GetMapping(value = "")
    public String testFeign();

    @PostMapping(value = "user")
    public String testFeignPost();
}

  重新启动 spring-cloud-web-admin-feign 项目,并关闭 spring-cloud-service-admin-provider 服务提供者项目,在浏览器中请求 http://localhost:9701/feign ,发现返回了 “网络错误,请稍后再试!”,然后再次启动 spring-cloud-service-admin-provider 服务提供者项目,重新进行请求,这次返回的是 “hello, I’m service admin from port :9601”。这证明回退生效了。在这种情况下,如果你的接口调用了多个服务的接口,spring-cloud-service-admin-provider 服务没有数据,不会影响别的服务,如果不用 Hystrix 回退处理,整个请求都将失败。

3 整合 Dashboard 查看监控数据

  在微服务架构中,Hystrix 除了实现容错外,还有提供了实时监控功能,使用 Dashboard 查看监控数据

  • 在 pom.xml 中添加如下依赖:
        <!-- dashboard 查看监控数据 -->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-hystrix-dashboard</artifactId>
        </dependency>
  • WebAdminFeignApplication 启动类添加 @EnableHystrixDashboard 注解。
  • 创建 hystrix.stream 的 Servlet 配置
      Spring Boot 2.x 版本开启 Hystrix Dashboard 与 Spring Boot 1.x 的方式略有不同,需要增加一个 HystrixMetricsStreamServlet 的配置,代码如下:
package com.pky.spring.cloud.web.admin.feign.config;

import com.netflix.hystrix.contrib.metrics.eventstream.HystrixMetricsStreamServlet;
import org.springframework.boot.web.servlet.ServletRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
public class HystrixDashboardConfiguration {

    @Bean
    public ServletRegistrationBean getServlet() {
        HystrixMetricsStreamServlet streamServlet = new HystrixMetricsStreamServlet();
        // 因为没有web.xml,springboot-web-servlet 提供的创建 servlet 的方式。
        ServletRegistrationBean registrationBean = new ServletRegistrationBean(streamServlet);
        // 注册 servlet
        registrationBean.setLoadOnStartup(1);   // servlet 启动顺序
        registrationBean.addUrlMappings("/hystrix.stream");  // servlet 的 mapping
        registrationBean.setName("HystrixMetricsStreamServlet");  // servlet 的 name
        return registrationBean;
    }
}
  • 测试 Hystrix Dashboard
      重新启动项目,浏览器端访问 http://localhost:9701/hystrix ,界面如下:
    在这里插入图片描述
      可以看到主页中有 3 个地方需要我们填写,第一行是监控的 stream 地址,也就是我们配置的hystrix.stream 的 servlet 的地址:http://localhost:9701/hystrix.stream。
      第二行的 Delay 是时间,表示用多少毫秒同步一次监控信息,Title 是标题,这个可以随便填写。
      输入完成后就可以点击 Monitor Stream 按钮以图形化的方式查看监控数据了,如下图所示:
    在这里插入图片描述
      然后我们不断刷新 http://localhost:9701/feign 请求,可以看到监控到成功的信息,如下图:
    在这里插入图片描述
      关闭 admin-provider 服务提供者,再次请求 http://localhost:9701/feign,则会监控到失败的信息,如下图:
    在这里插入图片描述

4 附:Hystrix 说明

4.1 什么情况下会触发 fallback 方法
名字触发fallback
EMIT值传递NO
SUCCESS执行完成,没有错误NO
FAILURE执行抛出异常YES
TIMEOUT执行开始,但没有在允许的时间内完成YES
BAD_REQUEST执行抛出HystrixBadRequestExceptionNO
SHORT_CIRCUITED断路器打开,不尝试执行YES
THREAD_POOL_REJECTED线程池拒绝,不尝试执行YES
SEMAPHORE_REJECTED信号量拒绝,不尝试执行YES
4.2 fallback 方法在什么情况下会抛出异常
名字描述抛异常
FALLBACK_EMITFallback值传递NO
FALLBACK_SUCCESSFallback执行完成,没有错误NO
FALLBACK_FAILUREFallback执行抛出出错YES
FALLBACK_REJECTEDFallback信号量拒绝,不尝试执行YES
FALLBACK_MISSING没有Fallback实例YES
4.3 Hystrix Dashboard 界面监控参数

在这里插入图片描述

4.4 Hystrix 常用配置信息

1)超时时间(默认1000ms,单位:ms)

  • hystrix.command.default.execution.isolation.thread.timeoutInMilliseconds:在调用方配置,被该调用方的所有方法的超时时间都是该值,优先级低于下边的指定配置
  • hystrix.command.HystrixCommandKey.execution.isolation.thread.timeoutInMilliseconds:在调用方配置,被该调用方的指定方法(HystrixCommandKey 方法名)的超时时间是该值

2)线程池核心线程数

  • hystrix.threadpool.default.coreSize:默认为 10

3)Queue

  • hystrix.threadpool.default.maxQueueSize:最大排队长度。默认 -1,使用 SynchronousQueue。其他值则使用 LinkedBlockingQueue。如果要从 -1 换成其他值则需重启,即该值不能动态调整,若要动态调整,需要使用到下边这个配置
  • hystrix.threadpool.default.queueSizeRejectionThreshold:排队线程数量阈值,默认为 5,达到时拒绝,如果配置了该选项,队列的大小是该队列

注意: 如果 maxQueueSize=-1 的话,则该选项不起作用

4)断路器

  • hystrix.command.default.circuitBreaker.requestVolumeThreshold:当在配置时间窗口内达到此数量的失败后,进行短路。默认 20 个(10s 内请求失败数量达到 20 个,断路器开)
  • hystrix.command.default.circuitBreaker.sleepWindowInMilliseconds:短路多久以后开始尝试是否恢复,默认 5s
  • hystrix.command.default.circuitBreaker.errorThresholdPercentage:出错百分比阈值,当达到此阈值后,开始短路。默认 50%

5)fallback

  • hystrix.command.default.fallback.isolation.semaphore.maxConcurrentRequests:调用线程允许请求 HystrixCommand.GetFallback() 的最大数量,默认 10。超出时将会有异常抛出,注意:该项配置对于 THREAD 隔离模式也起作用

6)属性配置参数

  • 参数说明:https://github.com/Netflix/Hystrix/wiki/Configuration
  • HystrixProperty 参考代码:http://www.programcreek.com/java-api-examples/index.php?source_dir=Hystrix-master/hystrix-contrib/hystrix-javanica/src/test/java/com/netflix/hystrix/contrib/javanica/test/common/configuration/command/BasicCommandPropertiesTest.java
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值