一种接口限速方案

如果一个接口的频繁调用会严重影响到系统稳定性的话,那就要考虑限速了。

谷歌有个现有的开源实现:Guava的RateLimiter类,RateLimiter使用的是一种叫令牌桶的流控算法,RateLimiter会按照一定的频率往桶里扔令牌,线程拿到令牌才能执行,比如你希望自己的应用程序QPS不要超过1000,那么RateLimiter设置1000的速率后,就会每秒往桶里扔1000个令牌。

详情请戳:  Guava官方文档-RateLimiter类


用法简单说一下吧,首先声明一个限速策略:


final RateLimiter rateLimiter = RateLimiter.create(10.0); 

然后在需要限流的地方贴上:


rateLimiter.acquire();

这样就可以保证每秒钟只能有两次请求成功了。

但是,我需要更多的功能来实现,比如超过限速阈值后也要返回一个东西,这时候就不太好办了,RateLimiter只是在内部采用令牌桶隐式地对接口进行了限速,下面是我的实现:

package com.lovnx.test;

import java.util.Timer;
import java.util.TimerTask;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.atomic.AtomicInteger;

public class mytst {
    //单位时间内的调用次数
    private int flag = 10;
    //线程数量
    private int threadNum = 10;
    //单位时间1000ms * 60 = 1min
    private int timeRound = 1000*60;
    //用来标记调用次数
    private AtomicInteger num = new AtomicInteger(0);

    public static void main(String[] args) {

        new mytst().call();
    }

    private void call() {
        ExecutorService executor = Executors.newFixedThreadPool(threadNum);

        Timer timer = new Timer();
        timer.schedule(new TimerTask(){
          @Override
          public void run() {
              num.set(0);;
          }
        }, 0, timeRound);

        for (int i = 0; i < 1000; i++) {
            executor.execute(new Runnable() {

                @Override
                public void run() {
                    try {
                        Thread.sleep(10000);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                    System.out.println(Thread.currentThread().getName()+"   进来了!");
                    num.incrementAndGet();
                    if (num.get() <= flag) {
                        System.out.println(Thread.currentThread().getName()+"   执行任务!");
                    } else {
                        System.out.println(Thread.currentThread().getName()+"   执行任务失败!调用超限!");
                    }
                }
            });
        }

        executor.shutdown();
    }
}

我比较粗暴,直接用一个原子类AtomicInteger来标记。

在接口层面就是这样的:

package com.lovnx.web;

import java.util.Timer;
import java.util.TimerTask;
import java.util.concurrent.atomic.AtomicInteger;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.client.discovery.DiscoveryClient;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class ComputeController {

    private final Logger logger = Logger.getLogger(getClass());

    //单位时间内的调用次数
    private int flag = 10;
    //单位时间1000ms * 60 = 1min
    private static int timeRound = 1000*60;
    //用来标记调用次数
    private static AtomicInteger num = new AtomicInteger(0);

    @Autowired
    private DiscoveryClient client;

    static{
        Timer timer = new Timer();
        timer.schedule(new TimerTask(){
          @Override
          public void run() {
              num.set(0);;
          }
        }, 0, timeRound);
    }

    @RequestMapping(value = "/add" ,method = RequestMethod.GET)
    public String add(@RequestParam Integer a, @RequestParam Integer b) {

        num.incrementAndGet();

        if (num.get() <= flag) {
            ServiceInstance instance = client.getLocalServiceInstance();
            Integer r = a + b;
            logger.info("/add, host:" + instance.getHost() + ", service_id:" + instance.getServiceId() + ", result:" + r);
            return "From Service-B, Result is " + r+"\nPort:"+instance.getPort();
        }
        return "调用次数超限,一分钟内最多只能调用10次!";
    }

}


你可能感兴趣的:(一种接口限速方案)