SpringCloud Hystrix 断路器介绍以及配置使用

2024-03-18 14:59

本文主要是介绍SpringCloud Hystrix 断路器介绍以及配置使用,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

一、Hystrix 断路器介绍

Hystrix的断路器为服务级别的断路器,每个配置有HystrixCommand注解的接口都有自己独立的断路器,断路器非应用级别。每个服务的断路器控制着对应服务对外提供服务的状态。即正常对外提供调用,或打开断路器状态即跳闸状态不能进行正常调用,或半开状态允许一个服务调用用来进行服务可用性测试。

二、Hystrix断路器状态

断路器有三种状态 关闭(CLOSE)、开启(OPEN)、半开状态(HALF_OPEN)。断路器默认关闭状态,即非工作中状态,当触发熔断后状态变更为开启(OPEN)状态。在等待到指定时间后Hystrix会放测试请求 测试服务是否开启,这期间断路器会变为半开状态(HALF_OPEN),熔断测试服务调用可用则变更断路器状态为关闭状态(CLOSE),若不可用则状态变更为开启状态(OPEN),再继续等到指定时间后放测试请求。。。

  • Closed:关闭状态(断路器关闭),所有请求都正常访问。代理类维护了最近失败的调用次数,如果某次调用失败,则使失败次数加一,如果最近失败次数超过了在给定时间内允许失败的阈值,则代理类切换到断开(OPEN)状态。此时代理类开启了一个超时时钟,当超时时钟超过了该时间,则切换到半开(HALF_OPEN)状态。该超时时间的设定是给系统一个时机来减轻系统压力或给系统一个修正错误的机会。
  • OPEN:打开状态(断路器开启),所有的请求都会被降级。 在关闭状态的短路器上,默认值:10s 内,有20次请求,有超过50%的错误调用则会触发断路器进入打开状态。默认5秒后进入半开状态。默认值参考类:com.netflix.hystrix.HystrixCommandProperties
  • HALF_OPEN:半开状态。半开状态不是永久的,打开后会进入休眠时间(默认5S),随后断路器进入半开状态。此时会释放一个请求通过,若这个请求是健康的,则会关闭断路器,否则又进入开启状态,然后进行休眠一段时间,如此重复。。。。

注意:在单次的调用失败或者超时时,也会走服务得降级方法。但并不一定会打开断路器。所以反过来说,当发生了服务降级,并不一定触发打开了断路器。

三、使用注解方式配置短路器

