Spring Cloud Hystrix

Hystrix是一个延迟和容错库,旨在隔离远程系统、服务和第三方库,阻止链接故障,在复杂的分布式系统中实现恢复能力。

一、快速入门

1)依赖:

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

2)启用断路模式

@SpringBootApplication
@EnableHystrix
@EnableDiscoveryClient
public class ClientApplication {
public static void main(String[] args) {
SpringApplication.run(ClientApplication.class, args);
}
}

3)降级

@Component
public class UserService implements IUserService {
@Override
@HystrixCommand(fallbackMethod = "defaultUser")
public String getUser(String username) throws Exception {
if (username.equals("spring")) {
return "Right user.";
} else {
throw new Exception();
}
}
/**
* 调用该方法返回预设友好错误
* */
public String defaultUser(String username) {
return "Use the default user.";
}
}

在Feign中默认是自带Hystrix功能的,在老版本中是默认打开的,从最近几个版本中开始默认

关闭了,所以需要通过配置文件打开它。

二、Hystrix Dashboard

Hystrix Dashboard仪表盘是根据系统一段时间内发生的请求情况来展示的可视化面板,

这些信息是每个HystrixCommand执行过程中的信息。

依赖:

<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-hystrix-dashboard</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-eureka-client</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
</dependencies>

启用:

SpringBootApplication
@EnableDiscoveryClient
@EnableHystrixDashboard
public class HystrixDashboardApplication {
public static void main(String[] args) {
SpringApplication.run(HystrixDashboardApplication.class, args);
}
}

三、Hystrix异常处理机制

Hystrix的异常处理中,有5种出错的情况会被fallback所截获:

1)FAILURE:执行失败,抛出异常。

2)TIMEOUT:执行超时。

3)SHORT_CIRCUITED:断路器打开。

4)THREAD_POOL_REJECTED:线程池拒绝。

5)SEMAPPHORE_REJECTED:信号量拒绝。

Spring Cloud 微服务笔记(六)Spring Cloud Hystrix的更多相关文章

  1. Spring Cloud 微服务五:Spring cloud gateway限流

    前言:在互联网应用中,特别是电商,高并发的场景非常多,比如:秒杀.抢购.双11等,在开始时间点会使流量爆发式地涌入,如果对网络流量不加控制很有可能造成后台实例资源耗尽.限流是指通过指定的策略削减流量, ...

  2. Spring Cloud微服务笔记(三)服务治理:Spring Cloud Eureka快速入门

    服务治理:Spring Cloud Eureka 一.服务治理 服务治理是微服务架构中最为核心和基础的模块,它主要用来实现各个微服务实例的自动化注册与发现. 1.服务注册: 在服务治理框架中,通常会构 ...

  3. Spring Cloud微服务笔记(一)微服务概念

    微服务概念 一.什么是微服务架构 微服务,是一个小的.松耦合的分布式服务. 为什么需要微服务: 1)单体系统部署在一个进程中,修改了一个小功能,为了部署上线就会影响其他功能. 2)单体应用各个功能模块 ...

  4. Spring Cloud微服务笔记(二)Spring Cloud 简介

    Spring Cloud 简介 Spring Cloud的设计理念是Integrate Everything,即充分利用现有的开源组件, 在它们之上设计一套统一的规范/接口使它们能够接入Spring ...

  5. spring cloud微服务实践六

    本片我们就来认识下spring cloud中的zuul组件. 注:这一个系列的开发环境版本为 java1.8, spring boot2.x, spring cloud Greenwich.SR2, ...

  6. Spring Cloud 微服务笔记(七) Zuul入门

    Zuul入门 Zuul是从设备和网站到后端应用程序所有请求的前门,为内部服务提供可配置的对外URL到服务的 映射关系,基于JVM的后端路由器.其具备一下功能: 1)认证与授权 2)压力控制 3)金丝雀 ...

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

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

  8. Spring Cloud微服务笔记(四)客户端负载均衡:Spring Cloud Ribbon

    客户端负载均衡:Spring Cloud Ribbon 一.负载均衡概念 负载均衡在系统架构中是一个非常重要,并且是不得不去实施的内容.因为负载均衡对系统的高可用性. 网络压力的缓解和处理能力的扩容的 ...

  9. Spring Cloud 微服务六:调用链跟踪Spring cloud sleuth +zipkin

    前言:随着微服务系统的增加,服务之间的调用关系变得会非常复杂,这给运维以及排查问题带来了很大的麻烦,这时服务调用监控就显得非常重要了.spring cloud sleuth实现了对分布式服务的监控解决 ...

随机推荐

  1. 1、IDEA的常用快捷键

    一.常用的快捷键1.Ctrl+/ 或 Ctrl+Shift+/ 注释(// 或者/*...*/ )###2.Ctrl+D 复制行### 注意在MyEclipse中Ctrl+D的作用是删除行3.Ctrl ...

  2. 移除文件(git rm)

    git rm`命令会把文件从已跟踪列表(及暂存区)中移除,并且移除把文件从工作目录中移除,这样下一次你就不会在未跟踪文件列表中看到这些文件了. 如果你只是简单的把文件从工作目录移除,而没有使用git ...

  3. 关于SVN报错 svn: E170013 E125006: contains invalid filesystem format option 'addressing logical'

    在使用svn的时候,遇到了这样的一个问题 首先我使用TortoiseSVN 右键创建的repository. 之后用IDEA,配置了1.9.4版本的SVN,去commit访问这个仓库 结果出现了以下的 ...

  4. 【题解】魔板—洛谷P1275。

    话说好久没更博了. 最近学了好多知识懒的加进来了. 有幸认识一位大佬. 让我有了继续更博的兴趣. 但这是一个旧的题解. 我在某谷上早就发过的. 拿过来直接用就当回归了吧. 其实这道题有一个特别关键的思 ...

  5. SpringMVC-简单总结

    要学习一项技术,首先要知道, 它是什么, 为什么要用它 , 它由哪些东西组成, 每个东西是干什么的, 它们怎么综合在一起的 参考博客: 平凡希: https://www.cnblogs.com/xia ...

  6. jQuery新版本没有了Toggle事件,两个按钮分别控制隐藏显示,同时这两个按钮点击也要互斥。

    十二月没来得及整理发布,一直在草稿箱.现在已经2019年1月了... 需求大概是这样的 //XX点击事件 var flagBar = 0; $("#doNotBaseRate"). ...

  7. flask学习(二)

    一.蓝图 作用:给开发者提供目录结构 功能:1.自定义模板.静态文件目录 2.给一类url加前缀    3.给一类url添加before_request 目录结构 from flask_werkzur ...

  8. 强行杀windows服务

    powershell命令:taskkill /f /fi "SERVICES eq 服务名称"

  9. LaTex basics

    分节: \section{Supplemental Material}\label{sec:supplemental} 小节: \noindent {\bf Preparing References: ...

  10. Android 8.0 无法收到broadcast

    参见此页 https://commonsware.com/blog/2017/04/11/android-o-implicit-broadcast-ban.html 目前最简单的方法就是:把targe ...