Commit c4cafd64 by 雷紫添

增加限流空置。

parent 5b2fb8bc
......@@ -17,6 +17,8 @@
<java.version>1.8</java.version>
<jwzh.framework.version>2.1.5-SNAPSHOT</jwzh.framework.version>
<jwzh.manager-api.version>1.0.33-SNAPSHOT</jwzh.manager-api.version>
<hutool.version>5.4.5</hutool.version>
<guava.version>29.0-jre</guava.version>
</properties>
<dependencies>
......@@ -43,7 +45,23 @@
</dependency>
<dependency>
<groupId>cn.hutool</groupId>
<artifactId>hutool-all</artifactId>
<version>${hutool.version}</version>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>com.google.guava</groupId>
<artifactId>guava</artifactId>
<version>${guava.version}</version>
</dependency>
<!-- 数据库驱动包 -->
<dependency>
<groupId>org.oracle</groupId>
......
......@@ -7,6 +7,7 @@ import com.alibaba.fastjson.JSONObject;
import com.founder.common.*;
import com.founder.model.Face;
import com.founder.model.Jccj;
import com.founder.ratelimit.guava.annotation.RateLimiter;
import com.founder.service.AutoSeqService;
import com.founder.service.FaceService;
import com.founder.service.JccjService;
......@@ -46,12 +47,13 @@ public class JccjController {
private String ftpfilePath;
@ResponseBody
@PostMapping("/xxbs")
@RateLimiter(value = 2.0)
public Object queryDatas( HttpServletRequest request, @RequestBody Jccj jccj) {
//1.验证是否非法调用()
Map<String, Object> res = new HashMap<>();
String idStr=identityCardVerification.IdentityCardVerification(jccj.getUser_id());
if( !jccj.getUser_id().equals(idStr)){
res.put("status_code", "010103");
res.put("status_code", "010104");
res.put("message", idStr);
res.put("taskid", jccj.getTaskid());
return res;
......
package com.founder.ratelimit.guava.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} 获取,才会生效
*
* @author yangkai.shen
* @date Created in 2019-09-12 14:14
* @see AnnotationUtils
* </p>
*/
@Target(ElementType.METHOD)
@Retention(RetentionPolicy.RUNTIME)
@Documented
public @interface RateLimiter {
int NOT_LIMITED = 0;
/**
* qps
*/
@AliasFor("qps") double value() default NOT_LIMITED;
/**
* qps
*/
@AliasFor("value") double qps() default NOT_LIMITED;
/**
* 超时时长
*/
int timeout() default 0;
/**
* 超时时间单位
*/
TimeUnit timeUnit() default TimeUnit.MILLISECONDS;
}
package com.founder.ratelimit.guava.aspect;
import lombok.extern.slf4j.Slf4j;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.aspectj.lang.reflect.MethodSignature;
import org.springframework.core.annotation.AnnotationUtils;
import org.springframework.stereotype.Component;
import com.founder.ratelimit.guava.annotation.RateLimiter;
import java.lang.reflect.Method;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ConcurrentMap;
/**
* <p>
* 限流切面
* </p>
*
* @author yangkai.shen
* @date Created in 2019-09-12 14:27
*/
@Slf4j
@Aspect
@Component
public class RateLimiterAspect {
private static final ConcurrentMap<String, com.google.common.util.concurrent.RateLimiter> RATE_LIMITER_CACHE = new ConcurrentHashMap<>();
@Pointcut("@annotation(com.founder.ratelimit.guava.annotation.RateLimiter)")
public void rateLimit() {
}
@Around("rateLimit()")
public Object pointcut(ProceedingJoinPoint point) throws Throwable {
MethodSignature signature = (MethodSignature) point.getSignature();
Method method = signature.getMethod();
// 通过 AnnotationUtils.findAnnotation 获取 RateLimiter 注解
RateLimiter rateLimiter = AnnotationUtils.findAnnotation(method, RateLimiter.class);
if (rateLimiter != null && rateLimiter.qps() > RateLimiter.NOT_LIMITED) {
double qps = rateLimiter.qps();
if (RATE_LIMITER_CACHE.get(method.getName()) == null) {
// 初始化 QPS
RATE_LIMITER_CACHE.put(method.getName(), com.google.common.util.concurrent.RateLimiter.create(qps));
}
log.debug("【{}】的QPS设置为: {}", method.getName(), RATE_LIMITER_CACHE.get(method.getName()).getRate());
// 尝试获取令牌
if (RATE_LIMITER_CACHE.get(method.getName()) != null && !RATE_LIMITER_CACHE.get(method.getName()).tryAcquire(rateLimiter.timeout(), rateLimiter.timeUnit())) {
throw new RuntimeException("手速太快了,慢点儿吧~");
}
}
return point.proceed();
}
}
package com.founder.ratelimit.guava.controller;
import cn.hutool.core.lang.Dict;
import com.founder.ratelimit.guava.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-09-12 14:22
*/
@Slf4j
@RestController
public class TestController {
@RateLimiter(value = 1.0, timeout = 300)
@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.0, timeout = 300)
@GetMapping("/test3")
public Dict test3() {
log.info("【test3】被执行了。。。。。");
return Dict.create().set("msg", "hello,world!").set("description", "别想一直看到我,不信你快速刷新看看~");
}
}
package com.founder.ratelimit.guava.handler;
import cn.hutool.core.lang.Dict;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;
import java.util.HashMap;
import java.util.Map;
/**
* <p>
* 全局异常拦截
* </p>
*
* @author yangkai.shen
* @date Created in 2019-09-12 15:00
*/
@RestControllerAdvice
public class GlobalExceptionHandler {
Map<String, Object> res = new HashMap<>();
@ExceptionHandler(RuntimeException.class)
public Map handler(RuntimeException ex) {
res.put("status_code", "10304");
res.put("message", "数据量超限");
return res;
}
}
......@@ -34,19 +34,11 @@ public class ScheduleTask {
/*
* 定时任务从采集平台获取的信息发送给公安部
* */
@Scheduled(cron = "*/5 * * * * ?")
public void taskForDictUpdate(){
logger.info("This is a say method!"+format.format(new Date()));
// xxbsSerevice();
}
/*
* 定时执行-查询一个表中的数据,存到字典表或者字典js文件
* */
@Scheduled(cron = "*/5 * * * * ?")
public void taskForDictUpdateManager(){
logger.info("This is a say method!"+format.format(new Date()));
// dictSerevice();
}
private void xxbsSerevice(){
logger.info("This is a say method!"+format.format(new Date()));
......
......@@ -13,6 +13,8 @@ import java.io.FileInputStream;
import java.io.InputStream;
import java.net.URL;
import java.util.Base64;
import java.util.Date;
@Component
public class test {
......@@ -31,7 +33,7 @@ public class test {
byte[] in_b = swapStream.toByteArray(); //in_b为转换之后的结果
final String encodedText = encoder.encodeToString(in_b);*/
Jccj jccj =new Jccj();
File file = new File("D:/JCXX-R1100000500002014070008.zip");
File file = new File("D:/appone.zip");
FileInputStream inputFile = new FileInputStream(file);
byte[] buffer = new byte[(int)file.length()];
inputFile.read(buffer);
......@@ -40,13 +42,13 @@ public class test {
final String encodedText = encoder.encodeToString(buffer);
jccj.setXxsb_zip(encodedText);
jccj.setTaskid("JCXX-R1100000500002014070008");
jccj.setUser_id("14272818899753333333823");
jccj.setTaskid("JCXX-R1100000500002014070006");
jccj.setUser_id("350128197311050034");
jccj.setUser_dept("1400000000");
jccj.setVersion("1.00");
//发送,httpclient 包含zip包
JSONObject jsonResult= JccjServiceImpl.requestByPost("http://47.92.223.200:9061/jccj/xxbs",jccj);
System.out.println("返回的参数为::"+jsonResult.toJSONString());
JSONObject jsonResult= JccjServiceImpl.requestByPost("http://127.0.0.1:9061/jccj/xxbs",jccj);
System.out.println("返回的参数为::"+jsonResult.toJSONString()+new Date());
} catch (Exception e) {
e.printStackTrace();
......
......@@ -26,7 +26,7 @@ spring:
ftpserverip: 47.92.129.99
ftpport: 4546
ftpuser: ftpuser
ftppass: p@ssw0rd[123]
ftppass: fou3rfnder4SD1
ftpbathPath: /home/ftpuser/www/neimeng/FZXZ
ftpfilePath: /neimeng/FZXZ
redistimeout: 60000
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment