Java漏桶算法的簡單代碼實例
Java漏桶算法
漏桶算法的意義在于能夠平滑請求,不給下游服務(wù)造成過大壓力,特別適用于突發(fā)流量或者定時任務(wù)拉取大量數(shù)據(jù)時,需要處理大量數(shù)據(jù)或者請求的場景。
使用單線程的for循環(huán)太慢,使用線程池仍無法避免一瞬間會發(fā)起很多請求,我們需要的是勻速的請求第三方。
拿定時任務(wù)補(bǔ)償數(shù)據(jù)來說,每隔一分鐘拉取100條數(shù)據(jù),希望下游服務(wù)能在1分鐘之內(nèi)將這些數(shù)據(jù)處理掉就行,如果使用線程池,可能1秒鐘之內(nèi)就將20條數(shù)據(jù)發(fā)出去了,即使使用的線程數(shù)比較少,在一瞬間也會有多個請求發(fā)出,我們希望每間隔一定時間,發(fā)出一個請求,讓下游服務(wù)勻速消化,即希望控制勻速的QPS。
@FunctionalInterface public interface Callback<Task> { void process(Task task) throws InterruptedException; }
/** * <p>簡單漏桶算法實現(xiàn)</p> */ public class LeakyBucketHandler<T> { // 漏水速率 /s(TPS/QPS) private Integer rate; private long lastTime = System.currentTimeMillis(); private final int capacity; // 最大并發(fā)量(桶最大容量) private final ArrayBlockingQueue<T> queue; // init public LeakyBucketHandler(Integer rate, int capacity) { this.rate = rate; this.capacity = capacity; this.queue = new ArrayBlockingQueue<T>(capacity); } public LeakyBucketHandler(int capacity) { this.capacity = capacity; this.queue = new ArrayBlockingQueue<T>(capacity); } public boolean acquire(T b) { if (queue.size() > capacity) { return false; } else { queue.offer(b); return true; } } public synchronized void consume(Callback<T> callBack) throws InterruptedException { if (rate == null || rate < 1) { throw new IllegalArgumentException("rate value is" + rate); } while (queue.size() > 0) { long now = System.currentTimeMillis(); if ((now-lastTime) > 1000/rate) { T t = queue.poll(); System.out.println("interval-" + (now - lastTime + "-ms"));; lastTime = now; callBack.process(t); System.out.println("bucket size is " + queue.size()); } else { Thread.sleep(1); } } } public Integer getQueueSize(){ return queue.size(); } }
public class Demo { public static void main(String[] args) throws InterruptedException { // 獲取任務(wù)隊列 List<String> taskList = getTaskList(); LeakyBucketHandler<String> leakyBucket = new LeakyBucketHandler<>(5, taskList.size()); for (String s : taskList) { leakyBucket.acquire(s); } System.out.println("leakyBucket.getQueueSize()="+leakyBucket.getQueueSize()); leakyBucket.consume(task -> { CompletableFuture.runAsync(()->{ System.out.println("消費(fèi)桶中對象---"+task+"開始"); try { // 模擬業(yè)務(wù)耗時 Thread.sleep(200); } catch (InterruptedException e) { e.printStackTrace(); } }); }); } // 一般從數(shù)據(jù)庫或其他數(shù)據(jù)源拉取數(shù)據(jù),這里mock一下 private static List<String> getTaskList() { List<String> list = new ArrayList<>(); for (int i = 0; i < 100; i++) { list.add(i + ""); } return list; } }
到此這篇關(guān)于Java漏桶算法的簡單代碼實例的文章就介紹到這了,更多相關(guān)Java漏桶算法內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Java實現(xiàn)給圖片添加圖片水印,文字水印及馬賽克的方法示例
這篇文章主要介紹了Java實現(xiàn)給圖片添加圖片水印,文字水印及馬賽克的方法,涉及java針對圖片的讀取、水印添加、馬賽克設(shè)置等相關(guān)操作技巧,需要的朋友可以參考下2018-01-01java 數(shù)據(jù)結(jié)構(gòu)二叉樹的實現(xiàn)代碼
這篇文章主要介紹了java 數(shù)據(jù)結(jié)構(gòu)二叉樹的實現(xiàn)代碼的相關(guān)資料,需要的朋友可以參考下2016-09-09springBoot動態(tài)加載jar及如何將類注冊到IOC
在SpringBoot項目中動態(tài)加載jar文件并將其類注冊到IOC容器是一種高級應(yīng)用方式,,這種方法為SpringBoot項目提供了更靈活的擴(kuò)展能力,使得項目可以在不修改原有代碼的基礎(chǔ)上增加新的功能模塊,感興趣的朋友一起看看吧2024-11-11idea指定maven的settings文件不生效的問題解決
本文主要介紹了idea指定maven的settings文件不生效的問題解決,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2023-06-06