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拉取注册表,从而知道其他服务在 ...
随机推荐
- thinkphp 视图(三)系统变量——原生标签
查看系统变量 dump($_SERVER); 在view中获取服务器变量 <p>{$Think.server.HTTP_HOST}</p> 获取env变量 status=dev ...
- Linux驱动之poll机制的理解与简单使用
之前在Linux驱动之按键驱动编写(中断方式)中编写的驱动程序,如果没有按键按下.read函数是永远没有返回值的,现在想要做到即使没有按键按下,在一定时间之后也会有返回值.要做到这种功能,可以使用po ...
- No module named HTMLTestRunner
今天把我在公司写的自动化项目copy回家,在家里的电脑运行是报了个No module named HTMLTestRunner,看到后心想这问题好解决嘛,一个pip install HTMLTestR ...
- PHP开发——变量
变量的概念 l 变量是临时存储数据的容器: l 变量是存储内存当中: l 我们现实中有很多数据:姓名.性别.年龄.学历等: l 在计算机中,用变量来代替一个一个的数据: l 我们可以把计算机 ...
- Elasticsearch tshark 封包分析 (转)
Elasticsearch tshark 封包分析 使用wireshark能解決許多網路問題,將側錄下來的封包傳至Elasticsearch上方便分析製作及時報表.tshark為wireshark的命 ...
- Linux下所有命令失效的解决方法
今天在设置环境变量时,一不小心,设置错了,系统中的所有命令全部失效了,可把我急坏了,下面用一条命令可解决: 解决办法:重新设置环境变量PATH export PATH=/usr/sbin:/usr/b ...
- c++ stl源码剖析学习笔记(二)iterator
ITERATOR 迭代器 template<class InputIterator,class T> InputIterator find(InputIterator first,Inpu ...
- struts2遇到的一个问题。
2018-09-12 好几年没配过struts2了,今天想用最新版的配一下,一直不成功,后来才知道,一堆红色输出里面有这样一句 ERROR StatusLogger No log4j2 configu ...
- 【转】Closeable, Readable, Flushable, Appendable
Closeable: package java.io; import java.io.IOException; public interface Closeable { /** * Closes th ...
- ssm中通过ajax或jquer的validate验证原密码与修改密码的正确性
一.ajax 1. <script type="text/javascript"> //验证原密码1.ajax,正则 var ok1=false,ok2=false,o ...