分布式限流

概述

  • 分布式限流介绍
    常见方案
    技术选型

  • 分布式限流常用算法

  • 基于客户端的限流方案
    Guava RateLimiter客户端限流
    [算法源码] Guava的预热模型

  • 基于Nginx的分布式限流
    基于IP地址的限流方案
    基于最大连接数的限流方案

  • 基于Redis + Lua的分布式限流

  • 30分钟了解Lua
    Lua基本用法和介绍
    Redis预加载Lua

  • 客户端分布式限流
    基于Redis+ Lua实现限流
    定义自定义注解封装限流逻辑

Guava RateLimiter客户端限流

创建rate-limit子项目,引入依赖项

<dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

        <dependency>
            <groupId>com.google.guava</groupId>
            <artifactId>guava</artifactId>
            <version>18.0</version>
        </dependency>
    </dependencies>

非阻塞式的限流方案

    RateLimiter limiter = RateLimiter.create(2.0);
    public String tryAcquire(Integer count) {
   	 	RateLimiter limiter = RateLimiter.create(2.0);
        if (limiter.tryAcquire(count)) {
            log.info("success, rate is {}", limiter.getRate());
            return "success";
        } else {
            log.info("fail, rate is {}", limiter.getRate());
            return "fail";
        }
    }
// 限定时间的非阻塞限流
    RateLimiter limiter = RateLimiter.create(2.0);
    public String tryAcquireWithTimeout(Integer count, Integer timeout) {
        if (limiter.tryAcquire(count, timeout, TimeUnit.SECONDS)) {
            log.info("success, rate is {}", limiter.getRate());
            return "success";
        } else {
            log.info("fail, rate is {}", limiter.getRate());
            return "fail";
        }
    }

同步阻塞式的限流方案

    RateLimiter limiter = RateLimiter.create(2.0);
	public String acquire(Integer count) {
        limiter.acquire(count);
        log.info("success, rate is {}", limiter.getRate());
        return "success";
    }

以上都可以通过 postman 测试

基于Nginx的IP限流

添加Controller方法

@GetMapping("/nginx")
    public String nginx() {
        log.info("Nginx success");
        return "success";
    }

网关层配置(修改Host文件和nginx.conf文件)

  1. 修改host文件 -> www.testLimit.com = localhost 127.0.0.1
    (127.0.0.1 www.testLimit.com)
  2. 修改nginx -> 将步骤1中的域名,添加到路由规则当中
    配置文件地址: /usr/local/nginx/conf/nginx.conf
  3. 修改 nginx 配置项:nginx.conf
limit_req_zone $binary_remote_addr zone=iplimit:20m rate=1r/s;
  1. 重新加载nginx(Nginx处于启动) => sudo /usr/local/nginx/sbin/nginx -s reload

基于Nginx的连接数限制和单机限流

配置单机限流(类似IP限流)

limit_req_zone $server_name zone=serverlimit:10m rate=100r/s;

添加Controller方法(耗时接口)

@GetMapping("/nginx-conn")
    public String nginxConn(@RequestParam(defaultValue = "0") int secs) {
        try {
            Thread.sleep(1000 * secs);
        } catch (Exception e) {
        }
        return "success";
    }

整体配置文件


worker_processes  1;

events {
    worker_connections  1024;
}

http {
    include       mime.types;
    default_type  application/octet-stream;
    sendfile        on;
    keepalive_timeout  65;
    server {
        listen       80;
        server_name  localhost;
        location / {
            root   html;
            index  index.html index.htm;
        }
        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
        }
    }
# 根据IP地址限制速度
# 1) 第一个参数 $binary_remote_addr
#    binary_目的是缩写内存占用,remote_addr表示通过IP地址来限流
# 2) 第二个参数 zone=iplimit:20m
#    iplimit是一块内存区域(记录访问频率信息),20m是指这块内存区域的大小
# 3) 第三个参数 rate=1r/s
#    比如100r/m,标识访问的限流频率
limit_req_zone $binary_remote_addr zone=iplimit:20m rate=1r/s;

# 根据服务器级别做限流
limit_req_zone $server_name zone=serverlimit:10m rate=100r/s;

