《spring cloud微服务实战》读书笔记——Spring Cloud Hystrix(四)使用详解(一)
来源:互联网 发布:套定额软件 编辑:程序博客网 时间:2024/05/16 17:02
在前面的快速入门中我们已经简单的使用到了spring cloud hystrix,现在就来详细的说一下spring cloud hystrix 的用法。
1、通过继承的方式来创建一个命令
首先在服务的提供方eureka-service项目中创建一个新的接口,该接口只返回字符串”haha”
@RestControllerpublic class CommandController { @RequestMapping(value="command-service",method = RequestMethod.GET) @ResponseBody public String index(){ return "haha"; }}
接下来回到服务的调用方ribbon-consumer项目中,在这里我们通过继承的方式创建一个命令MyHystrixCommand 类,并重写HystrixCommand类的run方法,在重写的run方法中封装了具体的依赖服务调用逻辑。
public class MyHystrixCommand extends HystrixCommand<String> { private RestTemplate restTemplate; public MyHystrixCommand(Setter setter,RestTemplate restTemplate){ super(setter); this.restTemplate = restTemplate; } @Override protected String run() throws Exception { return restTemplate.getForEntity("http://eureka-service/command-service", String.class).getBody(); }}
命令类已经创建完成,接下来要做的就是创建命令实例,并通过命令来调用相关服务。
接下来创建一个新的controller
完整的代码如下
@RestControllerpublic class CommandController { @Autowired ConsumerService consumerService; @Resource RestTemplate restTemplate; @RequestMapping(value="/helloCommand",method = RequestMethod.GET) @ResponseBody public String helloCommand(){ HystrixCommand.Setter setter = HystrixCommand.Setter.withGroupKey(HystrixCommandGroupKey.Factory.asKey("helloHystrixCommand")); MyHystrixCommand myHystrixCommand = new MyHystrixCommand(setter, restTemplate); String result = myHystrixCommand.execute(); return result; }}
分析:
1、从上面MyHystrixCommand类的构造函数中可以发现,MyHystrixCommand类的构造器中有一个形参Setter setter,这个Setter其实是我们继承的HystrixCommand类中的一个静态类。这个静态类可以实现命令命名,命令分组以及线程池的划分等一系列操作。所以上面代码的意思是给当前创建的命令取名”helloHystrixCommand”,这是固定操作。
2、创建完命令后通过该命令调用了一个execute()方法,用于同步的执行命令。
也可以使用异步的方式,通过调用命令的queue()方法就可以实现,代码如下。
Future<String> queue = myHystrixCommand.queue(); String result = queue.get();
两种方式都可以成功的返回字符串haha
阅读全文
0 0
- 《spring cloud微服务实战》读书笔记——Spring Cloud Hystrix(四)使用详解(一)
- 《spring cloud微服务实战》读书笔记——Spring Cloud Hystrix(四)使用详解(二)
- 《spring cloud微服务实战》读书笔记——Spring Cloud Hystrix(四)使用详解(三)
- 《spring cloud微服务实战》读书笔记——Spring Cloud Hystrix(一)快速入门案例
- 《spring cloud微服务实战》读书笔记——Spring Cloud Hystrix(二)工作流程
- 《spring cloud微服务实战》读书笔记——Spring Cloud Hystrix(三)断路器的原理
- 《spring cloud微服务实战》读书笔记——Spring Cloud Eureka(一)搭建一个注册中心
- 《spring cloud微服务实战》读书笔记——Spring Cloud Ribbon(一)RestTemplate
- 《spring cloud微服务实战》读书笔记——Spring Cloud Eureka(五)Eureka详解
- 《spring cloud微服务实战》读书笔记——Spring Cloud Eureka(四)服务的发现和消费
- 《spring cloud微服务实战》读书笔记------目录
- Spring cloud微服务实战——Eureka(一)
- 《Spring + Cloud微服务实战》读书笔记(一)————基础知识
- 170923_Spring Cloud 微服务实战(翟永超著) 读书笔记(一)_什么是Spring Cloud?
- 《Spring + Cloud微服务实战》读书笔记(二)————Spring Boot
- 《spring cloud微服务实战》读书笔记——Spring Cloud Eureka(二)注册一个服务的提供者
- 《spring cloud微服务实战》读书笔记——Spring Cloud Eureka(三)实现一个高可用的注册中心
- 《Spring Cloud微服务实战》开始预售
- POJ-3237(树链剖分+线段树)
- 删除指定字符
- 关于Activity和Fragment生命周期关系
- 2017年浙江工业大学大学生程序设计迎新赛决赛 F-栗酱的不等式 [KMP]
- 2017-12-23
- 《spring cloud微服务实战》读书笔记——Spring Cloud Hystrix(四)使用详解(一)
- 利用McAfee策略让入侵者无法创建用户+无法删除用户+无法改密码的设 置方法
- JS函数式编程指南笔记1-4章
- Spring set注入与autowire
- UUID随机数的生成,UUID编码
- 【raspberrypi】树莓派官方7寸屏入门指南
- Html5 canvas 应用于webkit浏览器实现电子签名
- python lambda的介绍以及使用
- Qt智能机器人控制系统