最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

Java中常見的4種限流算法詳解

 更新時間:2023年12月21日 09:00:55   作者:Colins~  
這篇文章主要介紹了Java中常見的4種限流算法詳解,FixedWindowRateLimiter 類表示一個固定窗口限流器,使用 limit 和 interval 參數(shù)分別表示限制請求數(shù)量和時間間隔,缺點是短時間內(nèi)可能會流量翻倍,需要的朋友可以參考下

固定窗口

FixedWindowRateLimiter 類表示一個固定窗口限流器,使用 limit 和 interval 參數(shù)分別表示限制請求數(shù)量和時間間隔(毫秒)。在 allowRequest() 方法中,通過比較當(dāng)前時間與上一次請求時間來判斷是否需要重置請求數(shù)和上一次請求時間。如果請求數(shù)還沒有達(dá)到限制數(shù)量,允許請求并增加請求數(shù),否則拒絕請求。 缺點:短時間內(nèi)可能會流量翻倍

public class FixedWindowRateLimiter {
    private final int limit; // 限制請求數(shù)量
    private final AtomicInteger count; // 當(dāng)前請求數(shù)
    private final long interval; // 時間間隔(毫秒)
    private long lastRequestTime; // 上一次請求時間
    public FixedWindowRateLimiter(int limit, long interval) {
        this.limit = limit;
        this.interval = interval;
        this.count = new AtomicInteger(0);
        this.lastRequestTime = System.currentTimeMillis();
    }
    public synchronized boolean allowRequest() {
        long currentTime = System.currentTimeMillis();
        if (currentTime - lastRequestTime > interval) {
            // 如果距離上一次請求時間已經(jīng)超過了時間間隔,重置請求數(shù)和上一次請求時間
            count.set(0);
            lastRequestTime = currentTime;
        }
        // 如果請求數(shù)還沒有達(dá)到限制數(shù)量,允許請求并增加請求數(shù)
        if (count.get() < limit) {
            count.incrementAndGet();
            return true;
        }
        return false; // 否則拒絕請求
    }
}
// 使用示例
FixedWindowRateLimiter limiter = new FixedWindowRateLimiter(10, 1000); // 每秒最多處理10個請求
for (int i = 0; i < 20; i++) { // 嘗試發(fā)起20個請求
    if (limiter.allowRequest()) {
        System.out.println("Allow request " + i);
    } else {
        System.out.println("Reject request " + i);
    }
    Thread.sleep(200); // 每次請求間隔200毫秒
}

滑動窗口

相比于固定窗口,滑動窗口有以下幾個好處:

  1. 平滑限流:滑動窗口限流算法會以時間為軸將請求限制平均到每個時間段內(nèi),從而平滑了請求的涌入。相比于簡單粗暴的限制請求的數(shù)量或速率等方式,這種平滑的限流方式能夠更好地保證服務(wù)的可用性和穩(wěn)定性。
  2. 精確控制:滑動窗口限流算法可以根據(jù)具體的業(yè)務(wù)需要設(shè)置窗口大小和時間間隔,從而實現(xiàn)對請求的精確控制。通過適當(dāng)調(diào)整窗口大小和時間間隔,可以達(dá)到更好的限流效果。

關(guān)于這個,我覺得sentinel中的滑動窗口就非常的nice,下面是從sentinel中摘出來改一下的示例(同時也運(yùn)用在我本人的中間件內(nèi)),總得來說有三部分:

  • 窗口存放的實體類(監(jiān)控指標(biāo)) HystrixEntity
  • 窗口的定義 HystrixWindow
  • 滑動窗口具體實現(xiàn) HystrixWindowArray
