1. 程式人生 > >Zuul的Fallback回退機制

Zuul的Fallback回退機制

文章目錄

Zuul回退機制

預設情況下,經過Zuul的請求都會使用Hystrix進行包裹,所以Zuul本身就具有斷路器的功能。

未指定路由配置回退

  • 程式碼示例如下:
package com.mmzs.cloud.fallback;

import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;

import
org.springframework.cloud.netflix.zuul.filters.route.FallbackProvider; import org.springframework.http.HttpHeaders; import org.springframework.http.HttpStatus; import org.springframework.http.MediaType; import org.springframework.http.client.ClientHttpResponse; import org.springframework.stereotype.
Component; import com.netflix.hystrix.exception.HystrixTimeoutException; /** * @author: mmzsit * @date: 2018年11月14日 * @Description: 用於定製FallBack返回的body、狀態碼、訊息頭Header * 部落格地址:https://blog.csdn.net/MobiusStrip * @version V1.0 */ @Component public class MyFallbackProvider implements FallbackProvider
{ @Override public String getRoute() { return "microservice-provider-user"; //微服務配了路由的話,就用配置的名稱 //return "customers"; //如果要為所有路由提供預設回退,可以建立FallbackProvider型別的bean並使getRoute方法返回*或null //return "*"; } @Override public ClientHttpResponse fallbackResponse(String route, final Throwable cause) { if (cause instanceof HystrixTimeoutException) { return response(HttpStatus.GATEWAY_TIMEOUT); } else { return response(HttpStatus.INTERNAL_SERVER_ERROR); } } private ClientHttpResponse response(final HttpStatus status) { return new ClientHttpResponse() { @Override public HttpStatus getStatusCode() throws IOException { //return status; return HttpStatus.BAD_REQUEST; } @Override public int getRawStatusCode() throws IOException { //return status.value(); return HttpStatus.BAD_REQUEST.value(); } @Override public String getStatusText() throws IOException { //return status.getReasonPhrase(); //return HttpStatus.BAD_REQUEST.name(); return HttpStatus.BAD_REQUEST.getReasonPhrase(); } @Override public void close() { } @Override public InputStream getBody() throws IOException { return new ByteArrayInputStream(("fallback:"+MyFallbackProvider.this.getRoute()).getBytes()); } @Override public HttpHeaders getHeaders() { HttpHeaders headers = new HttpHeaders(); headers.setContentType(MediaType.APPLICATION_JSON); return headers; } }; } }

[注] 其中代理的路由如果在 application.yml中配置過,也可以有如下示例:

zuul:
  routes:
    customers: /myuser/**

為所有路由提供預設回退

如果要為所有路由提供預設回退,可以建立FallbackProvider型別的bean並使getRoute方法返回*或null

  • 程式碼示例如下:
class MyFallbackProvider implements FallbackProvider {
    @Override
    public String getRoute() {
        return "*";
    }

    @Override
    public ClientHttpResponse fallbackResponse(String route, Throwable throwable) {
        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;
            }
        };
    }
}