# 基于连接数的配置
limit_conn_zone $binary_remote_addr zone=perip:20m;
limit_conn_zone $server_name zone=perserver:20m;
    server {
        server_name www.testLimit.com;
        location /access-limit/ {
            proxy_pass http://127.0.0.1:10086/;

            # 基于IP地址的限制
            # 1) 第一个参数zone=iplimit => 引用limit_req_zone中的zone变量
            # 2) 第二个参数burst=2,设置一个大小为2的缓冲区域,当大量请求到来。
            #     请求数量超过限流频率时,将其放入缓冲区域
            # 3) 第三个参数nodelay=> 缓冲区满了以后,直接返回503异常
            limit_req zone=iplimit burst=2 nodelay;

            # 基于服务器级别的限制
            # 通常情况下,server级别的限流速率是最大的
            limit_req zone=serverlimit burst=100 nodelay;

            # 每个server最多保持100个连接
            limit_conn perserver 100;
            # 每个IP地址最多保持1个连接
            limit_conn perip 5;

            # 异常情况,返回504(默认是503)
            limit_req_status 504;
            limit_conn_status 504;
        }

		# 限制下载速度
        location /download/ {
            limit_rate_after 100m;
            limit_rate 256k;
        }
    }
}

Lua 的介绍和基本用法

Lua的特点

嵌入式开发,插件开发
完美集成 redis
Redis 内置 Lua解释器,执行过程原子性,脚本预编译

安装Lua :

  1. 参考http://www.lua.org/ftp/教程,下载5.3.5_1版本,本地安装

  2. 安装IDEA插件,在IDEA->Preferences面板,Plugins,

  3. 配置Lua SDK的位置: IDEA->File->Project Structure,
    选择添加Lua,路径指向Lua SDK的bin文件夹,我本地的地址是:
    /usr/local/Cellar/lua/5.3.5_1/bin

就可以直接在 idea 中运行 lua

在 idea 中运行 lua

在 Redis 预加载Lua

在Redis中执行Lua脚本
启动 redis

cd /usr/local/Cellar/redis/4.0.8/bin
./redis-server

进入 redis 客户端

./redis-cli
127.0.0.1:6379> eval "return 'hello redis+lua'"
(error) ERR wrong number of arguments for 'eval' command
127.0.0.1:6379> eval "return 'hello redis+lua'" 0
"hello redis+lua"
127.0.0.1:6379> EVAL "return {KEYS[1],ARGV[1]}" 2 k1 k2 v1 v2 0
1) "k1"
2) "v1"
127.0.0.1:6379> EVAL "return {KEYS[1],ARGV[1]}" 0 k1 k2 v1 v2 0
(empty list or set)
127.0.0.1:6379>
127.0.0.1:6379> SCRIPT LOAD "return 'hello redis+lua'"
"53b2700be01e76aa1b060e09c828dae642520f2e"
127.0.0.1:6379> EVALSHA "53b2700be01e76aa1b060e09c828dae642520f2e"
(error) ERR wrong number of arguments for 'evalsha' command
127.0.0.1:6379> EVALSHA "53b2700be01e76aa1b060e09c828dae642520f2e" 0
"hello redis+lua"
127.0.0.1:6379>

带参数

127.0.0.1:6379> SCRIPT LOAD "return 'hello lua '..KEYS[1]"
"1689a78376800076aa8b094894d8e7f7ab78f710"
127.0.0.1:6379> EVALSHA "1689a78376800076aa8b094894d8e7f7ab78f710" 1 key1 val1
"hello lua key1"
127.0.0.1:6379>

检查脚本是否存在

127.0.0.1:6379> SCRIPT EXISTS "1689a78376800076aa8b094894d8e7f7ab78f710"
1) (integer) 1
127.0.0.1:6379> SCRIPT EXISTS "1689a78376800076aa8b094894d8e7f7ab8f710"
1) (integer) 0

清空脚本

127.0.0.1:6379> SCRIPT FLUSH
OK
127.0.0.1:6379> SCRIPT EXISTS "1689a78376800076aa8b094894d8e7f7ab8f710"
1) (integer) 0

限流组件封装-Redis+Lua

  1. 编写Lua限流脚本
  2. spring-data-redis组件集成Lua和Redis
  3. DefaultRedisScrip加载Lua脚本
  4. RedisTemplate配置(调用Redis )
  5. 在Controller中添加测试方法验证限流效果

引入依赖

<dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-redis</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-aop</artifactId>
        </dependency>
@Service
@Slf4j
public class AccessLimiter {

    @Autowired
    private StringRedisTemplate stringRedisTemplate;

    @Autowired
    private RedisScript<Boolean> rateLimitLua;

