jtoms 4 éve
szülő
commit
501025c7ce

+ 1 - 1
pom.xml

@@ -24,7 +24,7 @@
         <mysql-connector-java.version>8.0.27</mysql-connector-java.version>
         <mybatis-spring-boot-starter.version>2.2.0</mybatis-spring-boot-starter.version>
         <mybatis.version>3.5.7</mybatis.version>
-        <framework.version>1.0.12</framework.version>
+        <framework.version>1.0.15</framework.version>
 
         <!--        <slf4j.version>1.7.32</slf4j.version>-->
         <!--        <commons-lang3.version>3.12.0</commons-lang3.version>-->

+ 9 - 0
src/main/java/com/zhiqiyun/open/core/mapper/ApiRequestLogMapper.java

@@ -0,0 +1,9 @@
+package com.zhiqiyun.open.core.mapper;
+
+import com.baomidou.mybatisplus.core.mapper.BaseMapper;
+import com.zhiqiyun.open.core.models.ApiRequestLog;
+import org.apache.ibatis.annotations.Mapper;
+
+@Mapper
+public interface ApiRequestLogMapper extends BaseMapper<ApiRequestLog> {
+}

+ 22 - 0
src/main/java/com/zhiqiyun/open/core/models/ApiRequestLog.java

@@ -0,0 +1,22 @@
+package com.zhiqiyun.open.core.models;
+
+import com.baomidou.mybatisplus.annotation.IdType;
+import com.baomidou.mybatisplus.annotation.TableId;
+import com.baomidou.mybatisplus.annotation.TableName;
+import lombok.Data;
+
+import java.util.Date;
+
+@Data
+@TableName("api_request_log")
+public class ApiRequestLog {
+    @TableId(type = IdType.INPUT)
+    private String id;
+    private String appKey;
+    private String method;
+    private String version;
+    private String requestBody;
+    private Date serviceBeginTime;
+    private Date serviceEndTime;
+    private String clientIp;
+}

+ 7 - 0
src/main/java/com/zhiqiyun/open/core/service/ApiRequestLogService.java

@@ -0,0 +1,7 @@
+package com.zhiqiyun.open.core.service;
+
+import com.baomidou.mybatisplus.extension.service.IService;
+import com.zhiqiyun.open.core.models.ApiRequestLog;
+
+public interface ApiRequestLogService extends IService<ApiRequestLog> {
+}

+ 11 - 0
src/main/java/com/zhiqiyun/open/core/service/impl/ApiRequestLogServiceImpl.java

@@ -0,0 +1,11 @@
+package com.zhiqiyun.open.core.service.impl;
+
+import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
+import com.zhiqiyun.open.core.mapper.ApiRequestLogMapper;
+import com.zhiqiyun.open.core.models.ApiRequestLog;
+import com.zhiqiyun.open.core.service.ApiRequestLogService;
+import org.springframework.stereotype.Service;
+
+@Service
+public class ApiRequestLogServiceImpl extends ServiceImpl<ApiRequestLogMapper, ApiRequestLog> implements ApiRequestLogService {
+}

+ 37 - 1
src/main/java/com/zhiqiyun/open/mvc/manager/controller/AppKeyInfoController.java

@@ -5,9 +5,13 @@ import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
 import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
 import com.zhiqiyun.open.annotation.Permission;
 import com.zhiqiyun.open.core.enmus.YN;
+import com.zhiqiyun.open.core.models.ApiRequestLog;
 import com.zhiqiyun.open.core.models.AppKeyInfo;
+import com.zhiqiyun.open.core.service.ApiRequestLogService;
 import com.zhiqiyun.open.mvc.Result;
+import com.zhiqiyun.open.mvc.manager.params.QueryApiRequestLogParams;
 import com.zhiqiyun.open.mvc.manager.params.QueryAppKeyInfoParams;
+import com.zhiqiyun.open.mvc.manager.params.QueryPageParams;
 import com.zhiqiyun.open.mvc.manager.params.SaveAppKeyInfoParam;
 import com.zhiqiyun.open.core.service.AppKeyInfoService;
 import com.zhiqiyun.open.core.service.SequenceService;
