spring-cloud-hystrix熔断
依赖pom
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-netflix</artifactId>
<version>1.0.7RELEASE</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-eureka</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-hystrix</artifactId>
</dependency>
<dependencies>
源代码
@RestController
@SpringBootApplication
@EnableCircuitBreaker
public class Application extends SpringBootServletInitializer{
@AutoWired
FooService fooService;
@RequestMapping("/foo")
public String foo(){
return fooService.getFoo();
}
public static void main(String[] args){
SpringApplication.run(Application.class,args);
}
}
模拟服务失败的情况
@Component
public FooService{
@HystrixCommand(fallbackMethod="defaultFoo")
public static String getFoo() throws Exception {
throw new Exception();
}
public static String defaultFoo(){
return "defaultFoo";
}
}
浏览器调用 foo
展示 defaultFoo
熔断器是一种异常的处理机制
进一步尝试,模拟服务有时成功 有时失败的情况
@Component
public FooService{
static int count=0;
@HystrixCommand(fallbackMethod="defaultFoo")
public static String getFoo() throws Exception {
count++;
count%=20;
System.out.println("realGetFoo");
if(count<10){
throw new Exception();
}else{
return "foo";
}
}
public static String defaultFoo(){
return "defaultFoo";
}
}
插入一些日志输出,发现会先调用一下getFoo这个接口,如果出现异常则调用defaultFoo
熔断可能可以设置一些策略,在某些情况下不调用getFoo接口
启用hystrix报表
添加依赖
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-netflix-hystrix-dashboard</artifactId>
</dependency>
Application增加注解
@EnableHystrixDashBoard
在浏览器中输入
http://host:port/hytrix
并填入地址,可以进入hystrix-dashboard界面
测试发现如果多次快速调用foo接口 并且Foo接口抛出异常
断路器会变为open状态,此时程序不会尝试调用实际的getFoo方法,而是直接调用defaultFoo
过一段时间之后 circuit会自动闭合
spring-cloud-hystrix熔断的更多相关文章
- 第五章 服务容错保护:Spring Cloud Hystrix
在微服务架构中,我们将系统拆分为很多个服务,各个服务之间通过注册与订阅的方式相互依赖,由于各个服务都是在各自的进程中运行,就有可能由于网络原因或者服务自身的问题导致调用故障或延迟,随着服务的积压,可能 ...
- spring cloud: Hystrix(一):简单使用
在微服务架构中,我们将系统拆分为很多个服务,各个服务之间通过注册与订阅的方式相互依赖,由于各个服务都是在各自的进程中运行,就有可能由于网络原因或者服务自身的问题导致调用故障或延迟,随着服务的积压,可能 ...
- 第五章 服务容错保护: Spring Cloud Hystrix
在微服务架构中, 存在着那么多的服务单元, 若一个单元出现故障, 就很容易因依赖关系而引发故障的蔓延,最终导致整个系统的瘫痪,这样的架构相较传统架构更加不稳定.为了解决这样的问题, 产生了断路器等一系 ...
- SpringCloud---服务容错保护---Spring Cloud Hystrix
1.概述 1.1 在分布式架构中,存在着许多的服务单元,若一个单元出现故障,很容易因依赖关系引发故障的蔓延,最终导致整个系统的瘫痪: 为了解决这样的问题,产生了断路器等服务保护机制: 1.2 分布式架 ...
- Spring Cloud 微服务四:熔断器Spring cloud hystrix
前言:在微服务架构中,一般都是进程间通信,有可能调用链都比较长,当有底层某服务出现问题时,比如宕机,会导致调用方的服务失败,这样就会发生一连串的反映,造成系统资源被阻塞,最终可能造成雪崩.在sprin ...
- Spring Cloud Hystrix理解与实践(一):搭建简单监控集群
前言 在分布式架构中,所谓的断路器模式是指当某个服务发生故障之后,通过断路器的故障监控,向调用方返回一个错误响应,这样就不会使得线程因调用故障服务被长时间占用不释放,避免故障的继续蔓延.Spring ...
- Spring Cloud Hystrix 服务容错保护
目录 一.Hystrix 是什么 二.Hystrix断路器搭建 三.断路器优化 一.Hystrix 是什么 在微服务架构中,我们将系统拆分成了若干弱小的单元,单元与单元之间通过HTTP或者TCP等 ...
- 分布式系统的延时和故障容错之Spring Cloud Hystrix
本示例主要介绍 Spring Cloud 系列中的 Eureka,如何使用Hystrix熔断器容错保护我们的应用程序. 在微服务架构中,系统被拆分成很多个服务单元,各个服务单元的应用通过 HTTP 相 ...
- Spring Cloud Hystrix 服务容错保护 5.1
Spring Cloud Hystrix介绍 在微服务架构中,通常会存在多个服务层调用的情况,如果基础服务出现故障可能会发生级联传递,导致整个服务链上的服务不可用为了解决服务级联失败这种问题,在分布式 ...
- Spring Cloud学习 之 Spring Cloud Hystrix(基础知识铺垫)
Spring Boot版本:2.1.4.RELEASE Spring Cloud版本:Greenwich.SR1 文章目录 前述: 快速入门: 命令模式: RxJava: 前述: 在微服务架构中, ...
随机推荐
- c#线程问题(1)
delegate : public delegate void ParameterizedThreadStart(Object obj) public delegate void ThreadStar ...
- win7系统激活最简单方法
利用激活工具快速激活WIN7系统! 工具/原料 小马Oem7.未激活的WIN7系统 步骤/方法 1 复制BT种子 http://www.pccppc.com/download/oem7F.rar粘贴到 ...
- Android(java)学习笔记115:Android InputMethodManager输入法简介
正文 一.结构 public final class InputMethodManager extends Object Java.lang.Object android.view.inputmeth ...
- oracle顺序控制语句goto、null和分页过程中输入输出存储、java程序的调用过程
顺序控制语句1 goto建议不要使用 declare i number:=; begin loop dbms_output.put_line(i); then goto end_loop; end i ...
- mybatis缓存创建过程
带着 上篇 的问题,再来看看mybatis的创建过程 1.从SqlSessionFactoryBuilder解析mybatis-config.xml开始 对文件流解析 XMLConfigBuilder ...
- HTML的style属性
HTML的style属性 HTML的style属性提供了一种改变HTML样式的通用方法.style是在HTML4版本中引用的,它是一种首选的改变HTML元素样式的方法.可以使用style直接的将样式添 ...
- CygWin模拟Linux环境进行Ant批量打包
运行环境:Windows7 + Cygwin + ant 第一种:有源码 这种方式比较 简单.利用ant打包.直接shell脚本修改 配置渠道号的文件.我们目前是用的umeng的.在AndroidMa ...
- C# 32位md5
/// <summary> /// 获得32位的MD5加密 /// </summary> /// <param name="str">加密字符串 ...
- Jackson - Date Handling
Handling dates on Java platform is complex business. Jackson tries not to make it any harder than it ...
- Spring(3.2.3) - Beans(11): depends-on
大多数情况下,Bean 之间的依赖非常直接:被依赖的 Bean 作为属性.在 XML 配置文件中最常见的就是使用 <ref/> 元素.在一些特殊情况下,Bean 之间的依赖不够直接.比如, ...