    public void limitAccess(String key, Integer limit) {
        // step 1 : request Lua script
        boolean acquired = stringRedisTemplate.execute(
                rateLimitLua, // Lua script的真身
                Lists.newArrayList(key), // Lua脚本中的Key列表
                limit.toString() // Lua脚本Value列表
        );

        if (!acquired) {
            log.error("your access is blocked, key={}", key);
            throw new RuntimeException("Your access is blocked");
        }
    }

}

redis 配置

@Configuration
public class RedisConfiguration {

    // 如果本地也配置了StringRedisTemplate,可能会产生冲突
    // 可以指定@Primary,或者指定加载特定的@Qualifier
    @Bean
    public RedisTemplate<String, String> redisTemplate(
            RedisConnectionFactory factory) {
        return new StringRedisTemplate(factory);
    }

    @Bean
    public DefaultRedisScript loadRedisScript() {
        DefaultRedisScript redisScript = new DefaultRedisScript();
        redisScript.setLocation(new ClassPathResource("ratelimiter.lua"));
        redisScript.setResultType(java.lang.Boolean.class);
        return redisScript;
    }

}

lua 脚本

-- 获取方法签名特征
local methodKey = KEYS[1]
redis.log(redis.LOG_DEBUG, 'key is', methodKey)

-- 调用脚本传入的限流大小
local limit = tonumber(ARGV[1])

-- 获取当前流量大小
local count = tonumber(redis.call('get', methodKey) or "0")

-- 是否超出限流阈值
if count + 1 > limit then
    -- 拒绝服务访问
    return false
else
    -- 没有超过阈值
    -- 设置当前访问的数量+1
    redis.call("INCRBY", methodKey, 1)
    -- 设置过期时间
    redis.call("EXPIRE", methodKey, 1)
    -- 放行
    return true
end

配置 reids 地址
application.properties

spring.application.name=ratelimiter-test
server.port=10086

spring.redis.database=0
spring.redis.host=localhost
spring.redis.port=6379

logging.file=log/${spring.application.name}.log

controller

@RestController
@Slf4j
public class Controller {

    @Autowired
    private AccessLimiter accessLimiter;

    @GetMapping("test")
    public String test() {
        accessLimiter.limitAccess("ratelimiter-test", 3);
        return "success";
    }

}

在 postman 中 测试

http://localhost:10086/test

使用注解方式

@Target({ElementType.METHOD})
@Retention(RetentionPolicy.RUNTIME)
@Documented
public @interface AccessLimiter {

    int limit();

    String methodKey() default "";

}
@Slf4j
@Aspect
@Component
public class AccessLimiterAspect {

    @Autowired
    private StringRedisTemplate stringRedisTemplate;

    @Autowired
    private RedisScript<Boolean> rateLimitLua;

    @Pointcut("@annotation(com.testLimit.springcloud.annotation.AccessLimiter)")
    public void cut() {
        log.info("cut");
    }

    @Before("cut()")
    public void before(JoinPoint joinPoint) {
        // 1. 获得方法签名,作为method Key
        MethodSignature signature = (MethodSignature) joinPoint.getSignature();
        Method method = signature.getMethod();

        AccessLimiter annotation = method.getAnnotation(AccessLimiter.class);
        if (annotation == null) {
            return;
        }

        String key = annotation.methodKey();
        Integer limit = annotation.limit();

        // 如果没设置methodkey, 从调用方法签名生成自动一个key
        if (StringUtils.isEmpty(key)) {
            Class[] type = method.getParameterTypes();
            key = method.getClass() + method.getName();

            if (type != null) {
                String paramTypes = Arrays.stream(type)
                        .map(Class::getName)
                        .collect(Collectors.joining(","));
                log.info("param types: " + paramTypes);
                key += "#" + paramTypes;
            }
        }

        // 2. 调用Redis
        boolean acquired = stringRedisTemplate.execute(
                rateLimitLua, // Lua script的真身
                Lists.newArrayList(key), // Lua脚本中的Key列表
                limit.toString() // Lua脚本Value列表
        );

        if (!acquired) {
            log.error("your access is blocked, key={}", key);
            throw new RuntimeException("Your access is blocked");
        }
    }

}

controller

@RestController
@Slf4j
public class Controller {

    @Autowired
    private AccessLimiter accessLimiter;

    // 提醒! 注意配置扫包路径(com.testLimit.springcloud路径不同)
    @GetMapping("test-annotation")
    @com.testLimit.springcloud.annotation.AccessLimiter(limit = 1)
    public String testAnnotation() {
        return "success";
    }

}

在 postman 中 测试

http://localhost:10086/test-annotation
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值