zoukankan      html  css  js  c++  java
  • 0604-Zuul构建API Gateway-Zuul的回退

    一、概述

      参看地址:https://cloud.spring.io/spring-cloud-static/Edgware.SR3/single/spring-cloud.html#hystrix-fallbacks-for-routes

      Zuul本事是基于Hystrix和Ribbon等,默认会支持Hystrix的Command等

      在Zuul给定的路由回路跳闸,可以通过创建一个类型为ZuulFallbackProvider的bean来提供回退响应。在此bean中,您需要指定回退的路由ID,并提供ClientHttpResponse作为回退。这是一个非常简单的ZuulFallbackProvider实现。

     二、提供Hystrix路由回退

      当Zuul中给定路由的电路被触发时,您可以通过创建一个类型为ZuulFallbackProvider的bean来提供回退响应。在此bean中,您需要指定回退的路由ID,并提供ClientHttpResponse作为回退。这是一个非常简单的ZuulFallbackProvider实现。

    class MyFallbackProvider implements ZuulFallbackProvider {
        @Override
        public String getRoute() {
            return "customers";
        }
    
        @Override
        public ClientHttpResponse fallbackResponse() {
            return new ClientHttpResponse() {
                @Override
                public HttpStatus getStatusCode() throws IOException {
                    return HttpStatus.OK;
                }
    
                @Override
                public int getRawStatusCode() throws IOException {
                    return 200;
                }
    
                @Override
                public String getStatusText() throws IOException {
                    return "OK";
                }
    
                @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.APPLICATION_JSON);
                    return headers;
                }
            };
        }
    }

    路由配置

    zuul:
      routes:
        customers: /customers/**

    如果您希望为所有路由提供默认回退,则可以创建类型为ZuulFallbackProvider的bean,并让getRoute方法返回*或null

    class MyFallbackProvider implements ZuulFallbackProvider {
        @Override
        public String getRoute() {
            return "*";
        }
    
        @Override
        public ClientHttpResponse fallbackResponse() {
            return new ClientHttpResponse() {
                @Override
                public HttpStatus getStatusCode() throws IOException {
                    return HttpStatus.OK;
                }
    
                @Override
                public int getRawStatusCode() throws IOException {
                    return 200;
                }
    
                @Override
                public String getStatusText() throws IOException {
                    return "OK";
                }
    
                @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.APPLICATION_JSON);
                    return headers;
                }
            };
        }
    }

    如果您想选择基于故障原因的响应,请使用FallbackProvider,它将替换未来版本中的ZuulFallbackProvder。

    class MyFallbackProvider implements FallbackProvider {
    
        @Override
        public String getRoute() {
            return "*";
        }
    
        @Override
        public ClientHttpResponse fallbackResponse(final Throwable cause) {
            if (cause instanceof HystrixTimeoutException) {
                return response(HttpStatus.GATEWAY_TIMEOUT);
            } else {
                return fallbackResponse();
            }
        }
    
        @Override
        public ClientHttpResponse fallbackResponse() {
            return response(HttpStatus.INTERNAL_SERVER_ERROR);
        }
    
        private ClientHttpResponse response(final HttpStatus status) {
            return new ClientHttpResponse() {
                @Override
                public HttpStatus getStatusCode() throws IOException {
                    return status;
                }
    
                @Override
                public int getRawStatusCode() throws IOException {
                    return status.value();
                }
    
                @Override
                public String getStatusText() throws IOException {
                    return status.getReasonPhrase();
                }
    
                @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.APPLICATION_JSON);
                    return headers;
                }
            };
        }
    }

    都是

  • 相关阅读:
    happens-before规则和指令重排
    如何利用Google API为WordPress网页添加二维码
    Hadoop全分布式安装
    Hadoop完全分布式安装设置ssh免密登陆遇到的错误问题
    hadoop环境搭建过程中遇到的问题
    使用python去除图片周围的白色边框(黑色边框)
    Pandas(python)数据处理:只对某一列DataFrame数据进行归一化
    VM安装Ubuntu问题合集(无法联网、中文界面设置、中文输入法etc)
    C 语言控制台实现五子棋项目
    利用位运算求一个月的天数
  • 原文地址:https://www.cnblogs.com/bjlhx/p/9011337.html
Copyright © 2011-2022 走看看