zoukankan      html  css  js  c++  java
  • ZuulFilter 限流

    package com.example.apigeteway.filter;
    
    import com.example.apigeteway.exception.RateLimitException;
    import com.google.common.util.concurrent.RateLimiter;
    import com.netflix.zuul.ZuulFilter;
    import com.netflix.zuul.exception.ZuulException;
    import org.springframework.stereotype.Component;
    
    import static org.springframework.cloud.netflix.zuul.filters.support.FilterConstants.PRE_TYPE;
    import static org.springframework.cloud.netflix.zuul.filters.support.FilterConstants.SERVLET_DETECTION_FILTER_ORDER;
    
    /**
     * @Title: RateLimitFilter
     * @ProjectName api-geteway
     * @date 2019/12/1712:59
     * @Desc 令牌桶 限流
     */
    @Component
    public class RateLimitFilter extends ZuulFilter {
        // 每秒放多少个令牌 (100个)
        private static final RateLimiter RATE_LIMITER = RateLimiter.create(100);
        @Override
        public String filterType() {
            return PRE_TYPE;
        }
    
        @Override
        public int filterOrder() {
            return SERVLET_DETECTION_FILTER_ORDER - 1;
        }
    
        @Override
        public boolean shouldFilter() {
            return false;
        }
    
        @Override
        public Object run() throws ZuulException {
            if (RATE_LIMITER.tryAcquire()){
                throw new RateLimitException();
            }
            return null;
        }
    }
    

      

  • 相关阅读:
    使用nginx搭建https服务器
    CentOS6.*安装gitolite
    Nginx 下配置SSL证书的方法
    Nginx Location配置总结
    最优二叉树(哈夫曼树)知识点
    utf8字节
    utf8字节
    nginx 配置日志
    nginx 配置日志
    elk 索引
  • 原文地址:https://www.cnblogs.com/412013cl/p/12053972.html
Copyright © 2011-2022 走看看