断路器hystrix
![](https://images2018.cnblogs.com/blog/393620/201807/393620-20180717224150648-2105004254.png)
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-hystrix</artifactId>
</dependency>
就可以了,如果要查看监控页面,需要额外引入以下两个:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-hystrix-dashboard</artifactId>
</dependency>
@HystrixCommand(fallbackMethod = "sayError") //fallbackMethod为hystrix开启后执行的快速失败方法
public PersonVo getPerson(){
PersonVo personVo = null;
personVo = restTemplate.getForObject("http://sms-module/sms/getPerson?phoneNumber=17301394307",PersonVo.class);
return personVo;
} public PersonVo sayError(){
PersonVo vo = new PersonVo();
vo.setName("lisi");
return vo;
}
b、对于feign的配置:需要在@FeignClient注解中声明fallback对应方法所在的类,该类必须实现@FeignClient注解所修饰的接口,例如:
@FeignClient(name = "sms-module",fallback = FeignHystrix.class)
public interface FeignSmsClient {
@RequestMapping(value = "/sms/getPerson")
PersonVo queryPerson(@RequestParam("phoneNumber") String phoneNumber);
}
@Component //注意,这个实现类要纳入ioc管理
public class FeignHystrix implements FeignSmsClient {
@Override
public PersonVo queryPerson(String phoneNumber) {
PersonVo vo = new PersonVo();
vo.setName("王五");
return vo;
}
}
@RequestMapping("/demo")
@RestController
public class TestClientController {
@Autowired
FeignService feignService;
@Autowired
TemplateService templateService; @RequestMapping("/test")
public PersonVo myGetPerson(){
ServiceInstance instance = loadBalancerClient.choose("sms-module");
System.out.println("本次执行的实例是:"+instance.getHost()+":"+instance.getPort());
PersonVo personVo = templateService.getPerson();
return personVo;
} @RequestMapping("/test2")
public PersonVo myGetPerson2(){
PersonVo personVo = null;
personVo = feignService.queryPerson("15611273879");
return personVo;
}
}
测试resttemplate跟feign正常访问:
<!--监控相关-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-hystrix-dashboard</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-turbine</artifactId>
</dependency>
dashboard所在服务只依赖前两个starter
turbine:
appConfig: feign-demo,turbine-demo #这里是turbine要监控的服务名列表,逗号隔开,我本地turbine项目的服务名为feign-demo,hystrix项目的服务名为turbine-demo(项目名是从前边的学习一路过来的,这名字在这里比较容易混淆,,,,)
clusterNameExpression: new String("default")
其它代码共用上边hystrix的测试代码,在dashboard项目中将controller中url映射改为test3跟test4,相应的service方法名加222,启动项目分别访问http://localhost:port/hystrix
![](https://images2018.cnblogs.com/blog/393620/201807/393620-20180717223943894-639092920.png)
断路器hystrix的更多相关文章
- SpringCloud学习系列之三----- 断路器(Hystrix)和断路器监控(Dashboard)
前言 本篇主要介绍的是SpringCloud中的断路器(Hystrix)和断路器指标看板(Dashboard)的相关使用知识. SpringCloud Hystrix Hystrix 介绍 Netfl ...
- 【Spring Cloud笔记】 断路器-hystrix
在微服务架构中,一个微服务的超时失败可能导致瀑布式连锁反映,Spring Cloud Netflix 的断路器Hystrix通过自主反馈,防止了这种情况发生.下面介绍简单的断路器使用方法. [step ...
- spring cloud学习(五)断路器 Hystrix
断路器 Hystrix 断路器模式 (云计算设计模式) 断路器模式源于Martin Fowler的Circuit Breaker一文. 在分布式环境中,其中的应用程序执行访问远程资源和服务的操作,有可 ...
- 004声明式服务调用Feign & 断路器Hystrix
1.POM配置 和普通Spring Boot工程相比,添加了Eureka Client.Feign.Hystrix依赖和Spring Cloud依赖管理 <dependencies> &l ...
- SpringCloud IDEA 教学 (四) 断路器(Hystrix)
写在开始 在SpringCloud项目中,服务之间相互调用(RPC Remote Procedure Call —远程过程调用),处于调用链路底层的服务产生不可用情况时,请求会产生堆积使得服务器线程阻 ...
- 断路器Hystrix与Turbine集群监控-Spring Cloud学习第三天(非原创)
文章大纲 一.Hystrix基础介绍二.断路器Hystrix简单使用三.自定义Hystrix请求命令四.Hystrix的服务降级与异常处理五.Hystrix的请求缓存与请求合并六.Hystrix仪表盘 ...
- 断路器-Hystrix的深入了解
前言 高可用相关的技术以及架构,对于大型复杂的分布式系统,是非常重要的.而高可用架构中,非常重要的一个环节,就是如何将分布式系统中的各个服务打造成高可用的服务,从而足以应对分布式系统环境中的各种各样的 ...
- SpringCloud断路器(Hystrix)和服务降级案列
断路器(Hystrix) 为什么需要 Hystrix? 在微服务架构中,我们将业务拆分成一个个的服务,服务与服务之间可以相互调用(RPC).为了保证其高可用,单个服务又必须集群部署.由于网络原因或者自 ...
- 【Spring Cloud学习之六】断路器-Hystrix
环境 eclipse 4.7 jdk 1.8 Spring Boot 1.5.2 Spring Cloud 1.2 一.服务雪崩1.什么是服务雪崩分布式系统中经常会出现某个基础服务不可用造成整个系统不 ...
- 断路器Hystrix(Feign)
上一篇中我们讲了 断路器Hystrix(Ribbon) 本章讲解Feign+Hystrix已经Request请求传递,各种奇淫技巧…. - Hystrix Hystrix支持回退概念:当 断路器 打开 ...
随机推荐
- 小小c#算法题 - 7 - 堆排序 (Heap Sort)
在讨论堆排序之前,我们先来讨论一下另外一种排序算法——插入排序.插入排序的逻辑相当简单,先遍历一遍数组找到最小值,然后将这个最小值跟第一个元素交换.然后遍历第一个元素之后的n-1个元素,得到这n-1个 ...
- Java流机制详解
转自http://blog.csdn.net/qq_16558621/article/details/51377887 http://www.cr173.com/html/18666_1.html
- 正经学C#_判断[IF语句]:[c#入门经典]
判断语句几乎是最为常用的语句之一,是最有效的. 先说IF语句,IF语句也是属于分支的一种,用来控制流程的. IF的语句是这样的 IF(xxx 条件) { //代码块 } ,b; ) { b=a--; ...
- 浅谈《守望先锋》中的 ECS 构架
https://blog.codingnow.com/2017/06/overwatch_ecs.html 今天读了一篇 <守望先锋>架构设计与网络同步 .这是根据 GDC 2017 上的 ...
- 【转】PHP微信上传永久图片素材
$TOKEN="XXXX"; $file = "D:\www\weixin\game.jpg"; $data = array( 'media'=> new ...
- truts2标签-forEach标签
<c:forEach begin="1" end="${obj.portnum}" step="1" varStatus=" ...
- linux脚本遇到的一点问题
系统环境: # uname -r -.el6.x86_64 # cat /etc/redhat-release CentOS release 6.5 (Final) 对服务器状态监控的一段脚本中使用了 ...
- CoreAnimation 核心动画 / CABasicAnimation/ CAKeyframeAnimation
- (void)createBaseAnimation{ //基础动画 CABasicAnimation *animation = [CABasicAnimation animation]; anim ...
- powdesigner建表
默认打开powerDesigner时,创建table对应的自动生成sql语句没有注释. 方法1.comment注释信息 在Columns标签下,一排按钮中找到倒数第2个按钮:Customize Col ...
- SprimgMVC学习笔记(七)—— 上传图片
一.配置虚拟目录 在tomcat上配置图片虚拟目录,在tomcat下conf/server.xml中添加: <Context docBase="D:\upload\temp" ...