springcloud+Hystrix断路器

2024-08-28 02:38

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

springcloud+Hystrix断路器

1.Hystrix简介及相关概念

1.1简介

Hystrix是一个用于处理分布式系统的延迟和容错的开源库,在分布式系统里,许多依赖不可避免的会调用失败,比如超时、异常等;
Hystrix能够保证在一个依赖出问题的情况下,不会导致整体服务失败,避免级联故障,以提高分布式系统的弹性。

1.2相关概念

1.服务雪崩:

​ 一个服务,依赖于另一个功能服务的,如果这个功能服务挂掉了,那么依赖的服务就不能再用了,这种级联的失败, 我们可以称之为雪崩。

2.服务降级:

​ 当服务提供者因为某些原因响应过慢,服务提供者主动停掉一些不太重要的业务,释放服务器资源,提高响应速度。

​ 当服务提供者因为某些原因不可用,服务消费者调用本地降级逻辑,迅速返回给客户,避免卡顿。

3.服务熔断

​ 请求错误率达到某一阈值,熔断器全开,产生熔断(熔断期间会对所有请求采用降级处理);

​ 到熔断时间窗口之后,熔断器会进入半开状态,此时会放过试验性请求 ;

​ 如果该试验性请求成功,熔断器进入关闭状态 ;

​ 如果该试验性请求失败,熔断器重新进入全开状态 。

2.Ribbon中使用Hystrix

2.1前置工作:

1.需要搭建Eureka server,我们以一台Eureka Server为例:

https://blog.csdn.net/u013071014/article/details/111031306

2.Ribbon负载均衡

https://blog.csdn.net/u013071014/article/details/111361176

2.2改造服务消费者项目

1.在pom.xml中新增依赖

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

2.在主启动类上增加@EnableHystrix注解,开启Hystrix断路器功能

