iis服务器助手广告广告
返回顶部
首页 > 资讯 > 精选 >SpringCloud Hystrix熔断器如何使用
  • 411
分享到

SpringCloud Hystrix熔断器如何使用

2023-07-05 14:07:13 411人浏览 八月长安
摘要

本文小编为大家详细介绍“SpringCloud Hystrix熔断器如何使用”,内容详细,步骤清晰,细节处理妥当,希望这篇“springCloud Hystrix熔断器如何使用”文章能帮助大家解决疑惑,下面跟着小编的思路

本文小编为大家详细介绍“SpringCloud Hystrix熔断器如何使用”,内容详细,步骤清晰,细节处理妥当,希望这篇“springCloud Hystrix熔断器如何使用”文章能帮助大家解决疑惑,下面跟着小编的思路慢慢深入,一起来学习新知识吧。

Hystrix(hi si ju ke si)概述

Hystix 是 Netflix 开源的一个延迟和容错库,用于隔离访问远程服务、第三方库,防止出现级联失败(雪崩)。

雪崩:一个服务失败,导致整条链路的服务都失败的情形。

Hystix 主要功能

  • 隔离

  • 降级

  • 熔断

  • 限流

隔离

Hystrix 降级

Hystix 降级:当服务发生异常或调用超时,返回默认数据

SpringCloud Hystrix熔断器如何使用

Hystrix降级-服务提供方

  • 在服务提供方,引入 hystrix 依赖

  • 定义降级方法

  • 使用 @HystrixCommand 注解配置降级方法

  • 在启动类上开启Hystrix功能:@EnableCircuitBreaker

初始化程序和Fiegn程序一致

需要修改的程序

package com.itheima.provider;import org.springframework.boot.SpringApplication;import org.springframework.boot.autoconfigure.SpringBootApplication;import org.springframework.cloud.client.circuitbreaker.EnableCircuitBreaker;import org.springframework.cloud.netflix.eureka.EnableEurekaClient;@EnableEurekaClient //该注解 在新版本中可以省略@SpringBootApplication@EnableCircuitBreaker ///开启Hystrix功能public class ProviderApp {    public static void main(String[] args) {        SpringApplication.run(ProviderApp.class,args);    }}

测试的程序

