当前位置:网站首页>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拉取到。
边栏推荐
- Cisco * VRF(虚拟路由转发表)
- Unity AssetBundle资源打包与资源加载
- mysql打不开,闪退
- ruoyi集成积木报表(nice)
- appliedzkp zkevm(10)中的Transactions Proof
- Understand 12 convolution methods (including 1x1 convolution, transpose convolution and deep separable convolution)
- Chapter 5 trees and binary trees
- Summary of MySQL basic knowledge points
- 六月集训(第28天) —— 动态规划
- Must the MySQL table have a primary key for incremental snapshots?
猜你喜欢

Starting from full power to accelerate brand renewal, Chang'an electric and electrification products sound the "assembly number"

Dbeaver installation and use tutorial (super detailed installation and use tutorial)

为什么 Istio 要使用 SPIRE 做身份认证?

Decorator

PyGame game: "Changsha version" millionaire started, dare you ask? (multiple game source codes attached)

Several methods of using ABAP to operate Excel

etf持仓如何影响现货金价?

Application of X6 in data stack index management

手把手教你处理 JS 逆向之 SVG 映射

Realize an air conditioner with compose to bring you cool in summer
随机推荐
Generate token
MySQL基础知识点总结
Interface automation framework scaffolding - Implementation of parametric tools
Missed the golden three silver four, found a job for 4 months, interviewed 15 companies, and finally got 3 offers, ranking P7+
为什么 Istio 要使用 SPIRE 做身份认证?
第六天 脚本与动画系统
Chapter 3 stack and queue
etf持仓如何影响现货金价?
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
我大抵是卷上瘾了,横竖睡不着!竟让一个Bug,搞我两次!
接口自动化框架脚手架-参数化工具的实现
Summary of MySQL basic knowledge points
[Unity][ECS]学习笔记(三)
==And eqauls()
Several methods of using ABAP to operate Excel
广州海关支持保障食品、农产品和中药材等民生物资稳定供港
Cisco * VRF(虚拟路由转发表)
Crawler small operation
2D code generator for openharmony application development
idea连接sql sever失败