springboot整合canal 简单好用

1,下载canal,配置mysql开启binlog,配置canal的instance.properties

https://github.com/alibaba/canal/wiki/QuickStart

springboot整合canal 简单好用_第1张图片

 2,代码里引入pom.xml


    top.javatool
    canal-spring-boot-starter
    1.2.1-RELEASE

3实现接口EntryHandler


@Component
@CanalTable("user")
public class UserHandler implements EntryHandler {

    /**
     *  新增操作
     * @param user
     */
    @Override
    public void insert(User user) {
        //你的逻辑
        System.out.println("新增 {}"+user);
    }
    /**
     * 对于更新操作来讲,before 中的属性只包含变更的属性,after 包含所有属性,通过对比可发现那些属性更新了
     * @param before
     * @param after
     */
    @Override
    public void update(User before, User after) {
        //你的逻辑
        System.out.println("更新 before{}"+before);
        System.out.println("更新 after{}"+after);
    }
    /**
     *  删除操作
     * @param user
     */
    @Override
    public void delete(User user) {
        //你的逻辑
        System.out.println("删除"+user);
    }

4,实体类user加好字段以及注解

@Table(name = "user")
public class User {
    @Id
    @Column(name = "id")
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Integer id;
    @Column(name = "name")
    private String name;
    @Column(name = "age")
    private Integer age;

    public Integer getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public Integer getAge() {
        return age;
    }

    public void setAge(Integer age) {
        this.age = age;
    }

    @Override
    public String toString() {
        return "User{" +
                "id=" + id +
                ", name='" + name + '\'' +
                ", age=" + age +
                '}';
    }
}

5,大功告成 ok

6,监听到的日志截图

2022-10-02 13:14:24.882  INFO 19936 --- [l-client-thread] t.j.c.client.client.AbstractCanalClient  : 获取消息 Message[id=23,entries=[header {
  version: 1
  logfileName: "mysql-bin.000002"
  logfileOffset: 5397
  serverId: 1
  serverenCode: "UTF-8"
  executeTime: 1664687664000
  sourceType: MYSQL
  schemaName: ""
  tableName: ""
  eventLength: 67
}
entryType: TRANSACTIONBEGIN
storeValue: " \023"
, header {
  version: 1
  logfileName: "mysql-bin.000002"
  logfileOffset: 5510
  serverId: 1
  serverenCode: "UTF-8"
  executeTime: 1664687664000
  sourceType: MYSQL
  schemaName: "aaa"
  tableName: "user"
  eventLength: 66
  eventType: UPDATE
  props {
    key: "rowsCount"
    value: "1"
  }
}
entryType: ROWDATA
storeValue: "\b$\020\002P\000b\270\001\n\026\b\000\020\004\032\002id \001(\0000\000B\0011R\003int\n%\b\001\020\f\032\004name \000(\0000\000B\0051aaa3R\fvarchar(255)\n\031\b\002\020\004\032\003age \000(\0000\000B\003121R\003int\022\026\b\000\020\004\032\002id \001(\0000\000B\0011R\003int\022)\b\001\020\f\032\004name \000(\0010\000B\t121211212R\fvarchar(255)\022\031\b\002\020\004\032\003age \000(\0000\000B\003121R\003int"
, header {
  version: 1
  logfileName: "mysql-bin.000002"
  logfileOffset: 5576
  serverId: 1
  serverenCode: "UTF-8"
  executeTime: 1664687664000
  sourceType: MYSQL
  schemaName: ""
  tableName: ""
  eventLength: 27
}
entryType: TRANSACTIONEND
storeValue: "\022\003141"
],raw=true,rawEntries=[]]
更新 before{}User{id=null, name='1aaa3', age=null}
更新 after{}User{id=1, name='121211212', age=121}

你可能感兴趣的:(数据库那点事,mysql,数据库)