在很多系统架构中都需要考虑横向扩、单点故障等问题,对于一个庞大的应用集群,部分服务或者机器出现问题不可避免,在出现故障时,如何减少故障的影响、保障集群的高可用,成为一个重要的工作,Hystrix 是一个帮助解决分布式系统交互时超时处理和容错的类库,它同样拥有保护系统的能力。Hystrix 主要实现以下功能:

  • 当所依赖的网络服务发生延迟或者失败,对访问的客户端程序进行保护,在短时间内访问失败会导致执行回退逻辑。
  • 在分布式系统中,停止级联故障。
  • 网络服务恢复正常后,可以快速恢复客户端的访问能力。
  • 调用失败时,执行服务回退

Hystrix 使用示例

  • 创建项目

    创建Maven项目,命名为 hystrix-client,并增加 hystrix 依赖和 Http 提交相关依赖,POM.xml 内容如下:

    <?xmlversion="1.0"encoding="UTF-8"?>

    <projectxmlns="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.0http://maven.apache.org/xsd/maven-4.0.0.xsd">

    <modelVersion>4.0.0</modelVersion>

    <groupId>org.lixue</groupId>

    <artifactId>hystrix-client</artifactId>

    <version>1.0-SNAPSHOT</version>

    <dependencies>

    <!--Hystrix依赖-->

    <dependency>

    <groupId>com.netflix.hystrix</groupId>

    <artifactId>hystrix-core</artifactId>

    <version>1.5.12</version>

    </dependency>

    <!--logback日志依赖-->

    <dependency>

    <groupId>ch.qos.logback</groupId>

    <artifactId>logback-classic</artifactId>

    <version>1.2.3</version>

    </dependency>

    <!--http客户端依赖-->

    <dependency>

    <groupId>org.apache.httpcomponents</groupId>

    <artifactId>httpclient</artifactId>

    <version>4.5.3</version>

    </dependency>

    </dependencies>

    </project>

  • 创建 Hystrix 命令类

    命令类需要继承 HystrixCommand 类,并实现其 run 方法执行具体业务,实现 getFallback 方法执行回退业务,在调用 run 方法超时或者断路器处于打开状态时,会调用 getFallback 方法进行回退。

    package org.lixue.hystrixclient;

    import com.netflix.hystrix.HystrixCommand;

    import com.netflix.hystrix.HystrixCommandGroupKey;

    import com.netflix.hystrix.HystrixCommandProperties;

    import org.apache.http.HttpResponse;

    import org.apache.http.client.methods.HttpGet;

    import org.apache.http.impl.client.CloseableHttpClient;

    import org.apache.http.impl.client.HttpClients;

    import org.apache.http.util.EntityUtils;

    public class SpeakSleepCommand extends HystrixCommand<String>{

    private int sleep;

    private CloseableHttpClient httpClient;

    private String url;

    public SpeakSleepCommand(intsleep){

    super(Setter.withGroupKey(HystrixCommandGroupKey.Factory.asKey("Speak"))

    .andCommandPropertiesDefaults(HystrixCommandProperties.Setter()));

    this.sleep=sleep;

    this.httpClient=HttpClients.createDefault();

    this.url="http://localhost:8080/speak/sleep?seconds="+this.sleep;

    }

    protected String run() throws Exception{

    try{

    HttpGet request=new HttpGet(this.url);

    HttpResponse response=httpClient.execute(request);

    return EntityUtils.toString(response.getEntity());

    }catch(Exceptionex){

    ex.printStackTrace();

    return ex.getMessage();

    }

    }

    @Override

    protected String getFallback(){

    return"call fallback";

    }

    }

  • 创建启动类

    实例化我们创建的 SpeakSleepCommand 类,并调用 execute 来执行(调用 run 方法不会使用 Hystrix)

    package org.lixue.hystrixclient;

    public class HystrixClient{

    public static void main(String[]args){

    SpeakSleepCommand cmd=new SpeakSleepCommand(10);

    try{

    Stringresult=cmd.execute();

    System.out.println("请求结果="+result);

    }catch(Exceptionex){

    ex.printStackTrace();

    }

    }

    }

  • 测试验证

    默认情况下,Hystrix 是 1000 毫秒超时,我们在实例化传入的是10秒,因此在调用的时候会执行 getFallback 方法;如果修改在实例化传入 0 秒,不进行阻塞,会正常返回结果值。

