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拉取注册表,从而知道其他服务在 ...
随机推荐
- 记一次SQL性能优化,查询时间从4000ms优化到200ms.
以下这句SQL是从PLM中获取代办工作流的.没优化前SQL语句执行一次大概4000ms(4秒). select ch.change_number changeNumber, f.text change ...
- Tomcat9.0.13 Bug引发的java.io.IOException:(打开的文件过多 Too many open files)导致服务假死
问题背景: 笔者所在的项目组最近把生产环境Tomcat迁移到Linux,算是顺利运行了一段时间,最近一个低概率密度的(too many open files)问题导致服务假死并停止响应客户端客户端请求 ...
- Python3个人学习笔记--每天一点一滴成长!
简单的while循环:输入一个数字,while获取该数字,并输出该数字. 例子:猜幸运数字是多少? lucky_num = int(input("number:")) a = 0 ...
- 判断jquery对象是否具有某指定属性或者方法的几种方法
1.typeof 运算符:返回一个用来表示表达式的数据类型的字符串.("number", "string", "boolean", &quo ...
- Java输入输出流详解
通过数据流.序列化和文件系统提供系统输入和输出. Java把这些不同来源和目标的数据都统一抽象为数据流.Java语言的输入输出功能是十分强大而灵活的,美中不足的是看上去输入输出的代码并不是很简洁,因为 ...
- cropper,图片剪辑上传工具的使用
cropper工具是一个功能强,兼容性好的一个图片裁剪和上传工具 GitHub地址:https://github.com/kesixin/Head_Cut_PC <div class=" ...
- 利用ONENET平台控制MPC
可以用于广告机或者灾害预警,实时广播等行业 这个分控制端和服务端 控制端采用winform编写,服务端采用控制台程序编写 优点在于服务端不用有公网ip,比传统方案方便的多. 也不用租用费用高额的云服务 ...
- leetcode437--Path Sum III
https://leetcode.com/problems/path-sum-iii/ 理解比较困难,可以先看https://www.cnblogs.com/albert67/p/10416402.h ...
- cobbler学习
note.youdao.com/share/?id=2f8383d6e9824929012b041f069da26e&type=note#/ IPADDR=192.168.86.4 TYPE= ...
- VS从数据库表生成Model代码
1.工具——扩展和更新——安装下列插件 2.如图所示,在项目或者MODEL文件夹下添加 3.如图所示,生成了一个datanase.11 4.打开该文件后,将数据库连接字符串改为你自己项目中WebCof ...