SpringCloud 2020.0.4 系列之服务降级
1. 概述
老话说的好:做人要正直,做事要正派,胸怀坦荡、光明磊落,才会赢得他人的信赖与尊敬。
言归正传,之前聊了服务间通信的组件 Feign,今天我们来聊聊服务降级。
服务降级简单的理解就是给一个备选方案,当服务调用报错或者超时时,能终止远程调用,并很快的返回备选的结果,避免引发服务雪崩。
今天我们用两个例子,模拟一下 接口报错 和 接口超时 的服务降级实现。
我们使用 hystrix 实现服务降级,虽然从 Spring Cloud 2020.0 版本开始,移除了 hystrix 组件,但并不影响我们对他的使用。
闲话不多说,直接上代码。
2. 接口报错的服务降级
2.1 被调用服务
2.1.1 接口代码
@GetMapping("/exception")
String exception() throws Exception;
2.1.2 实现类代码
@Override
public String exception() throws Exception {
if(1==1) {
throw new Exception("模拟异常");
}
return null;
}
2.2 调用服务
2.2.1 主要依赖
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!-- 健康检查 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-openfeign</artifactId>
</dependency> <dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-hystrix</artifactId>
<version>2.2.9.RELEASE</version>
</dependency>
这里 hystrix 使用目前的最新版本 2.2.9.RELEASE。
2.2.2 主要配置
feign:
circuitbreaker:
enabled: true # 开启 feign 的断路器 hystrix:
command:
default:
fallback:
enabled: true # 开启服务降级
2.2.3 降级工厂类的开发
@Component
public class MyEurekaClientServiceFactory implements FallbackFactory<EurekaClientService> {
@Override
public EurekaClientService create(Throwable cause) { return new EurekaClientService() { @Override
public String exception() throws Exception {
System.out.println("exception方法 降级");
return "exception方法 降级";
}
};
}
}
2.2.4 在 Feign 接口配置降级工厂类
@FeignClient(name = "my-eureka-client", fallbackFactory = MyEurekaClientServiceFactory.class) // name为 调用服务的名称
@RequestMapping("/api")
public interface EurekaClientService { @GetMapping("/exception")
String exception() throws Exception; }
2.2.5 启动类增加 @EnableHystrix 注解
@SpringBootApplication
@EnableDiscoveryClient
@EnableFeignClients
@EnableHystrix
public class MyFeignApplication { public static void main(String[] args) {
SpringApplication.run(MyFeignApplication.class, args);
}
}
2.2.6 启动服务测试
可以正常进入降级方法。
3. 接口超时的服务降级
3.1 被调用服务
3.1.1 接口代码
@GetMapping("/timeout")
String timeout(@RequestParam Integer timeoutSecond);
3.1.2 实现类代码
@Override
public String timeout(Integer timeoutSecond) {
System.out.println("调用timeout方法");
try {
Thread.sleep(timeoutSecond * 1000L);
} catch (InterruptedException e) {
e.printStackTrace();
}
return "success:" + port;
}
3.2 调用服务
3.2.1 主要配置
feign:
client:
config:
# 全局配置
default:
connectTimeout: 1000 # 连接超时时间,单位ms
readTimeout: 3000 # 获取Response响应超时时间,单位ms # 针对 my-eureka-client 的 feign 配置,优先级高于全局配置
my-eureka-client:
connectTimeout: 300 # 连接超时时间,单位ms
readTimeout: 5000 # 获取Response响应超时时间,单位ms
circuitbreaker:
enabled: true # 开启 feign 的断路器 hystrix:
command:
default:
fallback:
enabled: true # 开启服务降级
execution:
timeout:
enabled: true # 全局超时配置
isolation:
thread:
timeoutInMilliseconds: 3000 # 超时时间配置
interruptOnTimeout: true # 超时后是否终止线程
interruptOnFutureCancel: true # 取消后是否终止线程
注意:feign 的 readTimeout 属性和 hystrix 的 timeoutInMilliseconds 属性会同时生效,以超时时间最短的为准。
3.2.2 降级工厂类的开发
@Component
public class MyEurekaClientServiceFactory implements FallbackFactory<EurekaClientService> {
@Override
public EurekaClientService create(Throwable cause) { return new EurekaClientService() { @Override
public String exception() throws Exception {
System.out.println("exception方法 降级");
return "exception方法 降级";
} @Override
public String timeout(Integer timeoutSecond) {
return "timeout方法 降级";
}
};
}
}
3.2.3 启动服务测试
可以正常进入降级方法。
4. 综述
今天聊了一下 服务降级 的相关知识,希望可以对大家的工作有所帮助。
欢迎帮忙点赞、评论、转发、加关注 :)
关注追风人聊Java,每天更新Java干货。
5. 个人公众号
追风人聊Java,欢迎大家关注
SpringCloud 2020.0.4 系列之服务降级的更多相关文章
- SpringCloud 2020.0.4 系列之服务降级的其他用法与熔断
1. 概述 老话说的好:控制好自己的情绪,才能控制好自己的人生.冲动是魔鬼,冷静才最重要. 言归正传,之前聊了在 Feign 调用时,如何给整个 Feign接口类 增加降级策略. 今天我们来聊一下 H ...
- SpringCloud 2020.0.4 系列之 Feign
1. 概述 老话说的好:任何问题都有不止一种的解决方法,当前的问题没有解决,只是还没有发现解决方法,而并不是无解. 言归正传,之前我们聊了 SpringCloud 的服务治理组件 Eureka,今天我 ...
- SpringCloud 2020.0.4 系列之 Stream 消息广播 与 消息分组 的实现
1. 概述 老话说的好:事情太多,做不过来,就先把事情记在本子上,然后理清思路.排好优先级,一件一件的去完成. 言归正传,今天我们来聊一下 SpringCloud 的 Stream 组件,Spring ...
- SpringCloud 2020.0.4 系列之 Stream 延迟消息 的实现
1. 概述 老话说的好:对待工作要有责任心,不仅要完成自己的部分,还要定期了解整体的进展. 言归正传,我们在开发产品时,常常会遇到一段时间后检查状态的场景,例如:用户下单场景,如果订单生成30分钟后, ...
- SpringCloud 2020.0.4 系列之 Stream 消息出错重试 与 死信队列 的实现
1. 概述 老话说的好:出错不怕,怕的是出了错,却不去改正.如果屡次出错,无法改对,就先记下了,然后找援军解决. 言归正传,今天来聊一下 Stream 组件的 出错重试 和 死信队列. RabbitM ...
- SpringCloud 2020.0.4 系列之Eureka
1. 概述 老话说的好:遇见困难,首先要做的是积极的想解决办法,而不是先去泄气.抱怨或生气. 言归正传,微服务是当今非常流行的一种架构方式,其中 SpringCloud 是我们常用的一种微服务框架. ...
- SpringCloud 2020.0.4 系列之 Bus
1. 概述 老话说的好:会休息的人才更会工作,身体是革命的本钱,身体垮了,就无法再工作了. 言归正传,之前我们聊了 SpringCloud 的 分布式配置中心 Config,文章里我们聊了config ...
- SpringCloud 2020.0.4 系列之 Gateway入门
1. 概述 老话说的好:做人要有幽默感,懂得幽默的人才会活的更开心. 言归正传,今天我们来聊聊 SpringCloud 的网关组件 Gateway,之前我们去访问 SpringCloud 不同服务的接 ...
- SpringCloud 2020.0.4 系列之Hystrix看板
1. 概述 老话说的好:沉默是金,有时适当的沉默,比滔滔不绝更加有效. 言归正传,前面我们聊了有关 Hystrix 降级熔断的话题,今天我们来聊聊如何使用 turbine 和 hystrix dash ...
随机推荐
- python轻量级orm框架 peewee常用功能速查
peewee常用功能速查 peewee 简介 Peewee是一种简单而小的ORM.它有很少的(但富有表现力的)概念,使它易于学习和直观的使用. 常见orm数据库框架 Django ORM peewee ...
- SQL-DELETE触发器练习
&练习一 如下所示三张表( student,grade,student_updata_before ): student表 grade表 Student_update_before表 # 触发 ...
- JS003. 事件监听和监听滚动条的三种参数( addEventListener( ) )
全局 1 window.addEventListener('scroll', () => { 2 console.log('------') 3 console.log(document.doc ...
- 一文搞懂如何使用Node.js进行TCP网络通信
摘要: 网络是通信互联的基础,Node.js提供了net.http.dgram等模块,分别用来实现TCP.HTTP.UDP的通信,本文主要对使用Node.js的TCP通信部份进行实践记录. 本文分享自 ...
- 01_初识C语言
第一章 - 初识C语言 基本了解C语言的基础知识,对C语言有一个大概的认识. 每个知识点就是简单认识,不做详细讲解. 1. 什么是C语言? C语言是一门通用计算机编程语言,广泛应用于底层开发.C语言的 ...
- 关于python中一切皆对象和深浅拷贝
- Nginx系列(3)- 负载均衡
负载均衡 Nginx提供的负载均衡策略有两种: 内置策略为轮询.加权轮询.ip hash 扩展策略,就天马行空了,只有你想不到的没有它做不到的 轮询 加权轮询(根据权重来) iphash对客户端请求 ...
- GoLang设计模式07 - 责任链模式
责任链模式是一种行为型设计模式.在这种模式中,会为请求创建一条由多个Handler组成的链路.每一个进入的请求,都会经过这条链路.这条链路上的Handler可以选择如下操作: 处理请求或跳过处理 决定 ...
- Matlab使用随记
Matlab 2020 想要看图像每一点的值大小 工具--->数据提示 想要导出的分辨率提高 导出设置--->渲染--->600dpi Matlab 2017b 程序运行后,画出图, ...
- django中admin一些方法
1.概述:内容发布,负责添加,修改,删除内容及公告访问2.配置admin应用在settings文件中INSTALLED_APPS添加:'django.contrib.admin', 默认是已经添加好的 ...