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 ...
随机推荐
- js改变div高度
用bootsrap响应式布局的时候,遇到个很恶心的问题:左边栏很短很难看!! 于是,想用js来自动改变左边的高度,没成功!!原来是设置的时候,没加单位,坑爹了. 参考:http://blog.sina ...
- FireFox 书签 缓存 路径设置
English ver down https://www.mozilla.org/en-US/firefox/new/ add ons https://addons.mozilla.org/en-US ...
- WDA-参考路径
1. SAP Netweave安装 https://wenku.baidu.com/view/b3ac371a227916888486d77c.html?sxts=1545717961793 2. ...
- Java多线程之使用ATM与柜台对同一账户取钱
钱数要设置成静态的变量,两种取钱方式操作的是同一个银行账户! 废话不多说,直接上代码.注释写的都很详细!!! package com.thread.multi2; public class Bank ...
- caffe openpose/Realtime Multi-Person 2D Pose Estimation using Part Affinity Fields配置(转)
Realtime Multi-Person 2D Pose Estimation using Part Affinity Fields 是CVPR2017的一篇论文,作者称是世界上第一个基于深度学习的 ...
- nginx-----惹不起的端口修改
1,查看端口是否被占用 打开CMD,输入输入netstat -aon | findstr :80 出现: TCP 0.0.0.0:80 0.0.0.0:0 ...
- django中怎么使用mysql数据库的事务
Mysql数据库事务: 在进行后端业务开始操作修改数据库时,可能会涉及到多张表的数据修改,对这些数据的修改应该是一个整体事务,即要么一起成功,要么一起失败. Django中对于数据库的事务,默认每执行 ...
- Consul Session
consul馆提供session机制,可用于构建分布式锁. session作为节点,健康检查和key/value数据之间的绑定层. 它们旨在提供粒度锁定,并受到“The Chubby Lock Ser ...
- Mysql update 一个表中自己的数据
update table ucf, table t2 set ucf.pcid = t2.pcid where t2.name = 'liming' and t2.gid= ucf.gid and ...
- html中的innerHTML
首先看个例子 <html> <head> <script type="text/javascript"> function getInnerHT ...