Spring Cloud对Hystrix的支持
Spring Cloud项目为Netflix OSS Hystrix库提供了全面的支持。 之前我已经写过有关如何使用原始Hystrix库包装远程调用的文章。 在这里,我将探讨如何将Hystrix与Spring Cloud结合使用
基本
实际上并没有什么大不了的,这些概念仅在特定于Spring引导的增强中保留下来。 考虑一个简单的Hystrix命令,该命令包含对Remote服务的调用:
import agg.samples.domain.Message;
import agg.samples.domain.MessageAcknowledgement;
import agg.samples.feign.RemoteServiceClient;
import com.netflix.hystrix.HystrixCommand;
import com.netflix.hystrix.HystrixCommandGroupKey;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;public class RemoteMessageClientCommand extends HystrixCommand<MessageAcknowledgement> {private static final String COMMAND_GROUP = "demo";private static final Logger logger = LoggerFactory.getLogger(RemoteMessageClientCommand.class);private final RemoteServiceClient remoteServiceClient;private final Message message;public RemoteMessageClientCommand(RemoteServiceClient remoteServiceClient, Message message) {super(HystrixCommandGroupKey.Factory.asKey(COMMAND_GROUP));this.remoteServiceClient = remoteServiceClient;this.message = message;}@Overrideprotected MessageAcknowledgement run() throws Exception {logger.info("About to make Remote Call");return this.remoteServiceClient.sendMessage(this.message);}@Overrideprotected MessageAcknowledgement getFallback() {return new MessageAcknowledgement(message.getId(), message.getPayload(), "Fallback message");}
}
这里没有与Spring相关的类,此命令可以直接在基于Spring的项目中使用,例如在控制器中以下列方式使用:
@RestController
public class RemoteCallDirectCommandController {@Autowiredprivate RemoteServiceClient remoteServiceClient;@RequestMapping("/messageDirectCommand")public MessageAcknowledgement sendMessage(Message message) {RemoteMessageClientCommand remoteCallCommand = new RemoteMessageClientCommand(remoteServiceClient, message);return remoteCallCommand.execute();}
}
Hystrix命令的行为自定义通常是通过NetflixOSS Archaius属性执行的,但是Spring Cloud提供了一个桥梁,使Spring定义的属性显示为Archaius属性,这简而言之意味着我可以使用Spring特定的配置文件来定义我的属性,自定义命令行为时将可见。
因此,如果较早进行自定义,则可以使用Archaius属性来表示HelloWorldCommand的行为,如下所示:
hystrix.command.HelloWorldCommand.metrics.rollingStats.timeInMilliseconds=10000
hystrix.command.HelloWorldCommand.execution.isolation.strategy=THREAD
hystrix.command.HelloWorldCommand.execution.isolation.thread.timeoutInMilliseconds=1000
hystrix.command.HelloWorldCommand.circuitBreaker.errorThresholdPercentage=50
hystrix.command.HelloWorldCommand.circuitBreaker.requestVolumeThreshold=20
hystrix.command.HelloWorldCommand.circuitBreaker.sleepWindowInMilliseconds=5000
这可以在Spring Cloud世界中以完全相同的方式在application.properties文件或application.yml文件中通过以下方式完成:
hystrix:command:HelloWorldCommand:metrics:rollingStats:timeInMilliseconds: 10000execution:isolation:strategy: THREADthread:timeoutInMilliseconds: 5000circuitBreaker:errorThresholdPercentage: 50requestVolumeThreshold: 20sleepWindowInMilliseconds: 5000
基于注释的方法
我个人更喜欢基于直接命令的方法,但是在Spring世界中使用Hystrix的更好方法可能是使用基于hystrix-javanica的注释。 最好通过一个示例说明如何使用此注释。 这是包装在Hystrix命令中并带有注释的远程调用:
import agg.samples.domain.Message;
import agg.samples.domain.MessageAcknowledgement;
import agg.samples.feign.RemoteServiceClient;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;@Service
public class RemoteMessageAnnotationClient {private final RemoteServiceClient remoteServiceClient;@Autowiredpublic RemoteMessageAnnotationClient(RemoteServiceClient remoteServiceClient) {this.remoteServiceClient = remoteServiceClient;}@HystrixCommand(fallbackMethod = "defaultMessage", commandKey = "RemoteMessageAnnotationClient" )public MessageAcknowledgement sendMessage(Message message) {return this.remoteServiceClient.sendMessage(message);}public MessageAcknowledgement defaultMessage(Message message) {return new MessageAcknowledgement("-1", message.getPayload(), "Fallback Payload");}}
这些注解使用方面转换为幕后的常规Hystrix命令,但很妙的是,在Spring Cloud项目中没有使用该注解的仪式,它只是工作而已。 与以前一样,如果需要自定义行为,则可以使用命令特定的属性来完成。 一个小问题是默认情况下命令名称是方法名称,因此在我的示例中,命令名称将是“ sendMessage”,我已使用注释将其自定义为其他名称。
- 如果您有兴趣进一步探索该示例,请参阅我的github项目 。
翻译自: https://www.javacodegeeks.com/2015/11/spring-cloud-support-hystrix.html
Spring Cloud对Hystrix的支持相关推荐
- Spring Cloud中Hystrix、Ribbon及Feign的熔断关系是什么?
导读 今天和大家聊一聊在Spring Cloud微服务框架实践中,比较核心但是又很容易把人搞得稀里糊涂的一个问题,那就是在Spring Cloud中Hystrix.Ribbon以及Feign它们三者之 ...
- Spring Cloud Netfix Hystrix(断路器)
一.灾难性雪崩 造成灾难性雪崩效应的原因,可以简单归结为下述三种: 服务提供者(Application Service)不可用.如:硬件故障.程序BUG.缓存击穿.并发请求量过大等. 重试加大流量.如 ...
- Spring Cloud 之 Hystrix
Spring Cloud 之 Hystrix 1. Feign+Hystrix实现RPC调用保护 2. Spring Cloud Hystrix失败回退 3. Spring Cloud Hystrix ...
- Spring Cloud中Hystrix仪表盘与Turbine集群监控
Hystrix仪表盘,就像汽车的仪表盘实时显示汽车的各项数据一样,Hystrix仪表盘主要用来监控Hystrix的实时运行状态,通过它我们可以看到Hystrix的各项指标信息,从而快速发现系统中存在的 ...
- Spring Cloud中Hystrix 线程隔离导致ThreadLocal数据丢失(续)
前言 上篇文章<Spring Cloud中Hystrix 线程隔离导致ThreadLocal数据丢失>我们对ThreadLocal数据丢失进行了详细的分析,并通过代码的方式复现了这个问题. ...
- k8s springboot 文件_Springboot整合Spring Cloud Kubernetes读取ConfigMap支持自动刷新配置的教程...
1 前言 欢迎访问南瓜慢说 www.pkslow.com获取更多精彩文章! Docker & Kubernetes相关文章:容器技术 之前介绍了Spring Cloud Config的用法,但 ...
- Spring Cloud之Hystrix服务容错
Spring Cloud之Hystrix服务容错 Hystrix的概述 Hystrix的使用 相关依赖 Eureka注册中心 服务提供者 服务消费者 执行测试 @HystrixCommand详解 服务 ...
- Spring Cloud中Hystrix的请求合并
在微服务架构中,我们将一个项目拆分成很多个独立的模块,这些独立的模块通过远程调用来互相配合工作,但是,在高并发情况下,通信次数的增加会导致总的通信时间增加,同时,线程池的资源也是有限的,高并发环境会导 ...
- Spring Cloud中Hystrix仪表盘与Turbine集群监控 1
Hystrix仪表盘,就像汽车的仪表盘实时显示汽车的各项数据一样,Hystrix仪表盘主要用来监控Hystrix的实时运行状态,通过它我们可以看到Hystrix的各项指标信息,从而快速发现系统中存在的 ...
最新文章
- 隐藏在Win XP中的28个秘密武器
- zookeeper安装教程(zookeeper3.4.5为例)
- code first基础
- 七牛云上传的图片进行删除
- VTK:隐式二次元用法实战
- C++相关:部分标准库特殊设施
- Oracle 中伪数列ROWID
- 学习Scala: 初学者应该了解的知识
- centos 7.4 + postgresql 10.1 + pg_amqp
- HTTPConnectionPool(host:XX)Max retries exceeded with url 解决方法
- java实现英文文件单词搜索系统_java对于目录下文件的单词查找操作代码实现
- 40个经典单片机实验_太经典啦!单片机常用的14个C语言算法(附详细代码)
- 双系统linux安装cetos,电脑安装windows和centOS系统双系统
- 红巨星转场特效预设AE插件 Red Giant Universe 6.0.1 WIN
- 惠普打印机双击之后没有扫描_惠普打印机 找不到 扫描图标 怎么办,急需扫描一些证件 ,求救...
- html文字发亮_CSS3字体发光效果
- 常见的笔记本电池使用技巧
- 方舟Mod:任何颜色集
- BZOJ1057: [ZJOI2007]棋盘制作
- PostgresConf.CNPGConf.Asia 2020五洲相会
热门文章
- Util包中Arrays
- (一)Web Service——基本概念
- 2020蓝桥杯省赛---java---A---2(既分数组)
- 9.1-微操作命令的分析(学习笔记)
- 自底向上伸展树(之字形旋转+一字形旋转)
- unsafe jdk9_JDK 9清单:Project Jigsaw,sun.misc.Unsafe,G1,REPL等
- jooq_jOOQ星期二:拉斐尔·温特豪德(Rafael Winterhalter)正在与字节好友合作字节码...
- java处理注释_如何处理Java注释
- aem 渲染_AEM中的单元测试(大声思考)
- javaserver_如何在JavaServer Pages中使用Salesforce REST API