漏桶、令牌桶算法原理与简单实现

漏桶算法

漏桶(Leaky Bucket)算法思路很简单,水(请求)先进入到漏桶里,漏桶以一定的速度出水(接口有响应速率),当水流入速度过大会直接溢出(访问频率超过接口响应速率),然后就拒绝请求,可以看出漏桶算法能强行限制数据的传输速率.示意图如下:
漏桶、令牌桶算法原理与简单实现_第1张图片

令牌桶算法

令牌桶算法(Token Bucket)和 Leaky Bucket 效果一样但方向相反的算法,更加容易理解.随着时间流逝,系统会按恒定1/QPS时间间隔(如果QPS=100,则间隔是10ms)往桶里加入Token(想象和漏洞漏水相反,有个水龙头在不断的加水),如果桶已经满了就不再加了.新请求来临时,会各自拿走一个Token,如果没有Token可拿了就阻塞或者拒绝服务.
漏桶、令牌桶算法原理与简单实现_第2张图片

漏桶算法简单实现

yml 去掉数据库配置
pom
DemoTestController

@RestController
public class DemoTestController {
    public static final String SUCCESS = "SUCCESS";
    public static Map MAP = null;
    /*** bucket size */
    public static final int BUCKET_SIZE = 1000;
    /*** minus size */
    public static final int MINUS_SIZE = 10;
    /*** current size identity */
    public static final String CURRENT_SIZE_IDENTITY = "CURRENT_SIZE_IDENTITY";

    @PostConstruct
    public void init() {
        //init container
        MAP = new ConcurrentHashMap<>(1);
        MAP.put(CURRENT_SIZE_IDENTITY, 0);
    }

    /**
     * execute once every 1 seconds
     * if it has executed ,current bucket size - MINUS_SIZE
     */
    @Scheduled(cron = "0/1 * * * * ?")
    public void minus() {
        //get current bucket size
        Integer currentBucketSize = MAP.get(CURRENT_SIZE_IDENTITY);
        if (currentBucketSize >= MINUS_SIZE) {
            MAP.put(CURRENT_SIZE_IDENTITY, currentBucketSize - MINUS_SIZE);
        }
        System.out.println(currentBucketSize);

    }

    @RequestMapping("/demoTest")
    public String demoTest() {
        //get current bucket size
        Integer currentBucketSize = MAP.get(CURRENT_SIZE_IDENTITY);
        if (currentBucketSize > BUCKET_SIZE - 1) {
            System.out.println("sorry,request too fast!");
        }else{
            MAP.put(CURRENT_SIZE_IDENTITY, currentBucketSize + 1);
        }
        return SUCCESS;
    }
}

App

@EnableScheduling
@SpringBootApplication
public class App {
    public static void main(String[] args) {
        SpringApplication.run(App.class, args);
    }

}

jmeter测试(下载使用jmeter)
漏桶、令牌桶算法原理与简单实现_第3张图片
漏桶、令牌桶算法原理与简单实现_第4张图片
漏桶、令牌桶算法原理与简单实现_第5张图片

令牌桶算法简单实现

yml,pom,App如上一样
DemoTestController

@RestController
public class DemoTestController {
    public static final String SUCCESS = "SUCCESS";
    public static Map MAP = null;
    /*** bucket size */
    public static final int BUCKET_SIZE = 10000;
    /*** add size */
    public static final int ADD_SIZE = 100;
    /*** current size identity */
    public static final String CURRENT_SIZE_IDENTITY = "CURRENT_SIZE_IDENTITY";

    @PostConstruct
    public void init() {
        //init container
        MAP = new ConcurrentHashMap<>(1);
        MAP.put(CURRENT_SIZE_IDENTITY, 1000);
    }

    /**
     * execute once every 1 seconds
     * if it has executed ,current bucket size - MINUS_SIZE
     */
    @Scheduled(cron = "0/1 * * * * ?")
    public void minus() {
        //get current bucket size
        Integer currentBucketSize = MAP.get(CURRENT_SIZE_IDENTITY);
        if (currentBucketSize <= BUCKET_SIZE - ADD_SIZE) {
            MAP.put(CURRENT_SIZE_IDENTITY, currentBucketSize + ADD_SIZE);
        }
        System.out.println(currentBucketSize);

    }

    @RequestMapping("/demoTest")
    public String demoTest() {
        //get current bucket size
        Integer currentBucketSize = MAP.get(CURRENT_SIZE_IDENTITY);
        if (currentBucketSize == 0) {
            System.out.println("sorry,request too fast!");
        } else {
            MAP.put(CURRENT_SIZE_IDENTITY, currentBucketSize - 1);
        }
        return SUCCESS;
    }
}

漏桶、令牌桶算法原理与简单实现_第6张图片
执行jmeter线程组
漏桶、令牌桶算法原理与简单实现_第7张图片

你可能感兴趣的:(算法系列)