前言

本文主要研究一下 spring cloud gateway 如何集成 hystrix。

当下游接口负载很大,或者接口不通等其他原因导致超时,如果接口不熔断的话将会影响到下游接口得不到喘息,网关也会因为超时连接一直挂起,很可能因为一个子系统的问题导致整个系统的雪崩。所以我们的网关需要设计熔断,当因为熔断器打开时,网关将返回一个降级的应答。

Maven 配置

添加 hystrix 依赖

pom.xml

        <dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-hystrix</artifactId>
</dependency>

项目实战

  1. provider1 服务中添加一个方法,延时 2 秒返回响应。
    @GetMapping("/timeout")
public String timeout() {
try {
Thread.sleep(2000);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println("休眠了2秒");
return "timeout test";
}
  1. 修改网关配置文件
server:
port: 2000
spring:
application:
name: idc-gateway2
redis:
host: localhost
port: 6379
timeout: 6000ms # 连接超时时长(毫秒)
jedis:
pool:
max-active: 1000 # 连接池最大连接数(使用负值表示没有限制)
max-wait: -1ms # 连接池最大阻塞等待时间(使用负值表示没有限制)
max-idle: 10 # 连接池中的最大空闲连接
min-idle: 5 # 连接池中的最小空闲连接
cloud:
consul:
host: localhost
port: 8500
gateway:
discovery:
locator:
enabled: true # gateway可以通过开启以下配置来打开根据服务的serviceId来匹配路由,默认是大写
routes:
- id: provider1
uri: lb://idc-provider1
predicates:
- Path=/p1/**
filters:
- StripPrefix=1
- name: Hystrix
args:
name: default
fallbackUri: forward:/defaultfallback # 只有该id下的服务会降级
- id: provider2
uri: lb://idc-provider2
predicates:
- Path=/p2/**
filters:
- StripPrefix=1 # hystrix 信号量隔离,1.5秒后自动超时
hystrix:
command:
default:
execution:
isolation:
strategy: SEMAPHORE
thread:
timeoutInMilliseconds: 1500
  1. 网关添加降级处理类
@RestController
public class FallbackController { @RequestMapping("/defaultfallback")
public Map<String,Object> defaultfallback(){
System.out.println("降级操作...");
Map<String,Object> map = new HashMap<>();
map.put("code",200);
map.put("msg","服务超时降级");
map.put("data",null);
return map;
}
}

降级测试

  1. 超时服务降级
curl http://localhost:2000/p1/timeout

返回

{"msg":"服务超时降级","code":200,"data":null}
  1. 其他异常

spring-cloud-gateway 调用下游服务返回的异常,网关不做任何处理,会直接返回。大家想一下为什么在网关不去处理下游异常呢? 因为很多时候下游的异常是包含有效信息的(异常信息千千万),如果在网关处做了统一返回,就失去了返回异常的意义。

spring-cloud-starter-netflix-hystrix 内置的 Hystrix 过滤器是

HystrixGatewayFilterFactory。 感兴趣的小伙伴可以自行阅读相关源码。

结语

本文到此结束,感谢大家的阅读。欢迎大家关注公众号【当我遇上你】。

spring-cloud-gateway降级的更多相关文章

  1. 网关服务Spring Cloud Gateway(三)

    上篇文章介绍了 Gataway 和注册中心的使用,以及 Gataway 中 Filter 的基本使用,这篇文章我们将继续介绍 Filter 的一些常用功能. 修改请求路径的过滤器 StripPrefi ...

  2. 微服务网关实战——Spring Cloud Gateway

    导读 作为Netflix Zuul的替代者,Spring Cloud Gateway是一款非常实用的微服务网关,在Spring Cloud微服务架构体系中发挥非常大的作用.本文对Spring Clou ...

  3. Spring Cloud Gateway使用

    简介 Spring Cloud Gateway是Spring Cloud官方推出的网关框架,网关作为流量入口,在微服务系统中有着十分重要的作用,常用功能包括:鉴权.路由转发.熔断.限流等. Sprin ...

  4. 跟我学SpringCloud | 第十四篇:Spring Cloud Gateway高级应用

    SpringCloud系列教程 | 第十四篇:Spring Cloud Gateway高级应用 Springboot: 2.1.6.RELEASE SpringCloud: Greenwich.SR1 ...

  5. Spring Cloud Gateway 使用

    简介 Spring Cloud Gateway是Spring Cloud官方推出的网关框架,网关作为流量入口,在微服务系统中有着十分重要的作用,常用功能包括:鉴权.路由转发.熔断.限流等. Sprin ...

  6. Spring Cloud gateway 五 Sentinel整合

    微服务当前这么火爆的程度,如果不能学会一种微服务框架技术.怎么能升职加薪,增加简历的筹码?spring cloud 和 Dubbo 需要单独学习.说没有时间?没有精力?要学俩个框架?而Spring C ...

  7. Spring Cloud gateway 六 Sentinel nacos存储动态刷新

    微服务当前这么火爆的程度,如果不能学会一种微服务框架技术.怎么能升职加薪,增加简历的筹码?spring cloud 和 Dubbo 需要单独学习.说没有时间?没有精力?要学俩个框架?而Spring C ...

  8. Spring Cloud Gateway、并发编程等等

    2019年 JUC线程池服务ExecutorService接口实现源码分析 Github Page:http://www.throwable.club/2019/07/27/java-concurre ...

  9. Spring Cloud Gateway入坑记

    Spring Cloud Gateway入坑记 前提 最近在做老系统的重构,重构完成后新系统中需要引入一个网关服务,作为新系统和老系统接口的适配和代理.之前,很多网关应用使用的是Spring-Clou ...

  10. springcloud(十七):服务网关 Spring Cloud GateWay 熔断、限流、重试

    上篇文章介绍了 Gataway 和注册中心的使用,以及 Gataway 中 Filter 的基本使用,这篇文章我们将继续介绍 Filter 的一些常用功能. 修改请求路径的过滤器 StripPrefi ...

随机推荐

  1. Spring MVC系列-(1) Spring概述

    1. Spring概述 本章主要介绍Spring中的体系结构和常见概念,比如bean.控制反转(Inverse of Control,IoC)等. 1.1 体系结构 Spring 框架提供约 20 个 ...

  2. sublime text3 搭建c++/c环境

    sublime搭建的c++/c使用很方便,实用性很强,自己阅览了无数的博客,csdn,博客园的都看了,最后还是自己摸索着搭建成功了,如果觉得还不错请给个评论谢谢.(提前声明本人专利不允许转载!!!!) ...

  3. js中所有函数的参数(按值和按引用)都是按值传递的,怎么理解?

    我觉着我可能对这块有点误解,所以单独开个博说下自己的理解,当然是研究后的正解了. 1,参数传递是基本类型,看个例子: function addTen(num){ num += 10; return n ...

  4. docker的安装使用

    目录 Docker 入门到精通 CentOS安装Docker 设置管理Docker的仓库 安装Docker Engine-Community Docker基础命令 开启关闭 镜像操作 容器操作 Doc ...

  5. Python学习笔记.基础一

    Python 语言:解释型.交互式.面向对象.   Python源代码遵循GPL协议   Python标识符 在python里,标识符有字母.数字.下划线组成. 在python中,所有标识符可以包括英 ...

  6. JSON Serialization/Deserialization in C#

    因为对C#不是特别熟悉,但是最近写个c#的demo,需要对获取的的json字符串进行解析,开始使用Newtonsoft.Json.Linq尝试了以下,但是感觉操作起来比较麻烦,尤其对与JSON结构比较 ...

  7. C++ 指针函数

    #include <stdio.h> #include <windows.h> using namespace std; template<typename T> ...

  8. 【简说Python WEB】Flask-Moment

    目录 [简说Python WEB]Flask-Moment 系统环境:Ubuntu 18.04.1 LTS Python使用的是虚拟环境:virutalenv Python的版本:Python 3.6 ...

  9. Requests发Post请求data里面嵌套字典

    一.Post请求,data里面嵌套字典 Requests发Post请求,data里面嵌套字典的常见形式如下: info = { "appid": "123", ...

  10. sql语句的基本用法总结

    一.sql语法 select */列名1,列名2... from 表名[连接查询 内连接/左连接 on条件] 必选的 where 条件 子查询/in/exists/between ... and .. ...