当前位置:网站首页>Hystrix 部署
Hystrix 部署
2022-06-28 10:13:00 【可以吧可以吧】
参考
Java版本实现: github地址
PHP版本实现: github地址
实现
Hystrix支持两种方式实现熔断降级功能:
编程式
声明式
编程式
在编程式实现中,基于Hystrix丰富的API,通过编码的方式实现断路器的设置。API要求用户将不同的外部交互封装成命令模式对象。在调用过程中,形成一层代理,Hystrix框架对命令对象执行周期接管,并根据窗口内获取到的状态报告决定断路器状态。
添加核心依赖 hystrix-core 到pom.xml文件
<dependency>
<groupId>com.netflix.hystrix</groupId>
<artifactId>hystrix-core</artifactId>
<version>1.5.18</version>
</dependency>
包裹需要熔断处理的代码逻辑到Command类中
static class CommandHello extends HystrixCommand<String> {
private final String name;
public CommandHello(String name) {
super(Setter.withGroupKey(HystrixCommandGroupKey.Factory.asKey("ExampleGroup"))
.andCommandPropertiesDefaults(HystrixCommandProperties.Setter().withExecutionTimeoutInMilliseconds(3000)));
this.name = name;
}
@Override
protected String runTest() {
return "Hello " + name + "!";
}
// Default fallback method
public String getFallback() {
return "Fallback of Hello was triggered";
}
}
初始化Command对象,并执行 execute() 方法。
声明式
顾名思义,通过配置声明的方式以达到以上方式同样的效果,这种方式对业务侵入小,无需编码自动植入代码功能,业务接受程度高。此方案需要引入依赖,并在main class中使用注释 EnableHystrix 激活该功能。
添加核心依赖 hystrix-javanica 到pom.xml文件
<dependency>
<groupId>com.netflix.hystrix</groupId>
<artifactId>hystrix-javanica</artifactId>
<version>x.y.z</version>
</dependency>
使用@HystrixCommand修饰被监控方法,并提供兜底逻辑:
在环境中引入 HystrixCommandAspect 注入切面,拦截所有带有HystrixCommand注释的方法,并生成其代理对象。业务调用无需调整,我
spring-cloud-netflix-hystrix 依赖于 hystrix-javanica ,使能Hystrix开箱即用。自动配置代码见
@HystrixCommand(fallbackMethod = "fallback_hello", commandProperties = {
@HystrixProperty(name = "execution.isolation.thread.timeoutInMilliseconds", value = "1000")})
public String hello() throws InterruptedException {
System.out.println("Hello");
Thread.sleep(3000);
return "Welcome Hystrix";
}
private String fallback_hello() {
return "Request fails. It takes long time to response";
}
Metrics桥接
在Hystrix设计支持以plugin的方式桥接metrics到外部系统,该插件要求继承 com.netflix.hystrix.strategy.metrics.HystrixMetricsPublisher 。
桥接后的metrics可统一导出到actuator端点,随后被Prometheus拉取到。
边栏推荐
- 增强 Jupyter Notebook 的功能,这里有四个妙招
- appliedzkp zkevm(10)中的Transactions Proof
- appliedzkp zkevm(9)中的Bytecode Proof
- Ideal interface automation project
- BLE蓝牙模块NRF518/NRF281/NRF528/NRF284芯片方案对比
- Looking at jBPM from jbm3 to jbm5 and activiti
- Generate token
- Read PDF Text and write excel operation
- To enhance the function of jupyter notebook, here are four tips
- Sword finger offer | Fibonacci sequence
猜你喜欢
[200 opencv routines] 213 Draw circle
MySQL cannot be opened. Flash back
纵观jBPM从jBPM3到jBPM5以及Activiti
What is the best way to learn machine learning
Interface automation framework scaffold - use reflection mechanism to realize the unified initiator of the interface
再见!IE浏览器,这条路由Edge替IE继续走下去
linux中源码安装mysql数据库(centos)
Proxy mode (proxy)
【OpenCV 例程200篇】213. 绘制圆形
一文读懂 12种卷积方法(含1x1卷积、转置卷积和深度可分离卷积等)
随机推荐
接口自动化框架脚手架-利用反射机制实现接口统一发起端
Read PDF Text and write excel operation
mysql打不开,闪退
老板叫我写个APP自动化--Yaml文件读取--内附整个框架源码
The R language uses the avplots function in the car package to create added variable plots. In image interaction, manually identify (add) strong influence points that have a great impact on each predi
Cisco * VRF (virtual route forwarding table)
Must the MySQL table have a primary key for incremental snapshots?
第五章 树和二叉树
I'm almost addicted to it. I can't sleep! Let a bug fuck me twice!
Dotnet uses crossgen2 to readytorun DLL to improve startup performance
Decorator
Starting from full power to accelerate brand renewal, Chang'an electric and electrification products sound the "assembly number"
bad zipfile offset (local header sig)
理想中的接口自动化项目
关于FTP的协议了解
Realize an air conditioner with compose to bring you cool in summer
Understand 12 convolution methods (including 1x1 convolution, transpose convolution and deep separable convolution)
[unity][ecs] learning notes (II)
QT signal and slot communication mechanism (when multiple windows communicate back and forth [parent and child windows])
Ffmpeg audio and video recording