Spring Cloud Zuul

POM依赖

<dependencies>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-zuul</artifactId>
        </dependency>
    </dependencies>

启动类加上: @EnableZuulProxy 注解

配置:

spring:
application:
name: zuul-gateway
eureka:
client:
service-url:
defaultZone: http://localhost:9001/eureka/
server:
port: 9501
zuul:
routes:
#把order-server服务映射到路径/my-order/**
order-test-route:
serviceId: order-server
path: /my-order/**
#过滤指定的url,即不路由指定的url
ignored-patterns:
- /*/order/list
# 当hystrix的值小的时候,hystrix生效,当ribbon.ReadTimeout小的时候,ribbon生效    
# 注意:只有zuul路由是url是才生效
# 如果使用的是serviceId 需要使用 ribbon.ReadTimeout 和 ribbon.SocketTimeout)
hystrix:
command:
default:
execution:
timeout:
enabled: true
isolation: #命令的执行超时时间 超时将执行回退
thread:
timeoutInMilliseconds: 8000
ribbon:
ReadTimeout: 10000 #读取超时时间
ConnectTimeout: 10000
zuul.routes.order-test-route 为配置一个名称为"order-test-route"的路由,名称可以任意.
serviceId 为服务名称id , 即 spring.application.name 指定的名称
path 为要映射的API
ignored-patterns 为忽略指定的API
自定义过滤器需要实现ZuulFilter接口:
/**
 * 用户访问时触发
 *
 * @author 阮胜
 * @date 2018/7/19 21:48
 */
@Component
public class TokenFilter extends ZuulFilter {

    private static final String TOKEN = "token";

    /**
     * 过滤器类型
     *
     * @return
     */
    @Override
    public String filterType() {
        return FilterConstants.PRE_TYPE;
    }

    /**
     * 过滤器在过滤器链中的位置
     *
     * @return
     */
    @Override
    public int filterOrder() {
        return FilterConstants.PRE_DECORATION_FILTER_ORDER - 1;
    }

    /**
     * 返回值为true时执行过滤器,false则不执行当前过滤器
     *
     * @return
     */
    @Override
    public boolean shouldFilter() {
        return true;
    }

    /**
     * 用户访问时触发该过滤器
     *
     * @return
     * @throws ZuulException
     */
    @Override
    public Object run() throws ZuulException {
        RequestContext requestContext = RequestContext.getCurrentContext();
        HttpServletRequest request = requestContext.getRequest();
        String token = request.getParameter(TOKEN);
        if (StringUtils.isEmpty(token)) {
            requestContext.setSendZuulResponse(false);
            requestContext.setResponseStatusCode(HttpStatus.SC_UNAUTHORIZED);
        }
        return null;
    }
}
/**
 * 用户访问完要返回的时候触发
 * @author 阮胜
 * @date 2018/7/19 21:57
 */
@Component
public class PostFilter extends ZuulFilter {
    @Override
    public String filterType() {
        return FilterConstants.POST_TYPE;
    }

    @Override
    public int filterOrder() {
        return FilterConstants.SEND_RESPONSE_FILTER_ORDER - 1;
    }

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

    @Override
    public Object run() throws ZuulException {
        RequestContext requestContext = RequestContext.getCurrentContext();
        HttpServletResponse resp = requestContext.getResponse();
        resp.setHeader("auth", "test auth");
        return null;
    }
}

 

 
posted @ 2018-07-20 15:20  cearnach  阅读(208)  评论(0编辑  收藏  举报