zuul进行rate limit

梦里梦外; 2022-02-23 06:22 225阅读 0赞

maven

  1. <dependency>
  2. <groupId>com.marcosbarbero.cloud</groupId>
  3. <artifactId>spring-cloud-zuul-ratelimit</artifactId>
  4. <version>1.0.7.RELEASE</version>
  5. </dependency>

配置

  1. product:
  2. ribbon:
  3. listOfServers: 192.168.99.100:8080
  4. zuul:
  5. routes:
  6. product:
  7. path: /product/**
  8. stripPrefix: false
  9. ratelimit:
  10. enabled: true #default false
  11. behind-proxy: true #default false
  12. policies:
  13. product:
  14. limit: 10
  15. refresh-interval: 120 #60 default value (in seconds)
  16. type: #optional
  17. - user
  18. - origin
  19. - url
  20. spring:
  21. redis:
  22. timeout: 10
  23. database: 0
  24. host: 192.168.99.100
  25. port: 6379
  26. pool:
  27. max-active: 8
  28. max-idle: 8
  29. max-wait: -1
  30. min-idle: 0

filterOrder

类似spring-core-4.3.4.RELEASE-sources.jar!/org/springframework/core/Ordered.java

  1. /**
  2. * Useful constant for the highest precedence value.
  3. * @see java.lang.Integer#MIN_VALUE
  4. */
  5. int HIGHEST_PRECEDENCE = Integer.MIN_VALUE;
  6. /**
  7. * Useful constant for the lowest precedence value.
  8. * @see java.lang.Integer#MAX_VALUE
  9. */
  10. int LOWEST_PRECEDENCE = Integer.MAX_VALUE;

默认越小优先级越高,排查负数的情况下,0优先级最高

RateLimitFilter
com/marcosbarbero/zuul/filters/pre/ratelimit/RateLimitFilter.java

它的order为-1,表示更先执行

测试

  1. wrk -t12 -c100 -d10s -T30s --latency http://localhost:8080/product

限流结果:

  1. workspace curl -i http://localhost:8080/product\?debug=true
  2. HTTP/1.1 429
  3. X-Application-Context: application
  4. X-RateLimit-Limit: 10
  5. X-RateLimit-Remaining: 0
  6. X-RateLimit-Reset: 13000
  7. Content-Length: 0
  8. Date: Sun, 09 Apr 2017 06:44:02 GMT

doc

  • spring-cloud-starter-zuul-ratelimit
  • 高并发系统限流中的漏桶算法和令牌桶算法,通过流量整形和速率限制提升稳定性

发表评论

表情:
评论列表 (有 0 条评论,225人围观)

还没有评论,来说两句吧...

相关阅读