package com.springcloud;import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.loadbalancer.LoadBalanced;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
import org.springframework.cloud.netflix.hystrix.EnableHystrix;
import org.springframework.cloud.netflix.ribbon.RibbonClient;
import org.springframework.context.annotation.Bean;
import org.springframework.web.client.RestTemplate;import com.springcloud.myRule.myRule;@SpringBootApplication
@EnableEurekaClient
@RibbonClient(name = "springcloud-consumer1", configuration = myRule.class)
@EnableHystrix
public class SpringCloudConsumerApplication {public static void main(String[] args) {// TODO Auto-generated method stubSpringApplication.run(SpringCloudConsumerApplication.class, args);}@Bean@LoadBalanced	//开启负载均衡public RestTemplate restTemplate() {return new RestTemplate();}}

3.在controller中需要有熔断机制的方法上添加@HystrixCommand注解,属性fallbackMethod是发生熔断时返回的方法。

package com.springcloud.controller;import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;@RestController
@RequestMapping(path = "/consumer")
public class ConsumerController {@Autowiredprivate RestTemplate restTemp;@HystrixCommand(fallbackMethod = "getDefaultResp") //发生熔断时,调用getDefaultResp方法@RequestMapping(path = "/getConsumer", method = RequestMethod.GET)public String getConsumer() {String result = restTemp.getForObject("http://springcloud-provider1/provider/getProvider", String.class);return result;}public String getDefaultResp() {return "发生熔断!!!";}
}

4.启动eureka server、provider以及consumer。

正常访问时:
在这里插入图片描述

此时关闭服务提供者,再次访问consumer的API
在这里插入图片描述

3.HystrixDashboard服务监控

在Hystrix中提供Hystrix Dashboard来进行微服务监控工作

3.1项目搭建步骤

1.在parent项目右键,New—> Maven Module,名称命名为springcloud-hystrix-dashboard

2.在pom.xml文件中引入依赖

<packaging>jar</packaging><dependencies><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>
</dependencies>

3.在需要被监控的服务的pom.xml文件中引入监控服务依赖库

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

4.在springcloud-hystrix-dashboard配置application.yml文件

server:port: 9999hystrix:dashboard:proxy-stream-allow-list: "*"# Actuator默认只启动了 health 和 info 端点,如下配置打开全部端点
management:endpoints:web:exposure:include: "*"

5.在springcloud-hystrix-dashboard添加主启动类,并使用@EnableHystrixDashboard注解

package com.springcloud;import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.hystrix.dashboard.EnableHystrixDashboard;@SpringBootApplication
@EnableHystrixDashboard
public class HystrixDashboardApplication {public static void main(String[] args) {SpringApplication.run(HystrixDashboardApplication.class, args);}
}

6.启动springcloud-hystrix-dashboard,访问http://localhost:9999/hystrix测试
在这里插入图片描述

7.在需要被监控的服务的主启动类中新增如下方法

@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;
}

8.启动eureka server、provider、consumer和dashboard,输入需要监控的url
在这里插入图片描述

3.2 dashboard数据解读

在这里插入图片描述

4.源码地址

https://github.com/DamonLiu666/springcloud_test

5.Feign + Hystrix使用

见下一篇博客:
https://blog.csdn.net/u013071014/article/details/111627988

这篇关于springcloud+Hystrix断路器的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



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

相关文章

Spring Boot 实现 IP 限流的原理、实践与利弊解析

《SpringBoot实现IP限流的原理、实践与利弊解析》在SpringBoot中实现IP限流是一种简单而有效的方式来保障系统的稳定性和可用性,本文给大家介绍SpringBoot实现IP限... 目录一、引言二、IP 限流原理2.1 令牌桶算法2.2 漏桶算法三、使用场景3.1 防止恶意攻击3.2 控制资源

Mac系统下卸载JAVA和JDK的步骤

《Mac系统下卸载JAVA和JDK的步骤》JDK是Java语言的软件开发工具包,它提供了开发和运行Java应用程序所需的工具、库和资源,:本文主要介绍Mac系统下卸载JAVA和JDK的相关资料,需... 目录1. 卸载系统自带的 Java 版本检查当前 Java 版本通过命令卸载系统 Java2. 卸载自定

springboot下载接口限速功能实现

《springboot下载接口限速功能实现》通过Redis统计并发数动态调整每个用户带宽,核心逻辑为每秒读取并发送限定数据量,防止单用户占用过多资源,确保整体下载均衡且高效,本文给大家介绍spring... 目录 一、整体目标 二、涉及的主要类/方法✅ 三、核心流程图解(简化) 四、关键代码详解1️⃣ 设置

Java Spring ApplicationEvent 代码示例解析

《JavaSpringApplicationEvent代码示例解析》本文解析了Spring事件机制,涵盖核心概念(发布-订阅/观察者模式)、代码实现(事件定义、发布、监听)及高级应用(异步处理、... 目录一、Spring 事件机制核心概念1. 事件驱动架构模型2. 核心组件二、代码示例解析1. 事件定义

SpringMVC高效获取JavaBean对象指南

《SpringMVC高效获取JavaBean对象指南》SpringMVC通过数据绑定自动将请求参数映射到JavaBean,支持表单、URL及JSON数据,需用@ModelAttribute、@Requ... 目录Spring MVC 获取 JavaBean 对象指南核心机制:数据绑定实现步骤1. 定义 Ja

javax.net.ssl.SSLHandshakeException:异常原因及解决方案

《javax.net.ssl.SSLHandshakeException:异常原因及解决方案》javax.net.ssl.SSLHandshakeException是一个SSL握手异常,通常在建立SS... 目录报错原因在程序中绕过服务器的安全验证注意点最后多说一句报错原因一般出现这种问题是因为目标服务器

Java实现删除文件中的指定内容

《Java实现删除文件中的指定内容》在日常开发中,经常需要对文本文件进行批量处理,其中,删除文件中指定内容是最常见的需求之一,下面我们就来看看如何使用java实现删除文件中的指定内容吧... 目录1. 项目背景详细介绍2. 项目需求详细介绍2.1 功能需求2.2 非功能需求3. 相关技术详细介绍3.1 Ja

springboot项目中整合高德地图的实践

《springboot项目中整合高德地图的实践》:本文主要介绍springboot项目中整合高德地图的实践,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录一:高德开放平台的使用二:创建数据库(我是用的是mysql)三:Springboot所需的依赖(根据你的需求再

spring中的ImportSelector接口示例详解

《spring中的ImportSelector接口示例详解》Spring的ImportSelector接口用于动态选择配置类,实现条件化和模块化配置,关键方法selectImports根据注解信息返回... 目录一、核心作用二、关键方法三、扩展功能四、使用示例五、工作原理六、应用场景七、自定义实现Impor

SpringBoot3应用中集成和使用Spring Retry的实践记录

《SpringBoot3应用中集成和使用SpringRetry的实践记录》SpringRetry为SpringBoot3提供重试机制,支持注解和编程式两种方式,可配置重试策略与监听器,适用于临时性故... 目录1. 简介2. 环境准备3. 使用方式3.1 注解方式 基础使用自定义重试策略失败恢复机制注意事项