Java漏桶算法的簡單代碼實例
Java漏桶算法
漏桶算法的意義在于能夠平滑請求,不給下游服務造成過大壓力,特別適用于突發(fā)流量或者定時任務拉取大量數據時,需要處理大量數據或者請求的場景。
使用單線程的for循環(huán)太慢,使用線程池仍無法避免一瞬間會發(fā)起很多請求,我們需要的是勻速的請求第三方。
拿定時任務補償數據來說,每隔一分鐘拉取100條數據,希望下游服務能在1分鐘之內將這些數據處理掉就行,如果使用線程池,可能1秒鐘之內就將20條數據發(fā)出去了,即使使用的線程數比較少,在一瞬間也會有多個請求發(fā)出,我們希望每間隔一定時間,發(fā)出一個請求,讓下游服務勻速消化,即希望控制勻速的QPS。
@FunctionalInterface public interface Callback<Task> { void process(Task task) throws InterruptedException; }
/** * <p>簡單漏桶算法實現</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 { // 獲取任務隊列 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("消費桶中對象---"+task+"開始"); try { // 模擬業(yè)務耗時 Thread.sleep(200); } catch (InterruptedException e) { e.printStackTrace(); } }); }); } // 一般從數據庫或其他數據源拉取數據,這里mock一下 private static List<String> getTaskList() { List<String> list = new ArrayList<>(); for (int i = 0; i < 100; i++) { list.add(i + ""); } return list; } }
到此這篇關于Java漏桶算法的簡單代碼實例的文章就介紹到這了,更多相關Java漏桶算法內容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!
相關文章
springBoot動態(tài)加載jar及如何將類注冊到IOC
在SpringBoot項目中動態(tài)加載jar文件并將其類注冊到IOC容器是一種高級應用方式,,這種方法為SpringBoot項目提供了更靈活的擴展能力,使得項目可以在不修改原有代碼的基礎上增加新的功能模塊,感興趣的朋友一起看看吧2024-11-11idea指定maven的settings文件不生效的問題解決
本文主要介紹了idea指定maven的settings文件不生效的問題解決,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧2023-06-06