Skip to content

ElanYoung/canal-spring-boot

Repository files navigation

author Maven Central Spring Boot LICENSE

star star star

简介

Canal 是阿里巴巴开源的一个基于 MySQL 数据库增量日志解析,提供增量数据订阅和消费的组件。

Canal 的原理是基于 MySQL 的主从复制,模拟 MySQL Slave 的交互协议,将自己伪装成 MySQL Slave,向 MySQL Master 发送 dump 协议,MySQL Master 收到 dump 请求后,开始推送 binary log 给 Canal,Canal 解析 binary log 字节流对象并且可以通过实现 CanalEventListener 或继承 AbstractCanalEventListener 来实现数据处理。

canal-spring-boot-starter 是基于 Canal 封装的 Spring Boot Starter,可以帮助你更快地在 Spring Boot 项目中集成 Canal,实现数据库增量订阅和消费。

快速开始

引入依赖

<dependency>
  <groupId>io.github.elanyoung</groupId>
  <artifactId>canal-spring-boot-starter</artifactId>
  <version>1.0.0</version>
</dependency>

参数配置

canal:
  server:
    # Canal 服务器地址
    ip: 127.0.0.1
    # Canal 服务器端口
    port: 11111
    # Canal 服务器用户名
    username: canal
    # Canal 服务器密码
    password: canal
  # 是否开启异步处理
  async: true
  # Canal 实例名称
  destination: example

配置说明

属性 描述 默认值
canal.server.ip Canal 服务端地址。 127.0.0.1
canal.server.port Canal 服务端端口号。 11111
canal.server.username Canal 用户名。 null
canal.server.password Canal 密码。 null
canal.destination Canal 实例名称。 null
canal.filter 配置订阅表名称。若配置,则仅订阅指定表;若未配置,则订阅所有表。 ""
canal.batch-size 消息消费数量。当消息数量达到该值时,将触发一次消费操作。 1
canal.timeout 消息消费时间间隔(单位:秒)。 1
canal.async 是否启用异步消费模式。若启用异步消费,消费时异常将不会触发消息回滚,且无法保证消息顺序消费。 true

创建监听器

@Slf4j
@Component
@CanalTable(value = "student")
public class StudentHandler implements EntryHandler<StudentDO> {

	@Override
	public void insert(StudentDO student) {
		log.info("insert message {}", student);
	}

	@Override
	public void update(StudentDO before, StudentDO after) {
		log.info("update before {} ", before);
		log.info("update after {}", after);
	}

	@Override
	public void delete(StudentDO student) {
		log.info("delete {}", student);
	}

}

TODO

  • RabbitMQ
  • ZooKeeper
  • Kafka

参考资料

项目趋势

Stargazers over time

开源协议

Apache License

Copyright (c) 2025 ElanYoung

About

⚡ Canal Spring Boot Starter

Topics

Resources

License

Stars

Watchers

Forks

Packages

No packages published