Ribbon使用Hystrix
1、导入依赖spring-cloud-starter-hystrix
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-hystrix</artifactId>
</dependency>
2、消费启动类开启@EnableCircuitBreaker
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.circuitbreaker.EnableCircuitBreaker;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.cloud.client.loadbalancer.LoadBalanced;
import org.springframework.cloud.netflix.ribbon.RibbonClient;
import org.springframework.context.annotation.Bean;
import org.springframework.web.client.RestTemplate; @SpringBootApplication
@EnableDiscoveryClient
//开启Ribbon
@RibbonClient(name="cloud-producer")
//启动断路器支持(Hystrix)
@EnableCircuitBreaker
public class RibbonConsumerApplication { @Bean
@LoadBalanced //负载均衡
public RestTemplate restTemplate() {
return new RestTemplate();
} public static void main(String[] args) {
SpringApplication.run(RibbonConsumerApplication.class, args);
} }
3、TestService——设置断路器核心类
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;
import org.springframework.web.client.RestTemplate; import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand; @Repository
public class TestService { @Autowired
private RestTemplate restTemplate; //设置断路器,当此方法无法应答时(把mima-cloud-producer服务停掉),调用getError方法
@HystrixCommand(fallbackMethod="getError")
public String get(String id) {
System.out.println(Thread.currentThread().getName()+".get before...");
String result = restTemplate.getForObject("http://cloud-producer/get/"+id, String.class);
System.out.println(Thread.currentThread().getName()+".get end...result="+result);
return result;
} public String getError(String id) {
System.out.println(Thread.currentThread().getName()+"断路器启动");
return "断路器fallback返回error";
}
}
4、TestController——测试类
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController; import com.mimaxueyuan.consumer.robbin.service.TestService; @RestController
public class TestController { @Autowired
private TestService testService; @GetMapping("/ribbon/get/{id}")
public String get(@PathVariable String id) {
return testService.get(id);
}
}
以上代码在cloud-consumer-ribbon-hystrix服务中,模拟远程调用cloud-producer服务。
5、模拟测试
5.1、启动服务
启动cloud-consumer-ribbon-hystrix、cloud-producer服务,保证服务正常。
5.2、正常请求
5.3、服务挂掉请求,触发断路器
把cloud-producer服务关闭,这时http://localhost:8807/ribbon/get/123456请求无法应答,会调用getError方法,触发断路器
5.4、多次请求控制台会打印如下信息:
Ribbon使用Hystrix的更多相关文章
- 003客户端负载均衡Ribbon & 短路器Hystrix
1.POM配置 和普通Spring Boot工程相比,仅仅添加了Eureka.Ribbon.Hystrix依赖和Spring Cloud依赖管理 <dependencies> <!- ...
- Spring Cloud中五大神兽总结(Eureka/Ribbon/Feign/Hystrix/zuul)
Spring Cloud中五大神兽总结(Eureka/Ribbon/Feign/Hystrix/zuul) 1.Eureka Eureka是Netflix的一个子模块,也是核心模块之一.Eureka是 ...
- Feign整合Ribbon和Hystrix源码解析
在上篇文章Feign自动装配中,我们提到了Feign的自动装配的原理,以及Feign整合Ribbon和Hystrix的核心在类FeignClientFactoryBean中,那么本篇文章就来揭开这个类 ...
- Spring Cloud Ribbon 整合 Hystrix
在前面随笔 Spring Cloud 之 Ribbon 的ribbon工程基础上进行改造 1.pom.xml 加入依赖 <dependency> <groupId>org.sp ...
- 一文读懂SpringCloud与Eureka,Feign,Ribbon,Hystrix,Zuul核心组件间的关系
概述 毫无疑问,Spring Cloud是目前微服务架构领域的翘楚,无数的书籍博客都在讲解这个技术.不过大多数讲解还停留在对Spring Cloud功能使用的层面,其底层的很多原理,很多人可能并不知晓 ...
- spring Cloud中,解决Feign/Ribbon整合Hystrix第一次请求失败的问题?
Spring Cloud中,Feign和Ribbon在整合了Hystrix后,可能会出现首次调用失败的问题,要如何解决该问题呢? 造成该问题的原因 Hystrix默认的超时时间是1秒,如果超过这个时间 ...
- 微服务:Eureka+Zuul+Ribbon+Feign+Hystrix构建微服务架构
原文地址:http://blog.csdn.net/qq_18675693/article/details/53282031 本案例将打架一个微服务框架,参考来源官方参考文档 微服务:是什么?网上有一 ...
- SpringCloud及其五大常用组件之Feign、Ribbon和Hystrix
1.Feign 我们已经将Eureka和Zuul开发完毕,而且上面注册了两个微服务,现在我们实现两个微服务之间的调用. String baseUrl = "http://127.0.0.1: ...
- SpringCloud与Eureka,Feign,Ribbon,Hystrix,Zuul核心组件间的关系
Eureka:各个服务启动时,Eureka Client都会将服务注册到Eureka Server,并且Eureka Client还可以反过来从Eureka Server拉取注册表,从而知道其他服务在 ...
随机推荐
- Linux 只列出目录的方法
1. ls -d 2. find -type d -maxdepth 1 3. ls -F | grep "/$" 4. ls -l | grep "^d"
- C++ 提取网页内容系列之三
标 题: C++ 提取网页内容系列作 者: itdef链 接: http://www.cnblogs.com/itdef/p/4171659.html 欢迎转帖 请保持文本完整并注明出处 这次继续下载 ...
- leveldb 学习记录(二) Slice
基本每个KV库都有一个简洁的字符串管理类 比如redis的sds 比如leveldb的slice 管理一个字符串指针和数据长度 通过对字符串指针 长度的管理实现一般的创建 判断是否为空 获取第N个位 ...
- javascript的异步编程
同步与异步 介绍异步之前,回顾一下,所谓同步编程,就是计算机一行一行按顺序依次执行代码,当前代码任务耗时执行会阻塞后续代码的执行. 同步编程,即是一种典型的请求-响应模型,当请求调用一个函数或方法后, ...
- foreach退出循环(新人请多多关照~)
今天做一个关于人员信息修改的页面时,我用foreach获取数据库数据时发现,用if else判断输入的内容时,会一个一个的做对比,导致错误提醒时会弹出与数据库内容行数相同条的提醒,最后发现将数据直接命 ...
- print('{:15}|{:^9}|{:^9}'.format('', 'lat.', 'long.'))是什么意思?
平台:win10 x64+Python3.7.0 先了解下——Python3 字符串格式化 Python字符串的格式化方法分为两种,分别为占位符(%)和format方式. 占位符方式在Python2. ...
- No趴笨小分队
这星期完成了小组的取名这一项重大的活动. 正所谓“名字是一个好开头”,取这个名义有以下的意义: 希望之后的学习以及工作能一帆风顺: 祝福各位小组成员之后的路能顺顺利利: 希望能在组员磨合的过程中可以愉 ...
- 编译与解释(java)
计算机不能直接理解高级语言,只能直接理解机器语言,所以必须要把高级语言翻译成机器语言,计算机才能执行高级语言编写的程序. 翻译的方式有两种,一个是编译,一个是解释.两种方式只是翻译的时间不同 计算机不 ...
- 2018.09.22 上海大学技术分享 - An Introduction To Go Programming Language
老实说笔者学习 Go 的时间并不长,积淀也不深厚,这次因缘巧合,同组的同事以前是上海大学的开源社区推动者之一,同时我们也抱着部分宣传公司和技术分享的意图,更进一步的,也是对所学做一个总结,所以拟定了这 ...
- Jquery 图片走马灯效果原理
本篇只讲解水平走马灯效果,垂直向上走马灯效果不讲解,原理一样,但是水平走马灯效果有一个小坑.待会讲解 照例先上代码: HTML: <div class="box"> & ...