RedisRateLimiterController.java 1.6 KB

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