当前位置:网站首页>服务熔断的实现
服务熔断的实现
2022-04-23 21:57:00 【Leon_Jinhai_Sun】
# 0.服务熔断的实现思路
- 引入hystrix依赖,并开启熔断器(断路器)
- 模拟降级方法
- 进行调用测试
# 1.项目中引入hystrix依赖
<!--引入hystrix-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-hystrix</artifactId>
</dependency>
# 2.开启断路器
@SpringBootApplication
@EnableCircuitBreaker //用来开启断路器
public class Products9998Application {
public static void main(String[] args) {
SpringApplication.run(Products9998Application.class, args);
}
}
# 3.使用HystrixCommand注解实现断路
//服务熔断
@GetMapping("/product/break")
@HystrixCommand(fallbackMethod = "testBreakFall" )
public String testBreak(int id){
log.info("接收的商品id为: "+ id);
if(id<=0){
throw new RuntimeException("数据不合法!!!");
}
return "当前接收商品id: "+id;
}
public String testBreakFall(int id){
return "当前数据不合法: "+id;
}
# 4.访问测试
- 正常参数访问
- 错误参数访问
# 5.总结
- 从上面演示过程中会发现如果触发一定条件断路器会自动打开,过了一点时间正常之后又会关闭。那么断路器打开条件是什么呢?
# 6.断路器打开条件
- 官网: https://cloud.spring.io/spring-cloud-netflix/2.2.x/reference/html/#circuit-breaker-spring-cloud-circuit-breaker-with-hystrix
A service failure in the lower level of services can cause cascading failure all the way up to the user. When calls to a particular service exceed circuitBreaker.requestVolumeThreshold
(default: 20 requests) and the failure percentage is greater than circuitBreaker.errorThresholdPercentage
(default: >50%) in a rolling window defined by metrics.rollingStats.timeInMilliseconds
(default: 10 seconds), the circuit opens and the call is not made. In cases of error and an open circuit, a fallback can be provided by the developer. --摘自官方
# 原文翻译之后,总结打开关闭的条件:
- 1、 当满足一定的阀值的时候(默认10秒内超过20个请求次数)
- 2、 当失败率达到一定的时候(默认10秒内超过50%的请求失败)
- 3、 到达以上阀值,断路器将会开启
- 4、 当开启的时候,所有请求都不会进行转发
- 5、 一段时间之后(默认是5秒),这个时候断路器是半开状态,会让其中一个请求进行转发。如果成功,断路器会关闭,若失败,继续开启。重复4和5。
# 面试重点问题: 断路器流程
# 7.默认的服务FallBack处理方法
- 如果为每一个服务方法开发一个降级,对于我们来说,可能会出现大量的代码的冗余,不利于维护,这个时候就需要加入默认服务降级处理方法
@GetMapping("/product/hystrix")
@HystrixCommand(fallbackMethod = "testHystrixFallBack") //通过HystrixCommand降级处理 指定出错的方法
public String testHystrix(String name) {
log.info("接收名称为: " + name);
int n = 1/0;
return "服务[" + port + "]响应成功,当前接收名称为:" + name;
}
//服务降级处理
public String testHystrixFallBack(String name) {
return port + "当前服务已经被降级处理!!!,接收名称为: "+name;
}
版权声明
本文为[Leon_Jinhai_Sun]所创,转载请带上原文链接,感谢
https://blog.csdn.net/leon_jinhai_sun/article/details/124359198
边栏推荐
- ROS learning notes - tutorial on the use of ROS
- DW basic course (II)
- DeNO 1.13.2 release
- Crisis is opportunity. Why will the efficiency of telecommuting improve?
- What if Jenkins forgot his password
- Question brushing plan - depth first search (II)
- Keras. Layers introduction to various layers
- Code cloud download history version process
- 2.整理华子面经--2
- Pytorch selects the first k maximum (minimum) values and their indexes in the data
猜你喜欢
Database Experiment 8 trigger experiment
2.整理华子面经--2
Database Experiment 5 Security Language Experiment
Online Excel to CSV tool
ROS学习笔记-----ROS的使用教程
[leetcode refers to offer 22. The penultimate node in the linked list (simple)]
[leetcode refers to the maximum profit of offer 63. Stock (medium)]
Tsinghua University | webface260m: benchmark for million level deep face recognition (tpami2022)
1.整理华子面经--1
ROS learning notes - tutorial on the use of ROS
随机推荐
Google tries to use rust in Chrome
2022-04-24日報:在生物科學領域應用深度學習的當前進展和開放挑戰
2022 - 04 - 24 Daily: Current Progress and Open Challenges of Applied Deep Learning in Biological Sciences
从严从重从快 上海全面加强疫情期间食品安全监管
April 24, 2022 Daily: current progress and open challenges of applying deep learning in the field of Bioscience
C# ftpHelper
2. Finishing huazi Mianjing -- 2
Solve importerror: cannot import name 'imread' from 'SciPy misc‘
Realrange, reduce, repeat and einops in einops package layers. Rearrange and reduce in torch. Processing methods of high-dimensional data
airbase 初步分析
Chrome 94 引入具有争议的 Idle Detection API,苹果和Mozilla反对
Swift import third-party library reports an error no such module““
Database Experiment 7 stored procedure experiment
Centos7 builds MySQL master-slave replication from scratch (avoid stepping on the pit)
Subcontracting of wechat applet based on uni app
Idea import a project
轻量化项目管理思路
CUDA, NVIDIA driver, cudnn download address and version correspondence
Pytorch selects the first k maximum (minimum) values and their indexes in the data
Pytorch: runtimeerror: an attempt has been made to start a new process Error reporting (resolved)