spring cloud 容错之zuul回退和Zuul过滤器

Posted 十月围城小童鞋

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了spring cloud 容错之zuul回退和Zuul过滤器相关的知识,希望对你有一定的参考价值。

  一、容错:Zuul回退

如果微服务下线了,针对每个微服务,都需要回复一个中文提示,而不是报异常

1、新建ConsumerFallbackProvider.java

package com.pupeiyuan.fallback;

import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.nio.charset.Charset;

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 org.springframework.stereotype.Component;

@Component
public class ConsumerFallbackProvider implements ZuulFallbackProvider {

    @Override
    public ClientHttpResponse fallbackResponse() {
        return new ClientHttpResponse() {
            
            @Override
            public HttpHeaders getHeaders() {
                // headers设定
                HttpHeaders headers = new HttpHeaders();
                MediaType mt = new MediaType("application","json", Charset.forName("UTF-8"));
                headers.setContentType(mt);
                return headers;
            }
            
            @Override
            public InputStream getBody() throws IOException {
                // 响应体
                return new ByteArrayInputStream(("moveserver微服务不可用,请稍后再试。" + ConsumerFallbackProvider.this.getRoute()).getBytes());
            }
            
            @Override
            public String getStatusText() throws IOException {
                 return HttpStatus.BAD_REQUEST.getReasonPhrase();
            }
            
            @Override
            public HttpStatus getStatusCode() throws IOException {
                return HttpStatus.BAD_REQUEST;
            }
            
            @Override
            public int getRawStatusCode() throws IOException {
                return HttpStatus.BAD_REQUEST.value();
            }
            
            @Override
            public void close() {
                // TODO Auto-generated method stub
                
            }
        };
    }

    @Override
    public String getRoute() {
        // TODO Auto-generated method stub
        return "MOVESERVER";
    }

}

2、效果

 

二、Zuul过滤器

zuul还提供了过滤功能, 只要实现接口ZuulFilter即可对请求先进行筛选和过滤之后再路由到具体服务。

1.新增PreZuulFilter.java

package com.pupeiyuan.fallback;

import javax.servlet.http.HttpServletRequest;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;

import com.netflix.zuul.ZuulFilter;
import com.netflix.zuul.context.RequestContext;

@Component
public class PreZuulFilter extends ZuulFilter {
  private static final Logger LOGGER = LoggerFactory.getLogger(PreZuulFilter.class);

  @Override
  public boolean shouldFilter() {
    return true;
  }

  @Override
  public Object run() {
    HttpServletRequest request = RequestContext.getCurrentContext().getRequest();
    String host = request.getRemoteHost();
    PreZuulFilter.LOGGER.info("请求的host:{}", host);
    return null;
  }

  @Override
  public String filterType() {
    return "pre";
  }

  @Override
  public int filterOrder() {
    return 1;
  }

}

2.效果如下

以上是关于spring cloud 容错之zuul回退和Zuul过滤器的主要内容,如果未能解决你的问题,请参考以下文章

Spring Cloud总结29.Zuul的FallBack回退机制

spring cloud:Edgware.RELEASE版本中zuul回退方法的变化

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

Spring cloud Zuul网关异常处理

Spring Cloud(09)——服务端负载工具Zuul

SpringCloud Hystrix实现容错和回退