<kbd id="afajh"><form id="afajh"></form></kbd>
<strong id="afajh"><dl id="afajh"></dl></strong>
    <del id="afajh"><form id="afajh"></form></del>
        1. <th id="afajh"><progress id="afajh"></progress></th>
          <b id="afajh"><abbr id="afajh"></abbr></b>
          <th id="afajh"><progress id="afajh"></progress></th>

          binlog4j輕量級 MySQL Binlog 客戶端

          聯(lián)合創(chuàng)作 · 2023-09-30 21:05

          Binlog4j 是輕量級 Mysql Binlog 客戶端,提供宕機(jī)續(xù)讀、高可用集群等特性。

          簡介

          • 集群模式, 通過集群部署的方式,保證服務(wù)高可用。

          • 宕機(jī)續(xù)讀, 避免宕機(jī)期間造成數(shù)據(jù)丟失。

          • 數(shù)據(jù)轉(zhuǎn)換, 基于泛型封裝 binlog Event 的序列化數(shù)據(jù)。

          • 兼容 傳統(tǒng)項(xiàng)目 與 Spring Boot / Cloud 項(xiàng)目。

          • 兼容 Spring Boot 2.x 與 Spring Boot 3.x 版本。

          下載安裝

          <dependency>
             <groupId>com.gitee.Jmysy</groupId>
             <artifactId>binlog4j-core</artifactId>
             <version>latest.version</version>
          </dependency>
          

          implementation group: 'com.gitee.Jmysy', name: 'binlog4j-core', version: 'latest.version'
          

          簡單使用

          通過 BinlogClient 創(chuàng)建 binlog 客戶端, IBinlogEventHandler 用于接受 binlog 事件通知, 該接口允許使用泛型, 數(shù)據(jù)將遵循駝峰規(guī)則進(jìn)行封裝。

          public class BootStrap {
          
              public static void main(String[] args) {
                  
                  BinlogClientConfig clientConfig = new BinlogClientConfig();
                  clientConfig.setHost("127.0.0.1");
                  clientConfig.setPort(3306);
                  clientConfig.setUsername("root");
                  clientConfig.setPassword("taoren@123");
                  clientConfig.setServerId(1990);
            
                  IBinlogClient binlogClient = new BinlogClient(clientConfig);
          
                  binlogClient.registerEventHandler("database", "table", new IBinlogEventHandler() {
                      
                      @Override
                      public void onInsert(Object data) {
                          System.out.println("插入數(shù)據(jù):{}", data);
                      }
          
                      @Override
                      public void onUpdate(Object originalData, Object data) {
                          System.out.println("修改數(shù)據(jù):{}", data);
                      }
          
                      @Override
                      public void onDelete(Object data) {
                          System.out.println("刪除數(shù)據(jù):{}", data);
                      }
                  });
          
                  binlogClient.connect();
              }
          }
          
          

          高級特性

          通過 Persistence 配置為 true 啟用宕機(jī)續(xù)讀功能, Binlog4j 會將 binlog 的 filename 與 position 記錄到 redis, 所以同時(shí)你需要設(shè)置 Redis 配置。

          public class BootStrap {
          
              public static void main(String[] args) {
          
                  RedisConfig redisConfig = new RedisConfig();
                  redisConfig.setHost("127.0.0.1");
                  redisConfig.setPort(6379);
                  redisConfig.setPassword("taoren@123");
          
                  BinlogClientConfig clientConfig = new BinlogClientConfig();
                  clientConfig.setHost("127.0.0.1");
                  clientConfig.setPort(3306);
                  clientConfig.setUsername("root");
                  clientConfig.setPassword("taoren@123");
                  clientConfig.setServerId(1990); // Client 編號
                  clientConfig.setRedisConfig(redisConfig); // Redis 配置
                  clientConfig.setPersistence(true); // 啟用持久化 (宕機(jī)重啟后, 從上次讀取的位置開始)
                  clientConfig.setMode(BinlogClientMode.cluster); // 高可用集群
          
                  BinlogClient binlogClient = new BinlogClient(clientConfig);
          
                  binlogClient.registerEventHandler("database", "table", new IBinlogEventHandler<User>() {
          
                      @Override
                      public void onInsert(Object data) {
                          System.out.println("插入數(shù)據(jù):{}", data);
                      }
          
                      @Override
                      public void onUpdate(Object originalData, Object data) {
                          System.out.println("修改數(shù)據(jù):{}", data);
                      }
          
                      @Override
                      public void onDelete(Object data) {
                          System.out.println("刪除數(shù)據(jù):{}", data);
                      }
                  });
          
                  binlogClient.connect();
              }
          }
          
          

          Spring Boot Starter

          <dependency>
              <groupId>com.gitee.Jmysy</groupId>
              <artifactId>binlog4j-spring-boot-starter</artifactId>
              <version>latest.version</version>
          </dependency>
          

          implementation group: 'com.gitee.Jmysy', name: 'binlog4j-spring-boot-starter', version: 'latest.version'
          

          首先, 在 application.yml 中填寫 BinlogClient 配置

          spring:
            binlog4j:
              redis-config:
                host: 127.0.0.1
                port: 6379
                password: taoren@123
              client-configs:
                master:
                  username: root
                  password: taoren@123
                  host: 127.0.0.1
                  port: 3306
                  serverId: 1990
          
          

          使用 @BinlogSubscriber 注解, 指定 IBinlogEventHandler 需要注冊到哪個(gè)客戶端, 并且指定監(jiān)聽的 database 與 table。

          @BinlogSubscriber(clientName = "master", database = "pear-admin", table ="sys_user")
          public class UserEventHandler implements IBinlogEventHandler<User> {
          
              @Override
              public void onInsert(User target) {
                  System.out.println("插入數(shù)據(jù):" + JSON.toJSONString(target));
              }
          
              @Override
              public void onUpdate(User source, User target) {
                  System.out.println("修改數(shù)據(jù):" + JSON.toJSONString(target));
              }
          
              @Override
              public void onDelete(User target) {
                  System.out.println("刪除數(shù)據(jù):" + JSON.toJSONString(target));
              }
          
          }
          
          瀏覽 22
          點(diǎn)贊
          評論
          收藏
          分享

          手機(jī)掃一掃分享

          編輯 分享
          舉報(bào)
          評論
          圖片
          表情
          推薦
          點(diǎn)贊
          評論
          收藏
          分享

          手機(jī)掃一掃分享

          編輯 分享
          舉報(bào)
          <kbd id="afajh"><form id="afajh"></form></kbd>
          <strong id="afajh"><dl id="afajh"></dl></strong>
            <del id="afajh"><form id="afajh"></form></del>
                1. <th id="afajh"><progress id="afajh"></progress></th>
                  <b id="afajh"><abbr id="afajh"></abbr></b>
                  <th id="afajh"><progress id="afajh"></progress></th>
                  久久午夜视频 | 五月天三级片 | 在线伊人成人网 | 围产精品久久久久久久妞妞 | 伊人中文在线 |