forked from qiurunze123/miaosha
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
qiurunze
committed
Feb 21, 2019
1 parent
42d4bb2
commit 0f92dcc
Showing
43 changed files
with
1,518 additions
and
114 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
58 changes: 58 additions & 0 deletions
58
...sha-service/src/main/java/com/geekq/miaosha/redis/redismanager/RedisLimitRateWithLUA.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
package com.geekq.miaosha.redis.redismanager; | ||
|
||
import redis.clients.jedis.Jedis; | ||
|
||
import java.io.IOException; | ||
import java.net.URISyntaxException; | ||
import java.util.ArrayList; | ||
import java.util.List; | ||
import java.util.concurrent.CountDownLatch; | ||
|
||
public class RedisLimitRateWithLUA { | ||
|
||
public static void main(String[] args) { | ||
final CountDownLatch latch = new CountDownLatch(1); | ||
|
||
for (int i = 0; i < 20; i++) { | ||
new Thread(new Runnable() { | ||
public void run() { | ||
try { | ||
latch.await(); | ||
System.out.println("请求是否被执行:"+accquire()); | ||
} catch (Exception e) { | ||
e.printStackTrace(); | ||
} | ||
} | ||
}).start(); | ||
|
||
} | ||
|
||
latch.countDown(); | ||
} | ||
|
||
public static boolean accquire() throws IOException, URISyntaxException { | ||
Jedis jedis = new Jedis("39.107.245.253"); | ||
|
||
String lua = | ||
"local key = KEYS[1] " + | ||
" local limit = tonumber(ARGV[1]) " + | ||
" local current = tonumber(redis.call('get', key) or '0')" + | ||
" if current + 1 > limit " + | ||
" then return 0 " + | ||
" else "+ | ||
" redis.call('INCRBY', key,'1')" + | ||
" redis.call('expire', key,'2') " + | ||
" end return 1 "; | ||
|
||
String key = "ip:" + System.currentTimeMillis()/1000; // 当前秒 | ||
String limit = "3"; // 最大限制 | ||
List<String> keys = new ArrayList<String>(); | ||
keys.add(key); | ||
List<String> args = new ArrayList<String>(); | ||
args.add(limit); | ||
jedis.auth("youxin11"); | ||
String luaScript = jedis.scriptLoad(lua); | ||
Long result = (Long)jedis.evalsha(luaScript, keys, args); | ||
return result == 1; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
32 changes: 32 additions & 0 deletions
32
...2version/miaosha-service/src/main/java/com/geekq/miaosha/redis/redismanager/lua/limit.lua
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
local key = KEYS[1] --限流KEY(一秒一个) | ||
local limit = tonumber(ARGV[1]) --限流大小 | ||
local current = tonumber(redis.call('get', key) or "0") | ||
if current + 1 > limit then --如果超出限流大小 | ||
return 0 | ||
else --请求数+1,并设置2秒过期 | ||
redis.call("INCRBY", key,"1") | ||
redis.call("expire", key,"2") | ||
end | ||
return 1 | ||
|
||
|
||
|
||
-- ip限流 | ||
|
||
|
||
local key = "rate.limit:" .. KEYS[1] | ||
local limit = tonumber(ARGV[1]) | ||
local expire_time = ARGV[2] | ||
|
||
local is_exists = redis.call("EXISTS", key) | ||
if is_exists == 1 then | ||
if redis.call("INCR", key) > limit then | ||
return 0 | ||
else | ||
return 1 | ||
end | ||
else | ||
redis.call("SET", key, 1) | ||
redis.call("EXPIRE", key, expire_time) | ||
return 1 | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
10 changes: 10 additions & 0 deletions
10
miaosha-2version/miaosha-service/src/main/resources/limit.lua
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
local key = KEYS[1] --限流KEY(一秒一个) | ||
local limit = tonumber(ARGV[1]) --限流大小 | ||
local current = tonumber(redis.call('get', key) or "0") | ||
if current + 1 > limit then --如果超出限流大小 | ||
return 0 | ||
else --请求数+1,并设置2秒过期 | ||
redis.call("INCRBY", key,"1") | ||
redis.call("expire", key,"2") | ||
end | ||
return 1 |
7 changes: 5 additions & 2 deletions
7
miaosha-2version/miaosha-web/src/main/java/com/geekq/miaosha/GeekQMainApplication.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.