SpringCloud(Greenwich版)Hystrix服务熔断降级方式

推荐以下稳定版本号:

Spring Boot: 2.1.9.RELEASE

Spring Cloud: Greenwich.SR3

一、Hystrix (豪猪) 简介

  在微服务架构中,服务与服务之间通过远程调用的方式进行通信,一旦某个被调用的服务发生了故障,其它服务也有可能跟着一起出错,此时就会发生雪崩效应,最终导致系统瘫痪。Hystrix 实现了断路器功能,当某个服务发生故障时,通过断路器进行监控,给调用方返回一个错误响应,而不是长时间的等待,这样就不会使得调用方由于长时间得不到响应而占用线程,从而防止雪崩效应的发生!!!

  当系统架构一切正常时,通过网络访问的服务看起来是这样的:

 

  在下图系统架构中由于服务 I 的异常(可能是程序运行错误、线程阻塞、负载过重等等),渐渐的导致整个系统崩溃,我们称之为雪崩效应:

Hystrix特性:

  • 服务熔断:当服务失败率超过一定阀值时,Hystrix会自动或手动执行电路跳闸(原因可能是网络故障/超时时间导致),断路器触发的快速失败会进行快速恢复。

  • 服务降级:当服务请求失败、超时、被拒绝或线程池/信号量饱满,执行降级处理逻辑的方法。根据业务场景的不同,一般采用以下两种模式进行降级:第一种(最常用)如果服务失败,则我们通过fallback进行降级,返回静态值。第二种:调用备选方案

  • 监控:Hystrix可以近乎实时地监控运行指标和配置的变化,例如成功、失败、超时、以及被拒绝的请求等。

二、实现Hystrix服务熔断

1)build.gradle项目依赖

创建gradle模块hystrix-service并添加web、eureka客户端与hystrix依赖

dependencies {
   compile group: 'org.springframework.boot', name: 'spring-boot-starter-web'

   compile group: 'org.springframework.cloud', name: 'spring-cloud-starter-netflix-eureka-client'

   compile group: 'org.springframework.cloud', name: 'spring-cloud-starter-netflix-hystrix'
}

2)application.yaml配置文件

server:
  port: 7070
spring:
  application:
    name: hystrix-service
eureka:
  instance:
    hostname: localhost
  client:
    fetch-registry: true
    register-with-eureka: true
    service-url:
      defaultZone: http://localhost:8761/eureka/
service-url:
  provider-service: http://provider-service
View Code

3)启动类HystrixServiceApplication.java

 在启动类添加@EnableCircuitBreaker注解,标记开启断路器功能

package org.wesson.springcloud.hystrix;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.circuitbreaker.EnableCircuitBreaker;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.cloud.client.loadbalancer.LoadBalanced;
import org.springframework.context.annotation.Bean;
import org.springframework.web.client.RestTemplate;

@EnableCircuitBreaker
@EnableDiscoveryClient
@SpringBootApplication
public class HystrixServiceApplication {

    @Bean
    @LoadBalanced
    public RestTemplate restTemplate() {
        return new RestTemplate();
    }

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

}
View Code

4)Controller

  改造之前Ribbon服务的消费方式,一旦调用服务方法失败并抛出错误信息后,就添加@HystrixCommand注解的fallbackMethod属性指定服务降级处理逻辑的方法该方法与info()具有相同的参数与返回值类型,让info()方法具备服务降级能力

package org.wesson.springcloud.hystrix.controller;

import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

@RestController
@RequestMapping("/client")
public class HystrixController {

    @Autowired
    private RestTemplate restTemplate;

    @Value("${service-url.provider-service}")
    private String consumerServiceUrl;

    @GetMapping("/info")
    @HystrixCommand(fallbackMethod = "infoFallback")
    public String info() {
        return restTemplate.getForObject(consumerServiceUrl + "/client/info", String.class);
    }
    
    public String infoFallback() {
        return "Error Warning!!!";
    }

}
View Code

5)测试

Step1:运行 eureka-server 启动类,端口为8761

Step2:运行 provider-service 启动类,端口为8081

Step3:运行 hystrix-service 启动类,端口为7070

Step4:先访问http://localhost:8761/,结果如下图:

Step5:访问http://localhost:7070/client/info,服务正常返回结果如下:

  • hello, service provider port is from:8081

Step6:停止 provider-service 应用程序

Step7:再次访问http://localhost:7070/client/info,获得如下结果:

  • Error Warning!!!

 
posted @ 2020-03-05 21:34  wessonshin  阅读(444)  评论(0编辑  收藏  举报