当前位置:网站首页>Hystri基本介绍和代码简单实现
Hystri基本介绍和代码简单实现
2022-07-29 15:12:00 【great-sun】
Hystrix
hystrix是一个容错组件,实现了超时机制和断路器模式。
hystrix提供了熔断和降级。
降级
降级其实就相当于,当我们向一个服务发起请求,当请求超时了,就会把这次请求记录到服务中,然后就会尝试向其他服务发请求,如果还没成功,就对这次请求进行处理(怎么处理取决于业务需求如)就相当于try catch一样的逻辑,当然hystrix底层使用aop来实现的。
熔断
熔断就是有一个阈值,向服务发起请求后,如果不成功,就会记录次数,然后当连续失败次数达到阈值时,下次请求的时候就会直接把这个服务停止。请求有三种状态,可以请求(开),不可请求(关),还有一个中间状态,相当于半开状态,半开状态是什么意思呢,就是可以尝试着去请求,就可以在关闭状态后一段时间,发一个请求尝试一下是否可以请求成功,如果是吧,继续保持关闭状态,如果请求成功,则变成开放状态。
隔离
每当向服务发起一个请求时,就是会发起一个http请求,每一个http请求就要开启一个线程,然后等待服务返回信息,这容易导致线程的堆积,所以就可以用http的URI作为一个标识,然后相同的URI可以开启一个线程池,然后线程池中限定线程数,这样就可以设置拒绝策略,当线程池满了,就可以快速的抛出异常或者拒绝请求,用线程池做到线程隔离来达到限流。
引入包
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-hystrix</artifactId>
</dependency>
<dependency>
<groupId>cn.hutool</groupId>
<artifactId>hutool-all</artifactId>
</dependency>
启动类加注解
@EnableHystrix
写一个测试实例,局部配置就在当前类上加上注解,全局配置就是在yml配置文件里面写配置
package com.example;
import cn.hutool.core.thread.ThreadUtil;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixProperty;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class DemoConroller {
@GetMapping("/test")
/*局部配置*/
/* @HystrixCommand(fallbackMethod = "testFallback",commandProperties={ //设置超时时间 @HystrixProperty(name = "execution.isolation.thread.timeoutInMilliseconds",value = "3000"), //是否开启断路器 @HystrixProperty(name = "circuitBreaker.enabled",value = "true"), //统计的时间窗口,默认为10s,一般不需要更改 @HystrixProperty(name = "metrics.rollingStats.timeInMilliseconds",value = "30000"), //请求最小触发次数 @HystrixProperty(name = "circuitBreaker.requestVolumeThreshold",value = "3"), //失败率达到多少后跳闸,在统计窗口期中,请求数大于阈值并且失败率达到50,则触发断路,断路器开启,链路中断 @HystrixProperty(name = "circuitBreaker.errorThresholdPercentage",value = "50"), //断路后休眠状态的时长,10s,默认为5s,断路10s后断路器进入半开状态 @HystrixProperty(name = "circuitBreaker.sleepWindowInMilliseconds",value = "80000"), })*/
@HystrixCommand(fallbackMethod = "testFallback")
/*test和testFallback入参和返回类型必须保持一致*/
public String test(@RequestParam Integer sleepTime){
System.out.println("进入正常方法1");
ThreadUtil.safeSleep(sleepTime);
System.out.println("进入正常方法2");
return "这是正常结果";
}
public String testFallback(@RequestParam Integer sleepTime){
System.out.println("进入兜底方法");
return "这是兜底";
}
}
application.yml全局配置
#全局配置
hystrix:
command:
default:
metrics:
rollingStats:
timeInMilliseconds: 30000 #统计的时间窗口,默认为10s,一般不需要更改
fallback:
enabled: true
circuitBreaker:
enabled: true #是否开启断路器
errorThresholdPercentage: 50 #失败率达到多少后跳闸,在统计窗口期中,请求数大于阈值并且失败率达到60,则触发断路,断路器开启,链路中断
requestVolumeThreshold: 3 #请求最小触发次数
sleepWindowInMilliseconds: 80000 #断路后休眠状态的时长,10s,默认为5s,断路10s后断路器进入半开状态
execution:
isolation:
thread:
interruptOnFutureCancel: true #取消是否中断
interruptOnTimeout: true #超时是否中断
timeoutInMilliseconds: 3000 #超时阈值,单位是毫秒
timeout:
enabled: true
边栏推荐
猜你喜欢

蚂蚁三面滑铁卢!遭分布式截胡,靠这些笔记潜修 30 天,挺进京东

@RequestMapping注解最详细解析

VMware 16.1软件安装包下载及安装教程

Mysql数据库及表的建立

从通信延伸到全行业,亚信科技AntDB 7.0蓄势待发

AOP implementation enterprise API access interface monitoring (via Google Guava cache data)

ES6 from entry to master # 11: the Map data type

BGP federation experiment

CSP:重庆八中宏帆初级中学校新初二编程社C2024liuyanjia暑假一期集训总结(2/6))

Unable to open the source file in qt vs2015 "QtWidgets" solution
随机推荐
DevOps的未来趋势
数据分析(二)
小米法务部:成功打击一处伪造Redmi蓝牙耳机窝点,查扣假冒伪劣产品2032个
I/O代码实践
Micro combat | centralized configuration service center Config asymmetric encryption and security management
bit field in c language
The future trend of the conversation
Floating point memory storage problem
日志的概念
走高质量、可持续的保障亿万家庭之路 水滴公司发布2020·2021ESG报告
Qt学习第一天
一文详解8086微处理器系统结构
UFLO:5、启动任务并自动完成第一个人工任务
Mysql数据库及表的建立
如何在CentOS 8上安装PHP
MySql数据库命令大全:数据库操作命令,表操作命令,修改表结构命令,数据操作命令,数据查询操作命令
PHP 读取/导出 CSV文件
【 LeetCode 】 566. Reshape the matrix
观光公交题解
论人生自动化