// 窗口存放的實體類(監(jiān)控指標(biāo))
public class HystrixEntity {
    // 窗口請求數(shù)
    private AtomicInteger requestCount;
    // 窗口異常數(shù)
    private AtomicInteger errorCount;
    public HystrixEntity(){
        this.requestCount=new AtomicInteger(0);
        this.errorCount=new AtomicInteger(0);
    }
    public int getRequestCountValue() {
        return requestCount.get();
    }
    public int getErrorCountValue() {
        return errorCount.get();
    }
    public void resetValue() {
        this.errorCount.set(0);
        this.requestCount.set(0);
    }
    public void addErrorCount(){
        this.errorCount.addAndGet(1);
    }
    public void addRequestCount(){
        this.requestCount.addAndGet(1);
    }
}
//窗口的定義 HystrixWindow
public class HystrixWindow {
    // 窗口的長度 單位:ms
    private final int windowLengthInMs;
    // 窗口的開始時間戳  單位:ms
    private long windowStartInMs;
    // 窗口內(nèi)存放的實體類
    private HystrixEntity hystrixEntity;
    public HystrixWindow(int windowLengthInMs, long windowStartInMs, HystrixEntity hystrixEntity) {
        this.windowLengthInMs = windowLengthInMs;
        this.windowStartInMs = windowStartInMs;
        this.hystrixEntity = hystrixEntity;
    }
    public int getWindowLengthInMs() {
        return windowLengthInMs;
    }
    public long getWindowStartInMs() {
        return windowStartInMs;
    }
    public HystrixEntity getHystrixEntity() {
        return hystrixEntity;
    }
    public void setHystrixEntity(HystrixEntity hystrixEntity) {
        this.hystrixEntity = hystrixEntity;
    }
    /**
     * @Description 重置窗口
     **/
    public HystrixWindow resetTo(long startTime) {
        this.windowStartInMs = startTime;
        hystrixEntity.resetValue();
        return this;
    }
    /**
     * @Description 判斷時間是否屬于該窗口
     **/
    public boolean isTimeInWindow(long timeMillis) {
        return windowStartInMs <= timeMillis && timeMillis < windowStartInMs + windowLengthInMs;
    }
}
//滑動窗口具體實現(xiàn)
public class HystrixWindowArray {
    // 單個窗口的長度
    private int windowLengthInMs;
    // 窗口數(shù)量
    private int sampleCount;
    // 所有窗口的總長度
    private int intervalInMs;
    // 窗口數(shù)組
    private final AtomicReferenceArray<HystrixWindow> array;
    /**
     * The conditional (predicate) update lock is used only when current bucket is deprecated.
     */
    private final ReentrantLock updateLock = new ReentrantLock();
    /**
     * @Param [sampleCount, intervalInMs]
     * sampleCount: 窗口數(shù)量  intervalInMs:所有窗口的總長度
     **/
    public HystrixWindowArray(int sampleCount, int intervalInMs) {
        Assert.isTrue(sampleCount > 0, "bucket count is invalid: " + sampleCount);
        Assert.isTrue(intervalInMs > 0, "total time interval of the sliding window should be positive");
        Assert.isTrue(intervalInMs % sampleCount == 0, "time span needs to be evenly divided");
        this.windowLengthInMs = intervalInMs / sampleCount;
        this.intervalInMs = intervalInMs;
        this.sampleCount = sampleCount;
        this.array = new AtomicReferenceArray<>(sampleCount);
    }
    /**
     * 獲取當(dāng)前時間所在的窗口下標(biāo)索引
     */
    private int calculateTimeIdx(long timeMillis) {
        long timeId = timeMillis / windowLengthInMs;
        // Calculate current index so we can map the timestamp to the leap array.
        return (int)(timeId % array.length());
    }
    /**
     * 獲取當(dāng)前時間所在的窗口開始時間
     */
    private long calculateWindowStart(long timeMillis) {
        return timeMillis - timeMillis % windowLengthInMs;
    }
    private HystrixEntity newEmptyWindowValue(long timeMillis){
        return new HystrixEntity();
    }
    /**
     * 獲取當(dāng)前窗口
     */
    public HystrixWindow currentWindow() {
        return currentWindow(System.currentTimeMillis());
    }
    private HystrixWindow currentWindow(long timeMillis) {
        if (timeMillis < 0) {
            return null;
        }
        int idx = calculateTimeIdx(timeMillis);
        // Calculate current bucket start time.
        long windowStart = calculateWindowStart(timeMillis);
        while (true) {
            HystrixWindow old = array.get(idx);
            if (old == null) {
                // 如果獲取為空,說明窗口還沒創(chuàng)建,所以我們創(chuàng)建一個新的窗口(CAS保證線程安全)
                HystrixWindow window = new HystrixWindow(windowLengthInMs, windowStart, newEmptyWindowValue(timeMillis));
                if (array.compareAndSet(idx, null, window)) {
                    // 創(chuàng)建成功則返回當(dāng)前窗口
                    return window;
                } else {
                    // 創(chuàng)建失敗說明發(fā)生了競爭,所以暫時先讓出CPU
                    Thread.yield();
                }
            } else if (windowStart == old.getWindowStartInMs()) {
                // 如果窗口已經(jīng)存在,則對比窗口的開始時間是否相同,相同說明是用同一個窗口,直接返回窗口就可以了
                return old;
            } else if (windowStart > old.getWindowStartInMs()) {
                // 如果窗口已經(jīng)存在,而且窗口開始時間比之前的窗口開始時間要大
                // 說明原來的窗口已經(jīng)過時了,需要替換一個新的窗口
                // 所以加鎖防止競爭
                if (updateLock.tryLock()) {
                    try {
                        // 這里我選擇直接重置之前的窗口()
                        return resetWindowTo(old, windowStart);
                    } finally {
                        updateLock.unlock();
                    }
                } else {
                    // Contention failed, the thread will yield its time slice to wait for bucket available.
                    Thread.yield();
                }
            } else if (windowStart < old.getWindowStartInMs()) {
                // 窗口的開始時間比之前的窗口開始時間還會小,這種屬于異常情況
                // 要是真出現(xiàn)了也只能新建一個窗口返回了
                return new HystrixWindow(windowLengthInMs, windowStart, newEmptyWindowValue(timeMillis));
            }
        }
    }
    /**
     * 獲取當(dāng)前窗口內(nèi)的值
     */
    public HystrixEntity getWindowValue() {
        return getWindowValue(System.currentTimeMillis());
    }
    public HystrixEntity getWindowValue(long timeMillis) {
        if (timeMillis < 0) {
            return null;
        }
        int idx = calculateTimeIdx(timeMillis);
        HystrixWindow bucket = array.get(idx);
        if (bucket == null || !bucket.isTimeInWindow(timeMillis)) {
            return null;
        }
        return bucket.getHystrixEntity();
    }
    /**
     * 重置一個窗口
     */
    private HystrixWindow resetWindowTo(HystrixWindow window, long startTime){
        return window.resetTo(startTime);
    }
    public List<HystrixEntity> values() {
        return values(System.currentTimeMillis());
    }
    private List<HystrixEntity> values(long timeMillis) {
        if (timeMillis < 0) {
            return new ArrayList<HystrixEntity>();
        }
        int size = array.length();
        List<HystrixEntity> result = new ArrayList<HystrixEntity>(size);
        for (int i = 0; i < size; i++) {
            HystrixWindow window = array.get(i);
            if (window == null || isWindowDeprecated(timeMillis, window)) {
                continue;
            }
            result.add(window.getHystrixEntity());
        }
        return result;
    }
    /**
     * 判斷窗口是否有效
     */
    public boolean isWindowDeprecated(long time, HystrixWindow window) {
        return time - window.getWindowStartInMs() > intervalInMs;
    }
}

