Coder Social home page Coder Social logo

api-qps's Introduction

令牌桶算法实现用户API访问QPS控制器

本项目是一个基于Spring Boot框架的Web应用程序,用于演示如何使用令牌桶算法实现用户API访问QPS控制。

令牌桶算法

令牌桶算法是一种流量控制算法,它可以限制单位时间内的请求数量。该算法将请求视为令牌,将令牌放入一个桶中,并限制桶的容量,当桶满时,不再接受新的令牌。每个请求在到达时都需要从桶中获取一个令牌,如果桶中没有令牌,则请求被拒绝。

实现

本项目使用Spring AOP实现令牌桶算法。在controller层的方法上添加@RateLimiterAnnotation注解,该注解包含一个ttl属性,用于指定令牌桶的时间窗口大小。本项目中时间窗口固定为10秒。

用户的QPS配置信息存储在ratelimiter.properties配置文件中,其中每个用户对应一个JSON字符串,该JSON字符串是一个Map,其中每个键表示一个API,值表示该API在时间窗口内的请求次数。本项目使用Gson反序列化ratelimiter.properties配置文件。

用户的身份信息存储在请求头中的X-KSC-ACCOUNT-ID字段中。

每次请求到达时,判断该请求是否超出限流阈值,如果超出则返回HTTP状态码409和响应体"request too much",否则放行该请求。

本项目中使用本地缓存实现令牌桶算法,缓存使用ConcurrentHashMap实现。

配置

在启动应用程序时,可以通过config_path参数指定ratelimiter.properties配置文件的路径。例如:

java -Dconfig_path=/path/to/ratelimiter.properties -jar api-qps.jar

使用

controller层的方法上添加@RateLimiterAnnotation注解,例如:

@RestController
public class RateLimiterTestController {

    @RateLimiterAnnotation
    @RequestMapping("/test/instance/query")
    public String describeInstance() {
        return "describe instance success";
    }

    @RateLimiterAnnotation
    @RequestMapping("/test/instance/create")
    public String createInstance() {
        return "create instance success";
    }
}

参考资料

api-qps's People

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.