@HystrixCommand(fallbackMethod = "getPaymentFallBack",commandProperties = {@HystrixProperty(name="circuitBreaker.enabled",value = "true"),//开启断路器@HystrixProperty(name="circuitBreaker.requestVolumeThreshold",value = "10"),//打开断路器的单位时间内最小请求数@HystrixProperty(name="circuitBreaker.sleepWindowInMilliseconds",value = "10000"), //开启断路器的单位时间@HystrixProperty(name="circuitBreaker.errorThresholdPercentage",value = "60") //开启断路器的最小失败率60%
})
@GetMapping("/consumer/get2/{id}")
public CommonResult<Payment> getPayment2(@PathVariable("id") Long id){

解释:在HystrixCommand注解的 fallbackMethod属性中指定了失败的降级方法。

commandProperties 属性中 

 @HystrixProperty(name="circuitBreaker.enabled",value = "true") 表示是否开启断路器功能;

@HystrixProperty(name="circuitBreaker.requestVolumeThreshold",value = "10") 表示指定触发断路器打开的单位时间内的最小请求数;

@HystrixProperty(name="circuitBreaker.sleepWindowInMilliseconds",value = "10000") 表示打开断路器多久后会进入半开状态,进行再次进行一次测试该服务请求。单位毫秒。

@HystrixProperty(name="circuitBreaker.errorThresholdPercentage",value = "60") 表示在上面10秒内的最小请求数为10次的情况下达到最小失败率60%时,会打开断路器。

四、测试验证短路器工作状态

测试步骤:

1,启动Eureka服务,启动监控服务,启动生产者服务工程,启动消费者工程服务。

2,访问一次请求接口http://127.0.0.1/consumer/product/get/6203063

3,打开仪表盘监控页面进行查看当前监控界面

4,根据服务设置的打开断路器的场景:10秒内进行6次以上的错误请求,然后查看该服务的断路器状态;

连续快速访问 http://127.0.0.1/consumer/product/get2/999 会进行触发报错。

通过上图我们可以看出,当出现6次失败时,仪表盘中断路器状态变更为OPEN,在OPEN的状态下,我们访问正常的参数6203063时,也不能进行了降级处理,但是隔了大约5秒我们访问正常参数时进行了正常返回后,断路器的状态变更为关闭状态。符合之前配置的效果。

五、项目完整目录结构文件如下

1.pom.xml  其中cloud-api-common 依赖为实体类公共jar包 代码参考《公共代码子模块创建》

<?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>SpringCloud</artifactId><groupId>com.xiaohui.springCloud</groupId><version>1.0-SNAPSHOT</version></parent><modelVersion>4.0.0</modelVersion><artifactId>hystrix_order_service_rest</artifactId><dependencies><dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-starter-netflix-hystrix</artifactId></dependency><dependency><groupId>com.xiaohui.springCloud</groupId><artifactId>cloud-api-common</artifactId><version>${project.version}</version></dependency><!-- Eureka 客户端 --><dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-starter-netflix-eureka-client</artifactId></dependency><dependency><groupId>org.springframework.retry</groupId><artifactId>spring-retry</artifactId></dependency><!-- web依赖开始 --><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-web</artifactId></dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-actuator</artifactId></dependency><!-- web依赖结束 --><dependency><groupId>org.projectlombok</groupId><artifactId>lombok</artifactId><optional>true</optional></dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-test</artifactId><scope>test</scope></dependency></dependencies>
</project>

2,application.yml 改配置文件中的配置为全局服务配置,优先级别低于Hystrix服务级别上的注解配置

server:port: 80 #服务端口
spring:application:name: cloud-order-rest-service #服务名称
eureka:client:register-with-eureka: truefetch-registry: trueservice-url:defaultZone: http://eureka1.com:9000/eureka/instance:prefer-ip-address: true #使用ip进行注册instance-id: ${spring.cloud.client.ip-address}:${server.port} #向注册中心注册服务IDlease-renewal-interval-in-seconds: 5 #发送心跳间隔时间 秒lease-expiration-duration-in-seconds: 10 # 服务续约时间 10秒内没有发送心跳(宕机)hystrix:command:default:execution:isolation:thread:timeoutInMilliseconds: 3000circuitBreaker:requestVolumeThreshold: 5 #触发熔断的最小请求次数每10秒,默认20 /10ssleepWindowInMilliseconds: 10000 #熔断多少秒后去重新尝试请求,默认 5s。打开状态的持续时间errorThresholdPercentage: 50 #触发熔断的失败请求数最小占比,默认50%#actuator配置暴露的端点 * 表示全部。 还有 info、health、beans等
management:endpoints:web:exposure:include: '*'

3,启动类 com.xiaohui.hystrix.RestOrderApplication

package com.xiaohui.hystrix;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;@SpringBootApplication
@EnableEurekaClient
@EnableCircuitBreaker
public class RestOrderApplication {public static void main(String[] args) {SpringApplication.run(RestOrderApplication.class,args);}
}

4,配置类 com.xiaohui.hystrix.config.ApplicationContextConfig,主要为在spring容器中注入 RestTemplate  对象,并进行配置负载均衡注解。

package com.xiaohui.hystrix.config;import org.springframework.cloud.client.loadbalancer.LoadBalanced;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.client.RestTemplate;/**** 相当于 spring的 application.xml bean注解相当于 bean 标签*/
@Configuration
public class ApplicationContextConfig {@LoadBalanced@Beanpublic RestTemplate getRestTemplate(){return  new RestTemplate();}
}

5,业务类 Controller,  com.xiaohui.hystrix.controller.OrderAntoController

package com.xiaohui.hystrix.controller;import com.netflix.hystrix.contrib.javanica.annotation.DefaultProperties;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixProperty;
import com.xiaohui.springcloud.entities.CommonResult;
import com.xiaohui.springcloud.entities.Payment;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;import java.util.concurrent.TimeUnit;@RestController
@DefaultProperties(defaultFallback = "defaultFallBack")
public class OrderAntoController {@Autowiredprivate RestTemplate restTemplate;/*** 使用注解配置熔断保护* @param id* @return*/@HystrixCommand(fallbackMethod = "getPaymentFallBack",commandProperties = {@HystrixProperty(name="execution.isolation.thread.timeoutInMilliseconds",value = "5000")})@GetMapping("/consumer/get/{id}")public CommonResult<Payment> getPayment(@PathVariable("id") Long id){if(id != 6203063L){throw new RuntimeException("查询商品服务挂了.....");}try{TimeUnit.SECONDS.sleep(4);}catch (Exception e){}CommonResult<Payment> commonResult = restTemplate.getForObject("http://cloud-payment-service/payment/get/"+id,CommonResult.class);return  commonResult;}/*** 降级方法,需要和降级的方法入参和返回值一致* @param id* @return*/public CommonResult<Payment> getPaymentFallBack( Long id){System.out.println("触发了降级逻辑");return new CommonResult<>(400,"触发了FallBack降级逻辑。。。",null);}@HystrixCommand(fallbackMethod = "getPaymentFallBack",commandProperties = {@HystrixProperty(name="circuitBreaker.enabled",value = "true"),//开启断路器@HystrixProperty(name="circuitBreaker.requestVolumeThreshold",value = "10"),//打开断路器的单位时间内最小请求数@HystrixProperty(name="circuitBreaker.sleepWindowInMilliseconds",value = "10000"), //开启断路器的单位时间@HystrixProperty(name="circuitBreaker.errorThresholdPercentage",value = "60") //开启断路器的最小失败率60%})@GetMapping("/consumer/get2/{id}")public CommonResult<Payment> getPayment2(@PathVariable("id") Long id){if(id != 6203063L){throw new RuntimeException("查询商品服务挂了.....");}CommonResult<Payment> commonResult = restTemplate.getForObject("http://cloud-payment-service/payment/get/"+id,CommonResult.class);return  commonResult;}/*** 统一降级方法不需要入参* @return*/public CommonResult<Payment> defaultFallBack(){System.out.println("触发了统一降级逻辑");return new CommonResult<>(400,"触发了defaultFallBack降级逻辑。。。",null);}
}

 

 

这篇关于SpringCloud Hystrix 断路器介绍以及配置使用的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/822720

相关文章

Spring Security常见问题及解决方案

《SpringSecurity常见问题及解决方案》SpringSecurity是Spring生态的安全框架,提供认证、授权及攻击防护,支持JWT、OAuth2集成,适用于保护Spring应用,需配置... 目录Spring Security 简介Spring Security 核心概念1. ​Securit

postgresql使用UUID函数的方法

《postgresql使用UUID函数的方法》本文给大家介绍postgresql使用UUID函数的方法,本文给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友参考下吧... 目录PostgreSQL有两种生成uuid的方法。可以先通过sql查看是否已安装扩展函数,和可以安装的扩展函数

SpringBoot+EasyPOI轻松实现Excel和Word导出PDF

《SpringBoot+EasyPOI轻松实现Excel和Word导出PDF》在企业级开发中,将Excel和Word文档导出为PDF是常见需求,本文将结合​​EasyPOI和​​Aspose系列工具实... 目录一、环境准备与依赖配置1.1 方案选型1.2 依赖配置(商业库方案)二、Excel 导出 PDF

SpringBoot改造MCP服务器的详细说明(StreamableHTTP 类型)

《SpringBoot改造MCP服务器的详细说明(StreamableHTTP类型)》本文介绍了SpringBoot如何实现MCPStreamableHTTP服务器,并且使用CherryStudio... 目录SpringBoot改造MCP服务器(StreamableHTTP)1 项目说明2 使用说明2.1

spring中的@MapperScan注解属性解析

《spring中的@MapperScan注解属性解析》@MapperScan是Spring集成MyBatis时自动扫描Mapper接口的注解,简化配置并支持多数据源,通过属性控制扫描路径和过滤条件,利... 目录一、核心功能与作用二、注解属性解析三、底层实现原理四、使用场景与最佳实践五、注意事项与常见问题六

Spring的RedisTemplate的json反序列泛型丢失问题解决

《Spring的RedisTemplate的json反序列泛型丢失问题解决》本文主要介绍了SpringRedisTemplate中使用JSON序列化时泛型信息丢失的问题及其提出三种解决方案,可以根据性... 目录背景解决方案方案一方案二方案三总结背景在使用RedisTemplate操作redis时我们针对

Java中Arrays类和Collections类常用方法示例详解

《Java中Arrays类和Collections类常用方法示例详解》本文总结了Java中Arrays和Collections类的常用方法,涵盖数组填充、排序、搜索、复制、列表转换等操作,帮助开发者高... 目录Arrays.fill()相关用法Arrays.toString()Arrays.sort()A

Spring Boot Maven 插件如何构建可执行 JAR 的核心配置

《SpringBootMaven插件如何构建可执行JAR的核心配置》SpringBoot核心Maven插件,用于生成可执行JAR/WAR,内置服务器简化部署,支持热部署、多环境配置及依赖管理... 目录前言一、插件的核心功能与目标1.1 插件的定位1.2 插件的 Goals(目标)1.3 插件定位1.4 核

如何使用Lombok进行spring 注入

《如何使用Lombok进行spring注入》本文介绍如何用Lombok简化Spring注入,推荐优先使用setter注入,通过注解自动生成getter/setter及构造器,减少冗余代码,提升开发效... Lombok为了开发环境简化代码,好处不用多说。spring 注入方式为2种,构造器注入和setter

MySQL中比较运算符的具体使用

《MySQL中比较运算符的具体使用》本文介绍了SQL中常用的符号类型和非符号类型运算符,符号类型运算符包括等于(=)、安全等于(=)、不等于(/!=)、大小比较(,=,,=)等,感兴趣的可以了解一下... 目录符号类型运算符1. 等于运算符=2. 安全等于运算符<=>3. 不等于运算符<>或!=4. 小于运