coding ++: high concurrent solutions limiting technology ----- leaky bucket restrictor

1, the leaky bucket algorithm

When the bucket as a measuring tool (The Leaky Bucket Algorithm as a Meter), may be used for traffic shaping (Traffic Shaping) and flow control (TrafficPolicing), leaky bucket algorithm is described as follows:

A fixed capacity of the bucket, the outflow rate of the water droplets in accordance with a fixed constant;

If the bucket is empty, you do not need out of the water droplets;

Water droplets can flow into the bucket at any rate;

If the inflow exceeds the capacity of the drum drops, droplets flowing into the overflow (discarded), the bucket capacity is constant.

2、

,

3, represents the maximum capacity of the tub concurrency, if the bucket is full, the request is discarded

      Fixed rate outflow

      Random inflow rate, inflow representatives request, if the inflow rate soon, the bucket was full, the overflow is abandoned in order to achieve the effect of limiting.

4, java class codes bucket

package com.aiyuesheng.utils;

import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicInteger;

import lombok.Getter;
import lombok.Setter;

/**
 * 
 * @author caich5 可以把水滴看成请求
 */
@Setter
@Getter
public class LeakyBucket {
    // 桶的容量
    private int capacity = 100;
    // 木桶剩余的水滴的量(初始化的时候的空的桶)
    private AtomicInteger water = new AtomicInteger(0);
    // 水滴的流出的速率 每1000毫秒流出1滴
    private int leakRate;
    // 第一次请求之后,木桶在这个时间点开始漏水
    private long leakTimeStamp;

    public LeakyBucket(int leakRate) {
        this.leakRate = leakRate;
    }

    public boolean acquire() {
        // 如果是空桶,就当前时间作为桶开是漏出的时间
        if (water.get() == 0) {
            leakTimeStamp = System.currentTimeMillis();
            water.addAndGet(1);
            return capacity == 0 ? false : true;
        }
        // 先执行漏水,计算剩余水量
        int waterLeft = water.get() - ((int) ((System.currentTimeMillis() - leakTimeStamp) / 1000)) * leakRate;
        water.set(Math.max(0, waterLeft));
        // 重新更新leakTimeStamp
        leakTimeStamp = System.currentTimeMillis();
        // 尝试加水,并且水还未满
        if ((water.get()) < capacity) {
            water.addAndGet(1);
            return true;
        } else {
            // 水满,拒绝加水
            return false;
        }
    }
}

实现:

package com.aiyuesheng.controller;

import java.util.concurrent.TimeUnit;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.aiyuesheng.hystrix.OrderHystrixCommand;
import com.aiyuesheng.service.OrderService;
import com.aiyuesheng.utils.LeakyBucket;
import com.aiyuesheng.utils.LimitService;
import com.alibaba.fastjson.JSONObject;
import com.google.common.util.concurrent.RateLimiter;

@RestController
public class Index {
//漏桶:水滴的漏出速率是每秒 1 滴
    private LeakyBucket leakyBucket = new LeakyBucket(1);

    @Autowired
    private OrderService orderService;
//漏桶限流
    @RequestMapping("/searchCustomerInfoByLeakyBucket")
    public Object searchCustomerInfoByLeakyBucket() {
        // 1.限流判断
        boolean acquire = leakyBucket.acquire();
        if (!acquire) {
            System.out.println("稍后再试!");
            return "稍后再试!";
        }
        // 2.如果没有达到限流的要求,直接调用接口查询
        System.out.println(orderService.searchCustomerInfo());
        return orderService.searchCustomerInfo();
    }
    

}

漏桶算法与令牌桶算法区别

主要区别在于“漏桶算法”能够强行限制数据的传输速率,

而“令牌桶算法”在能够限制数据的平均传输速率外,还允许某种程度的突发传输。在“令牌桶算法”中,只要令牌桶中存在令牌,那么就允许突发地传输数据直到达到用户配置的门限,因此它适合于具有突发特性的流量

Guess you like

Origin www.cnblogs.com/codingmode/p/11873466.html