一。注解使用:
(一)注解同步执行
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使用的更多相关文章

  1. Spring Cloud @HystrixCommand和@CacheResult注解使用,参数配置

    使用Spring Cloud时绕不开Hystrix,他帮助微服务实现断路器功能.该框架的目标在于通过控制那些访问远程系统.服务和第三方库的节点,从而对延迟和故障提供更强大的容错能力.Hystrix具备 ...

  2. spring cloud: Hystrix(二):简单使用@HystrixCommand的commandProperties配置@HistrixProperty隔离策略

    spring cloud: Hystrix(二):简单使用@HystrixCommand的commandProperties配置@HistrixProperty隔离策略 某电子商务网站在一个黑色星期五 ...

  3. 读书笔记——spring cloud 中 HystrixCommand的四种执行方式简述

    读了<Spring Cloud 微服务实战>第151-154页, 总结如下: Hystrix存在两种Command,一种是HystrixCommand,另一种是HystrixObserva ...

  4. Spring cloud Hystrix使用@HystrixCommand使用Hystrix组件及@EnableCircuitBreaker原理介绍

    通过@HystrixCommand注解实现在Spring Cloud使用Hystrix组件相关的工程 cloud-registration-center:注册中心 cloud-service-hyst ...

  5. 综合使用spring cloud技术实现微服务应用

    在之前的章节,我们已经实现了配置服务器.注册服务器.微服务服务端,实现了服务注册与发现.这一章将实现微服务的客户端,以及联调.实现整个spring cloud框架核心应用. 本文属于<7天学会s ...

  6. 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 ...

  7. Spring Cloud介绍 Spring Cloud与Dubbo对比

    spring Cloud是一个基于Spring Boot实现的云应用开发工具,它为基于JVM的云应用开发中的配置管理.服务发现.断路器.智能路由.微代理.控制总线.全局锁.决策竞选.分布式会话和集群状 ...

  8. 基于Spring Cloud和Netflix OSS构建微服务,Part 2

    在上一篇文章中,我们已使用Spring Cloud和Netflix OSS中的核心组件,如Eureka.Ribbon和Zuul,部分实现了操作模型(operations model),允许单独部署的微 ...

  9. Spring Cloud 学习笔记(二)——Netflix

    4 Spring Cloud Netflix Spring Cloud 通过自动配置和绑定到Spring环境和其他Spring编程模型惯例,为Spring Boot应用程序提供Netflix OSS集 ...

  10. Spring Cloud Ribbon 整合 Hystrix

    在前面随笔 Spring Cloud 之 Ribbon 的ribbon工程基础上进行改造 1.pom.xml 加入依赖 <dependency> <groupId>org.sp ...

随机推荐

  1. Declaration terminated incorrectly 讨厌 这样就不可以了

    #include "vcl.fctreeview.hpp"#include "RM_Class.hpp"#include "RM_Common.hpp ...

  2. as3 区别中文 英文 数字

    1)英文a-z是65-90,A-Z是97-112 2)数字是0-9是,48-57 3)上万的都是中文字符 var str:String = "hello world! 你好世界! 88!&q ...

  3. UI5-文档-4.7-JSON Model

    现在我们已经设置了视图和控制器,现在是时候考虑MVC中的M了. 我们将在app中添加一个输入字段,将它的值绑定到模型上,并将相同的值绑定到输入字段的描述上.描述将根据用户类型直接更新. Preview ...

  4. 删除数据恢复数据语句 Oracle

    SELECT * FROM TBL_DZYJ_GEORELICSINFO AS OF TIMESTAMP TO_TIMESTAMP('2017-5-8 9:00:00','YYYY-MM-DD HH: ...

  5. int和Integer区别

    Java是一个近乎纯洁的面向对象编程语言,但是为了编程的方便还是引入了基本数据类型,但是为了能够将这些基本数据类型当成对象操作,Java为每一个基本数据类型都引入了对应的包装类型(wrapper cl ...

  6. springboot+jsp 遇到的坑

    springboot  使用jsp: 1,修改配置文件, spring: mvc: view: prefix: /WEB-INF/jsp/ suffix: .jsp 2,pom 加入: <dep ...

  7. 3类与对象——重拾Java

    面向对象编程的3个特性 1 封装性 面向对象编程核心思想之一就是将数据和对数据的操作封装在一起.通过抽象,即从具体的实例中抽取共同的性质形成一般的概念,比如类的概念. 在实际生活中,我们每时每刻都在与 ...

  8. 数据文件导入mysql时出现中文乱码问题

    http://www.ynpxrz.com/n773257c2024.aspx 从shell进入mysql, mysql> show variables like ‘%char%'; +---- ...

  9. np.random.seed()

    124.np.random.seed()的作用 陈容喜 关注 2018.01.11 21:36 字数 3 阅读 4460评论 0喜欢 6 今天看到一段代码时遇到了np.random.seed(),搞不 ...

  10. War(最短路+最大流)

    War http://acm.hdu.edu.cn/showproblem.php?pid=3599 Time Limit: 2000/1000 MS (Java/Others)    Memory ...