RedisRateLimiterController.java 1.4 KB

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