Skip to content

Commit dab50b4

Browse files
committed
🎉 添加限流注解,异常捕获,测试类
1 parent 575fa43 commit dab50b4

File tree

4 files changed

+116
-0
lines changed

4 files changed

+116
-0
lines changed
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
package com.xkcoding.ratelimit.redis.annotation;
2+
3+
import org.springframework.core.annotation.AliasFor;
4+
import org.springframework.core.annotation.AnnotationUtils;
5+
6+
import java.lang.annotation.*;
7+
import java.util.concurrent.TimeUnit;
8+
9+
/**
10+
* <p>
11+
* 限流注解,添加了 {@link AliasFor} 必须通过 {@link AnnotationUtils} 获取,才会生效
12+
* </p>
13+
*
14+
* @author yangkai.shen
15+
* @date Created in 2019/9/30 10:31
16+
* @see AnnotationUtils
17+
*/
18+
@Target(ElementType.METHOD)
19+
@Retention(RetentionPolicy.RUNTIME)
20+
@Documented
21+
public @interface RateLimiter {
22+
long DEFAULT_REQUEST = 10;
23+
24+
/**
25+
* max 最大请求数
26+
*/
27+
@AliasFor("max") long value() default DEFAULT_REQUEST;
28+
29+
/**
30+
* max 最大请求数
31+
*/
32+
@AliasFor("value") long max() default DEFAULT_REQUEST;
33+
34+
/**
35+
* 限流key
36+
*/
37+
String key() default "";
38+
39+
/**
40+
* 超时时长,默认1分钟
41+
*/
42+
long timeout() default 1;
43+
44+
/**
45+
* 超时时间单位,默认 分钟
46+
*/
47+
TimeUnit timeUnit() default TimeUnit.MINUTES;
48+
}
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
package com.xkcoding.ratelimit.redis.controller;
2+
3+
import cn.hutool.core.lang.Dict;
4+
import com.xkcoding.ratelimit.redis.annotation.RateLimiter;
5+
import lombok.extern.slf4j.Slf4j;
6+
import org.springframework.web.bind.annotation.GetMapping;
7+
import org.springframework.web.bind.annotation.RestController;
8+
9+
/**
10+
* <p>
11+
* 测试
12+
* </p>
13+
*
14+
* @author yangkai.shen
15+
* @date Created in 2019/9/30 10:30
16+
*/
17+
@Slf4j
18+
@RestController
19+
public class TestController {
20+
21+
@RateLimiter(value = 5)
22+
@GetMapping("/test1")
23+
public Dict test1() {
24+
log.info("【test1】被执行了。。。。。");
25+
return Dict.create().set("msg", "hello,world!").set("description", "别想一直看到我,不信你快速刷新看看~");
26+
}
27+
28+
@GetMapping("/test2")
29+
public Dict test2() {
30+
log.info("【test2】被执行了。。。。。");
31+
return Dict.create().set("msg", "hello,world!").set("description", "我一直都在,卟离卟弃");
32+
}
33+
34+
@RateLimiter(value = 2)
35+
@GetMapping("/test3")
36+
public Dict test3() {
37+
log.info("【test3】被执行了。。。。。");
38+
return Dict.create().set("msg", "hello,world!").set("description", "别想一直看到我,不信你快速刷新看看~");
39+
}
40+
}
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
package com.xkcoding.ratelimit.redis.handler;
2+
3+
import cn.hutool.core.lang.Dict;
4+
import lombok.extern.slf4j.Slf4j;
5+
import org.springframework.web.bind.annotation.ExceptionHandler;
6+
import org.springframework.web.bind.annotation.RestControllerAdvice;
7+
8+
/**
9+
* <p>
10+
* 全局异常拦截
11+
* </p>
12+
*
13+
* @author yangkai.shen
14+
* @date Created in 2019/9/30 10:30
15+
*/
16+
@Slf4j
17+
@RestControllerAdvice
18+
public class GlobalExceptionHandler {
19+
20+
@ExceptionHandler(RuntimeException.class)
21+
public Dict handler(RuntimeException ex) {
22+
return Dict.create().set("msg", ex.getMessage());
23+
}
24+
}
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
server:
2+
port: 8080
3+
servlet:
4+
context-path: /demo

0 commit comments

Comments
 (0)