支持按包扫描class中所有的Controller接口
功能:
支持java -jar **.war的模式,以反射形式按包名扫描所有的类(含jar包中的类),以便于获取类的:接口,注解,以及接口注解的属性值等信息
按包名扫描class中所有的Controller接口(支持从springboot的war中再查找jar包中的类)
同时支持扫描class中的Annotation注解及注解的属性值
/**
* 从类中直接加载所有controller类的接口信息
* 并可与正在运行的接口关联
*
* @param request HttpServletRequest
* @param requestVo RequestVo
* @return ResultData
*/
@RequestMapping(value = "/requestScans", method = {RequestMethod.POST, RequestMethod.GET})
@CheckLogin
public ResultData requestScans(HttpServletRequest request, RequestVo requestVo
, @RequestParam(value = "type", required = false, defaultValue = "current") String type
, @RequestParam(value = "day", required = false, defaultValue = "-1") Integer day) {
String token = RequestUtils.getToken(request);
UserVo userVo = dflUserLoginBiz.getUserByToken(token);
checkUserPermission(userVo, UserOperType.VIEW);
String pkg = request.getParameter("pkg");
pkg = (String) CommUtils.nvl(pkg, "org.ccs.opendfl");
AuditLogUtils.addAuditLog(request, userVo, "list", "ok", TIME_NULL);
//找出Controller下的所有注解,以及频率限制、分布式锁配置参数
List<RequestVo> list = AnnotationControllerUtils.getControllerRequests(pkg);
List<RequestShowVo> showList = list.stream().map(RequestShowVo.class::cast).collect(Collectors.toList());
//支持按requestUri查询接口
if (StringUtils.isNotBlank(requestVo.getRequestUri())) {
showList = showList.stream().filter(t -> t.getRequestUri().contains(requestVo.getRequestUri())).collect(Collectors.toList());
}
//显示从启动到现在各接口的调用情况(调用次数,调用时间超过1秒的最大时长,超限次数)
requestRunCount(type, day, showList);
return ResultData.success(showList);
}
功能查询效果如下

支持查近期的数据
每天以redis的zset存一份对每个有调用的接口调用次数,执行最大时长的记录
系统用本地缓存记录调用次数,通过异步单线程每10秒同步一次到redis的zset

核心代码
见:opendfl项目中的以下代码:
org.ccs.opendfl.core.utils.AnnotationControllerUtils.java
org.ccs.opendfl.core.utils.AnnotationClassUtils.java
本文介绍了一种通过包名扫描所有Controller接口的方法,并展示了如何获取接口、注解及其属性值等信息。支持从Spring Boot的WAR文件中查找Jar包内的类。此外,还介绍了如何查询接口的调用情况,包括调用次数、最大执行时长等。
484





