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 系列之服务降级的更多相关文章

  1. SpringCloud 2020.0.4 系列之服务降级的其他用法与熔断

    1. 概述 老话说的好:控制好自己的情绪,才能控制好自己的人生.冲动是魔鬼,冷静才最重要. 言归正传,之前聊了在 Feign 调用时,如何给整个 Feign接口类 增加降级策略. 今天我们来聊一下 H ...

  2. SpringCloud 2020.0.4 系列之 Feign

    1. 概述 老话说的好:任何问题都有不止一种的解决方法,当前的问题没有解决,只是还没有发现解决方法,而并不是无解. 言归正传,之前我们聊了 SpringCloud 的服务治理组件 Eureka,今天我 ...

  3. SpringCloud 2020.0.4 系列之 Stream 消息广播 与 消息分组 的实现

    1. 概述 老话说的好:事情太多,做不过来,就先把事情记在本子上,然后理清思路.排好优先级,一件一件的去完成. 言归正传,今天我们来聊一下 SpringCloud 的 Stream 组件,Spring ...

  4. SpringCloud 2020.0.4 系列之 Stream 延迟消息 的实现

    1. 概述 老话说的好:对待工作要有责任心,不仅要完成自己的部分,还要定期了解整体的进展. 言归正传,我们在开发产品时,常常会遇到一段时间后检查状态的场景,例如:用户下单场景,如果订单生成30分钟后, ...

  5. SpringCloud 2020.0.4 系列之 Stream 消息出错重试 与 死信队列 的实现

    1. 概述 老话说的好:出错不怕,怕的是出了错,却不去改正.如果屡次出错,无法改对,就先记下了,然后找援军解决. 言归正传,今天来聊一下 Stream 组件的 出错重试 和 死信队列. RabbitM ...

  6. SpringCloud 2020.0.4 系列之Eureka

    1. 概述 老话说的好:遇见困难,首先要做的是积极的想解决办法,而不是先去泄气.抱怨或生气. 言归正传,微服务是当今非常流行的一种架构方式,其中 SpringCloud 是我们常用的一种微服务框架. ...

  7. SpringCloud 2020.0.4 系列之 Bus

    1. 概述 老话说的好:会休息的人才更会工作,身体是革命的本钱,身体垮了,就无法再工作了. 言归正传,之前我们聊了 SpringCloud 的 分布式配置中心 Config,文章里我们聊了config ...

  8. SpringCloud 2020.0.4 系列之 Gateway入门

    1. 概述 老话说的好:做人要有幽默感,懂得幽默的人才会活的更开心. 言归正传,今天我们来聊聊 SpringCloud 的网关组件 Gateway,之前我们去访问 SpringCloud 不同服务的接 ...

  9. SpringCloud 2020.0.4 系列之Hystrix看板

    1. 概述 老话说的好:沉默是金,有时适当的沉默,比滔滔不绝更加有效. 言归正传,前面我们聊了有关 Hystrix 降级熔断的话题,今天我们来聊聊如何使用 turbine 和 hystrix dash ...

随机推荐

  1. SQL-DELETE触发器练习

    &练习一 如下所示三张表( student,grade,student_updata_before ): student表 grade表 Student_update_before表 # 触发 ...

  2. JS003. 事件监听和监听滚动条的三种参数( addEventListener( ) )

    全局 1 window.addEventListener('scroll', () => { 2 console.log('------') 3 console.log(document.doc ...

  3. MySQL实战45讲(01--05)-笔记

    目录 MySQL复习 01 | 基础架构:一条SQL查询语句是如何执行的? 连接器 查询缓存 分析器 优化器 执行器 02 | 日志系统:一条SQL更新语句是如何执行的? 重要的日志模块:redo l ...

  4. WPF 第三方资源

    1.XCeed 开发的Extended WPF Toolkit http://wpftoolkit.codeplex.com/ http://www.csdn123.com/html/blogs/20 ...

  5. Windows安装Docker & Docker-Compose & 配置docker私有仓库

    一定要给windows先创建软连接,不然系统盘会爆表的: mklink /j .docker D:\Administrator\.docker Win7安装Docker Dockerfile # FR ...

  6. Catch That Cow----BFS

    Catch That Cow Description 农夫知道一头牛的位置,想要抓住它.农夫和牛都位于数轴上 ,农夫起始位于点 N(0<=N<=100000) ,牛位于点 K(0<= ...

  7. mysql操作中卡死 解决方法

    1.使用指令查询当前进程 show full processlist; -- 查询全部当前进程; show processlist;-- 只列出前100条 2.找出卡死的进程id 3.删除卡死进程 k ...

  8. 大前端快闪二:react开发模式 一键启动多个服务

    最近全权负责了一个前后端分离的web项目,前端使用create-react-app, 后端使用golang做的api服务. npx create-react-app my-app cd my-app ...

  9. Unity3D组成

    从宏观的角度来看,分为七个模块: 1.图形模块(Graphics). 2.物理模块(Physics) 3. 音效模块(Audio) 4.动作模块(Animation) 5.导航模块(Navigatio ...

  10. Go语言核心36讲(Go语言基础知识一)--学习笔记

    01 | 工作区和GOPATH 从 Go 1.5 版本的自举(即用 Go 语言编写程序来实现 Go 语言自身),到 Go 1.7 版本的极速 GC(也称垃圾回收器),再到 2018 年 2 月发布的 ...