SystemRedisRateLimiter.java 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220
  1. package com.diagbot.config.redislimiter;
  2. import com.diagbot.util.StringUtil;
  3. import org.springframework.beans.BeansException;
  4. import org.springframework.beans.factory.annotation.Autowired;
  5. import org.springframework.beans.factory.annotation.Qualifier;
  6. import org.springframework.cloud.gateway.filter.ratelimit.AbstractRateLimiter;
  7. import org.springframework.cloud.gateway.filter.ratelimit.RateLimiter;
  8. import org.springframework.cloud.gateway.support.ConfigurationService;
  9. import org.springframework.context.ApplicationContext;
  10. import org.springframework.context.ApplicationContextAware;
  11. import org.springframework.data.redis.core.ReactiveRedisTemplate;
  12. import org.springframework.data.redis.core.RedisTemplate;
  13. import org.springframework.data.redis.core.script.RedisScript;
  14. import org.springframework.util.ObjectUtils;
  15. import org.springframework.validation.annotation.Validated;
  16. import reactor.core.publisher.Flux;
  17. import reactor.core.publisher.Mono;
  18. import javax.validation.constraints.Min;
  19. import java.time.Instant;
  20. import java.util.ArrayList;
  21. import java.util.Arrays;
  22. import java.util.HashMap;
  23. import java.util.List;
  24. import java.util.Map;
  25. import java.util.concurrent.atomic.AtomicBoolean;
  26. /**
  27. * @Description:
  28. * @author: gaodm
  29. * @time: 2019/12/26 16:34
  30. */
  31. public class SystemRedisRateLimiter extends AbstractRateLimiter<SystemRedisRateLimiter.Config> implements ApplicationContextAware {
  32. public static final String REPLENISH_RATE_KEY = "replenishRate";
  33. public static final String BURST_CAPACITY_KEY = "burstCapacity";
  34. public static final String CONFIGURATION_PROPERTY_NAME = "sys-redis-rate-limiter";
  35. public static final String REDIS_SCRIPT_NAME = "redisRequestRateLimiterScript";
  36. public static final String REMAINING_HEADER = "X-RateLimit-Remaining";
  37. public static final String REPLENISH_RATE_HEADER = "X-RateLimit-Replenish-Rate";
  38. public static final String BURST_CAPACITY_HEADER = "X-RateLimit-Burst-Capacity";
  39. //处理速度
  40. private static final String DEFAULT_REPLENISHRATE = "default.replenishRate";
  41. //容量
  42. private static final String DEFAULT_BURSTCAPACITY = "default.burstCapacity";
  43. private ReactiveRedisTemplate<String, String> redisTemplate;
  44. private RedisScript<List<Long>> script;
  45. private AtomicBoolean initialized = new AtomicBoolean(false);
  46. @Autowired
  47. @Qualifier("redisTemplateForCache")
  48. private RedisTemplate myredisTemplate;
  49. private String remainingHeader = REMAINING_HEADER;
  50. /**
  51. * The name of the header that returns the replenish rate configuration.
  52. */
  53. private String replenishRateHeader = REPLENISH_RATE_HEADER;
  54. /**
  55. * The name of the header that returns the burst capacity configuration.
  56. */
  57. private String burstCapacityHeader = BURST_CAPACITY_HEADER;
  58. private Config defaultConfig;
  59. public SystemRedisRateLimiter(ReactiveRedisTemplate<String, String> redisTemplate,
  60. RedisScript<List<Long>> script, ConfigurationService configurationService) {
  61. super(Config.class, CONFIGURATION_PROPERTY_NAME, configurationService);
  62. this.redisTemplate = redisTemplate;
  63. this.script = script;
  64. initialized.compareAndSet(false, true);
  65. }
  66. public SystemRedisRateLimiter(int defaultReplenishRate, int defaultBurstCapacity) {
  67. super(Config.class, CONFIGURATION_PROPERTY_NAME, (ConfigurationService) null);
  68. defaultConfig = new Config()
  69. .setReplenishRate(defaultReplenishRate)
  70. .setBurstCapacity(defaultBurstCapacity);
  71. }
  72. @Override
  73. public Mono<RateLimiter.Response> isAllowed(String routeId, String id) {
  74. if (!this.initialized.get()) {
  75. throw new IllegalStateException("RedisRateLimiter is not initialized");
  76. }
  77. if (ObjectUtils.isEmpty(rateLimiterConf)) {
  78. throw new IllegalArgumentException("No Configuration found for route " + routeId);
  79. }
  80. //黑名单校验
  81. String blackListKey = getBlackListKey(id);
  82. Integer blackListValue = (Integer)this.myredisTemplate.opsForValue().get(blackListKey);
  83. if (null != blackListValue) {
  84. if (blackListValue > 20) {
  85. throw new IllegalArgumentException("In Blacklist: " + blackListKey);
  86. }
  87. }
  88. Map<String, Integer> rateLimitMap = rateLimiterConf.getRateLimitMap();
  89. //缓存的key
  90. String replenishRateKey = routeId + "." + REPLENISH_RATE_KEY;
  91. int replenishRate = ObjectUtils.isEmpty(rateLimitMap.get(replenishRateKey)) ? rateLimitMap.get(DEFAULT_REPLENISHRATE) : rateLimitMap.get(replenishRateKey);
  92. //容量key
  93. String burstCapacityKey = routeId + "." + BURST_CAPACITY_KEY;
  94. int burstCapacity = ObjectUtils.isEmpty(rateLimitMap.get(burstCapacityKey)) ? rateLimitMap.get(DEFAULT_BURSTCAPACITY) : rateLimitMap.get(burstCapacityKey);
  95. try {
  96. List<String> keys = getKeys(id);
  97. List<String> scriptArgs = Arrays.asList(replenishRate + "", burstCapacity + "",
  98. Instant.now().getEpochSecond() + "", "1");
  99. Flux<List<Long>> flux = this.redisTemplate.execute(this.script, keys, scriptArgs);
  100. return flux.onErrorResume((throwable) -> Flux.just(Arrays.asList(1L, -1L)))
  101. .reduce(new ArrayList<Long>(), (longs, l) -> {
  102. longs.addAll(l);
  103. return longs;
  104. }).map(results -> {
  105. boolean allowed = results.get(0) == 1L;
  106. Long tokensLeft = results.get(1);
  107. if (!allowed) {
  108. this.myredisTemplate.opsForValue().increment(blackListKey, 1);
  109. }
  110. RateLimiter.Response response = new RateLimiter.Response(allowed, getHeaders(replenishRate, burstCapacity, tokensLeft));
  111. return response;
  112. });
  113. } catch (Exception e) {
  114. e.printStackTrace();
  115. }
  116. return Mono.just(new RateLimiter.Response(true, getHeaders(replenishRate, burstCapacity, -1L)));
  117. }
  118. private RateLimiterConf rateLimiterConf;
  119. @Override
  120. public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
  121. this.rateLimiterConf = applicationContext.getBean(RateLimiterConf.class);
  122. }
  123. public HashMap<String, String> getHeaders(Integer replenishRate, Integer burstCapacity, Long tokensLeft) {
  124. HashMap<String, String> headers = new HashMap<>();
  125. headers.put(this.remainingHeader, tokensLeft.toString());
  126. headers.put(this.replenishRateHeader, String.valueOf(replenishRate));
  127. headers.put(this.burstCapacityHeader, String.valueOf(burstCapacity));
  128. return headers;
  129. }
  130. static List<String> getKeys(String id) {
  131. // use `{}` around keys to use Redis Key hash tags
  132. // this allows for using redis cluster
  133. // Make a unique key per user.
  134. String prefix = "request_sys_rate_limiter.{" + id;
  135. // You need two Redis keys for Token Bucket.
  136. String tokenKey = prefix + "}.tokens";
  137. String timestampKey = prefix + "}.timestamp";
  138. return Arrays.asList(tokenKey, timestampKey);
  139. }
  140. static String getBlackListKey(String id) {
  141. String ip = id.substring(0, id.indexOf("_"));
  142. String key = "ip_black_list_" + ip;
  143. return key;
  144. }
  145. static String getBlackListKey(String routeId, String id) {
  146. String ip = id.substring(0, id.indexOf("_"));
  147. String key = "ip_black_list_" + routeId + "_" + ip;
  148. return key + "_" + ip;
  149. }
  150. @Validated
  151. public static class Config {
  152. @Min(1)
  153. private int replenishRate;
  154. @Min(1)
  155. private int burstCapacity = 1;
  156. public int getReplenishRate() {
  157. return replenishRate;
  158. }
  159. public Config setReplenishRate(int replenishRate) {
  160. this.replenishRate = replenishRate;
  161. return this;
  162. }
  163. public int getBurstCapacity() {
  164. return burstCapacity;
  165. }
  166. public Config setBurstCapacity(int burstCapacity) {
  167. this.burstCapacity = burstCapacity;
  168. return this;
  169. }
  170. @Override
  171. public String toString() {
  172. return "Config{" +
  173. "replenishRate=" + replenishRate +
  174. ", burstCapacity=" + burstCapacity +
  175. '}';
  176. }
  177. }
  178. }