spring cloud--------------------HystrixCommand使用
一。注解使用:
(一)注解同步执行
1.注解开启断路器功能 @EnableCircuitBreaker
2.方法事例
@HystrixCommand(fallbackMethod = "erro")
public String succese(){
Person person=new Person();
person.setAddress("beijing");
person.setAge(18);
person.setName("zzq");
return restTemplate.postForObject("http://hello-service/hello-post",person,String.class);
}
注意: fallbackMethod:请求失败的情况下调用的方法
public String erro(){
return "404-----------405";
}
(二)异步执行
1.在项目的入口类中配置一个HystrixCommandAspect的Bean,如下: @Bean
public HystrixCommandAspect hystrixCommandAspect() {
return new HystrixCommandAspect();
}
2. 方法事例:@HystrixCommand(fallbackMethod = "erro")
public Future<String> succeseYibu(){
return new AsyncResult<String>() {
@Override
public String invoke() {
Person person=new Person();
person.setAddress("beijing");
person.setAge(18);
person.setName("zzq");
return restTemplate.postForObject("http://hello-service/yanshi",person,String.class);
}
};
}
3.controller调用方法 @RequestMapping("/xiaofeizhe-duanlu-HystrixCommandYibu")
public String HystrixCommandYibu() throws ExecutionException, InterruptedException {
Future<String> stringFuture = helloService.succeseYibu();
System.out.println("HystrixCommandYibu---------异步调用方法---------");
String s = stringFuture.get();
System.out.println(s);
return s;
}
二。继承HystrixCommand类使用
1.class类事例
public class PersonCommand extends HystrixCommand<String> {
private RestTemplate restTemplate;
private Long id;
public PersonCommand(Setter setter, RestTemplate restTemplate,Long id) {
super(setter);
this.restTemplate=restTemplate;
this.id=id;
} @Override
protected String run() throws Exception {
return restTemplate.getForObject("http://hello-service/yanshi",String.class);
} @Override
protected String getFallback() {
return "HystrixCommand<String> 返回失败";
}
}
备注:run()方法设计执行的方法 ,getFallback()调用run失败执行的方法
2.controller使用展示
(1)同步执行
@RequestMapping("/xiaofeizhe-tongbu")
public String agin(){
PersonCommand myfirstHy = new PersonCommand(HystrixCommand.Setter.withGroupKey(
HystrixCommandGroupKey.Factory.asKey("myfirstHy")).andCommandPropertiesDefaults(
HystrixCommandProperties.Setter().withExecutionTimeoutInMilliseconds(2000)), restTemplate(), 1l);
return myfirstHy.execute();
}
(2)异步执行
execute()方法变成queue()方法
spring cloud--------------------HystrixCommand使用的更多相关文章
- Spring Cloud @HystrixCommand和@CacheResult注解使用,参数配置
使用Spring Cloud时绕不开Hystrix,他帮助微服务实现断路器功能.该框架的目标在于通过控制那些访问远程系统.服务和第三方库的节点,从而对延迟和故障提供更强大的容错能力.Hystrix具备 ...
- spring cloud: Hystrix(二):简单使用@HystrixCommand的commandProperties配置@HistrixProperty隔离策略
spring cloud: Hystrix(二):简单使用@HystrixCommand的commandProperties配置@HistrixProperty隔离策略 某电子商务网站在一个黑色星期五 ...
- 读书笔记——spring cloud 中 HystrixCommand的四种执行方式简述
读了<Spring Cloud 微服务实战>第151-154页, 总结如下: Hystrix存在两种Command,一种是HystrixCommand,另一种是HystrixObserva ...
- Spring cloud Hystrix使用@HystrixCommand使用Hystrix组件及@EnableCircuitBreaker原理介绍
通过@HystrixCommand注解实现在Spring Cloud使用Hystrix组件相关的工程 cloud-registration-center:注册中心 cloud-service-hyst ...
- 综合使用spring cloud技术实现微服务应用
在之前的章节,我们已经实现了配置服务器.注册服务器.微服务服务端,实现了服务注册与发现.这一章将实现微服务的客户端,以及联调.实现整个spring cloud框架核心应用. 本文属于<7天学会s ...
- Building microservices with Spring Cloud and Netflix OSS, part 2
In Part 1 we used core components in Spring Cloud and Netflix OSS, i.e. Eureka, Ribbon and Zuul, to ...
- Spring Cloud介绍 Spring Cloud与Dubbo对比
spring Cloud是一个基于Spring Boot实现的云应用开发工具,它为基于JVM的云应用开发中的配置管理.服务发现.断路器.智能路由.微代理.控制总线.全局锁.决策竞选.分布式会话和集群状 ...
- 基于Spring Cloud和Netflix OSS构建微服务,Part 2
在上一篇文章中,我们已使用Spring Cloud和Netflix OSS中的核心组件,如Eureka.Ribbon和Zuul,部分实现了操作模型(operations model),允许单独部署的微 ...
- Spring Cloud 学习笔记(二)——Netflix
4 Spring Cloud Netflix Spring Cloud 通过自动配置和绑定到Spring环境和其他Spring编程模型惯例,为Spring Boot应用程序提供Netflix OSS集 ...
- Spring Cloud Ribbon 整合 Hystrix
在前面随笔 Spring Cloud 之 Ribbon 的ribbon工程基础上进行改造 1.pom.xml 加入依赖 <dependency> <groupId>org.sp ...
随机推荐
- c++builder XE6 Remote Debuger 远程调试
1.远程目标机器 安装D:\Program Files (x86)\Borland\Remote Debugger\20,没有光盘从已安装的xe6电脑上Bin目录下拷贝文件 bccide.dll bo ...
- Activity服务类-4 HistoryService服务类
一共个方法15个方法 用于查询历史工作流信息1.创建查询(7个方法)//创建一个新的编程查询来搜索{@link HistoricProcessInstance}.HistoricProcessInst ...
- 机器学习入门-主成分分析(PCA)
主成分分析: 用途:降维中最常用的一种方法 目标:提取有用的信息(基于方差的大小) 存在的问题:降维后的数据将失去原本的数据意义 向量的内积:A*B = |A|*|B|*cos(a) 如果|B| = ...
- 使用root用户,在centos7中安装rabbitMQ
参考地址: https://www.linuxidc.com/Linux/2018-01/150600.htm https://blog.csdn.net/summerhust/article/det ...
- Java HashMap两种遍历方式
第一种: Map map = new HashMap(); Iterator iter = map.entrySet().iterator(); while (iter.hasNext()) { Ma ...
- 两个关于URL解析的例子
例一: /* 解析URL查寻串中的name=value参数对 将name=value对存储在对象属性中,并返回对象 alert(getQuery().name) */ function getQuer ...
- Javascript 浏览器检测
推荐 Browser Detecter, 很好用,自己也很容易扩展. 原文链接:http://www.quirksmode.org/js/detect.html <script type=&qu ...
- rook 记录
更改rook 集群的配置 https://github.com/rook/rook/blob/master/design/cluster-update.md rook集群升级流程 https://ro ...
- ionic 2,带着运气成分
npm config set loglevel info 查看安装信息 npm cache clean 清除缓存 cnpm sync ionic ...
- ftp上传下载工具类
package com.taotao.utils; import java.io.File; import java.io.FileInputStream; import java.io.FileNo ...