当前位置:网站首页>服务熔断的实现
服务熔断的实现
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
边栏推荐
- NVM introduction, NVM download, installation and use (node version management)
- Use 3080ti to run tensorflow GPU = 1 X version of the source code
- Solve importerror: cannot import name 'imread' from 'SciPy misc‘
- Tensorflow and pytorch middle note feature map size adjustment to achieve up sampling
- Is rust more suitable for less experienced programmers?
- pytorch 1.7. The model saved by X training cannot be loaded in version 1.4 or earlier
- Leaf Smecta
- presto on spark 支持3.1.3记录
- [leetcode refers to offer 47. Maximum value of gift (medium)]
- Pytorch selects the first k maximum (minimum) values and their indexes in the data
猜你喜欢

Use 3080ti to run tensorflow GPU = 1 X version of the source code

MySQL 回表

JS merge duplicate data in array object

Deno 1.13.2 发布

DW basic tutorial (I)

Database Experiment 8 trigger experiment

Cancel the default open project setting of idea

Google tries to use rust in Chrome

NVM introduction, NVM download, installation and use (node version management)

C, print the source program of beautiful bell triangle
随机推荐
NVM introduction, NVM download, installation and use (node version management)
MySQL back to table
ERP function_ Financial management_ Basic concepts of Finance
ERP function_ Financial management_ The difference between red and blue words in invoices
Getting started with detectron2
ROS学习笔记-----ROS的使用教程
Based on jsplumb JS to achieve multi list one to many connection effect
Thinkphp5 + data large screen display effect
Database experiment VI integrity language experiment
Opencv application -- jigsaw puzzle
Alibaba cloud responded to the disclosure of user registration information
Oracle ora-01033: Oracle initialization or shutdown in progressprocess solution
ROS learning notes - tutorial on the use of ROS
Deno 1.13.2 发布
Some thoughts on super in pytorch, combined with code
C# ftpHelper
Database Experiment 5 Security Language Experiment
Strictly, severely and quickly strengthen food safety supervision during the epidemic in Shanghai
Centos7 builds MySQL master-slave replication from scratch (avoid stepping on the pit)
Database experiment I database definition and data import