SpringCloud:Hystrix服务熔断降级方式

一、Hystrix (豪猪) 简介

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

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

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

Hystrix特性:

  • 降级机制:Fallback 相当于是降级操作. 对于查询操作, 可以实现一个 fallback 方法, 当请求后端服务出现异常的时候, 可以使用 fallback 方法返回的值. fallback 方法的返回值一般是设置的默认值或者来自缓存. 告知后面的请求服务不可用了。

二、实现Hystrix服务降级操作

1)创建gradle模块hystrix-service并添加如下依赖

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

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

   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

3)编写启动类添加@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);
    }

}

4)改造之前Ribbon服务的消费方式,添加@HystrixCommand注解指定 fallback(回退)默认提示方法,该方法与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!!!";
    }

}

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!!!

猜你喜欢

转载自www.cnblogs.com/wessonshin/p/12422887.html