binlog4j 1.1.0 released, a lightweight MySQL Binlog client for Java

Binlog4j

Lightweight Mysql Binlog client, providing features such as continuous reading after downtime, high-availability clusters, etc.

project characteristics

  • Cluster mode ensures high availability of services through cluster deployment.

  • Continue reading after downtime to avoid data loss during downtime.

  • Data conversion, serialized data based on generic encapsulation binlog Event.

  • Compatible with traditional projects and Spring Boot / Cloud projects.

  • Compatible with Spring Boot 2.x and Spring Boot 3.x versions.

What's new (1.1.0)

[Broken] The input parameter of IBinlogEventHandler interface is adjusted from T to BinlogEvent.
[New] @BinlogSubscriber annotation database matches table attribute pattern.
[New] @BinlogSubscriber annotation database and table attribute default value is .*.
[MOVED] BinlogUtils package path is com.gitee.Jmysy.binlog4j.core.utils.
[New] PatternUtils built-in tool Class

Download and install

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

or

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

easy to use

Create a binlog client through BinlogClient, and IBinlogEventHandler is used to accept binlog event notifications. This interface allows the use of generics, and the data will be encapsulated according to the hump rule.

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(BinlogEvent event) {
                System.out.println("插入数据:{}", event.getData());
            }

            @Override
            public void onUpdate(BinlogEvent event) {
                System.out.println("修改数据:{}", event.getData());
            }

            @Override
            public void onDelete(BinlogEvent event) {
                System.out.println("删除数据:{}", event.getData());
            }
        });

        binlogClient.connect();
    }
}

advanced features

By configuring Persistence to true to enable the function of continuous reading after downtime, Binlog4j will record the filename and position of binlog to redis, so you need to set the Redis configuration at the same time.

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); // 启用持久化 (宕机重启后, 从上次读取的位置开始)
        clientConfig.setMode(BinlogClientMode.cluster); // 高可用集群

        BinlogClient binlogClient = new BinlogClient(clientConfig);

        binlogClient.registerEventHandler("database", "table", new IBinlogEventHandler<User>() {

            @Override
            public void onInsert(BinlogEvent<User> event) {
                System.out.println("插入数据:{}", event.getData());
            }

            @Override
            public void onUpdate(BinlogEvent<User> event) {
                System.out.println("修改数据:{}", event.getData());
            }

            @Override
            public void onDelete(BinlogEvent<User> event) {
                System.out.println("删除数据:{}", event.getData());
            }
        });

        binlogClient.connect();
    }
}

Spring Boot Starter

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

or

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

First, fill in the BinlogClient configuration in application.yml

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

Use the @BinlogSubscriber annotation to specify which client IBinlogEventHandler needs to register to, and specify the database and table to monitor.

@BinlogSubscriber(clientName = "master", database = "pear-admin", table ="sys_user")
public class UserEventHandler implements IBinlogEventHandler<User> {

    @Override
    public void onInsert(BinlogEvent<User> event) {
        System.out.println("插入数据:" + event.getData());
    }

    @Override
    public void onUpdate(BinlogEvent<User> event) {
        System.out.println("修改数据:" + event.getData());
    }

    @Override
    public void onDelete(BinlogEvent<User> event) {
        System.out.println("删除数据:" + event.getData());
    }

}

Multi-table monitoring, database and table use Pattern matching, generics should no longer be used, data defaults to Map<String, Object> type

@BinlogSubscriber(clientName = "master", database = ".*", table ="sys.*")
public class UserEventHandler implements IBinlogEventHandler<User> {

    @Override
    public void onInsert(BinlogEvent<User> event) {
        System.out.println("数据库:" + event.getDatabase());
        System.out.println("数据表:" + event.getTable());
        System.out.println("新数据:" + event.getData());
    }

    @Override
    public void onUpdate(BinlogEvent<User> event) {
        System.out.println("数据库:" + event.getDatabase());
        System.out.println("数据表:" + event.getTable());
        System.out.println("原数据:" + event.getOriginalData());
        System.out.println("新数据:" + event.getData());
    }

    @Override
    public void onDelete(BinlogEvent<User> event) {
        System.out.println("数据库:" + event.getDatabase());
        System.out.println("数据表:" + event.getTable());
        System.out.println("新数据:" + event.getData());
    }

}

Guess you like

Origin www.oschina.net/news/254826/binlog4j-1-1-0