阅读背景:

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.效果如下


分享到: