SpringBoot+Redis+Lua实现IP限流

引入依赖

org.springframework.boot spring-boot-starter-web org.springframework.boot spring-boot-starter-data-redis org.springframework.boot spring-boot-starter-aop io.lettuce lettuce-core org.springframework.boot spring-boot-starter-test org.apache.commons commons-lang3

application.properties 增加redis配置

server.port=8081

spring.redis.host=192.168.0.103
spring.redis.port=6379
spring.redis.jedis.pool.max-active=8
spring.redis.jedis.pool.max-wait=1
spring.redis.timeout=10000

编写Lua脚本

vim rateLimit.lua
:wq
放置在resources目录下
local num = redis.call(‘incr’, KEYS[1])
if tonumber(num) == 1 then
redis.call(‘expire’, KEYS[1], ARGV[1])
return 1
elseif tonumber(num) > tonumber(ARGV[2]) then
return 0
el

猜你喜欢

转载自blog.csdn.net/u013449046/article/details/104300102