⒈Hystrix是什么?

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

⒉断路器&服务熔断

  “断路器”本身使一种开关装置,当某个服务单元发生故障之后,通过断路器的故障监控(类似于熔断保险丝),向服务调用方返回一个符合预期的、可处理的备选响应(FallBack),而不是长时间的等待或者抛出调用方无法处理的异常,这样就保证了服务调用方的线程不会被长时间、不必要的占用,从而避免了故障在分布式系统中的蔓延,乃至雪崩。

  熔断机制是应对雪崩效应的一种微服务链路保护机制。当扇出链路的某个微服务不可用或者响应时间太长时,会进行服务的降级,进而熔断该节点微服务的调用,快速返回“错误”的响应信息,当检测到该节点的微服务调用响应正常后恢复调用链路。在SpringCloud框架里,熔断机制通过Hystrix实现,Hystrix会监控微服务间调用的状况,当失败的调用达到一定的阈值(默认是5秒内20次调用失败),就会启动熔断机制。

⒊示例

  ①在服务提供者项目中添加Hystrix starter依赖 

         <dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-hystrix</artifactId>
</dependency>

  ②在服务提供者项目中对控制器中的Action方法指定熔断调用方法

 package cn.coreqi.controller;

 import cn.coreqi.entities.User;
import cn.coreqi.service.UserService;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
import java.util.List; @RestController
public class UserController {
@Autowired
private UserService userService; @GetMapping("/users")
@HystrixCommand(fallbackMethod = "getUsersFallback") //一旦服务消费者远程调用该方法失败并抛出错误信息后,Hystrix会自动调用@HystrixCommand注解fallbackMethod属性中标注的方法返回
public List<User> getUsers(){
throw new NullPointerException();
//return userService.getList();
} public List<User> getUsersFallback(){
return null;
}
}

  ③在主程序启动类上添加@EnableCircuitBreaker注解

 package cn.coreqi;

 import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.circuitbreaker.EnableCircuitBreaker;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient; @SpringBootApplication
@EnableEurekaClient //启用Eureka客户端功能
@EnableCircuitBreaker //对Hystrix熔断机制的支持
public class SpringbootcloudserviceproviderApplication { public static void main(String[] args) {
SpringApplication.run(SpringbootcloudserviceproviderApplication.class, args);
} }

⒋服务降级

  服务器整体资源快不够了,将某些服务先关掉,待渡过难关再开启回来,服务降级处理是在服务消费者实现完成的,与服务提供者没有关系

⒌示例

  ①在服务消费者配置文件中开启  

 feign.hystrix.enabled=true

  ②编写回退方法

 package cn.coreqi.fallbackfactory;

 import cn.coreqi.entities.User;
import cn.coreqi.service.UserService;
import feign.hystrix.FallbackFactory;
import org.springframework.stereotype.Component; import java.util.ArrayList;
import java.util.List; @Component
public class UserServiceFallbackFactory implements FallbackFactory<UserService> {
@Override
public UserService create(Throwable throwable) {
return new UserService() {
@Override
public void addUser(User user) { } @Override
public void delById(Integer id) { } @Override
public void modifyUser(User user) { } @Override
public User getById(Integer id) {
return null;
} @Override
public List<User> getList() {
List<User> userList = new ArrayList<>();
userList.add(new User(0,"Error","Error",0));
return userList;
}
};
}
}

  ③在@FeignClient注解中指定fallbackFactory的属性值

 package cn.coreqi.feign;

 import cn.coreqi.entities.User;
import cn.coreqi.fallbackfactory.UserServiceFallbackFactory;
import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.web.bind.annotation.GetMapping; import java.util.List; @FeignClient(value = "USER-PROVIDER",fallbackFactory = UserServiceFallbackFactory.class) //指定微服务实例名称
public interface UserFeignClient {
@GetMapping("/users") //指定调用微服务的服务地址
public List<User> getList();
}

⒍服务监控-Hystrix Dashboard

  Hystrix提供了准实时的调用监控(Hystrix Dashboard),Hystrix 会持续的记录所有通过Hystrix 发起请求的执行信息,并以统计报表和图形的形式展示给用户,Netfilx通过hystrix-metrics-event-stream项目实现了对以上指标的监控,Spring Cloud也提供了对Hystrix Dashboard的整合,对监控内容转化为可视化界面。

7示例

  ①新建监控项目添加依赖

         <dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<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>

  ②配置文件中指定运行端口 

 server.port=9001

  ③主程序启动类添加@EnableHystrixDashboard注解

 package cn.coreqi;

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

  ④对所有需要监控的服务提供者项目添加以下依赖

         <dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-actuator</artifactId>
</dependency>

  ⑤访问监控项目web管理地址

    http://localhost:9001/hystrix

