美文网首页
APIGateway中流控介绍

APIGateway中流控介绍

作者: 加大装益达 | 来源:发表于2020-08-11 18:05 被阅读0次

流控或者叫限流,可以通过控制流量来保护我们的系统不被大流量或者异常流量冲垮,常用的限流算法有:计数器算法、令牌桶算法、漏桶算法。

计数器算法

计数器算法最简单,可以实现在指定的时间段内流量不能超过多少,比如同一个ip在1秒内请求次数不能超过100次这种情形。

需要使用两个map,一个用来记录同一个ip访问的次数,一个用来记录同一个ip上次访问的时间戳。防止map无限制增长,可以单独开启一个线程,用来定时清除超过时间窗口的ip数据。

计数器算法可能会产生突刺,请求集中到达处理后,后面时间就会空闲掉。

示例代码如下:

public class IPCounter {

    /**
     * 保存ip访问的次数
     * key:ip
     * value:访问次数
     */
    private Map<String, AtomicInteger> counterMap = new ConcurrentHashMap<>();

    /**
     * 保存ip访问的时间
     * key:ip
     * value:时间戳
     */
    private Map<String, Long> timeMap = new ConcurrentHashMap<>();

    /**
     * 指定的次数
     */
    private int countRule;

    /**
     * 指定的时间,毫秒
     */
    private long timeRule;

    public IPCounter(int countRule, long timeRule) {
        this.countRule = countRule;
        this.timeRule = timeRule * 1000;
    }

    public boolean allow(String ip) {
        Long time = timeMap.get(ip);
        Long now = System.currentTimeMillis();

        // 不存在或者上一个时间窗口已经过去,重置时间和计数器
        if (time == null || (now - time) > timeRule) {
            timeMap.put(ip, now);
            counterMap.put(ip, new AtomicInteger());
        }

        AtomicInteger count = counterMap.get(ip);
        int temp = 1;
        if (count != null) {
            temp = count.incrementAndGet();
        }

        return temp <= countRule;
    }

    public static void main(String[] args) {
        // 10秒不能超过5次
        IPCounter counter = new IPCounter(5, 10);
        String ip = "192.168.1.1";
        System.out.println(counter.allow(ip));
        System.out.println(counter.allow(ip));
        System.out.println(counter.allow(ip));
        System.out.println(counter.allow(ip));
        System.out.println(counter.allow(ip));
        System.out.println(counter.allow(ip));
    }
}

漏桶算法

漏桶算法,漏桶的容量是固定的,大批流量进来,超过漏桶数量的抛弃掉,进入到漏桶的请求可以匀速流出。

漏桶算法能够限制请求的速率。

令牌桶算法

令牌桶算法是以固定的速度往桶里产生令牌,桶满了新的令牌被丢弃或者拒绝,请求到达的时候会先从桶里获取令牌,再继续执行。

令牌桶算法可以限制请求调用速率,也允许一定程度的突发调用。

可以使用guava包中的令牌桶算法限流器。

源码:https://github.com/dachengxi/APIGateway
原文链接:https://cxis.me/2020/04/09/APIGateway%E4%B8%AD%E6%B5%81%E6%8E%A7%E4%BB%8B%E7%BB%8D/

相关文章

  • APIGateway中流控介绍

    流控或者叫限流,可以通过控制流量来保护我们的系统不被大流量或者异常流量冲垮,常用的限流算法有:计数器算法、令牌桶算...

  • APIGateway中加密验签介绍

    需要提供给接口调用方一个用来加密的key,调用方根据key、一些其他参数以及业务参数进行加密,还需要对报文进行签名...

  • APIGateway

    概念介绍 APIGateway是微服务对外提供服务的一个屏障,它的核心点在于: 屏蔽微服务之间通过消息队列、rpc...

  • 如何架构一个合适的企业API网关

    APIGateway(APIGW/API网关),顾名思义,是出现在系统边界上的一个面向API的、串行集中式的强管控...

  • APIGateway简介

    1. APIGateway是什么 APIGateway 即API网关,所有请求首先会经过这个网关,然后到达后端服务...

  • 集中网关系列

    APIGateway 在学习 APIGateway 之前,首先应该清楚一些我们经常谈论或者平时使用的技术概念 1....

  • APIGateway总结

    用了几天时间来设计和实现一个APIGateway,这里进行一下文档和代码的汇总。 文档汇总: APIGateway...

  • APIGateway设计文档

    APIGateway的设计文档,包括整体架构和数据库设计。 整体架构 使用draw io画的图,这里是源文件:AP...

  • Android 阿里云身份识别

    准备工作: 1.在libs下添加 alicloud-Android-apigateway-sdk-1.0.1.ja...

  • 网关

    通过网关访问服务 传递Cookie头 Zuul限流 Zuul的权限校验 APIGateWay鉴权

网友评论

      本文标题:APIGateway中流控介绍

      本文链接:https://www.haomeiwen.com/subject/nvaqdktx.html