• 【微服务】Hystrix的概念、作用以及使用方法


            

    目录

    概念

    作用

    使用方法

    Hystrix的实现


            Hystrix 是 Netflix 提供的一个用于分布式系统的延迟和容错库。它旨在通过在客户端库中实现断路器模式,从而防止在一个分布式环境中的雪崩效应并提供回退选项,从而增强了分布式系统的弹性和可靠性。

    概念

    • 断路器模式: 断路器模式是一种容错设计模式,用于在系统组件之间进行通信时处理故障。它通过在发生故障时暂时中断系统组件之间的连接,从而防止故障的扩散和传播,提高了系统的稳定性。

    作用

    • 防止雪崩效应: 在分布式系统中,如果一个服务出现故障导致延迟,那么可能会导致对其他服务的连锁反应,最终导致整个系统崩溃。Hystrix 通过在发生故障时快速失败和提供回退选项来防止雪崩效应的发生。
    • 提供容错能力: Hystrix 提供了断路器模式,当被封装的服务的错误率超过一定阈值时,断开服务调用,防止对底层依赖的过度使用,从而提供了容错能力。

    使用方法

    1. 依赖注入: 将 Hystrix 相关的注解和依赖添加到你的项目中。在 Spring Cloud 中,通常使用 spring-cloud-starter-netflix-hystrixspring-cloud-starter-netflix-hystrix-dashboard 依赖。
    2. 使用注解: 在需要进行容错处理的方法上使用 @HystrixCommand 注解,以声明该方法是一个 Hystrix 命令。你可以通过该注解提供一些配置参数,例如超时时间、线程池大小等。
    3. 定义回退方法:@HystrixCommand 注解中指定的方法中定义一个回退方法,当命令执行失败或超时时将会调用该方法。
    4. 监控和可视化: 可以使用 Hystrix Dashboard 或 Turbine 等工具监控 Hystrix 命令的执行情况,并进行可视化展示和分析。

            使用 Hystrix 能够帮助你编写健壮的分布式系统,提高系统的可靠性和弹性。

            下面我们使用Java代码实现一个Hystrix,走你~~~

    Hystrix的实现

    以下是一个简单的使用 Java 和 Spring Boot 实现 Hystrix 的示例:

            首先,确保在 pom.xml 文件中添加了 Hystrix 和 Spring Boot 的依赖:

    1. <dependency>
    2. <groupId>org.springframework.cloudgroupId>
    3. <artifactId>spring-cloud-starter-netflix-hystrixartifactId>
    4. dependency>
    5. <dependency>
    6. <groupId>org.springframework.bootgroupId>
    7. <artifactId>spring-boot-starter-webartifactId>
    8. dependency>

            创建一个简单的服务类 HelloService,该服务类中包含一个可能会失败的方法 getHelloMessage

    1. import org.springframework.stereotype.Service;
    2. import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
    3. @Service
    4. public class MyService {
    5. // 定义一个 Hystrix 命令,并指定回退方法
    6. @HystrixCommand(fallbackMethod = "fallbackMethod")
    7. public String hello() {
    8. // 这里模拟一个可能会出现异常的方法调用
    9. if (Math.random() > 0.5) {
    10. throw new RuntimeException("Random error occurred");
    11. }
    12. return "Hello, World!";
    13. }
    14. // 回退方法,当命令执行失败或超时时调用
    15. public String fallbackMethod() {
    16. return "Fallback: Hello, World!";
    17. }
    18. }

            接下来,在 Spring Boot 应用程序的主类中添加 @EnableCircuitBreaker 注解来启用 Hystrix 功能:

    1. import org.springframework.boot.SpringApplication;
    2. import org.springframework.boot.autoconfigure.SpringBootApplication;
    3. import org.springframework.cloud.client.circuitbreaker.EnableCircuitBreaker;
    4. @SpringBootApplication
    5. @EnableCircuitBreaker
    6. public class Application {
    7. public static void main(String[] args) {
    8. SpringApplication.run(Application.class, args);
    9. }
    10. }

            最后,创建一个 REST 控制器 MyController 来调用 MyService 中的方法:

    1. import org.springframework.beans.factory.annotation.Autowired;
    2. import org.springframework.web.bind.annotation.GetMapping;
    3. import org.springframework.web.bind.annotation.RestController;
    4. @RestController
    5. public class MyController {
    6. @Autowired
    7. private MyService myService;
    8. @GetMapping("/hello")
    9. public String hello() {
    10. // 调用 MyService 中的 hello 方法
    11. return myService.hello();
    12. }
    13. }

            现在,启动你的 Spring Boot 应用程序,访问 http://localhost:8080/hello,你将看到输出的结果可能是 "Hello, World!" 或者 "Fallback: Hello, World!",这取决于调用 hello() 方法时是否发生异常。如果发生异常,Hystrix 会自动调用回退方法。

            非常的实用,喜欢的小伙伴可以动动你们发财的小手,给博主一个小小的点赞或者关注,就是对博主最大的鼓励,爱你们哦~~

  • 相关阅读:
    电路布线问题动态规划详解(做题思路)
    jquery.cookie.min.js 1.4.1版本
    PlantUML绘制类图
    规则引擎基础知识
    【ARM】v8架构programmer guide(3)_ARMv8的寄存器
    3分钟读懂OKR | 不和绩效挂钩的OKR到底有什么用?
    c语言的编译过程
    Qt 下拉复选框(MultiSelectComboBox)(一) 实现下拉框多选,搜索下拉框内容
    oracle创建数据库以及用户,并导入dmp格式数据
    多任务联合训练,出现Nan的问题
  • 原文地址:https://blog.csdn.net/qq_22193961/article/details/137846015