需求:统计研发测试云各个功能使用情况,即各项目下各个功能调用次数的统计
    初步解决方案:使用SpringAOP切面思想,自定义个注解,将注解放在关键接口上,以这个接口为切入点,当调用具有此注解的接口时对方法进行拦截,获取方法上的RequestMapping或者PostMapping等注解的属性值进行统计。

    1. package com.haiyisoft.cloud.devops.util;
    2. import com.haiyisoft.cloud.devops.web.util.UserViewUtil;
    3. import com.haiyisoft.cloud.jpa.util.JPAUtil;
    4. import com.haiyisoft.cloud.mservice.util.CommonUtil;
    5. import com.haiyisoft.cloud.mservice.util.SequenceUtil;
    6. import com.haiyisoft.cloud.web.annotation.PrimaryAccess;
    7. import com.haiyisoft.cloud.web.annotation.ProjectMapping;
    8. import com.haiyisoft.entity.devops.AccessInterface;
    9. import org.aspectj.lang.JoinPoint;
    10. import org.aspectj.lang.Signature;
    11. import org.aspectj.lang.annotation.*;
    12. import org.aspectj.lang.reflect.MethodSignature;
    13. import org.springframework.stereotype.Component;
    14. import org.springframework.web.bind.annotation.GetMapping;
    15. import org.springframework.web.bind.annotation.PostMapping;
    16. import org.springframework.web.bind.annotation.RequestMapping;
    17. import org.springframework.web.context.request.RequestContextHolder;
    18. import org.springframework.web.context.request.ServletRequestAttributes;
    19. import javax.servlet.http.HttpServletRequest;
    20. import java.lang.reflect.Method;
    21. import java.util.*;
    22. /**
    23. * @author seanwang
    24. * @version 1.0
    25. * @date 2021/3/1 15:22
    26. * 接口访问记录统计。
    27. */
    28. @Component
    29. @Aspect
    30. public class InterfaceAccessHandler{
    31. /**
    32. * 定义切面
    33. * Pointcut("execution(* com.haiyisoft.cloud.*.*.controller..*.*(..))")
    34. * com.haiyisoft.cloud.devops.*.controller包及其子包下的所有接口都会被统计
    35. */
    36. @Pointcut("@annotation(com.haiyisoft.cloud.web.annotation.PrimaryAccess)")
    37. public void pointCut(){
    38. }
    39. /**
    40. * 在接口原有的方法执行前,将会首先执行此处的代码
    41. */
    42. @Before("pointCut()")
    43. public void doBefore(JoinPoint joinPoint) {
    44. ServletRequestAttributes attributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
    45. HttpServletRequest request = attributes.getRequest();
    46. String projectIdStr = request.getParameter("projectId");
    47. Long projectId = null;
    48. if (projectIdStr != null){
    49. projectId = Long.parseLong(projectIdStr);
    50. }
    51. //记录请求信息
    52. Signature signature = joinPoint.getSignature();
    53. MethodSignature methodSignature = (MethodSignature) signature;
    54. Method method = methodSignature.getMethod();
    55. String value = "";
    56. String name = "";
    57. if (method.isAnnotationPresent(RequestMapping.class)) {
    58. RequestMapping requestMapping = method.getAnnotation(RequestMapping.class);
    59. value = Arrays.toString(requestMapping.value());
    60. name = requestMapping.name();
    61. }else if (method.isAnnotationPresent(GetMapping.class)) {
    62. GetMapping getMapping = method.getAnnotation(GetMapping.class);
    63. value = Arrays.toString(getMapping.value());
    64. name = getMapping.name();
    65. }else if (method.isAnnotationPresent(PostMapping.class)) {
    66. PostMapping postMapping = method.getAnnotation(PostMapping.class);
    67. value = Arrays.toString(postMapping.value());
    68. name = postMapping.name();
    69. }else if (method.isAnnotationPresent(ProjectMapping.class)) {
    70. ProjectMapping projectMapping = method.getAnnotation(ProjectMapping.class);
    71. value = Arrays.toString(projectMapping.value());
    72. name = projectMapping.name();
    73. }else if (method.isAnnotationPresent(PrimaryAccess.class)) {
    74. PrimaryAccess primaryAccess = method.getAnnotation(PrimaryAccess.class);
    75. value = primaryAccess.accessInterface();
    76. name = primaryAccess.desc();
    77. }
    78. //持久化
    79. collect(value,name,projectId);
    80. }
    81. private void collect(String interfaceName, String interfaceDesc, Long projectId) {
    82. AccessInterface accessInterface = new AccessInterface();
    83. accessInterface.setAccessId(SequenceUtil.genEntitySequenceNo(AccessInterface.class));
    84. accessInterface.setInterfaceUrl(formatUrl(interfaceName));
    85. accessInterface.setInterfaceDesc(interfaceDesc);
    86. accessInterface.setAccessTime(CommonUtil.getDateTime());
    87. accessInterface.setProjectId(projectId);
    88. try {
    89. accessInterface.setAccessUser(UserViewUtil.getUserName());
    90. }catch (NullPointerException e){}
    91. JPAUtil.create(accessInterface);
    92. }
    93. /**
    94. * [/getNoticeCount] ==> /getNoticeCount
    95. */
    96. private String formatUrl(String url) {
    97. return url.substring(1,url.length()-1);
    98. }
    99. }