Java漏桶算法的簡(jiǎn)單代碼實(shí)例
Java漏桶算法
漏桶算法的意義在于能夠平滑請(qǐng)求,不給下游服務(wù)造成過(guò)大壓力,特別適用于突發(fā)流量或者定時(shí)任務(wù)拉取大量數(shù)據(jù)時(shí),需要處理大量數(shù)據(jù)或者請(qǐng)求的場(chǎng)景。
使用單線程的for循環(huán)太慢,使用線程池仍無(wú)法避免一瞬間會(huì)發(fā)起很多請(qǐng)求,我們需要的是勻速的請(qǐng)求第三方。
拿定時(shí)任務(wù)補(bǔ)償數(shù)據(jù)來(lái)說(shuō),每隔一分鐘拉取100條數(shù)據(jù),希望下游服務(wù)能在1分鐘之內(nèi)將這些數(shù)據(jù)處理掉就行,如果使用線程池,可能1秒鐘之內(nèi)就將20條數(shù)據(jù)發(fā)出去了,即使使用的線程數(shù)比較少,在一瞬間也會(huì)有多個(gè)請(qǐng)求發(fā)出,我們希望每間隔一定時(shí)間,發(fā)出一個(gè)請(qǐng)求,讓下游服務(wù)勻速消化,即希望控制勻速的QPS。
@FunctionalInterface
public interface Callback<Task> {
void process(Task task) throws InterruptedException;
}/**
* <p>簡(jiǎn)單漏桶算法實(shí)現(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ù)隊(duì)列
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)桶中對(duì)象---"+task+"開(kāi)始");
try {
// 模擬業(yè)務(wù)耗時(shí)
Thread.sleep(200);
} catch (InterruptedException e) {
e.printStackTrace();
}
});
});
}
// 一般從數(shù)據(jù)庫(kù)或其他數(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漏桶算法的簡(jiǎn)單代碼實(shí)例的文章就介紹到這了,更多相關(guān)Java漏桶算法內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
SpringSecurity權(quán)限控制實(shí)現(xiàn)原理解析
這篇文章主要介紹了SpringSecurity權(quán)限控制實(shí)現(xiàn)原理解析,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-03-03
spring 自動(dòng)注入AutowiredAnnotationBeanPostProcessor源碼解析
這篇文章主要介紹了spring自動(dòng)注入AutowiredAnnotationBeanPostProcessor源碼解析,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-03-03
Java 數(shù)組元素倒序的三種方式(小結(jié))
這篇文章主要介紹了Java 數(shù)組元素倒序的三種方式(小結(jié)),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2019-09-09
javaWeb實(shí)現(xiàn)簡(jiǎn)單文件上傳
這篇文章主要為大家詳細(xì)介紹了JAVAWeb實(shí)現(xiàn)簡(jiǎn)單文件上傳,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-06-06
springboot+mongodb 實(shí)現(xiàn)按日期分組分頁(yè)查詢功能
這篇文章主要介紹了springboot+mongodb 實(shí)現(xiàn)按日期分組分頁(yè)查詢功能,本文通過(guò)實(shí)例代碼給大家介紹的非常詳細(xì),具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2018-07-07

