开发者

Java binLog日志监听方式

目录
  • Java binLog日志监听
    • 一、Windows下开启mysql binLog日志
    • 二、Java代码示例演示
  • 总结

    Java binLog日志监听

    监听指定的表去做一些处理逻辑,首先是要开启M有SQL的配置,然后再撸代码。

    一、Windows下开启MySQL binLog日志

    首先要开启MySQL的BinLog 管理

    show variables like '%log_bin%';

    Java binLog日志监听方式

    如果发现是OFF,打开mysql文件夹下面的my.ini,修改一下

    如果不知道my.ini 在哪里,打开【服务】-> 右击属性

    Java binLog日志监听方式

    Java binLog日志监听方式

    Java binLog日志监听方式

    拉到最后就可以看见my.ini,然后找到文件后

    [mysqld] 下面加

    # 开启bin-log
    log-bin=mysql-bin # 开启binlog功能
    binlog-format=ROW # 设置binlog格式
    server_id=1 # 设置服务ID号

    Java binLog日志监听方式

    然后 重启服务,就会发现已经起好了

    二、Java代码示例演示

    首先引入Maven包

     <dependency>
         <groupId>com.github.shyiko</groupId>
         <artifactId>mysql-binljsog-connector-java</artifactId>
         <version>0.21.0</version>
     </dependency>

    上代码

    import cn.hutool.core.collection.ListUtil;
    import com.alibaba.fastjson2.JSON;
    import com.github.shyiko.mysql.binlog.BinaryLogClient;
    import com.github.shyiko.mysql.binlog.event.*;
    import com.ruoyi.common.utils.StringUtils;
    import com.ruoyi.web.controller.websocket.AlarmWebSocket;
    import com.ruoyi.web.service.IWidfireDataService;
    import lombok.extern.slf4j.Slf4j;
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.stereotype.Component;
    
    import java.io.IOException;
    import java.util.List;
    import java.util.Map;
    
    
    /**
     * mysql bin log 日志监听
     */
    @Component
    @Slf4j
    public class MySQLBinaryLogConfig {
    
    
        public static IWidfireDataService widfireDataService;
    
        @Autowired
        public void setSenderSerjavascriptvice(IWidfireDataService widfireDataService){
            MySQLBinaryLogConfig.widfireDataService= widfireDataService;
        }
    
    
        private static final List<String> TABLE_NAME = Lis编程客栈tUtil.of("alart_ai"); //数据库表,需要监听的表
    
    
        {
            System.out.println("启动监听:启动中");
            getThread().start();
            System.out.println("启动监听:成功");
        }
    
        public Thread getThread() {
            BinaryLogClient client = new BinaryLogClient("127.0.0.1", 3306, "root", "123456");
            client.setServerId(1);
            return new Thread(() -> {
                client.registerEventListener(event -> {
                    String table =null;
                    final EventData data =http://www.devze.com event.getData();
                    if (data instanceof TableMapEventData) {
                        TableMapEventData tableMapEventData = (TableMapEventData) data;
                        String database = tableMapEventData.getDatabase();
                        table = tableMapEventData.getTable();
    
                        log.info("数据表:{},data:{},database:{}",table,data.toString(),database);
                    }else if (data instanceof UpdateRowsEventData) {
                        UpdateRowsEventData tableMapEventData = (UpdateRowsEventData) data;
    
                        System.out.println("更新:");
                    } else if (data instanceof WriteRowsEventData) {
                        System.out.println("添加:");
                    } else if (data instanceof DeleteRowsEventData) {
                        System.out.println("删除:");
                    }
    
                    if(StringUtils.isNotEmpty(table) && TABLE_NAME.contains(table)){
                        log.info("<<<<<< 收到MySQL binLog 日志推送 >>>>>>>");
                        //开始编写具体的逻辑
                        
                    }
    
                });
                try {
                    client.connect();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            });
    
        }
    }

    总结

    以上为个人经验,希望能给大家一个参考www.devze.com,也希望大家多多支持编程客栈(www.devze.com)。

    0

    上一篇:

    下一篇:

    精彩评论

    暂无评论...
    验证码 换一张
    取 消

    最新开发

    开发排行榜