当前位置: 首页 > 编程笔记 >

详解SpringCloud微服务架构之Hystrix断路器

黄涵畅
2023-03-14
本文向大家介绍详解SpringCloud微服务架构之Hystrix断路器,包括了详解SpringCloud微服务架构之Hystrix断路器的使用技巧和注意事项,需要的朋友参考一下

一:什么是Hystrix

在分布式环境中,许多服务依赖项中的一些将不可避免地失败。Hystrix是一个库,通过添加延迟容差和容错逻辑来帮助您控制这些分布式服务之间的交互。Hystrix通过隔离服务之间的访问点,停止其间的级联故障以及提供回退选项,从而提高系统的整体弹性。

Hystrix旨在执行以下操作

1:对通过第三方客户端库访问(通常通过网络)的依赖关系提供保护并控制延迟和故障。

2:隔离复杂分布式系统中的级联故障。

3:快速发现故障,尽快恢复。

4:回退,尽可能优雅地降级。

5:启用近实时监控,警报和操作控制。

二:为什么需要Hystrix?

大型分布式系统中,一个客户端或者服务依赖外部服务,如果一个服务宕了,那么由于我们设置了服务调用系统超时时间,势必会影响相应时间,在高并发的情况下大多数服务器的线程池就出现阻塞(BLOCK),影响整个线上服务的稳定性。

(图片官方图片)

当一切都健康时,请求可以看起来像这样

当许多后端服务系统中的一个宕掉时,整个用户请求:

如果多个客户端调用同一个异常服务的时候,出现的情况是:

 

三:Hystrix解决什么问题?

分布式架构中的应用程序具有几十个依赖关系,每个依赖关系在某个时刻将不可避免的出现异常。如果应用程序不与这些外部故障隔离,则可能出现线程池阻塞,引起系统雪崩。

例如,对于依赖30个服务的应用程序,每个服务的正常运行时间为99.99%,您可以:

99.99%的30次方 = 99.7%正常运行时间

0.3%的10亿次请求= 3,000,000次故障

2+小时宕机/月,即使所有依赖关系正常运行时间。

当使用Hystrix进行熔断后,每个依赖关系彼此隔离了,限制了当发生延迟时的阻塞。

 

四:Hystrix结合Feign使用

创建一个工程eureka_feign_hystrix_client

pom.xml文件内容

<dependencies>

    <dependency>

      <groupId>org.springframework.cloud</groupId>

      <artifactId>spring-cloud-starter-feign</artifactId>

    </dependency>

    <dependency>

      <groupId>org.springframework.cloud</groupId>

      <artifactId>spring-cloud-starter-eureka</artifactId>

    </dependency>

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

    <dependency>

      <groupId>org.springframework.boot</groupId>

      <artifactId>spring-boot-starter-test</artifactId>

      <scope>test</scope>

    </dependency>

  </dependencies>

  <dependencyManagement>

    <dependencies>

      <dependency>

        <groupId>org.springframework.cloud</groupId>

        <artifactId>spring-cloud-dependencies</artifactId>

        <version>Brixton.SR5</version>

        <type>pom</type>

        <scope>import</scope>

      </dependency>

    </dependencies>

  </dependencyManagement> 

创建启动文件

FeignHystrixApplication

@SpringBootApplication
@EnableDiscoveryClient
@EnableFeignClients
public class FeignHystrixApplication {
  public static void main(String[] args) {

    SpringApplication.run(FeignHystrixApplication.class, args);
  }
} 

UserClient类

@FeignClient(value = "biz-service-0",fallback = UserClientHystrix.class)
public interface UserClient {
  @RequestMapping(method = RequestMethod.GET, value = "/getuser")
  public User getuserinfo();
  @RequestMapping(method = RequestMethod.GET, value = "/getuser")
  public String getuserinfostr();
  @RequestMapping(method = RequestMethod.GET, value = "/info")
  public String info();
} 


创建熔断类UserClientHystrix

@Service
public class UserClientHystrix implements UserClient {

   @Override
  public User getuserinfo() {
    throw new NullPointerException(" User getuserinfo() 服务不可用。。");

  }

