在线时间:8:00-16:00
迪恩网络APP
随时随地掌握行业动态
扫描二维码
关注迪恩网络微信公众号
在《高可用服务设计之二:Rate limiting 限流与降级》的应用级限流中,介绍了多种方法例如: 1、使用guava提供工具库里的RateLimiter类(内部采用令牌捅算法实现)进行限流
回忆一下令牌桶算法: 令牌桶算法(Token Bucket)和 Leaky Bucket 效果一样但方向相反的算法,更加容易理解.随着时间流逝,系统会按恒定1/QPS时间间隔(如果QPS=100,则间隔是10ms)往桶里加入Token(想象和漏洞漏水相反,有个水龙头在不断的加水),如果桶已经满了就不再加了.新请求来临时,会各自拿走一个Token,如果没有Token可拿了就阻塞或者拒绝服务. last_mill_second 最后时间毫秒 local ratelimit_info=redis.pcall("HMGET",KEYS[1],"last_mill_second","curr_permits","max_burst","rate","app") local last_mill_second=ratelimit_info[1] local curr_permits=tonumber(ratelimit_info[2]) local max_burst=tonumber(ratelimit_info[3]) local rate=tonumber(ratelimit_info[4]) local app=tostring(ratelimit_info[5]) if app == nil then return 0 end local local_curr_permits=max_burst; if(type(last_mill_second) ~='boolean' and last_mill_second ~=nil) then local reverse_permits=math.floor((ARGV[2]-last_mill_second)/1000)*rate if(reverse_permits>0) then redis.pcall("HMSET",KEYS[1],"last_mill_second",ARGV[2]) end local expect_curr_permits=reverse_permits+curr_permits local_curr_permits=math.min(expect_curr_permits,max_burst); else redis.pcall("HMSET",KEYS[1],"last_mill_second",ARGV[2]) end local result=-1 if(local_curr_permits-ARGV[1]>0) then result=1 redis.pcall("HMSET",KEYS[1],"curr_permits",local_curr_permits-ARGV[1]) else redis.pcall("HMSET",KEYS[1],"curr_permits",local_curr_permits) end return result Lua脚本在Redis中运行,保证了取令牌和生成令牌两个操作的原子性。 # REDIS (RedisProperties) # Redis数据库索引(默认为0) spring.redis.database=0 # Redis服务器地址 spring.redis.host=127.0.0.1 # Redis服务器连接端口 spring.redis.port=6379 # Redis服务器连接密码(默认为空) spring.redis.password= # 连接池最大连接数(使用负值表示没有限制) spring.redis.jedis.pool.max-active=8 # 连接池最大阻塞等待时间(使用负值表示没有限制) spring.redis.jedis.pool.max-wait=-1 # 连接池中的最大空闲连接 spring.redis.jedis.pool.max-idle=8 # 连接池中的最小空闲连接 spring.redis.jedis.pool.min-idle=0 # 连接超时时间(毫秒) spring.redis.timeout=2000
@Configuration @EnableCaching public class RedisConfig extends CachingConfigurerSupport { @Override @Bean public KeyGenerator keyGenerator() { return new KeyGenerator() { @Override public Object generate(Object target, Method method, Object... params) { StringBuilder sb = new StringBuilder(); sb.append(target.getClass().getName()); sb.append(method.getName()); for (Object obj : params) { sb.append(obj.toString()); } return sb.toString(); } }; } @Bean public RedisTemplate<String, String> redisTemplate(RedisConnectionFactory factory) { StringRedisTemplate template = new StringRedisTemplate(factory); Jackson2JsonRedisSerializer jackson2JsonRedisSerializer = new Jackson2JsonRedisSerializer(Object.class); ObjectMapper om = new ObjectMapper(); om.setVisibility(PropertyAccessor.ALL, JsonAutoDetect.Visibility.ANY); om.enableDefaultTyping(ObjectMapper.DefaultTyping.NON_FINAL); jackson2JsonRedisSerializer.setObjectMapper(om); template.setValueSerializer(jackson2JsonRedisSerializer); template.afterPropertiesSet(); return template; } @Bean("ratelimitLua") public DefaultRedisScript getRedisScript() { DefaultRedisScript redisScript = new DefaultRedisScript(); redisScript.setLocation(new ClassPathResource("ratelimit.lua")); redisScript.setResultType(java.lang.Long.class); return redisScript; } @Bean("ratelimitInitLua") public DefaultRedisScript getInitRedisScript() { DefaultRedisScript redisScript = new DefaultRedisScript(); redisScript.setLocation(new ClassPathResource("ratelimitInit.lua")); redisScript.setResultType(java.lang.Long.class); return redisScript; } } public class Constants { public static final String RATE_LIMIT_KEY = "ratelimit:"; } public enum Token { SUCCESS, FAILED; public boolean isSuccess(){ return this.equals(SUCCESS); } public boolean isFailed(){ return this.equals(FAILED); } }
@Service public class RateLimitClient { @Autowired StringRedisTemplate stringRedisTemplate; @Qualifier("getRedisScript") @Resource RedisScript<Long> ratelimitLua; @Qualifier("getInitRedisScript") @Resource RedisScript<Long> ratelimitInitLua; public Token initToken(String key){ Token token = Token.SUCCESS; Long currMillSecond = stringRedisTemplate.execute( (RedisCallback<Long>) redisConnection -> redisConnection.time() ); /** * redis.pcall("HMSET",KEYS[1], "last_mill_second",ARGV[1], "curr_permits",ARGV[2], "max_burst",ARGV[3], "rate",ARGV[4], "app",ARGV[5]) */ Long accquire = stringRedisTemplate.execute(ratelimitInitLua, Collections.singletonList(getKey(key)), currMillSecond.toString(), "1", "10", "10", "skynet"); if (accquire == 1) { token = Token.SUCCESS; } else if (accquire == 0) { token = Token.SUCCESS; } else { token = Token.FAILED; } return token; } /** * 获得key操作 * * @param key * @return */ public Token accquireToken(String key) { return accquireToken(key, 1); } public Token accquireToken(String key, Integer permits) { Token token = Token.SUCCESS; Long currMillSecond = stringRedisTemplate.execute( (RedisCallback<Long>) redisConnection -> redisConnection.time() ); Long accquire = stringRedisTemplate.execute(ratelimitLua, Collections.singletonList(getKey(key)), permits.toString(), currMillSecond.toString()); if (accquire == 1) { token = Token.SUCCESS; } else { token = Token.FAILED; } return token; } public String getKey(String key) { return Constants.RATE_LIMIT_KEY + key; } }
local result=1 redis.pcall("HMSET",KEYS[1], "last_mill_second",ARGV[1], "curr_permits",ARGV[2], "max_burst",ARGV[3], "rate",ARGV[4], "app",ARGV[5]) return result
转至:https://www.cnblogs.com/duanxz/p/3465559.html |
请发表评论