惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

Google DeepMind News
Google DeepMind News
博客园 - 司徒正美
WordPress大学
WordPress大学
爱范儿
爱范儿
小众软件
小众软件
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
罗磊的独立博客
博客园_首页
V
V2EX
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
T
Tailwind CSS Blog
大猫的无限游戏
大猫的无限游戏
The Cloudflare Blog
MyScale Blog
MyScale Blog
IT之家
IT之家
H
Help Net Security
Blog — PlanetScale
Blog — PlanetScale
Microsoft Security Blog
Microsoft Security Blog
H
Hackread – Cybersecurity News, Data Breaches, AI and More
Recent Announcements
Recent Announcements
F
Fortinet All Blogs
The GitHub Blog
The GitHub Blog
Y
Y Combinator Blog
人人都是产品经理
人人都是产品经理

博客园 - 立3807

ROS 系统架构及概念 ROS 在 Ubuntu 18.04 安装 利用 Skywalking 搭建 APM(应用性能管理)— 安装与配置 elasticsearch 集群搭建及启动常见错误 Git 基本操作 Git 环境配置 Git 安装 Git 基础和原理 Spring Cloud(Dalston.SR5)--Config 集群配置中心-加解密 Spring Cloud(Dalston.SR5)--Config 集群配置中心-刷新配置 Java NIO 概述 Spring Boot - AOP(面向切面)-切入点表达式 Spring Boot - AMQP 消息中间件 Spring Boot - AOP(面向切面) Spring Boot - 基础 POM 文件 Spring Boot - 配置介绍 Spring Boot - 项目构建与解析 Mycat 镜像-创建 Docker 镜像 Spring Cloud(Dalston.SR5)--Config 集群配置中心 Spring Cloud(Dalston.SR5)--Zuul 网关-过滤器
Spring Cloud(Dalston.SR5)--Zuul 网关-Hystrix 回退
立3807 · 2018-04-23 · via 博客园 - 立3807

当我们对网关进行配置让其调用集群的服务时,将会执行 Ribbon 路由过滤器,该过滤器在进行转发时会封装为一个 Hystrix 命令予以执行,Hystrix 命令具有容错的功能,如果"源服务"出现问题(例如超时),那边所执行的 Hystrix 命令将会触发回退,我们需要实现 org.springframework.cloud.netflix.zuul.filters.route.ZuulFallbackProvider 接口,该接口主要需要实现 getRoute 方法 、fallbackResponse 方法,getRoute 方法主要返回路由的名称,用于匹配 Hystrix 回退方法的路由;fallbackResponse 方法主要返回回退的返回信息,示例代码如下:

  • 回退方法类

    实现了 getRoute 方法,返回 HELLOWORLD-PROVIDER 表示该回退类,用于 HELLOWORLD-PROVIDER 服务,实现了 fallbackResponse 方法,表示回退方法返回的具体内容

    package org.lixue.zuul;

    import org.springframework.cloud.netflix.zuul.filters.route.ZuulFallbackProvider;

    import org.springframework.http.HttpHeaders;

    import org.springframework.http.HttpStatus;

    import org.springframework.http.MediaType;

    import org.springframework.http.client.ClientHttpResponse;

    import java.io.ByteArrayInputStream;

    import java.io.IOException;

    import java.io.InputStream;

    public class HelloWorldFallback implements ZuulFallbackProvider{

    @Override

    public String getRoute(){

    return"HELLOWORLD-PROVIDER";

    }

    @Override

    public ClientHttpResponse fallbackResponse(){

    return new ClientHttpResponse(){

    @Override

    public HttpStatus getStatusCode()throwsIOException{

    return HttpStatus.OK;

    }

    @Override

    public int getRawStatusCode()throwsIOException{

    return HttpStatus.OK.value();

    }

    @Override

    public String getStatusText()throwsIOException{

    returnH ttpStatus.OK.toString();

    }

    @Override

    public void close(){

    }

    @Override

    public InputStream getBody() throws IOException{

    return new ByteArrayInputStream("fallback".getBytes());

    }

    @Override

    public HttpHeaders getHeaders(){

    HttpHeaders headers=new HttpHeaders();

    headers.setContentType(MediaType.TEXT_PLAIN);

    return headers;

    }

    };

    }

    }

  • Zuul 配置类

    创建配置类,在配置类中创建回退类的实例 Bean 即可

    package org.lixue.zuul;

    import org.springframework.context.annotation.Bean;

    import org.springframework.context.annotation.Configuration;

    @Configuration

    public class ZuulFallbackConfiguration{

    @Bean

    public HelloWorldFallback helloWorldFallback(){

    return new HelloWorldFallback();

    }

    }

  • 增加路由配置

    #配置应用名称

    spring:

    application:

    name:spring-cloud-zuul-microservices

    #服务端口

    server:

    port:9200

    #设置eureka服务注册中心的地址,如果多个以逗号分割

    eureka:

    client:

    service-url:

    #defaultZone表示默认的区域的eureka服务地址,多个使用逗号分割

    defaultZone:http://eurekaserver01:9000/eureka/

    #zuul路由配置

    zuul:

    routes:

    hello:

    path:/hello/**

    serviceId:HELLOWORLD-PROVIDER

  • 测试验证

    该项目依赖一个 eureka-sserver、service-provider 服务,首先启动 eureka-server 和 service-provider 服务,然后启动 spring-cloud-zuul-microservices 服务,访问 http://localhost:9200/hello/speaks?names=123 地址,可以看到能正常返回,如下:

    {"123":"Hello World 123 Port=8080"}

    此时,关闭 service-provider 服务,再次访问,可以看到已经无法访问 service-provider 服务了,返回的是回退方法的数据,如下:

    fallback

本文版权归作者 李雪(博客地址:https://www.cnblogs.wiki)所有,欢迎转载和商用,请在文章页面明显位置给出原文链接并保留此段声明,否则保留追究法律责任的权利,其他事项,可留言咨询。