码迷,mamicode.com
首页 > 编程语言 > 详细

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

时间:2018-12-25 11:24:58      阅读:238      评论:0      收藏:0      [点我收藏+]

标签:cep   turn   set   info   gets   分享图片   phrase   null   framework   

  一、容错: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过滤器

标签:cep   turn   set   info   gets   分享图片   phrase   null   framework   

原文地址:https://www.cnblogs.com/pypua/p/10172351.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!