Java

java接口限流如何实现

小亿
171
2023-10-24 23:22:14
栏目: 编程语言

在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接口限流的实现方式,可以根据实际需求选择适合的方式。

0
看了该问题的人还看了