【SpringCloud深入浅出系列】SpringCloud组件之集成Hystrix实现断路器

本文介绍了如何在SpringCloud项目中集成Hystrix以实现断路器功能,防止服务故障导致的级联问题。通过改造FeignClient接口并配置fallback类,实现了服务调用失败时的默认返回。同时,详细讲解了配置Hystrix仪表盘的步骤,包括添加依赖、配置暴露的endpoints以及在启动类中注入ServletRegistrationBean,以便监控断路器的运行状态。
摘要由CSDN通过智能技术生成


一、Hystrix 是什么?

Hystrix 由 Netflix 开源,实现断路器。在微服务架构中,通常有多层服务调用。较低级别的服务的服务故障可能导致用户级联故障。当对特定服务的呼叫达到一定阈值时(Hystrix 中默认为5秒20次),断路器会被打开,向调用方返回一个错误响应,而不是长时间的等待。这样就不会使得线程因调用故障服务被长时间占用不释放,避免了故障在分布式系统中的蔓延。
Hystrix 的主要优点之一是它收集关于每个 HystrixCommand 的一套指标。Hystrix 提供仪表盘以有效的方式显示每个断路器的运行状况。

二、创建项目实现断路器

1.项目说明

Feign 自带断路器,因此我们基于 SpringCloud 组件之集成Feign实现负载均衡 中的工程进行改造。

2.改造接口类 HelloFeignClient

改造接口类 HelloFeignClient,在注解 @FeignClient 上指定 fallback 类 HelloFeignClientHystrix。
改造前:

@FeignClient(name = "service-provider")
public interface HelloFeignClient {

    @RequestMapping(value = "/sayHi", method = RequestMethod.GET)
    String sayHi();
}

改造后:

@FeignClient(name = "service-provider", fallback = HelloFeignClientHystrix.class)
public interface HelloFeignClient {

    @RequestMapping(value = "/sayHi", method = RequestMethod.GET)
    String sayHi();
}

3.新建 HelloFeignClientHystrix 类

package com.chaoyue.consumer.feignClient;

import org.springframework.stereotype.Component;

@Component
public class HelloFeignClientHystrix implements HelloFeignClient {

    @Override
    public String sayHi() {
        return "调用失败";
    }
}

application.yml 中添加配置:

feign:
  hystrix:
    enabled: true

4.启动后断路器不生效的处理方法

启动后断路器不生效,将 SpringBoot 和 SpringCloud 依赖的版本降低后问题解决。
SpringBoot 版本由 2.6.3 降为 2.2.4.RELEASE
SpringCloud 版本由 2021.0.0 降为 Hoxton.SR12
修改后的 pom.xml 文件:

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.2.4.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.chaoyue</groupId>
    <artifactId>consumer</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>consumer</name>
    <description>Demo project for Spring Boot</description>
    <properties>
        <java.version>1.8</java.version>
        <spring-cloud.version>Hoxton.SR12</spring-cloud.version>
    </properties>
    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <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-openfeign</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
    </dependencies>
    <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-dependencies</artifactId>
                <version>${spring-cloud.version}</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
        </dependencies>
    </dependencyManagement>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>

</project>

5.测试

启动服务后,浏览器输入:http://localhost:8082/sayHi
启动 provider 时页面显示:
Hi,我来自端口:8081
关闭 provider 时页面显示:
调用失败

6.Hystrix 仪表盘

Hystrix 仪表盘以有效的方式显示每个断路器的运行状况。

(1).添加依赖

pom.xml 文件中加入依赖 spring-boot-starter-actuator 和 spring-cloud-starter-netflix-hystrix-dashboard,spring-boot-starter-actuator 为服务监控依赖:

    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-actuator</artifactId>
    </dependency>   
     <dependency>
        <groupId>org.springframework.cloud</groupId>
        <artifactId>spring-cloud-starter-netflix-hystrix-dashboard</artifactId>
    </dependency>

(2).添加配置

application.yml 文件中添加配置用来暴露 endpoints,由于 endpoints 中会包含很多敏感信息,除了 health 和 info 两个支持直接访问外,其他的默认不能直接访问,所以进行指定:

management:
  endpoints:
    web:
      exposure:
        include: hystrix.stream

(3).启动类加入注解

在启动类中加入注解 @EnableHystrixDashboard,开启Hystrix Dashboard:

package com.chaoyue.consumer;

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

@SpringBootApplication
@EnableEurekaClient
@EnableFeignClients
@EnableHystrixDashboard
public class ConsumerApplication {

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

}

(4).启动类注入 ServletRegistrationBean

在启动类注入 ServletRegistrationBean,该 Bean 的作用是在 Hystrix Dashboard 中打开 hystrix.stream:

package com.chaoyue.consumer;

import com.netflix.hystrix.contrib.metrics.eventstream.HystrixMetricsStreamServlet;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.web.servlet.ServletRegistrationBean;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
import org.springframework.cloud.netflix.hystrix.dashboard.EnableHystrixDashboard;
import org.springframework.cloud.openfeign.EnableFeignClients;
import org.springframework.context.annotation.Bean;

@SpringBootApplication
@EnableEurekaClient
@EnableFeignClients
@EnableHystrixDashboard
public class ConsumerApplication {

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

    @Bean
    public ServletRegistrationBean getServlet(){
        HystrixMetricsStreamServlet streamServlet = new HystrixMetricsStreamServlet();
        ServletRegistrationBean registrationBean = new ServletRegistrationBean(streamServlet);
        registrationBean.setLoadOnStartup(1);
        registrationBean.addUrlMappings("/hystrix.stream");
        registrationBean.setName("HystrixMetricsStreamServlet");
        return registrationBean;
    }
}

(5).启动服务

启动服务。浏览器输入:http://localhost:8082/hystrix
在这里插入图片描述
监控 Stream 输入:http://localhost:8082/hystrix.stream,点击【Monitor Stream】
在这里插入图片描述

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

奔跑吧邓邓子

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值