0604-Zuul构建API Gateway-Zuul的回退

2021-04-11 20:33

阅读:596

标签:static   tco   iat   ica   替换   blank   原因   aws   stat   

一、概述

  参看地址: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;
            }
        };
    }
}

 

 

 

 

 

 

 

 

 

都是

0604-Zuul构建API Gateway-Zuul的回退

标签:static   tco   iat   ica   替换   blank   原因   aws   stat   

原文地址:https://www.cnblogs.com/bjlhx/p/9011337.html


评论


亲,登录后才可以留言!