Hystrix 使用入门的更多相关文章

  1. Netflix Hystrix - 快速入门

    Hystrix最初是由Netflix的API team研发的,用于提高API的弹性和性能,2012年在公司内部广受好评. 如果你的应用是一个单独的应用,那几乎不用在意断路的问题. 但在分布式环境中,各 ...

  2. Hystrix快速入门

    祝大家国庆快乐! 对大部分电商和快递公司来说,每年年底(Q4季度)由于双11等大促活动的存在,将面对大量的用户流量,尤其是属于大促的那几天,无论是用户的商品订单还是物流订单,都将是平时的3倍以上.对于 ...

  3. Hystrix【入门】

    公共依赖配置: <parent> <groupId>org.springframework.boot</groupId> <artifactId>spr ...

  4. hystrix动态修改参数

    Hystrix 从入门到深入——运行时修改动态配置 /** * * @author zhangshuo * */ @Component public class DynamicConfigSource ...

  5. Spring Cloud微服务笔记(五)Feign

    Feign 一.Feign概述 Feign是一个声明式的Web Service客户端.在Spring Cloud 中使用Feign,可以做到 使用HTTP请求访问远程服务,就像调用本地方法一样,同时它 ...

  6. Spring-cloud(六) Hystrix入门

    前提 一个可用的Eureka注册中心(文中以之前博客中双节点注册中心,不重要) 一个连接到这个注册中心的服务提供者 快速入门 项目搭建 搭建一个新maven项目,artifactid为Ribbon-c ...

  7. spring cloud 入门系列四:使用Hystrix 实现断路器进行服务容错保护

    在微服务中,我们将系统拆分为很多个服务单元,各单元之间通过服务注册和订阅消费的方式进行相互依赖.但是如果有一些服务出现问题了会怎么样? 比如说有三个服务(ABC),A调用B,B调用C.由于网络延迟或C ...

  8. 架构师入门:Spring Cloud系列,Hystrix与Eureka的整合

    和Ribbon等组件一样,在项目中,Hystrix一般不会单独出现,而是会和Eureka等组件配套出现.在Hystrix和Eureka整合后的框架里,一般会用到Hystrix的断路器以及合并请求等特性 ...

  9. Spring Cloud 入门教程(八): 断路器指标数据监控Hystrix Dashboard 和 Turbine

    1. Hystrix Dashboard (断路器:hystrix 仪表盘)  Hystrix一个很重要的功能是,可以通过HystrixCommand收集相关数据指标. Hystrix Dashboa ...

随机推荐

  1. java学习笔记17(Calendarl类)

    Calendar类:(日历) 用法:Calendar是一个抽象类:不能实例化(不能new),使用时通过子类完成实现,不过这个类不需要创建子类对象,而是通过静态方法直接获取: 获取对象方法:getIns ...

  2. Spring MVC之ResposeEntity下载文件

    Spring Mvc中用ResponseEntity方式下载文件如下: @RequestMapping("/download") public ResponseEntity< ...

  3. 【Python】多进程1

    1.    进程定义: (1) 进程是一个实体.每个进程都有他自己的地址空间,一般包括文本区域.数据区域和堆栈.进程是线程的容器. (2) 进程是一个“执行中的程序” 2.    进程的特征: (1) ...

  4. pyx文件 生成pyd 文件用于 cython调用

    转于:https://www.2cto.com/kf/201405/304168.html 1. 初衷 最近学用python,python不愧是为程序员考虑的编程语言,写起来很快很方便,大大节省开发效 ...

  5. builtroot 添加git 下载方式

    1.buildroot/Config.in 配置default git server eg:config xxxx_GIT_SITE string "git site" defau ...

  6. POJ 2456: Aggressive cows(二分,贪心)

    Aggressive cows Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 20485   Accepted: 9719 ...

  7. Python基础练习及答案

    1.请用代码实现:利用下划线将列表的每一个元素拼接成字符串,li=['alex', 'eric', 'rain'] 该题目主要是考的字符串的拼接,join方法, s = "" li ...

  8. C++ 作业(哈夫曼树)

    #include<bits/stdc++.h> #define fi first #define se second #define int long long using namespa ...

  9. ★ MYSQL隔离级别 通俗理解 + mysql、oracle默认事务隔离级别

    ★  脏读 : 读取了前一事务 未提交 的数据 ; 不可重复读    : 读取了前一事务     提交 的数据: ★ 幻读 与 不可重复读 common :都是读取了另一条已经提交的事务(这点与脏读不 ...

  10. 【UOJ#22】【UR#1】外星人

    2044年,Picks建成了人类第一台基于量子理论的银河系信息传递机. Picks游遍了宇宙,雇用了 n 个外星人来帮他作为信息传递机的中转站.我们将外星人依次编号为 1 到 n,其中 i 号外星人有 ...