当前位置: 首页 > news >正文

hystrix超详细教学

1、什么是hystrix?

是一个做熔断的框架,当程序被高并发访问时可能会造成微服务的宕机,hystrix可以熔断微服务之间通信。防止后台服务发生雪崩。

2、Hystrix作用

  • 熔断
  • 查看微服务请求状态

3、Hystrix使用场景

是在微服务架构下才有意义,做各个微服务通信熔断的。

  • 并发和负载量如果都不大,可用可不用,一旦负载过大,一定要用熔断。
  • 看业务需求,如果对熔断的需求比较简单,选用Hystrix,如果需求复杂,可用选用sentinel(sentinel功能比较丰富,比如限流,比如可以设置服务器限流,一秒钟只处理多少个请求,超过这个请求熟练的 ,就自动熔断了)。

4、准备工作

【1.启动nacos和redis】

在这里插入图片描述

【2.以openfeign代码为例子】

openfeign-Score工程(没有代码改变)先启动之后断开(模拟宕机),

openfeign-User工程(修改一下工程的注册名字就可以)。

spring.application.name=hystrixdemo

运行效果:

在这里插入图片描述

【开启Score工程:】

在这里插入图片描述

【关闭Score工程】

在这里插入图片描述

5、应用

1、引入依赖

在User工程里添加下面的依赖

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

2、开启Hystrix

在User工程的启动类上添加开启Hystrix的注解

@EnableHystrix

在这里插入图片描述

此时完成上述两个操作后,再次关闭Score工程,页面也是报错,但不是“连接超时”的错误,而是“熔断类型”的错误。为了让用户体验度好一些,报错信息不暴露给用户,我们完成下面的编码。

3、添加熔断

import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
import lombok.Setter;
import org.jsoft.demo.dto.UserDto;
import org.jsoft.demo.service.IUserService;
import org.jsoft.demo.utils.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @author szsw
 * @date 2024/4/12 8:06:07
 */
@RestController
@RequestMapping("/user")
@Setter
public class UserController {

    @Autowired
    private IUserService userService;

    @GetMapping
    @HystrixCommand(fallbackMethod = "infoHystrix") //一旦熔断了,就去执行infoHystrix方法。
    public Result info() {
        User user = userService.info();
        return Result.ok().put("data", user);
    }

    public Result infoHystrix() {
        return Result.error().setMessage("被熔断了");
    }
}

如果发生了熔断,那么执行注解中fallbackMethod定义的方法。

配置文件

feign:
  hystrix:
    enabled: true

开启feign的hystrix做熔断,默认是开启的,需要知道有这个配置,在开发中可能会用到。

【连接成功:】

在这里插入图片描述

【连接失败】

在这里插入图片描述

6、添加仪表盘【了解,功能单一使用少】

添加依赖

在user工程里添加如下依赖:

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

添加配置类

在user工程里添加如下配置类:直接粘贴就可以,是固定写法。

import com.netflix.hystrix.contrib.metrics.eventstream.HystrixMetricsStreamServlet;
import org.springframework.boot.web.servlet.ServletRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import javax.servlet.http.HttpServlet;

@Configuration
public class HystrixConfig {

    @Bean
    public ServletRegistrationBean<HttpServlet> httpServletServletRegistrationBean() {
        ServletRegistrationBean<HttpServlet> result = new ServletRegistrationBean<>(new HystrixMetricsStreamServlet());
        result.addUrlMappings("/actuator/hystrix.stream");
        return result;
    }

}

启动类添加注解@EnableHystrixDashboard

在这里插入图片描述

启动项目,访问如下地址

地址1:http://localhost:100/actuator/hystrix.stream

地址2:http://localhost:100/hystrix

第一个地址是用来在仪表盘中配置的,第二个地址是访问仪表盘的。

在访问仪表盘之前先访问一下接口,Hystrix是懒加载,不访问接口第一个请求地址会一直输出ping

在这里插入图片描述

在这里插入图片描述

相关文章:

  • Python常见面试题的详解11
  • AI 量化炒股:噱头还是未来?从技术发展与投资实践深度分析
  • 最长回文子串(蓝桥云课)
  • T5 大模型
  • 进制和编码
  • 前端(AJAX)学习笔记(CLASS 2):图书管理案例以及图片上传
  • vcf2phylip v2.8-生信工具46
  • c++基础知识(六)
  • langchain应用-RAG
  • 【Unity Shader编程】之图元装配与光栅化
  • springcloud的组件及作用
  • 2012年下半年软件设计师上午题知识点及其详细解释(附真题及答案解析)
  • 【linux】更换ollama的deepseek模型默认安装路径
  • Vue 3 生命周期和生命周期函数
  • sql server查询IO消耗大的排查sql诊断语句
  • 机器学习入门实战 4 - 基本模型
  • 【SQL】SQL多表查询
  • FastAdmin后端列表导入表格数据
  • 瑞芯微RV1126部署YOLOv8全流程:环境搭建、pt-onnx-rknn模型转换、C++推理代码、错误解决、优化、交叉编译第三方库
  • 确保设备始终处于最佳运行状态,延长设备的使用寿命,保障系统的稳定运行的智慧地产开源了
  • 有人悬赏十万寻找“全国仅剩1只”的斑鳖,发帖者回应并证实
  • 中标多家学校采购项目的App查成绩需付费?涉事公司回应
  • 《黎明的一切》:与正常世界脱轨后,我选择不再回去
  • 这个部位最容易变老,却被很多姑娘忽视了
  • 媒体评特朗普对进口电影征100%关税:让好莱坞时代加速谢幕
  • 新华社:让历史照鉴未来