zhangshuling 1 rok temu
rodzic
commit
ca69507932

+ 16 - 0
xzl-admin/pom.xml

@@ -86,6 +86,22 @@
             <artifactId>lombok</artifactId>
         </dependency>
 
+
+        <!-- 监听MySQL的binlog实现监听数据变化 -->
+
+        <dependency>
+            <groupId>com.zendesk</groupId>
+            <artifactId>mysql-binlog-connector-java</artifactId>
+            <version>0.27.1</version>
+        </dependency>
+        <!--
+        <dependency>
+            <groupId>com.github.shyiko</groupId>
+            <artifactId>mysql-binlog-connector-java</artifactId>
+            <version>0.16.1</version>
+        </dependency>
+        -->
+
     </dependencies>
 
     <build>

+ 1 - 1
xzl-admin/src/main/java/com/xzl/web/core/config/SwaggerConfig.java

@@ -113,7 +113,7 @@ public class SwaggerConfig
         // 用ApiInfoBuilder进行定制
         return new ApiInfoBuilder()
                 // 设置标题
-                .title("标题:管理系统_接口文档")
+                .title("标题:XZL管理系统_接口文档")
                 // 描述
                 .description("描述:用于管理集团旗下公司的人员信息,具体包括XXX,XXX模块...")
                 // 作者信息

+ 119 - 0
xzl-admin/src/main/java/com/xzl/web/mysql/MysqlBinLogClient.java

@@ -0,0 +1,119 @@
+package com.xzl.web.mysql;
+
+/**
+ * @ClassName: MysqlBinLogClient
+ * @Description: 用于
+ * Modification History:
+ * Date                  Author                 Version       Description
+ * ---------------------------------------------------------
+ * 2023/10/16             ZhangShuling      v1.0.0
+ */
+
+import com.github.shyiko.mysql.binlog.BinaryLogClient;
+import com.github.shyiko.mysql.binlog.event.*;
+import org.apache.commons.collections.MapUtils;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+import org.springframework.beans.factory.annotation.Value;
+import org.springframework.boot.ApplicationArguments;
+import org.springframework.boot.ApplicationRunner;
+import org.springframework.stereotype.Component;
+
+import java.io.IOException;
+import java.util.HashMap;
+import java.util.Map;
+
+//此类可以监控MySQL库数据的增删改
+//@Component
+//在SpringBoot中,提供了一个接口:ApplicationRunner。
+//该接口中,只有一个run方法,他执行的时机是:spring容器启动完成之后,就会紧接着执行这个接口实现类的run方法。
+public class MysqlBinLogClient implements ApplicationRunner {
+
+  private static final Logger logger = LoggerFactory.getLogger(MysqlBinLogClient.class);
+
+  @Value("${spring.datasource.druid.master.url}")
+  String url;
+  @Value("${spring.datasource.druid.master.username}")
+  String username;
+  @Value("${spring.datasource.druid.master.password}")
+  String password;
+
+
+  @Override
+  public void run(ApplicationArguments args) throws Exception {
+    //项目启动完成连接bin-log
+    new Thread(() -> {
+      connectMysqlBinLog();
+    }).start();
+
+  }
+
+  /**
+   * 连接mysqlBinLog
+   */
+  public void connectMysqlBinLog() {
+    logger.info("监控BinLog服务已启动");
+    //自己MySQL的信息。host,port,username,password
+    Map dbMap = parseDbUrl(url);
+    BinaryLogClient client = new BinaryLogClient(MapUtils.getString(dbMap, "host"), MapUtils.getIntValue(dbMap, "port"), username, password);
+    /**因为binlog不是以数据库为单位划分的,所以监控binglog不是监控的单个的数据库,而是整个当前所设置连接的MySQL,
+     *其中任何一个库发生数据增删改,这里都能检测到,
+     *所以不用设置所监控的数据库的名字(我也不知道怎么设置,没发现有包含这个形参的构造函数)
+     *如果需要只监控指定的数据库,可以看后面代码,可以获取到当前发生变更的数据库名称。可以根据名称来决定是否监控
+     **/
+
+    client.setServerId(100); //和自己之前设置的server-id保持一致,但是我不知道为什么不一致也能成功
+
+//下面直接照抄就行
+    client.registerEventListener(event -> {
+      EventData data = event.getData();
+      if (data instanceof TableMapEventData) {
+        //只要连接的MySQL发生的增删改的操作,则都会进入这里,无论哪个数据库
+
+        TableMapEventData tableMapEventData = (TableMapEventData) data;
+
+        //可以通过转成TableMapEventData类实例的tableMapEventData来获取当前发生变更的数据库
+        System.err.println("发生变更的数据库:" + tableMapEventData.getDatabase());
+
+        System.err.print("TableID:");
+        //表ID
+        System.err.println(tableMapEventData.getTableId());
+        System.err.print("TableName:");
+        //表名字
+        System.err.println(tableMapEventData.getTable());
+      }
+      //表数据发生修改时触发
+      if (data instanceof UpdateRowsEventData) {
+        System.err.println("Update:");
+        System.err.println(data.toString());
+        //表数据发生插入时触发
+      } else if (data instanceof WriteRowsEventData) {
+        System.err.println("Insert:");
+        System.err.println(data.toString());
+        //表数据发生删除后触发
+      } else if (data instanceof DeleteRowsEventData) {
+        System.err.println("Delete:");
+        System.err.println(data.toString());
+      }
+    });
+
+    try {
+      client.connect();
+    } catch (IOException e) {
+      e.printStackTrace();
+    }
+
+  }
+
+  private Map parseDbUrl(String url) {
+    String[] split = url.split(":");
+    String[] hostSplit = split[2].split("//");
+    String host = hostSplit[1];
+    String[] portSplit = split[3].split("/");
+    String port = portSplit[0];
+    Map rs = new HashMap();
+    rs.put("host", host);
+    rs.put("port", port);
+    return rs;
+  }
+}

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