package com.itheima.provider.controller;import com.itheima.provider.domain.Goods;import com.itheima.provider.service.GoodsService;import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;import com.netflix.hystrix.contrib.javanica.annotation.HystrixProperty;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.PathVariable;import org.springframework.web.bind.annotation.RequestMapping;import org.springframework.web.bind.annotation.RestController;import java.util.Date;@RestController@RequestMapping("/goods")public class GoodsController {    @Autowired    private GoodsService goodsService;    @Value("${server.port}")    private int port;        @GetMapping("/findOne/{id}")    @HystrixCommand(fallbackMethod = "findOne_fallback",commandProperties = {            //设置Hystrix的超时时间 默认1s            @HystrixProperty(name="execution.isolation.thread.timeoutInMilliseconds",value = "3000")    })///指定降级后的方法    public Goods findOne(@PathVariable("id") int id)  {        ///1 造个异常        //int i =3/0;        try {            Thread.sleep(2000);//sleep interrupted        } catch (InterruptedException e) {            e.printStackTrace();        }        Goods goods = goodsService.findOne(id);        goods.setTitle(goods.getTitle() + ":" + port);//将端口号,设置到了 商品标题上        return goods;    }        public Goods findOne_fallback(int id) {        Goods goods = new Goods();        goods.setTitle("降级了~~~");        return goods;    }}

指定坐标

   <!--hystrix-->        <dependency>            <groupId>org.springframework.cloud</groupId>            <artifactId>spring-cloud-starter-netflix-hystrix</artifactId>        </dependency>

Hystrix降级-服务消费方

  • feign 组件已经集成了 hystrix 组件。

  • 定义feign 调用接口实现类,复写方法,即降级方法

  • 在 @FeignClient 注解中使用 fallback 属性设置降级处理类。

  • 配置开启 feign.hystrix.enabled = true

provider与Fiegin一致

application.yml修改

server:
  port: 9000

eureka:
  instance:
    hostname: localhost # 主机名
  client:
    service-url:
      defaultZone: Http://localhost:8761/eureka
spring:
  application:
    name: hystrix-consumer # 设置当前应用的名称。将来会在eureka中Application显示。将来需要使用该名称来获取路径
#开启feign对hystrix支持
feign:
  hystrix:
    enabled: true

ConsumerApp修改

package com.itheima.consumer;import org.springframework.boot.SpringApplication;import org.springframework.boot.autoconfigure.SpringBootApplication;import org.springframework.cloud.client.discovery.EnableDiscoveryClient;import org.springframework.cloud.netflix.eureka.EnableEurekaClient;import org.springframework.cloud.openfeign.EnableFeignClients;@EnableDiscoveryClient // 激活DiscoveryClient@EnableEurekaClient@SpringBootApplication@EnableFeignClients //开启Feign的功能public class ConsumerApp {    public static void main(String[] args) {        SpringApplication.run(ConsumerApp.class,args);    }}

修改GoodsFeignClient方法

package com.itheima.consumer.feign;import com.itheima.consumer.domain.Goods;import org.springframework.cloud.openfeign.FeignClient;import org.springframework.web.bind.annotation.GetMapping;import org.springframework.web.bind.annotation.PathVariable;@FeignClient(value = "HYSTRIX-PROVIDER",fallback = GoodsFeignClientFallback.class)public interface GoodsFeignClient {    @GetMapping("/goods/findOne/{id}")    public Goods findGoodsById(@PathVariable("id") int id);}

复写方法

package com.itheima.consumer.feign;import com.itheima.consumer.domain.Goods;import org.springframework.stereotype.Component;@Componentpublic class GoodsFeignClientFallback implements  GoodsFeignClient{    @Override    public Goods findGoodsById(int id) {        Goods goods = new Goods();        goods.setTitle("又被降级了~~");        return goods;    }}

Hystrix 熔断

Hystrix 熔断机制,用于监控微服务调用情况,当失败的情况达到预定的阈值(5秒失败20次),会打开断路器,拒绝所有请求,直到服务恢复正常为止。

  • circuitBreaker.sleepWindowInMilliseconds:监控时间

  • circuitBreaker.requestVolumeThreshold:失败次数

  • circuitBreaker.errorThresholdPercentage:失败率

SpringCloud Hystrix熔断器如何使用

//设置Hystrix的超时时间 默认1s@HystrixProperty(name="execution.isolation.thread.timeoutInMilliseconds",value = "3000"),//监控的时间 默认5000ms@HystrixProperty(name="circuitBreaker.sleepWindowInMilliseconds",value = "1000"),//失败次数,默认20次@HystrixProperty(name="circuitBreaker.requestVolumeThreshold",value = "2"),//失败率 百分之50@HystrixProperty(name="circuitBreaker.errorThresholdPercentage",value = "100")

Hystrix 熔断监控

  • Hystrix 提供了 Hystrix-dashboard 功能,用于实时监控微服务运行状态。

  • 但是Hystrix-dashboard只能监控一个微服务。

  • Netflix 还提供了 Turbine ,进行聚合监控。

SpringCloud Hystrix熔断器如何使用

Turbine聚合监控

搭建监控模块

创建监控模块

创建hystrix-monitor模块,使用Turbine聚合监控多个Hystrix dashboard功能,

引入Turbine聚合监控起步依赖

<?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 http://maven.apache.org/xsd/maven-4.0.0.xsd">    <parent>        <artifactId>hystrix-parent</artifactId>        <groupId>com.itheima</groupId>        <version>1.0-SNAPSHOT</version>    </parent>    <modelVersion>4.0.0</modelVersion>    <artifactId>hystrix-monitor</artifactId>    <properties>        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>        <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>        <java.version>1.8</java.version>    </properties>    <dependencies>    <!--单独熔断监控-->        <dependency>            <groupId>org.springframework.cloud</groupId>            <artifactId>spring-cloud-starter-netflix-hystrix-dashboard</artifactId>        </dependency><!--聚合熔断监控-->        <dependency>            <groupId>org.springframework.cloud</groupId>            <artifactId>spring-cloud-starter-netflix-turbine</artifactId>        </dependency>        <dependency>            <groupId>org.springframework.boot</groupId>            <artifactId>spring-boot-starter-actuator</artifactId>        </dependency>        <dependency>            <groupId>org.springframework.cloud</groupId>            <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>        </dependency>        <dependency>            <groupId>org.springframework.boot</groupId>            <artifactId>spring-boot-starter-test</artifactId>            <scope>test</scope>        </dependency>    </dependencies>    <build>        <plugins>            <plugin>                <groupId>org.springframework.boot</groupId>                <artifactId>spring-boot-maven-plugin</artifactId>            </plugin>        </plugins>    </build></project>

修改application.yml

spring:
  application.name: hystrix-monitor
server:
  port: 8769
turbine:
  combine-host-port: true
  # 配置需要监控的服务名称列表
  app-config: hystrix-provider,hystrix-consumer
  cluster-name-expression: "'default'"
  aggregator:
    cluster-config: default
  #instanceUrlSuffix: /actuator/hystrix.stream
eureka:
  client:
    serviceUrl:
      defaultZone: http://localhost:8761/eureka/

创建启动类

@SpringBootApplication@EnableEurekaClient@EnableTurbine //开启Turbine 很聚合监控功能@EnableHystrixDashboard //开启Hystrix仪表盘监控功能public class HystrixMonitorApp {    public static void main(String[] args) {        SpringApplication.run(HystrixMonitorApp.class, args);    }}

修改被监控模块

需要分别修改 hystrix-provider 和 hystrix-consumer 模块:

导入依赖:

<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</artifactId>        </dependency>        <dependency>            <groupId>org.springframework.cloud</groupId>            <artifactId>spring-cloud-starter-netflix-hystrix-dashboard</artifactId>        </dependency>

配置Bean

此处为了方便,将其配置在启动类中。

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

启动类上添加注解@EnableHystrixDashboard

@EnableDiscoveryClient@EnableEurekaClient@SpringBootApplication@EnableFeignClients @EnableHystrixDashboard // 开启Hystrix仪表盘监控功能public class ConsumerApp {    public static void main(String[] args) {        SpringApplication.run(ConsumerApp.class,args);    }    @Bean    public ServletRegistrationBean getServlet() {        HystrixMetricsStreamServlet streamServlet = new HystrixMetricsStreamServlet();        ServletRegistrationBean registrationBean = new ServletRegistrationBean(streamServlet);        registrationBean.setLoadOnStartup(1);        registrationBean.addUrlMappings("/actuator/hystrix.stream");        registrationBean.setName("HystrixMetricsStreamServlet");        return registrationBean;    }}

启动测试

启动服务:

  • eureka-server

  • hystrix-provider

  • hystrix-consumer

  • hystrix-monitor

访问:

在浏览器访问http://localhost:8769/hystrix/ 进入Hystrix Dashboard界面

SpringCloud Hystrix熔断器如何使用

界面中输入监控的Url地址 http://localhost:8769/turbine.stream,监控时间间隔2000毫秒和title,如下图

SpringCloud Hystrix熔断器如何使用

  • 实心圆:它有颜色和大小之分,分别代表实例的监控程度和流量大小。如上图所示,它的健康度从绿色、黄色、橙色、红色递减。通过该实心圆的展示,我们就可以在大量的实例中快速的发现故障实例和高压力实例。

  • 曲线:用来记录 2 分钟内流量的相对变化,我们可以通过它来观察到流量的上升和下降趋势。

SpringCloud Hystrix熔断器如何使用

读到这里,这篇“SprinGCloud Hystrix熔断器如何使用”文章已经介绍完毕,想要掌握这篇文章的知识点还需要大家自己动手实践使用过才能领会,如果想了解更多相关内容的文章,欢迎关注编程网精选频道。

--结束END--

本文标题: SpringCloud Hystrix熔断器如何使用

本文链接: https://www.lsjlt.com/news/352112.html(转载时请注明来源链接)

有问题或投稿请发送至: 邮箱/279061341@qq.com    QQ/279061341

本篇文章演示代码以及资料文档资料下载

下载Word文档到电脑,方便收藏和打印~

下载Word文档
猜你喜欢
  • SpringCloud Hystrix熔断器如何使用
    本文小编为大家详细介绍“SpringCloud Hystrix熔断器如何使用”,内容详细,步骤清晰,细节处理妥当,希望这篇“SpringCloud Hystrix熔断器如何使用”文章能帮助大家解决疑惑,下面跟着小编的思路...
    99+
    2023-07-05
  • SpringCloud微服务熔断器Hystrix如何使用
    这篇文章主要介绍了SpringCloud微服务熔断器Hystrix如何使用的相关知识,内容详细易懂,操作简单快捷,具有一定借鉴价值,相信大家阅读完这篇SpringCloud微服务熔断器Hystrix如何使用文章都会有所收获,下面我们一起来看...
    99+
    2023-07-02
  • springcloud(四):熔断器Hystrix
    说起springcloud熔断让我想起了去年股市中的熔断,多次痛的领悟,随意实施的熔断对整个系统的影响是灾难性的,好了接下来我们还是说正事。愿意了解源码的朋友直接求求交流分享技术:二一四七七七五六三三雪崩效应在微服务架构中通常会有多个服务层...
    99+
    2023-06-05
  • springcloud中如何使用熔断监控Hystrix Dashboard
    这篇文章给大家分享的是有关springcloud中如何使用熔断监控Hystrix Dashboard的内容。小编觉得挺实用的,因此分享给大家做个参考,一起跟随小编过来看看吧。Hystrix-dashboard是一款针对Hystrix进行实时...
    99+
    2023-06-05
  • SpringCloud微服务熔断器Hystrix使用详解
    目录什么是HystrixHystrix实战总结什么是Hystrix 在日常生活用电中,如果我们的电路中正确地安置了保险丝,那么在电压异常升高时,保险丝就会熔断以便切断电流,从而起到保...
    99+
    2024-04-02
  • SpringCloud集成Hystrix熔断过程分步分解
    版本: SpringBoot 2.6.1 SpringCloud 2021.0.0 依赖 <dependency> <groupId>org.springf...
    99+
    2024-04-02
  • SpringCloud怎么实现服务调用feign、熔断hystrix和网关gateway
    本文小编为大家详细介绍“SpringCloud怎么实现服务调用feign、熔断hystrix和网关gateway”,内容详细,步骤清晰,细节处理妥当,希望这篇“SpringCloud怎么实现服务调用feign、熔断hystrix和网关gat...
    99+
    2023-07-05
  • SpringCloud微服务熔断器使用详解
    目录一、简介二、作用三、核心概念3.1 熔断目的3.2 降级目的四、实例4.1 基于Hystrix4.1.1 熔断触发降级4.1.2 超时触发降级4.1.3 资源隔离触发降级4.2 ...
    99+
    2024-04-02
  • Spring Cloud中熔断器Hystrix有什么用
    这篇文章主要为大家展示了“Spring Cloud中熔断器Hystrix有什么用”,内容简而易懂,条理清晰,希望能够帮助大家解决疑惑,下面让小编带领大家一起研究并学习一下“Spring Cloud中熔断器Hystrix有什么用”这篇文章吧。...
    99+
    2023-06-19
  • SpringCloud Hystrix的使用
    目录简介 服务熔断 实践 服务降级 实践 服务熔断与服务降级的区别 服务监控 Dashboard 建立项目 简介 在分布式系统中,服务与服务之间依赖错综复杂,一种不可避免的情况就是...
    99+
    2024-04-02
  • SpringCloud实现服务调用feign与熔断hystrix和网关gateway详细分析
    回归cloud的学习,对于springcloud的架构与原理以及性能的分析我们都在之前的文章里写过: springcloud架构的认识 我们之前测试过eureka服务注册功能,它能很...
    99+
    2023-05-14
    SpringCloud服务调用feign SpringCloud熔断hystrix SpringCloud网关gateway
  • springcloud中如何实现熔断监控Turbine
    小编给大家分享一下springcloud中如何实现熔断监控Turbine,相信大部分人都还不怎么了解,因此分享这篇文章给大家参考一下,希望大家阅读完这篇文章后大有收获,下面让我们一起去了解一下吧!在复杂的分布式系统中,相同服务的节点经常需要...
    99+
    2023-06-05
  • SpringCloud使用Resilience4j实现服务熔断的方法
    目录CircuitBreaker 断路器隔舱Bulkhead限速器RateLimiterCircuitBreaker 断路器 服务熔断是为了保护我们的服务,比如当某个服务出现问题的时...
    99+
    2022-12-29
    Spring Cloud 服务熔断 Spring Cloud Resilience4j 服务熔断
  • 如何自定义feign调用实现hystrix超时、异常熔断
    需求描述 spring cloud 项目中feign 整合 hystrix经常使用,但是最近发现hystrix功能强大,但是对我们来说有些大材小用。 首先我只需要他的一个熔断作用,就...
    99+
    2024-04-02
  • SpringCloud Hystrix怎么使用
    这篇文章主要介绍“SpringCloud Hystrix怎么使用”的相关知识,小编通过实际案例向大家展示操作过程,操作方法简单快捷,实用性强,希望这篇“SpringCloud Hystrix怎么使用”文章能帮助大家解决问...
    99+
    2023-07-02
  • SpringCloud之Hystrix的详细使用
    目录***一.服务降级***2.不使用Hystrix的项目3. 使用Hystrix4. 全局的Hystrix配置 ***二.服务熔断***1.熔断机制概述2.项目中使用1....
    99+
    2024-04-02
  • golang熔断器如何实现
    这篇“golang熔断器如何实现”文章的知识点大部分人都不太理解,所以小编给大家总结了以下内容,内容详细,步骤清晰,具有一定的借鉴价值,希望大家阅读完这篇文章能有所收获,下面我们一起来看看这篇“golang熔断器如何实现”文章吧。熔断器像是...
    99+
    2023-06-26
  • Spring Cloud中如何使用Hystrix实现断路器
    这篇文章主要介绍了Spring Cloud中如何使用Hystrix实现断路器的相关知识,内容详细易懂,操作简单快捷,具有一定借鉴价值,相信大家阅读完这篇Spring Cloud中如何使用Hystrix实现断路器文章都会有所收获,下面我们一起...
    99+
    2023-06-04
  • SpringCloud hystrix断路器与全局解耦怎么实现
    这篇文章主要介绍“SpringCloud hystrix断路器与全局解耦怎么实现”,在日常操作中,相信很多人在SpringCloud hystrix断路器与全局解耦怎么实现问题上存在疑惑,小编查阅了各式资料,整理出简单好...
    99+
    2023-07-04
  • SpringCloud hystrix断路器与全局解耦全面介绍
    第七章中在ProductController 和OrderController 中都使用了局部服务降级,但同时也导致两个问题, 通过观察两个局部降级的案例,可以发现: 每个业务方法都...
    99+
    2022-11-13
    SpringCloud hystrix断路器 SpringCloud hystrix服务降级
软考高级职称资格查询
编程网,编程工程师的家园,是目前国内优秀的开源技术社区之一,形成了由开源软件库、代码分享、资讯、协作翻译、讨论区和博客等几大频道内容,为IT开发者提供了一个发现、使用、并交流开源技术的平台。
  • 官方手机版

  • 微信公众号

  • 商务合作