提交 | 用户 | 时间
|
722af2
|
1 |
package com.dl.demo.controller; |
X |
2 |
|
|
3 |
import com.dl.common.annotation.RateLimiter; |
|
4 |
import com.dl.common.core.domain.R; |
|
5 |
import com.dl.common.enums.LimitType; |
|
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 |
* 测试分布式限流样例 |
|
14 |
* |
|
15 |
* @author Lion Li |
|
16 |
*/ |
|
17 |
@Slf4j |
|
18 |
@RestController |
|
19 |
@RequestMapping("/demo/rateLimiter") |
|
20 |
public class RedisRateLimiterController { |
|
21 |
|
|
22 |
/** |
|
23 |
* 测试全局限流 |
|
24 |
* 全局影响 |
|
25 |
*/ |
|
26 |
@RateLimiter(count = 2, time = 10) |
|
27 |
@GetMapping("/test") |
|
28 |
public R<String> test(String value) { |
|
29 |
return R.ok("操作成功", value); |
|
30 |
} |
|
31 |
|
|
32 |
/** |
|
33 |
* 测试请求IP限流 |
|
34 |
* 同一IP请求受影响 |
|
35 |
*/ |
|
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 |
*/ |
|
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 |
|
|
52 |
/** |
|
53 |
* 测试请求IP限流(key基于参数获取) |
|
54 |
* 同一IP请求受影响 |
|
55 |
* |
|
56 |
* 简单变量获取 #变量 复杂表达式 #{#变量 != 1 ? 1 : 0} |
|
57 |
*/ |
|
58 |
@RateLimiter(count = 2, time = 10, limitType = LimitType.IP, key = "#value") |
|
59 |
@GetMapping("/testObj") |
|
60 |
public R<String> testObj(String value) { |
|
61 |
return R.ok("操作成功", value); |
|
62 |
} |
|
63 |
|
|
64 |
} |