@@ -7,8 +7,13 @@ spring:
             # 主库数据源
             master:
                 url: jdbc:mysql://webapplication.mysql.polardb.rds.aliyuncs.com:3306/xtdb?allowMultiQueries=true&useUnicode=true&characterEncoding=UTF-8&useSSL=false
+#                url: jdbc:mysql://webapplication.rwlb.rds.aliyuncs.com:3306/xtdb?allowMultiQueries=true&useUnicode=true&characterEncoding=UTF-8&useSSL=false
                 username: jerry
                 password: zjr38zjR@
+
+#                url: jdbc:mysql://localhost:3306/xtdb?allowMultiQueries=true&useUnicode=true&characterEncoding=UTF-8&useSSL=false
+#                username: root
+#                password: 1234
             # 从库数据源
             slave:
                 # 从数据源开关/默认关闭

+ 1 - 1
xzl-admin/src/main/resources/application.yml

@@ -18,7 +18,7 @@ xzl:
 # 开发环境配置
 server:
   # 服务器的HTTP端口,默认为8080
-  port: 8080
+  port: 8084
   servlet:
     # 应用的访问路径
     context-path: /

+ 1 - 23
xzl-admin/src/main/resources/banner.txt

@@ -1,24 +1,2 @@
 Application Version: ${xzl.version}
-Spring Boot Version: ${spring-boot.version}
-////////////////////////////////////////////////////////////////////
-//                          _ooOoo_                               //
-//                         o8888888o                              //
-//                         88" . "88                              //
-//                         (| ^_^ |)                              //
-//                         O\  =  /O                              //
-//                      ____/`---'\____                           //
-//                    .'  \\|     |//  `.                         //
-//                   /  \\|||  :  |||//  \                        //
-//                  /  _||||| -:- |||||-  \                       //
-//                  |   | \\\  -  /// |   |                       //
-//                  | \_|  ''\---/''  |   |                       //
-//                  \  .-\__  `-`  ___/-. /                       //
-//                ___`. .'  /--.--\  `. . ___                     //
-//              ."" '<  `.___\_<|>_/___.'  >'"".                  //
-//            | | :  `- \`.;`\ _ /`;.`/ - ` : | |                 //
-//            \  \ `-.   \_ __\ /__ _/   .-` /  /                 //
-//      ========`-.____`-.___\_____/___.-`____.-'========         //
-//                           `=---='                              //
-//      ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^        //
-//             佛祖保佑       永不宕机      永无BUG               //
-////////////////////////////////////////////////////////////////////
+Spring Boot Version: ${spring-boot.version}

+ 1 - 1
xzl-generator/src/main/java/com/xzl/generator/util/GenUtils.java

@@ -215,7 +215,7 @@ public class GenUtils
      */
     public static String replaceText(String text)
     {
-        return RegExUtils.replaceAll(text, "(?:表|)", "");
+        return RegExUtils.replaceAll(text, "(?:表|XZL)", "");
     }
 
     /**