Browse Source

🎉 添加限流注解,异常捕获,测试类

pull/1/head
Yangkai.Shen 6 years ago
parent
commit
dab50b4e14
4 changed files with 116 additions and 0 deletions
  1. +48
    -0
      spring-boot-demo-ratelimit-redis/src/main/java/com/xkcoding/ratelimit/redis/annotation/RateLimiter.java
  2. +40
    -0
      spring-boot-demo-ratelimit-redis/src/main/java/com/xkcoding/ratelimit/redis/controller/TestController.java
  3. +24
    -0
      spring-boot-demo-ratelimit-redis/src/main/java/com/xkcoding/ratelimit/redis/handler/GlobalExceptionHandler.java
  4. +4
    -0
      spring-boot-demo-ratelimit-redis/src/main/resources/application.yml

+ 48
- 0
spring-boot-demo-ratelimit-redis/src/main/java/com/xkcoding/ratelimit/redis/annotation/RateLimiter.java View File

@@ -0,0 +1,48 @@
package com.xkcoding.ratelimit.redis.annotation;

import org.springframework.core.annotation.AliasFor;
import org.springframework.core.annotation.AnnotationUtils;

import java.lang.annotation.*;
import java.util.concurrent.TimeUnit;

/**
* <p>
* 限流注解,添加了 {@link AliasFor} 必须通过 {@link AnnotationUtils} 获取,才会生效
* </p>
*
* @author yangkai.shen
* @date Created in 2019/9/30 10:31
* @see AnnotationUtils
*/
@Target(ElementType.METHOD)
@Retention(RetentionPolicy.RUNTIME)
@Documented
public @interface RateLimiter {
long DEFAULT_REQUEST = 10;

/**
* max 最大请求数
*/
@AliasFor("max") long value() default DEFAULT_REQUEST;

/**
* max 最大请求数
*/
@AliasFor("value") long max() default DEFAULT_REQUEST;

/**
* 限流key
*/
String key() default "";

/**
* 超时时长,默认1分钟
*/
long timeout() default 1;

/**
* 超时时间单位,默认 分钟
*/
TimeUnit timeUnit() default TimeUnit.MINUTES;
}

+ 40
- 0
spring-boot-demo-ratelimit-redis/src/main/java/com/xkcoding/ratelimit/redis/controller/TestController.java View File

@@ -0,0 +1,40 @@
package com.xkcoding.ratelimit.redis.controller;

import cn.hutool.core.lang.Dict;
import com.xkcoding.ratelimit.redis.annotation.RateLimiter;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

/**
* <p>
* 测试
* </p>
*
* @author yangkai.shen
* @date Created in 2019/9/30 10:30
*/
@Slf4j
@RestController
public class TestController {

@RateLimiter(value = 5)
@GetMapping("/test1")
public Dict test1() {
log.info("【test1】被执行了。。。。。");
return Dict.create().set("msg", "hello,world!").set("description", "别想一直看到我,不信你快速刷新看看~");
}

@GetMapping("/test2")
public Dict test2() {
log.info("【test2】被执行了。。。。。");
return Dict.create().set("msg", "hello,world!").set("description", "我一直都在,卟离卟弃");
}

@RateLimiter(value = 2)
@GetMapping("/test3")
public Dict test3() {
log.info("【test3】被执行了。。。。。");
return Dict.create().set("msg", "hello,world!").set("description", "别想一直看到我,不信你快速刷新看看~");
}
}

+ 24
- 0
spring-boot-demo-ratelimit-redis/src/main/java/com/xkcoding/ratelimit/redis/handler/GlobalExceptionHandler.java View File

@@ -0,0 +1,24 @@
package com.xkcoding.ratelimit.redis.handler;

import cn.hutool.core.lang.Dict;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

/**
* <p>
* 全局异常拦截
* </p>
*
* @author yangkai.shen
* @date Created in 2019/9/30 10:30
*/
@Slf4j
@RestControllerAdvice
public class GlobalExceptionHandler {

@ExceptionHandler(RuntimeException.class)
public Dict handler(RuntimeException ex) {
return Dict.create().set("msg", ex.getMessage());
}
}

+ 4
- 0
spring-boot-demo-ratelimit-redis/src/main/resources/application.yml View File

@@ -0,0 +1,4 @@
server:
port: 8080
servlet:
context-path: /demo

Loading…
Cancel
Save