Zuul为单个或全部微服务提供容错与回退功能

    新建微服务gateway-zuul-fallback

  主要是两个方法

  • getRoutes 方法,返回为哪个微服务提供回退功能
  • getBody 方法,微服务不可用时返回的信息
package com.smart.fallback;

import com.netflix.hystrix.exception.HystrixTimeoutException;
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 java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;

@Component
public class MyFallbackProvider implements FallbackProvider {

    @Override
    public String getRoute() {
        // 为哪个微服供提供回退服务,返回微服务的名字,必须和注册在Eureka Server上的名字一致
        return "provider-user";
    }

    @Override
    public ClientHttpResponse fallbackResponse(String route, Throwable cause) {
        //fallback的状态码
        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;
            }

            @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(("("+getRoute()+") fallback").getBytes());
            }

            @Override
            public HttpHeaders getHeaders() {
                HttpHeaders httpHeaders = new HttpHeaders();
                httpHeaders.setContentType(MediaType.APPLICATION_JSON);
                return httpHeaders;
            }
        };
    }
}

  配置文件

server:
  port: 4534
spring:
  application:
      name: gateway-zuul-fallback

eureka:
  client:
     service-url:
        defaultZone: http://Jim:land123@localhost:8761/eureka
  instance:
     instance-id: ${spring.application.name}:${spring.application.instance_id:${server.port}}

#actuator  启用所有的监控端点 “*”号代表启用所有的监控端点,可以单独启用,例如,health,info,metrics
#  spring boot 升为 2.0 后,为了安全,默认 Actuator 只暴露了2个端点,heath 和 info
management:
  endpoints:
    web:
      exposure:
        include: "*"
  endpoint:
      health:
        show-details: ALWAYS

zuul:
  routes:
    provider-user: /userprovider/**

  测试

  1、启动Eureka Server
  2、启动microservice-gateway-zuul-fallback
  3、启动micorservice-provider-user
  此时,通过zuul去访问微服务micorservice-provider-user

  http://localhost:4535/userprovider/user/4
  停掉micorservice-provider-user,再次访问

  http://localhost:4535/userprovider/user/4

  为全部微服务提供回退

  将 getRoute方法 返回 * or null

@Override
    public String getRoute() {
        return "*";
    }

  

 

posted on 2019-05-28 22:39  溪水静幽  阅读(259)  评论(0)    收藏  举报