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

  • 相关阅读:
    GZOI 2017配对统计 树状数组
    关于线段树的一些问题
    BZOJ 压力 tarjan 点双联通分量+树上差分+圆方树
    洛谷4552 差分
    洛谷5026 Lycanthropy 差分套差分
    【锁】MySQL和Oracle行锁比较
    oracle体系结构
    【加密】RSA验签及加密
    【Shiro】SpringBoot集成Shiro
    【Eureka】实现原理
  • 原文地址:https://www.cnblogs.com/pypua/p/10172351.html
Copyright © 2020-2023  润新知