通过java方式监听mysql的binlog变化

1引入依赖

  1. <dependency>
  2. <groupId>com.xy</groupId>
  3. <artifactId>xy-core-data-mysql-binlog-boot</artifactId>
  4. <version>${xy-core-data-mysql-binlog-boot.version}</version>
  5. </dependency>

2.添加配置

  1. spring.mysql.binlog.enable=true
  2. spring.mysql.binlog.host=127.0.0.1
  3. spring.mysql.binlog.port=3306
  4. spring.mysql.binlog.schema=company-demo
  5. spring.mysql.binlog.username=root
  6. spring.mysql.binlog.password=1q2w3e4r@
  7. spring.mysql.binlog.server-id=2

3.实现接口

  1. package com.company.demo.message.listener;
  2. import lombok.extern.slf4j.Slf4j;
  3. import org.springframework.stereotype.Component;
  4. /**
  5. * <p>description</p >
  6. *
  7. * @author jack.li
  8. * @version 1.0
  9. * @date 2022/3/22 下午12:25
  10. */
  11. @Slf4j
  12. @Component
  13. public class MysqlBinLogChangeImplListener implements MysqlBinLogChangeListener {
  14. @Override
  15. public void insert(String tableName, WriteRowsEventData writeRowsEventData) {
  16. log.info("insert.........................");
  17. }
  18. @Override
  19. public void delete(String tableName, DeleteRowsEventData deleteRowsEventData) {
  20. log.info("delete...........................");
  21. }
  22. @Override
  23. public void update(String tableName, UpdateRowsEventData updateRowsEventData) {
  24. log.info("update...........................");
  25. }
  26. }