使用示例:

// 初始化 代表監(jiān)控1s內(nèi)的指標(biāo)  窗口數(shù)為2
HystrixWindowArray hystrixWindowArray = new HystrixWindowArray(2, 1000);
// 指標(biāo)監(jiān)控  數(shù)量+1 
windowArray.currentWindow().getHystrixEntity().addErrorCount();
windowArray.currentWindow().getHystrixEntity().addRequestCount();
// 獲取所有窗口的指標(biāo)累計  判斷是否超標(biāo),也就是1s內(nèi)的總計
List<HystrixEntity> windowValues = windowArray.values();
Integer errorCount = hystrixEntities.stream().map(HystrixEntity::getErrorCountValue).reduce(Integer::sum).get();
Integer requestCount = hystrixEntities.stream().map(HystrixEntity::getRequestCountValue).reduce(Integer::sum).get();

令牌桶算法

思想: 固定時間內(nèi)(例如 1 秒)通過一個桶來存儲令牌,每當(dāng)接收到一個請求時就會消耗一個令牌。如果請求過來時沒有令牌,則無法繼續(xù)處理該請求。在sentinel中被稱為冷啟動

優(yōu)點:

相比于漏桶算法,令牌桶算法具有更好的適應(yīng)性,可以應(yīng)對短時間內(nèi)的流量波動。(漏桶算法只能處理恒定速率的流量)