SpringCloud Hystrix的更多相关文章

  1. SpringCloud Hystrix熔断之线程池

    服务熔断 雪崩效应:是一种因服务提供者的不可用导致服务调用者的不可用,并导致服务雪崩的过程. 服务熔断:当服务提供者无法调用时,会通过断路器向调用方直接返回一个错误响应,而不是长时间的等待,避免服务雪 ...

  2. SpringBoot + SpringCloud Hystrix 实现服务熔断

    什么是Hystrix 在分布式系统中,每个服务都可能会调用很多其他服务,被调用的那些服务就是依赖服务,有的时候某些依赖服务出现故障也是很常见的. Hystrix是Netflix公司开源的一个项目,它提 ...

  3. springCloud Hystrix 断路由

    第一步加入依赖: <dependency> <groupId>org.springframework.cloud</groupId> <artifactId& ...

  4. SpringCloud+Hystrix服务容错

    Netflix Hystrix — 应对复杂分布式系统中的延时和故障容错 +应用场景 分布式系统中经常会出现某个基础服务不可用造成整个系统不可用的情况, 这种现象被称为服务雪崩效应. 为了应对服务雪崩 ...

  5. springcloud hystrix 部分参数整理

    hystrix.command.default和hystrix.threadpool.default中的default为默认CommandKey Command Properties Executio ...

  6. springcloud Hystrix fallback无效

    在使用feign调用服务的时候防止雪崩效应,因此需要添加熔断器.(基于springboot2.0) 一.在控制器的方法上添加  fallbackMethod ,写一个方法返回,无须在配置文件中配置,因 ...

  7. SpringCloud Hystrix 参数

    hystrix.command.default和hystrix.threadpool.default中的default为默认CommandKey Command PropertiesExecution ...

  8. 关于springcloud hystrix 执行 hystrix.stream 跳转失败的问题

    经过观看网友的总结:应该时版本的问题.某些版本没有对/hystrix.stream进行配置 所以解决方案(网友答案): 需要配置类配置下面 @Bean public ServletRegistrati ...

  9. Hippo4J v1.3.1 发布,增加 Netty 监控上报、SpringCloud Hystrix 线程池监控等特性

    文章首发在公众号(龙台的技术笔记),之后同步到博客园和个人网站:xiaomage.info Hippo4J v1.3.1 正式发布,本次发布增加了 Netty 上传动态线程池监控数据.适配 Hystr ...

随机推荐

  1. 【强大知名的CAD绘图工具】AutoCAD 2019 for Mac

    以上图片来源于互联网分享,如涉及版权问题请联系作者删除. 文章素材来源:风云社区(www.scoee.com) 下载地址:风云社区(www.scoee.com)   [简介] AutoCAD 2019 ...

  2. Go-day05

    今日概要: 1. 结构体和方法 2. 接口 一.go中的struct 1. 用来自定义复杂数据结构 2. struct里面可以包含多个字段(属性) 3. struct类型可以定义方法,注意和函数的区分 ...

  3. mysql5.6.40单实例安装二进制快捷安装

    mysql5.6.40单实例安装二进制快捷安装 近期因不同环境需要不同版本的mysql实例,故为了方便操作,特此记录下来,方便自己查找. # 1.1.Centos最小化安装推荐常用依赖包 yum cl ...

  4. MySQL数据库优化_索引

    1.添加索引后减少查询需要的行数,提高查询性能 (1) 建表 CREATE TABLE `site_user` ( `id` ) NOT NULL AUTO_INCREMENT COMMENT '自增 ...

  5. ranger部署文档(记)

    目录 概览... 2 1.      ranger-admin. 2 2.      ranger-user-sync. 2 3.      ranger-*-plugins. 2 安装... 3 1 ...

  6. python遇到的知识点

    python遇到的知识点,记录一下.方便学习. 文件相关操作 查了资料,关于open()的mode参数: 'r':读 'w':写 'a':追加 'r+' == r+w(可读可写,文件若不存在就报错(I ...

  7. C语言数据类型的转换(隐式转换)

    算术运算符中的转换规则: double ←── float 高↑long↑unsigned↑int ←── char,short 低 注意: 图中横向箭头表示必须的转换,如两个float型数参加运算, ...

  8. 【leetcode-84】 柱状图中最大的矩形

    (1pass,比较简单的hard) 给定 n 个非负整数,用来表示柱状图中各个柱子的高度.每个柱子彼此相邻,且宽度为 1 . 求在该柱状图中,能够勾勒出来的矩形的最大面积. 以上是柱状图的示例,其中每 ...

  9. 项目thymeleaf

    官方文档:https://www.thymeleaf.org/doc/tutorials/3.0/usingthymeleaf.html#link-urls 定义和引用片段 在我们的模板中,我们经常需 ...

  10. 056、macvlan网络结构分析(2019-03-25 周一)

    参考https://www.cnblogs.com/CloudMan6/p/7383919.html   macvlan不依赖linux bridge   brctl show 可以确认没有创建新的b ...