服务容错保护Spring Cloud Hystrix之快速入门

目录

Spring Cloud Hystrix介绍

Spring Cloud Hystrix实现了断路器、线程隔离等一系列服务保护功能。它是基于Netflix的开源框架Hystrix实现,该框架的目标在于通过控制那些访问远程系统,服务和第三方库的节点,从而对延迟和故障提供更强大的容错能力。Hystrix具备服务降级,服务熔断、线程和信号隔离、请求缓存、请求合并以及服务监控等功能。
场景描述:如果我们在微服务中进行通信时,发现其中微服务宕机了,这时候另外一个服务调用了宕机的服务,如果在没有断路器机制的前提下访问,这时候会一直等待,一直等待到服务达到超时的点,会使得线程因调用故障服务被长时间占用不能释放,通过断路器的故障监控,如果发现故障时,会向调用者返回一个错误相应,这样就不用长时间等待。

Spring Cloud Hystrix整合

  1. 添加依赖项。
    1
    2
    3
    4
    <dependency>
    <groupId>org.springframework.cloud</>
    <artifactId>spring-cloud-starter-hystrix</artifactId >
    </denpendecy>

同时需要添加spring-boot-starter-actuator,这是Spring Boot对监控的依赖项,只有添加了这些东西后监控相关的endpoint才会被注册。

1
2
3
4
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
  1. 添加注解@EnableCircuitBreaker,对Hystrix进行开启功能。

    1
    2
    3
    4
    5
    6
    7
    8
    @SpringBootApplication
    @EnableEurekaClient #开启Eureka客户端注册
    @EnableCircuitBreaker #开启Hystrix功能
    public class SpringCloudUserApplication {
    public static void main(String[] args) {
    SpringApplication.run(SpringCloudUserApplication.class, args);
    }
    }
  2. 改造服务消费方式,通过添加@HystrixCommod注解对服务接口的熔断技术。

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    @RestController
    public class TestController {
    private static Logger logger = LoggerFactory.getLogger(TestController.class); @Autowired
    private RestTemplate restTemplate;
    /**
    * 调用Service来获取内容。
    *
    * @return 返回hello world。
    */
    @GetMapping(value = "getHelloWorld")
    @HystrixCommand(fallbackMethod = "fallback")
    public String getHelloWorld() {
    return restTemplate.getForObject("http://springcloudservice/getHelloWorld", String.class);
    }
    /**
    * getHelloWorld的熔断机制。
    *
    * @return 熔断信息。
    */
    public String fallback() {
    return "error";
    }
    }

代码分析:

  1. 通过依赖注入RestTemplate,该Bean主要是对Rest Api进行访问,可以裂解调用服务接口的封装。
  2. 定义了getHelloWorld去访问另外一个服务springcloudservice的getHelloWorld方法
  3. 对该接口上添加了@HystrixCommod注解,通过制定fallbackMethod对出现问题接口进行错误转向
  4. 定义错误转向方法:fallback()
  5. 当springcloudservice服务宕机后,可以快速返回“error“字样。
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    @HystrixCommod
    public @interface HystrixCommand {
    // HystrixCommand 命令所属的组的名称:默认注解方法类的名称
    String groupKey() default "";
    // HystrixCommand 命令的key值,默认值为注解方法的名称
    String commandKey() default "";
    // 线程池名称,默认定义为groupKey
    String threadPoolKey() default "";
    // 定义回退方法的名称, 此方法必须和hystrix的执行方法在相同类中
    String fallbackMethod() default "";
    // 配置hystrix命令的参数
    HystrixProperty[] commandProperties() default {};
    // 配置hystrix依赖的线程池的参数
    HystrixProperty[] threadPoolProperties() default {};
    // 如果hystrix方法抛出的异常包括RUNTIME_EXCEPTION,则会被封装HystrixRuntimeException异常。我们也可以通过此方法定义哪些需要忽略的异常
    Class<? extends Throwable>[] ignoreExceptions() default {};
    // 定义执行hystrix observable的命令的模式,类型详细见ObservableExecutionMode
    ObservableExecutionMode observableExecutionMode() default ObservableExecutionMode.EAGER;
    // 如果hystrix方法抛出的异常包括RUNTIME_EXCEPTION,则会被封装HystrixRuntimeException异常。此方法定义需要抛出的异常
    HystrixException[] raiseHystrixExceptions() default {};
    // 定义回调方法:但是defaultFallback不能传入参数,返回参数和hystrix的命令兼容
    String defaultFallback() default "";
    }

常用配置

1
2
3
4
5
6
7
8
# Hystrix超时时间
hystrix:
command:
default:
execution:
isolation:
thread:
timeoutInMilliseconds: 5000
文章目录
  1. 1. 目录
  2. 2. Spring Cloud Hystrix介绍
  3. 3. Spring Cloud Hystrix整合
    1. 3.1. 常用配置
|
载入天数...载入时分秒...