代碼如下:

public class TokenBucket {
    private long lastTime;  // 上次請求時間
    private double rate;    // 令牌放入速率
    private long capacity;  // 令牌桶容量
    private long tokens;    // 當(dāng)前令牌數(shù)量
    public TokenBucket(double rate, long capacity) {
        this.lastTime = System.currentTimeMillis();
        this.rate = rate;
        this.capacity = capacity;
        this.tokens = capacity;
    }
    public synchronized boolean getToken() {
        long now = System.currentTimeMillis();
        long timeElapsed = now - lastTime;
        tokens += timeElapsed * rate;
        if (tokens > capacity) {
            tokens = capacity;
        }
        lastTime = now;
        if (tokens >= 1) {
            tokens--;
            return true;
        } else {
            return false;
        }
    }
}

漏斗桶算法

漏桶算法(Leaky Bucket)是網(wǎng)絡(luò)世界中流量整形(Traffic Shaping)或速率限制(Rate Limiting)時經(jīng)常使用的一種算法,它的主要目的是控制數(shù)據(jù)注入到網(wǎng)絡(luò)的速率,平滑網(wǎng)絡(luò)上的突發(fā)流量。漏桶算法提供了一種機(jī)制,通過它,突發(fā)流量可以被整形以便為網(wǎng)絡(luò)提供一個穩(wěn)定的流量。在sentinel中被稱為勻速器

優(yōu)點:

  1. 控制速率:漏斗桶算法可以限制數(shù)據(jù)流量的傳輸速率,確保各個環(huán)節(jié)的數(shù)據(jù)處理能力都得到了滿足,從而避免了系統(tǒng)因為數(shù)據(jù)過多而導(dǎo)致的崩潰和癱瘓。
  2. 平滑輸出:漏斗桶算法通過將數(shù)據(jù)分散到不同的時間段內(nèi)進(jìn)行處理,使得數(shù)據(jù)傳輸?shù)妮敵龈悠椒€(wěn),不會出現(xiàn)明顯的波動,提高了網(wǎng)絡(luò)的穩(wěn)定性。

代碼如下:

public class LeakyBucket {
    private int capacity; //漏桶容量
    private int rate; //漏水速率
    private int water; //當(dāng)前水量
    private Instant timestamp; //上次漏水時間
    public LeakyBucket(int capacity, int rate) {
        this.capacity = capacity;
        this.rate = rate;
        this.water = 0;
        this.timestamp = Instant.now();
    }
    public synchronized boolean allow() { //判斷是否允許通過
        Instant now = Instant.now();
        long duration = now.toEpochMilli() - timestamp.toEpochMilli(); //計算距上次漏水過去了多久
        int outflow = (int) (duration * rate / 1000); //計算過去的時間內(nèi)漏出的水量
        water = Math.max(0, water - outflow); //更新當(dāng)前水量,不能小于0
        if (water < capacity) { //如果漏桶還沒滿,放行
            water++;
            timestamp = now;
            return true;
        }
        return false; //否則拒絕通過
    }
}

