ソースを参照

数据库变化检测保存日志

zhangshuling 1 年間 前
コミット
ccbfb2cf67

+ 71 - 10
xzl-admin/src/main/java/com/xzl/web/mysql/MysqlBinLogClient.java

@@ -11,20 +11,25 @@ package com.xzl.web.mysql;
 
 import com.github.shyiko.mysql.binlog.BinaryLogClient;
 import com.github.shyiko.mysql.binlog.event.*;
+import com.xzl.system.domain.SysDataLog;
+import com.xzl.system.service.ISysDataLogService;
 import org.apache.commons.collections.MapUtils;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
+import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.beans.factory.annotation.Value;
 import org.springframework.boot.ApplicationArguments;
 import org.springframework.boot.ApplicationRunner;
+import org.springframework.data.redis.core.RedisTemplate;
 import org.springframework.stereotype.Component;
 
 import java.io.IOException;
 import java.util.HashMap;
+import java.util.List;
 import java.util.Map;
 
 //此类可以监控MySQL库数据的增删改
-//@Component
+@Component
 //在SpringBoot中,提供了一个接口:ApplicationRunner。
 //该接口中,只有一个run方法,他执行的时机是:spring容器启动完成之后,就会紧接着执行这个接口实现类的run方法。
 public class MysqlBinLogClient implements ApplicationRunner {
@@ -38,6 +43,16 @@ public class MysqlBinLogClient implements ApplicationRunner {
   @Value("${spring.datasource.druid.master.password}")
   String password;
 
+  @Value("#{'${mysql.binlog.tables}'.split(',')}")
+  List<String> tables;
+
+  @Autowired
+  ISysDataLogService sysDataLogService;
+
+  @Autowired
+  RedisTemplate redisTemplate;
+
+  String redisKeyMysqlTable = "mysql:table:";
 
   @Override
   public void run(ApplicationArguments args) throws Exception {
@@ -64,39 +79,56 @@ public class MysqlBinLogClient implements ApplicationRunner {
 
     client.setServerId(100); //和自己之前设置的server-id保持一致,但是我不知道为什么不一致也能成功
 
-//下面直接照抄就行
     client.registerEventListener(event -> {
       EventData data = event.getData();
+      if (data instanceof TableMapEventData) {
+        TableMapEventData tableMapEventData = (TableMapEventData) data;
+        String tableName = tableMapEventData.getDatabase() + "." + tableMapEventData.getTable();
+        long tableId = tableMapEventData.getTableId();
+        // 存放到redis中(没有就增加)
+        redisTemplate.opsForValue().setIfAbsent(redisKeyMysqlTable + tableId, tableName);
+      }
+      if (data instanceof UpdateRowsEventData) { //表数据发生修改时触发
+        UpdateRowsEventData eventData = (UpdateRowsEventData) data;
+        saveData(eventData.getTableId(), data);
+      } else if (data instanceof WriteRowsEventData) { //表数据发生插入时触发
+        WriteRowsEventData eventData = (WriteRowsEventData) data;
+        saveData(eventData.getTableId(), data);
+      } else if (data instanceof DeleteRowsEventData) {//表数据发生删除后触发
+        DeleteRowsEventData eventData = (DeleteRowsEventData) data;
+        saveData(eventData.getTableId(), data);
+      }
+      /*
+      String threadName = Thread.currentThread().getName();
       if (data instanceof TableMapEventData) {
         //只要连接的MySQL发生的增删改的操作,则都会进入这里,无论哪个数据库
 
         TableMapEventData tableMapEventData = (TableMapEventData) data;
 
         //可以通过转成TableMapEventData类实例的tableMapEventData来获取当前发生变更的数据库
-        System.err.println("发生变更的数据库:" + tableMapEventData.getDatabase());
+        System.err.println("[" + threadName + "]" + "发生变更的数据库:" + tableMapEventData.getDatabase());
 
-        System.err.print("TableID:");
+        System.err.print("[" + threadName + "]" + "TableID:");
         //表ID
         System.err.println(tableMapEventData.getTableId());
-        System.err.print("TableName:");
+        System.err.print("[" + threadName + "]" + "TableName:");
         //表名字
         System.err.println(tableMapEventData.getTable());
       }
       //表数据发生修改时触发
       if (data instanceof UpdateRowsEventData) {
-        System.err.println("Update:");
+        System.err.print("[" + threadName + "]" + "Update:");
         System.err.println(data.toString());
         //表数据发生插入时触发
       } else if (data instanceof WriteRowsEventData) {
-        System.err.println("Insert:");
+        System.err.print("[" + threadName + "]" + "Insert:");
         System.err.println(data.toString());
         //表数据发生删除后触发
       } else if (data instanceof DeleteRowsEventData) {
-        System.err.println("Delete:");
+        System.err.print("[" + threadName + "]" + "Delete:");
         System.err.println(data.toString());
-      }
+      }*/
     });
-
     try {
       client.connect();
     } catch (IOException e) {
@@ -116,4 +148,33 @@ public class MysqlBinLogClient implements ApplicationRunner {
     rs.put("port", port);
     return rs;
   }
+
+  private void saveData(long tableId, EventData data) {
+    String tableName = (String) redisTemplate.opsForValue().get(redisKeyMysqlTable + tableId);
+    if (tableName == null) {
+      logger.info("没取到表名 ...");
+      return;
+    }
+    if (tables.contains(tableName)) {
+      String[] db = tableName.split("\\.");
+      SysDataLog sysDataLog = new SysDataLog();
+      sysDataLog.setDatabaseName(db[0]);
+      sysDataLog.setTableName(db[1]);
+      if (data instanceof WriteRowsEventData) { //表数据发生插入时触发
+        WriteRowsEventData eventData = (WriteRowsEventData) data;
+        sysDataLog.setUpdateCount(Long.valueOf(eventData.getRows().size()));
+        sysDataLog.setActionType(0);
+      } else if (data instanceof UpdateRowsEventData) { //表数据发生修改时触发
+        UpdateRowsEventData eventData = (UpdateRowsEventData) data;
+        sysDataLog.setUpdateCount(Long.valueOf(eventData.getRows().size()));
+        sysDataLog.setActionType(1);
+      } else if (data instanceof DeleteRowsEventData) {//表数据发生删除后触发
+        DeleteRowsEventData eventData = (DeleteRowsEventData) data;
+        sysDataLog.setUpdateCount(Long.valueOf(eventData.getRows().size()));
+        sysDataLog.setActionType(2);
+      }
+      sysDataLog.setDataContent(data.toString());
+      sysDataLogService.insertDataLog(sysDataLog);
+    }
+  }
 }

+ 5 - 0
xzl-admin/src/main/resources/application.yml

@@ -129,3 +129,8 @@ xss:
   excludes: /system/notice
   # 匹配链接
   urlPatterns: /system/*,/monitor/*,/tool/*
+
+# binglog需要监控的表
+mysql:
+  binlog:
+    tables: db_cgb.sys_log, db_cgb.3rd_file ,xtdb_standard.buss_file,xtdb.sys_logininfor

+ 25 - 75
xzl-system/src/main/java/com/xzl/system/domain/SysDataLog.java

@@ -20,38 +20,12 @@ public class SysDataLog extends BaseEntity {
   @Excel(name = "操作序号", cellType = Excel.ColumnType.NUMERIC)
   private Long logId;
 
-  @Excel(name = "日志内容")
-  private String logContent;
-
-  @Excel(name = "操作用户ID")
-  private String userId;
-
-  @Excel(name = "操作地址")
-  private String ip;
-
-  @Excel(name = "请求方法")
-  private String method;
-  /**
-   * 请求参数
-   */
-  @Excel(name = "请求参数")
-  private String param;
-
-  /**
-   * 返回参数
-   */
-  @Excel(name = "返回参数")
-  private String result;
-
-  /**
-   * 操作状态(0正常 1异常)
-   */
-  @Excel(name = "状态", readConverterExp = "0=正常,1=异常")
-  private Integer status;
-
-  @Excel(name = "错误消息")
-  private String errorMsg;
 
+  private String databaseName;
+  private String tableName;
+  private Integer actionType;
+  private Long updateCount;
+  private String dataContent;
 
   public Long getLogId() {
     return logId;
@@ -61,67 +35,43 @@ public class SysDataLog extends BaseEntity {
     this.logId = logId;
   }
 
-  public String getLogContent() {
-    return logContent;
-  }
-
-  public void setLogContent(String logContent) {
-    this.logContent = logContent;
-  }
-
-  public String getUserId() {
-    return userId;
-  }
-
-  public void setUserId(String userId) {
-    this.userId = userId;
-  }
-
-  public String getIp() {
-    return ip;
-  }
-
-  public void setIp(String ip) {
-    this.ip = ip;
-  }
-
-  public String getMethod() {
-    return method;
+  public String getDatabaseName() {
+    return databaseName;
   }
 
-  public void setMethod(String method) {
-    this.method = method;
+  public void setDatabaseName(String databaseName) {
+    this.databaseName = databaseName;
   }
 
-  public String getParam() {
-    return param;
+  public String getTableName() {
+    return tableName;
   }
 
-  public void setParam(String param) {
-    this.param = param;
+  public void setTableName(String tableName) {
+    this.tableName = tableName;
   }
 
-  public String getResult() {
-    return result;
+  public Integer getActionType() {
+    return actionType;
   }
 
-  public void setResult(String result) {
-    this.result = result;
+  public void setActionType(Integer actionType) {
+    this.actionType = actionType;
   }
 
-  public Integer getStatus() {
-    return status;
+  public Long getUpdateCount() {
+    return updateCount;
   }
 
-  public void setStatus(Integer status) {
-    this.status = status;
+  public void setUpdateCount(Long updateCount) {
+    this.updateCount = updateCount;
   }
 
-  public String getErrorMsg() {
-    return errorMsg;
+  public String getDataContent() {
+    return dataContent;
   }
 
-  public void setErrorMsg(String errorMsg) {
-    this.errorMsg = errorMsg;
+  public void setDataContent(String dataContent) {
+    this.dataContent = dataContent;
   }
 }

+ 7 - 7
xzl-system/src/main/java/com/xzl/system/service/impl/SysDataLogServiceImpl.java

@@ -35,13 +35,13 @@ public class SysDataLogServiceImpl implements ISysDataLogService {
 
 
     // 补充登录用户, IP等公共信息
-    try {
-      LoginUser loginUser = SecurityUtils.getLoginUser();
-      dataLog.setUserId(loginUser.getUserId().toString());
-    } catch (Exception e) {
-      logger.error("获取登录用户异常", e);
-    }
-    dataLog.setIp(IpUtils.getIpAddr());
+//    try {
+//      LoginUser loginUser = SecurityUtils.getLoginUser();
+//      dataLog.setUserId(loginUser.getUserId().toString());
+//    } catch (Exception e) {
+//      logger.error("获取登录用户异常", e);
+//    }
+//    dataLog.setIp(IpUtils.getIpAddr());
     sysDataLogMapper.insertDataLog(dataLog);
   }
 

+ 0 - 3
xzl-system/src/main/java/com/xzl/system/service/impl/SysUserServiceImpl.java

@@ -76,9 +76,6 @@ public class SysUserServiceImpl implements ISysUserService {
   @Override
   @DataScope(deptAlias = "d", userAlias = "u")
   public List<SysUser> selectUserList(SysUser user) {
-    SysDataLog dataLog = new SysDataLog();
-    dataLog.setLogContent("查询用户信息 ...");
-    dataLogService.insertDataLog(dataLog);
     return userMapper.selectUserList(user);
   }
 

+ 11 - 21
xzl-system/src/main/resources/mapper/system/SysDataLogMapper.xml

@@ -3,43 +3,33 @@
         PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
         "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
 <mapper namespace="com.xzl.system.mapper.SysDataLogMapper">
-
     <resultMap type="SysDataLog" id="SysDataLogResult">
         <id property="logId" column="log_id"/>
-        <result property="logContent" column="log_content"/>
-        <result property="method" column="method"/>
-        <result property="userId" column="user_id"/>
-        <result property="ip" column="ip"/>
-        <result property="param" column="param"/>
-        <result property="result" column="result"/>
-        <result property="status" column="status"/>
-        <result property="errorMsg" column="error_msg"/>
+        <result property="databaseName" column="database_name"/>
+        <result property="tableName" column="table_name"/>
+        <result property="actionType" column="action_type"/>
+        <result property="updateCount" column="update_count"/>
+        <result property="dataContent" column="data_content"/>
+        <result property="createBy" column="create_by"/>
         <result property="createTime" column="create_time"/>
+        <result property="updateBy" column="update_by"/>
+        <result property="updateTime" column="update_time"/>
     </resultMap>
 
     <sql id="selectDataLogVo">
-        select log_id, log_content, method, user_id, ip, param, result, status, error_msg, create_time
+        select log_id, database_name, table_name, action_type, update_count, data_content,create_by, create_time, update_by, update_time
         from sys_data_log
     </sql>
 
     <insert id="insertDataLog" parameterType="SysDataLog">
-		insert into sys_data_log(log_content, method, user_id, ip, param, result, status, error_msg, create_time)
-        values (#{logContent}, #{method}, #{userId}, #{ip}, #{param}, #{result}, #{status}, #{errorMsg}, sysdate())
+		insert into sys_data_log(database_name, table_name, action_type, update_count, data_content, create_time)
+        values (#{databaseName}, #{tableName}, #{actionType}, #{updateCount}, #{dataContent}, sysdate())
 	</insert>
 
 
     <select id="selectDataLogList" parameterType="SysDataLog" resultMap="SysDataLogResult">
         <include refid="selectDataLogVo"/>
         <where>
-            <if test="ip != null and ip != ''">
-                AND ip like concat('%', #{ip}, '%')
-            </if>
-            <if test="userId != null">
-                AND user_id = #{userId}
-            </if>
-            <if test="status != null">
-                AND status = #{status}
-            </if>
             <if test="params.beginTime != null and params.beginTime != ''"><!-- 开始时间检索 -->
                 AND create_time &gt;= #{params.beginTime}
             </if>