In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-19 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/03 Report--
This article mainly explains "how to understand spring-cloud-gateway 's own redis current limit script". The content of the article is simple and clear, and it is easy to learn and understand. Please follow the editor's train of thought to study and learn "how to understand spring-cloud-gateway 's own redis current limit script".
Filter entry: org.springframework.cloud.gateway.filter.factory.RequestRateLimiterGatewayFilterFactory#apply
Judging the entrance of current restriction: org.springframework.cloud.gateway.filter.ratelimit.RedisRateLimiter#isAllowed
Override @ SuppressWarnings ("unchecked") public Mono isAllowed (String routeId, String id) {if (! this.initialized.get ()) {throw new IllegalStateException ("RedisRateLimiter is not initialized");} Config routeConfig = loadConfiguration (routeId); / / How many requests per second do you want a user to be allowed to do? Int replenishRate = routeConfig.getReplenishRate (); / / How much bursting do you want to allow? Int burstCapacity = routeConfig.getBurstCapacity (); try {List keys = getKeys (id); / / The arguments to the LUA script. Time () returns unixtime in seconds. List scriptArgs = Arrays.asList (replenishRate + ", burstCapacity +", Instant.now (). GetEpochSecond () + "," 1 ") / / allowed, tokens_left = redis.eval (SCRIPT, keys, args) Flux flux = this.redisTemplate.execute (this.script, keys, scriptArgs); / / .log ("redisratelimiter", Level.FINER) Return flux.onErrorResume (throwable-> Flux.just (Arrays.asList (1L,-1L)). Reduce (new ArrayList (), (longs, l)-> {longs.addAll (l); return longs }) .map (results-> {boolean allowed = results.get (0) = = 1L; Long tokensLeft = results.get (1) Response response = new Response (allowed, getHeaders (routeConfig, tokensLeft)) If (log.isDebugEnabled ()) {log.debug ("response:" + response);} return response });} catch (Exception e) {/ * * We don't want a hard dependency on Redis to allow traffic. Make sure to set * an alert so you know if this is happening too much. Stripe's observed * failure rate is 0.01%. * / log.error ("Error determining if user allowed from redis", e);} return Mono.just (new Response (true, getHeaders (routeConfig,-1L));}
Lua script loading entry: org.springframework.cloud.gateway.config.GatewayRedisAutoConfiguration#redisRequestRateLimiterScript
@ Bean @ SuppressWarnings ("unchecked") public RedisScript redisRequestRateLimiterScript () {DefaultRedisScript redisScript = new DefaultRedisScript (); redisScript.setScriptSource (new ResourceScriptSource (new ClassPathResource ("META-INF/scripts/request_rate_limiter.lua"); redisScript.setResultType (List.class); return redisScript;}
Request_rate_limiter.lua script
Local tokens_key = KEYS [1] local timestamp_key = KEYS [2]-- redis.log (redis.LOG_WARNING, "tokens_key".. Tokens_key) local rate = tonumber (ARGV [1]) local capacity = tonumber (ARGV [2]) local now = tonumber (ARGV [3]) local requested = tonumber (ARGV [4]) local fill_time = capacity/ratelocal ttl = math.floor (fill_time*2)-redis.log (redis.LOG_WARNING, "rate".. ARGV [1])-redis.log (redis.LOG_WARNING, "capacity".. ARGV [2])-redis.log (redis.LOG_WARNING, "now".. ARGV [3])-redis.log (redis.LOG_WARNING, "requested".. ARGV [4])-redis.log (redis.LOG_WARNING, "filltime".. Fill_time)-redis.log (redis.LOG_WARNING, "ttl".. Ttl) local last_tokens = tonumber (redis.call ("get", tokens_key)) if last_tokens = = nil then last_tokens = capacityend--redis.log (redis.LOG_WARNING, "last_tokens".. Last_tokens) local last_refreshed = tonumber (redis.call ("get", timestamp_key)) if last_refreshed = = nil then last_refreshed = 0end--redis.log (redis.LOG_WARNING, "last_refreshed".. Last_refreshed) local delta = math.max (0, now-last_refreshed)-the point is here, rate is relative to capacity. If it is greater than or equal to capacity, then the concurrency per second is capacity,--. If it is less than capacity, then rate tokens will be permanently added to the bucket per second. -- it is recommended that the normal current limit setting is less than capacity, otherwise, when the capacity is used up instantly, the maximum concurrency threshold of the system has been reached, and the maximum token bucket threshold will be restored the next second. If the rate is too high, it will not have the effect of current limit. Local filled_tokens = math.min (capacity, last_tokens+ (delta*rate)) local allowed = filled_tokens > = requestedlocal new_tokens = filled_tokenslocal allowed_num = 0if allowed then new_tokens = filled_tokens-requested allowed_num = 1end--redis.log (redis.LOG_WARNING, "delta".. Delta)-redis.log (redis.LOG_WARNING, "filled_tokens".. Filled_tokens)-redis.log (redis.LOG_WARNING, "allowed_num".. Allowed_num)-redis.log (redis.LOG_WARNING, "new_tokens".. New_tokens) redis.call ("setex", tokens_key, ttl, new_tokens) redis.call ("setex", timestamp_key, ttl, now) return {allowed_num, new_tokens} Thank you for your reading. This is the content of "how to understand spring-cloud-gateway 's own redis limit script". After the study of this article, I believe you have a deeper understanding of how to understand spring-cloud-gateway 's own redis current limit script. The specific use situation still needs to be verified by practice. Here is, the editor will push for you more related knowledge points of the article, welcome to follow!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.