自研網關:限流功能的開發

自研網關系統已開源,求star
項目地址:

網關系統,如果沒有限流功能,感覺就沒有了靈魂。
所以一直想把限流功能加上。
經查資料,實現功能如下:

限流功能配置

測試限流功能

具體實現如下:

@Slf4j
@Component
public class GatewayRedisRateLimiter implements InitializingBean {


    @Autowired
    @Lazy
    private RedisTemplate stringRedisTemplate;

    private RedisScript<List<Long>> script;

    private AtomicBoolean initialized = new AtomicBoolean(false);



    /**
     * This uses a basic token bucket algorithm and relies on the fact that Redis scripts
     * execute atomically. No other operations can run between fetching the count and
     * writing the new count.
     *
     * @param key           is rule id
     * @param replenishRate replenishRate
     * @param burstCapacity burstCapacity
     * @return {@code Mono<Response>} to indicate when request processing is complete
     */

    public Boolean isAllowed(final String key, final double replenishRate, final double burstCapacity) {
        if (!this.initialized.get()) {
            throw new IllegalStateException("RedisRateLimiter is not initialized");
        }
        List<String> keys = getKeys(key);
        String[] scriptArgs = new String[]{replenishRate + "", burstCapacity + "", Instant.now().getEpochSecond() + "", "1"};
        List<Long> resultFlux = (List<Long>) stringRedisTemplate.execute(this.script, keys, scriptArgs);
       if (CollectionUtils.isEmpty(resultFlux)) {
             resultFlux = Arrays.asList(1L, -1L);

       }
         boolean allowed = resultFlux.get(0) == 1L;
         Long tokensLeft = resultFlux.get(1);

        log.info("RateLimiter key:{},allowed:{},tokensLeft:{}", key, allowed, tokensLeft);
        return allowed;

    }

    private static List<String> getKeys(final String id) {
        String prefix = "request_rate_limiter.{" + id;
        String tokenKey = prefix + "}.tokens";
        String timestampKey = prefix + "}.timestamp";
        return Arrays.asList(tokenKey, timestampKey);
    }

    @SuppressWarnings("unchecked")
    private RedisScript<List<Long>> redisScript() {
        DefaultRedisScript redisScript = new DefaultRedisScript<>();
        redisScript.setScriptSource(new ResourceScriptSource(new ClassPathResource("/META-INF/scripts/request_rate_limiter.lua")));
        redisScript.setResultType(List.class);
        return redisScript;
    }

    @Override
    public void afterPropertiesSet()  {
        this.script = redisScript();
        initialized.compareAndSet(false, true);

    }
}

這是主要的核心代碼,可用于普通的spring mvc項目
具體代碼大家可以去我的開源項目中看,歡迎提ISSUE及需求。
因為只有一個人,也歡迎大家參與進來.

網關系統暫時一段落,后面只修改缺陷不新增功能,主要是人不夠,只有我一個人
其實網關還有很多我想做的。

  1. 協議泛化功能,將dubbo轉http,將webservice轉http
  2. 特定的日志查詢與跟蹤。需要將部分的日志做特殊處理,好做數據分析及跟蹤
  3. 引入GraphQl,將普通的Rest接口轉成GraphQl接口
    可惜我一個人忙不過來
    下期計劃:
    流程引擎(暫未開源)
    1。增加機器人節點,可考慮做RPA的相關事情
    2。優化流程圖設計器,這方面涉及前端,不太擅長
最后編輯于
?著作權歸作者所有,轉載或內容合作請聯系作者
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發布,文章內容僅代表作者本人觀點,簡書系信息發布平臺,僅提供信息存儲服務。