  @Override
  public String getuserinfostr() {
    return " UserClientHystrix getuserinfostr() is fallback 服务不可用。。";

  }

  @Override
  public String info() {
    return " UserClientHystrix info() is fallback 服务不可用。。";

  }
} 

当网络出现异常的时候或直接跳转到这里实现类里面

创建action类

UserController

@Autowired
  UserClient userClient;

  @RequestMapping(value = "/getuserinfo", method = RequestMethod.GET)
  public User getuserinfo() {
    return userClient.getuserinfo();
  }

  @RequestMapping(value = "/getuserinfostr", method = RequestMethod.GET)
  public String getuserinfostr() {
    return userClient.getuserinfostr();
  }

  @RequestMapping(value = "/info", method = RequestMethod.GET)
  public String info() {
    return userClient.info();
  } 

先启动:eureka_register_service(注册中心)工程

然后运行我们写好的FeignHystrixApplication

这个时候我们明显发现没有运行biz-service-0 服务,那么我们 打开 http://127.0.0.1:8005/getuserinfostr

出现

UserClientHystrix getuserinfostr() is fallback 服务不可用。。

这个就是我们自定义的熔断返回结果

如果不用熔断 页面会出现这个

Whitelabel Error Page

This application has no explicit mapping for /error, so you are seeing this as a fallback.

Wed Mar 22 14:32:21 CST 2017

There was an unexpected error (type=Internal Server Error, status=500).

getuserinfo failed and fallback failed. 

代码地址:https://github.com/zhp8341/SpringCloudDemo

本人也看了一些Hystrix相关原理,由于没有全部看完所以暂时没有写上去,本文是结合Feign使用和学习。

有兴起的可以看下官方的Hystrix很详细,就是看起来有点费劲,

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持小牛知识库。

 类似资料:
  • 本文向大家介绍详解Java 微服务架构,包括了详解Java 微服务架构的使用技巧和注意事项,需要的朋友参考一下 一、传统的整体式架构 传统的整体式架构都是模块化的设计逻辑,如展示(Views)、应用程序逻辑(Controller)、业务逻辑(Service)和数据访问对象(Dao),程序在编写完成后被打包部署为一个具体的应用。如图所示: 系统的水平扩展 如果要对系统进行水平扩展,通常情况下,只需要

  • Kubernetes 设计之初就是按照 Cloud Native 的理念设计的,Cloud Native 中有个重要概念就是微服务的架构设计,当将单体应用拆分微服务后, 随着服务数量的增多,如何微服务进行管理以保证服务的 SLA 呢?为了从架构层面上解决这个问题,解放程序员的创造性,避免繁琐的服务发现、监控、分布式追踪等事务,Service mesh 应运而生。 微服务 下图是Bilgin Ibr

  • 让我们讨论一下微服务环境的体系结构。我们正在公司内部进行讨论,我想得到一些反馈。我认真考虑的是编排层(代码复制、更多移动部件改变api)。 网络应用- 原料药- 在这种情况下,服务不允许相互对话。业务流程层中的聚合服务 网络应用- 原料药- 这里允许服务相互对话,这里存在聚合服务。 账单属于哪里

  • 有一种显而易见的方法可以将业务异常包装到holder对象中,从run()方法返回它,然后将其解包装回异常并重新抛出。但它想知道是否有更干净的方法。

  • 应用程序可以使用Spring Cloud Netflix项目提供的Hystrix断路器将这个启动器包含在项目pom.xml:spring-cloud-starter-hystrix中。Hystrix不依赖于Netflix Discovery Client。@EnableHystrix注释应放置在配置类(通常是主类)上。那么方法可以用@HystrixCommand注释来被断路器保护。有关详细信息,请

  • 本文向大家介绍什么是微服务架构?相关面试题,主要包含被问及什么是微服务架构?时的应答技巧和注意事项,需要的朋友参考一下 在前面你理解什么是微服务,那么对于微服务架构基本上就已经理解了。 微服务架构 就是 对微服务进行管理整合应用的。微服务架构 依赖于 微服务,是在微服务基础之上的。 例如:上面已经列举了什么是微服务。在医院里,每一个科室都是一个独立的微服务,那么 这个医院 就是 一个大型的微服务架