RedisRateLimiterController.java 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  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. @Slf4j
  16. @RestController
  17. @RequestMapping("/demo/rateLimiter")
  18. public class RedisRateLimiterController {
  19. /**
  20. * 测试全局限流
  21. * 全局影响
  22. */
  23. @RateLimiter(count = 2, time = 10)
  24. @GetMapping("/test")
  25. public R<String> test(String value) {
  26. return R.ok("操作成功", value);
  27. }
  28. /**
  29. * 测试请求IP限流
  30. * 同一IP请求受影响
  31. */
  32. @RateLimiter(count = 2, time = 10, limitType = LimitType.IP)
  33. @GetMapping("/testip")
  34. public R<String> testip(String value) {
  35. return R.ok("操作成功", value);
  36. }
  37. /**
  38. * 测试集群实例限流
  39. * 启动两个后端服务互不影响
  40. */
  41. @RateLimiter(count = 2, time = 10, limitType = LimitType.CLUSTER)
  42. @GetMapping("/testcluster")
  43. public R<String> testcluster(String value) {
  44. return R.ok("操作成功", value);
  45. }
  46. }