RedisRateLimiterController.java 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  1. package com.ruoyi.demo.controller;
  2. import com.ruoyi.common.annotation.RateLimiter;
  3. import com.ruoyi.common.core.domain.R;
  4. import com.ruoyi.common.enums.LimitType;
  5. import lombok.extern.slf4j.Slf4j;
  6. import org.springframework.web.bind.annotation.GetMapping;
  7. import org.springframework.web.bind.annotation.RequestMapping;
  8. import org.springframework.web.bind.annotation.RestController;
  9. /**
  10. * 测试分布式限流样例
  11. *
  12. * @author Lion Li
  13. */
  14. @Slf4j
  15. @RestController
  16. @RequestMapping("/demo/rateLimiter")
  17. public class RedisRateLimiterController {
  18. /**
  19. * 测试全局限流
  20. * 全局影响
  21. */
  22. @RateLimiter(count = 2, time = 10)
  23. @GetMapping("/test")
  24. public R<String> test(String value) {
  25. return R.ok("操作成功", value);
  26. }
  27. /**
  28. * 测试请求IP限流
  29. * 同一IP请求受影响
  30. */
  31. @RateLimiter(count = 2, time = 10, limitType = LimitType.IP)
  32. @GetMapping("/testip")
  33. public R<String> testip(String value) {
  34. return R.ok("操作成功", value);
  35. }
  36. /**
  37. * 测试集群实例限流
  38. * 启动两个后端服务互不影响
  39. */
  40. @RateLimiter(count = 2, time = 10, limitType = LimitType.CLUSTER)
  41. @GetMapping("/testcluster")
  42. public R<String> testcluster(String value) {
  43. return R.ok("操作成功", value);
  44. }
  45. }