@@ -33,9 +37,12 @@ public class AppKeyInfoController {
     @Autowired
     private SequenceService sequenceService;
 
+    @Autowired
+    private ApiRequestLogService apiRequestLogService;
+
     @Permission(value = "app.key.find", tags = "查询AppKey")
     @PostMapping("/findPage")
-    public Result findRolePage(@RequestBody QueryAppKeyInfoParams params) {
+    public Result findPage(@RequestBody QueryAppKeyInfoParams params) {
 
         QueryWrapper<AppKeyInfo> queryWrapper = new QueryWrapper<>();
         if (params.getId() != null) {
@@ -52,6 +59,35 @@ public class AppKeyInfoController {
         return Result.instance(Result.Code.SUCCESS).setData(resultData);
     }
 
+    @Permission(value = "app.key.find", tags = "Api调用日志查询")
+    @PostMapping("/findRequestLogsPage")
+    public Result findRequestLogsPage(@RequestBody QueryApiRequestLogParams params) {
+
+        QueryWrapper<ApiRequestLog> queryWrapper = new QueryWrapper<>();
+        if (StringUtils.isNotBlank(params.getAppKey())) {
+            queryWrapper.eq("app_key", params.getAppKey());
+        }
+        if (StringUtils.isNotBlank(params.getMethod())) {
+            queryWrapper.eq("method", params.getMethod());
+        }
+        if (StringUtils.isNotBlank(params.getVersion())) {
+            queryWrapper.eq("version", params.getVersion());
+        }
+        if (StringUtils.isNotBlank(params.getClientIp())) {
+            queryWrapper.eq("client_ip", params.getClientIp());
+        }
+        if (StringUtils.isNotBlank(params.getRequestBody())) {
+            queryWrapper.like("request_body", params.getRequestBody());
+        }
+        if (params.getServiceBeginTime() != null) {
+            queryWrapper.between("service_begin_time", params.getServiceBeginTime().getStatDate(), params.getServiceBeginTime().getEndDate());
+        }
+
+        Page<ApiRequestLog> resultData = this.apiRequestLogService.page(params.getPage(), queryWrapper);
+
+        return Result.instance(Result.Code.SUCCESS).setData(resultData);
+    }
+
     @Permission(value = "app.key.add", tags = "新建AppKey")
     @PostMapping("/save")
     public Result save(@RequestBody SaveAppKeyInfoParam param) {

+ 25 - 0
src/main/java/com/zhiqiyun/open/mvc/manager/params/QueryApiRequestLogParams.java

@@ -0,0 +1,25 @@
+package com.zhiqiyun.open.mvc.manager.params;
+
+import com.zhiqiyun.open.core.models.RangeDate;
+import lombok.Data;
+import lombok.EqualsAndHashCode;
+
+import java.util.Date;
+import java.util.List;
+
+@Data
+@EqualsAndHashCode(callSuper = true)
+public class QueryApiRequestLogParams extends QueryPageParams {
+
+    private String appKey;
+    private String method;
+    private String version;
+    private String requestBody;
+    private RangeDate serviceBeginTime;
+    private String clientIp;
+
+    public void setServiceBeginTime(List<String> loginTime) {
+        this.serviceBeginTime = RangeDate.build(loginTime);
+    }
+
+}

+ 59 - 0
src/main/java/com/zhiqiyun/open/router/config/RequestLogInterceptor.java

@@ -0,0 +1,59 @@
+package com.zhiqiyun.open.router.config;
+
+import com.alibaba.fastjson.JSON;
+import com.dliyun.oap.framework.annotation.ServiceMethod;
+import com.dliyun.oap.framework.context.OapRequestContext;
+import com.dliyun.oap.framework.interceptor.AbstractOapInterceptor;
+import com.zhiqiyun.open.core.models.ApiRequestLog;
+import com.zhiqiyun.open.core.service.ApiRequestLogService;
+import com.zhiqiyun.open.utils.DateUtil;
+import lombok.extern.slf4j.Slf4j;
+import org.apache.commons.lang3.time.DateUtils;
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;
+import org.springframework.stereotype.Component;
+
+import java.util.Date;
+import java.util.Map;
+
+/**
+ * @author stjdydayou
+ */
+@Component
+@Slf4j
+public class RequestLogInterceptor extends AbstractOapInterceptor {
+
+    @Autowired
+    private ThreadPoolTaskExecutor threadPoolTaskExecutor;
+
+    @Autowired
+    private ApiRequestLogService apiRequestLogService;
+
+    @Override
+    public void beforeService(OapRequestContext orc, Map<String, Object> requestParams) {
+        ServiceMethod serviceMethod = orc.getServiceMethodHandler().getHandlerMethod().getAnnotation(ServiceMethod.class);
+        log.debug("beforeService ...{}", serviceMethod.method());
+    }
+
+    @Override
+    public void beforeResponse(OapRequestContext orc, Map<String, Object> requestParams) {
+        ServiceMethod serviceMethod = orc.getServiceMethodHandler().getHandlerMethod().getAnnotation(ServiceMethod.class);
+        log.debug("beforeResponse ...{}", serviceMethod.method());
+        this.threadPoolTaskExecutor.execute(() -> {
+            try {
+                ApiRequestLog requestLog = new ApiRequestLog();
+                requestLog.setId(orc.getRequestId());
+                requestLog.setAppKey(orc.getAppKey());
+                requestLog.setMethod(orc.getMethod());
+                requestLog.setVersion(orc.getVersion());
+                requestLog.setRequestBody(JSON.toJSONString(requestParams));
+                requestLog.setServiceBeginTime(new Date(orc.getServiceBeginTime()));
+                requestLog.setServiceEndTime(new Date(orc.getServiceEndTime()));
+                requestLog.setClientIp(orc.getClientIp());
+                this.apiRequestLogService.save(requestLog);
+            } catch (Exception e) {
+                log.warn("save ApiRequestLog error", e);
+            }
+        });
+    }
+}

+ 22 - 2
src/main/resources/db/migration/V1.0.1__app_key.sql

@@ -4,7 +4,8 @@ VALUES (1300, 0, 'app.key', 'AppKey管理'),
        (1302, 1300, 'app.key.add', '添加'),
        (1303, 1300, 'app.key.edit', '修改'),
        (1304, 1300, 'app.key.disable', '禁用'),
-       (1305, 1300, 'app.key.enable', '启用');
+       (1305, 1300, 'app.key.enable', '启用'),
+       (1306, 1300, 'app.key.request.logs', '日志查询');
 
 DROP TABLE IF EXISTS `app_key_info`;
 CREATE TABLE `app_key_info`
@@ -14,8 +15,27 @@ CREATE TABLE `app_key_info`
     `remark`       VARCHAR(100) NOT NULL DEFAULT '',
     `permissions`  TEXT         NULL     DEFAULT NULL,
     `is_enable`    INT(1)       NOT NULL,
-    `created_time` DATETIME   NOT NULL,
+    `created_time` DATETIME     NOT NULL,
     PRIMARY KEY (`id`),
     KEY `is_enable_index` (`is_enable`),
     KEY `created_time_index` (`created_time`)
 ) ENGINE = InnoDB;
+
+DROP TABLE IF EXISTS `api_request_log`;
+CREATE TABLE `api_request_log`
+(
+    `id`                 VARCHAR(32) NOT NULL,
+    `app_key`            VARCHAR(20) NULL DEFAULT NULL,
+    `method`             VARCHAR(50) NULL DEFAULT NULL,
+    `version`            VARCHAR(50) NULL DEFAULT NULL,
+    `request_body`       TEXT        NULL DEFAULT NULL,
+    `client_ip`          VARCHAR(64) NOT NULL,
+    `service_begin_time` DATETIME    NOT NULL,
+    `service_end_time`   DATETIME    NOT NULL,
+    PRIMARY KEY (`id`),
+    KEY `app_key_index` (`app_key`),
+    KEY `method_index` (`method`),
+    KEY `version_index` (`version`),
+    KEY `client_ip_index` (`client_ip`),
+    KEY `service_begin_time_index` (`service_begin_time`)
+) ENGINE = InnoDB;

+ 0 - 1
src/main/resources/oap.appSecret.properties

@@ -1 +0,0 @@
-A100=1234567890