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 ...
随机推荐
- openswan协商流程之(三):main_inR1_outI2
主模式第三包:main_inR1_outI2 1. 序言 main_inR1_outI2()函数是ISAKMP协商过程中第三包的核心处理函数的入口.这里我们主要说明main_inR1_outI2的函数 ...
- js 显示日期时间,时间过一秒加1
html: <div id="data"><font>2017年10月17日 15:11:11</font></span> js: ...
- C# .NET Core 3.1 中 AssemblyLoadContext 的基本使用
C# .NET Core 3.1 中 AssemblyLoadContext 的基本使用 前言 之前使用 AppDomain 写过一个动态加载和释放程序的案例,基本实现了自己"兔死狗烹&qu ...
- request,response统一编码
方法:统一使用编码(例如UTF-8编码)解决session或jsp等各种值传递时的中文乱码问题 request.setCharacterEncoding("UTF-8"); res ...
- java循环结构、数组
数组 数组是是多个相同类型数据按一定顺序排列的集合,并使用一个名字命名,并通过编号的方式对这些数据进行统一管理. 数组本身是引用数据类型,既可以存储基本数据类型,也可以存储引用数据类型.它的元素相当于 ...
- Jmeter扩展组件开发(8) - 函数助手扩展开发demo
前提条件 1.pom文件引用ApacheJMeter_functions包 <dependency> <groupId>org.apache.jmeter</groupI ...
- fibnacci数列
斐波那契数列(Fibonacci sequence),又称黄金分割数列.因数学家列昂纳多·斐波那契(Leonardoda Fibonacci)以兔子繁殖为例子而引入,故又称为"兔子数列&qu ...
- MSSQL数据库安全实验
管理SQL Server认证模式 (1)确认 SQL Server 验证 ①在桌面上单击"开始",选择"程序"→"Microsoft SQL Serv ...
- Python return self
在Python中,return self的作用为: Returning self from a method simply means that your method returns a refer ...
- python二级 第9套
1. prInt.默认输出空格 2. 我的这种想法也行不通啊 format() 一次只能有一个未知量 2. 分割的结果就是列表 3. 对比"大学" 上一套的split('&q ...