java接口限流如何实现

45次阅读
没有评论

共计 2497 个字符,预计需要花费 7 分钟才能阅读完成。

在 Java 中实现接口限流可以使用以下几种方式:

  1. 计数器:维护一个计数器来统计每个接口的请求数量,当请求数量超过设定的阈值时,拒绝后续的请求。这可以使用并发容器如 ConcurrentHashMap 来实现,其中接口作为 key,计数器作为 value。
import java.util.concurrent.ConcurrentHashMap;

public class RateLimiter {private static ConcurrentHashMap<String, Integer> counters = new ConcurrentHashMap<>();
    private static final int MAX_REQUESTS = 100; // 设定的阈值 

    public static boolean allowRequest(String interfaceName) {counters.putIfAbsent(interfaceName, 0);
        int count = counters.get(interfaceName);
        if (count >= MAX_REQUESTS) {return false;
        }
        counters.put(interfaceName, count + 1);
        return true;
    }

    public static void main(String[] args) {String interfaceName = "interface1";
        for (int i = 0; i < 110; i++) {if (allowRequest(interfaceName)) {System.out.println("Allow request for interface: " + interfaceName);
            } else {System.out.println("Reject request for interface: " + interfaceName);
            }
        }
    }
}
  1. 滑动窗口:使用一个固定长度的时间窗口,统计窗口内的请求数量。当请求数量超过设定的阈值时,拒绝后续的请求。这可以使用队列或数组来保存请求的时间戳,并通过计算窗口内的请求数量来进行限流。
import java.util.ArrayDeque;
import java.util.Queue;

public class RateLimiter {private static Queue<Long> timestamps = new ArrayDeque<>();
    private static final int WINDOW_SIZE = 1000; // 窗口大小,单位为毫秒 
    private static final int MAX_REQUESTS = 100; // 设定的阈值 

    public static boolean allowRequest() {long now = System.currentTimeMillis();
        timestamps.offer(now);
        while (!timestamps.isEmpty() && now - timestamps.peek() > WINDOW_SIZE) {timestamps.poll();
        }
        return timestamps.size() <= MAX_REQUESTS;}

    public static void main(String[] args) {for (int i = 0; i < 110; i++) {if (allowRequest()) {System.out.println("Allow request");
            } else {System.out.println("Reject request");
            }
            try {Thread.sleep(100);
            } catch (InterruptedException e) {e.printStackTrace();
            }
        }
    }
}
  1. 令牌桶:使用一个固定速率产生令牌,每个请求需要获取一个令牌才能通过。当令牌数量不足时,拒绝后续的请求。这可以使用 ScheduledExecutorService 来定时产生令牌,并使用 Semaphore 来控制令牌的获取。
import java.util.concurrent.Semaphore;
import java.util.concurrent.ScheduledThreadPoolExecutor;
import java.util.concurrent.TimeUnit;

public class RateLimiter {private static Semaphore tokens = new Semaphore(10); // 初始令牌数量 
    private static final int RATE = 1; // 产生令牌的速率,单位为个 / 秒 

    public static boolean allowRequest() {return tokens.tryAcquire();}

    public static void main(String[] args) {ScheduledThreadPoolExecutor executor = new ScheduledThreadPoolExecutor(1);
        executor.scheduleAtFixedRate(() -> {int availableTokens = tokens.availablePermits();
            if (availableTokens < RATE) {tokens.release(RATE - availableTokens);
            }
        }, 0, 1, TimeUnit.SECONDS);

        for (int i = 0; i < 20; i++) {if (allowRequest()) {System.out.println("Allow request");
            } else {System.out.println("Reject request");
            }
            try {Thread.sleep(100);
            } catch (InterruptedException e) {e.printStackTrace();
            }
        }

        executor.shutdown();}
}

以上是几种常见的 Java 接口限流的实现方式,可以根据实际需求选择适合的方式。

丸趣 TV 网 – 提供最优质的资源集合!

正文完
 
丸趣
版权声明:本站原创文章,由 丸趣 2023-12-16发表,共计2497字。
转载说明:除特殊说明外本站除技术相关以外文章皆由网络搜集发布,转载请注明出处。
评论(没有评论)