到此這篇關(guān)于Java中常見的4種限流算法詳解的文章就介紹到這了,更多相關(guān)Java限流算法內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

  • Java 判斷線程池所有任務(wù)是否執(zhí)行完畢的操作

    Java 判斷線程池所有任務(wù)是否執(zhí)行完畢的操作

    這篇文章主要介紹了Java 判斷線程池所有任務(wù)是否執(zhí)行完畢的操作,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2020-08-08
  • Java數(shù)組的特性_動力節(jié)點Java學(xué)院整理

    Java數(shù)組的特性_動力節(jié)點Java學(xué)院整理

    數(shù)組是基本上所有語言都會有的一種數(shù)據(jù)類型,它表示一組相同類型的數(shù)據(jù)的集合,具有固定的長度,并且在內(nèi)存中占據(jù)連續(xù)的空間。在C,C++等語言中,數(shù)組的定義簡潔清晰,而在Java中確有一些會讓人迷惑的特性。本文就嘗試分析這些特性
    2017-04-04
  • Java集合之Disruptor操作示例

    Java集合之Disruptor操作示例

    這篇文章主要為大家介紹了Java集合之Disruptor操作示例介紹,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪
    2023-08-08
  • java調(diào)用未知類的指定方法簡單實例

    java調(diào)用未知類的指定方法簡單實例

    這篇文章介紹了java調(diào)用未知類的指定方法簡單實例,有需要的朋友可以參考一下
    2013-09-09
  • 使用maven的profile構(gòu)建不同環(huán)境配置的方法

    使用maven的profile構(gòu)建不同環(huán)境配置的方法

    這篇文章主要介紹了使用maven的profile構(gòu)建不同環(huán)境配置的方法,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2021-01-01
  • maven項目在實踐中的構(gòu)建管理之路的方法

    maven項目在實踐中的構(gòu)建管理之路的方法

    這篇文章主要介紹了maven項目在實踐中的構(gòu)建管理之路的方法,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2019-05-05
  • Java的SPI機(jī)制實例詳解

    Java的SPI機(jī)制實例詳解

    這篇文章主要介紹了Java的SPI機(jī)制實例詳解的相關(guān)資料,需要的朋友可以參考下
    2017-06-06
  • 詳解怎么用Java的super關(guān)鍵字

    詳解怎么用Java的super關(guān)鍵字

    今天帶大家學(xué)習(xí)Java中super關(guān)鍵字是怎么用的,文中有非常詳細(xì)的介紹,對正在學(xué)習(xí)的小伙伴們很有幫助,需要的朋友可以參考下
    2021-06-06
  • Spring使用@Autowired注解實現(xiàn)自動裝配方式

    Spring使用@Autowired注解實現(xiàn)自動裝配方式

    這篇文章主要介紹了Spring使用@Autowired注解實現(xiàn)自動裝配方式,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2021-09-09
  • MyBatis中基于別名typeAliases的設(shè)置

    MyBatis中基于別名typeAliases的設(shè)置

    這篇文章主要介紹了MyBatis中基于別名typeAliases的設(shè)置,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2021-07-07

最新評論

科尔| 同仁县| 涿鹿县| 酉阳| 江西省| 新乡县| 宣恩县| 从江县| 禄丰县| 浦北县| 文登市| 肇源县| 达拉特旗| 陈巴尔虎旗| 明水县| 建昌县| 漾濞| 陇川县| 凤山市| 邵东县| 阜城县| 昆明市| 盘山县| 平乐县| 长治县| 宜阳县| 勃利县| 广汉市| 察哈| 高州市| 邯郸市| 进贤县| 荔波县| 静安区| 衡阳县| 镇雄县| 鹿泉市| 额尔古纳市| 四会市| 双流县| 马山县|