# springboot-canal-rabbitmq(实现数据同步更新)
**Repository Path**: chenmingxu1314/springboot-canal-rabbitmq
## Basic Information
- **Project Name**: springboot-canal-rabbitmq(实现数据同步更新)
- **Description**: SpringBoot 整合 Canal + RabbitMQ 监听 MySQL 的 binlog 文件,实现数据同步更新 Redis 缓存
- **Primary Language**: Unknown
- **License**: MulanPSL-2.0
- **Default Branch**: master
- **Homepage**: None
- **GVP Project**: No
## Statistics
- **Stars**: 0
- **Forks**: 10
- **Created**: 2024-06-17
- **Last Updated**: 2024-06-17
## Categories & Tags
**Categories**: Uncategorized
**Tags**: None
## README
# springboot-canal-rabbitmq
### 介绍
SpringBoot 整合 Canal + RabbitMQ 监听 MySQL 的 binlog 文件,实现数据同步更新 Redis 缓存
### 软件架构
1. SpringBoot
2. MySQL
3. Redis
4. RabbitMQ
5. Canal
### 修改配置
#### 1. MySQL 开启 binlog
- Windows:
```
C:\ProgramData\MySQL\MySQL Server 5.7\my.ini
```
- Linux:
```
vim /etc/my.cnf
```
- 添加内容:
```
[mysqld]
log-bin=mysql-bin # 开启binlog
binlog-format=ROW # 选择ROW模式
server_id=1 # 配置MySQL replaction需要定义,不和Canal的slaveId重复即可
```
- 效果如下:
- 检测是否成功:
```
SHOW VARIABLES LIKE 'log_bin';
```
#### 2. RabbitMQ 配置
- 创建交换机:canal.exchange
- 创建队列:canal.queue
- 队列绑定交换机:canal.routing.key
#### 3. 修改 Canal 配置
- Canal 下载地址
```
https://github.com/alibaba/canal/releases/download/canal-1.1.5/canal.deployer-1.1.5.tar.gz
```
- 修改 canal.properties 文件:
```
\springboot-canal-rabbitmq\canal\conf\canal.properties
```
- MQ 连接信息
```
# tcp, kafka, rocketMQ, rabbitMQ
canal.serverMode = rabbitMQ
```
```
rabbitmq.host = localhost
rabbitmq.virtual.host =/
rabbitmq.exchange =canal.exchange
rabbitmq.username =guest
rabbitmq.password =guest
rabbitmq.deliveryMode =
```
- 修改 instance.properties 文件:
```
\springboot-canal-rabbitmq\canal\conf\example\instance.properties
```
- 数据库连接信息
```
# position info
canal.instance.master.address=127.0.0.1:3306
```
```
# username/password
canal.instance.dbUsername=root
canal.instance.dbPassword=123456
```
```
# mq config
canal.mq.topic=canal.routing.key
```
#### 4、SpringBoot 整合 RabbitMQ
- 引入 maven 依赖:
```
org.springframework.boot
spring-boot-starter-amqp
```
- 配置信息:
```
spring:
# rabbitmq
rabbitmq:
host: localhost
port: 5672
username: guest
password: guest
virtual-host: /
```
- RabbitMQ 监听器:
```
import com.alibaba.fastjson.JSONObject;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.rabbit.annotation.Exchange;
import org.springframework.amqp.rabbit.annotation.Queue;
import org.springframework.amqp.rabbit.annotation.QueueBinding;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Component;
/**
* Canal + RabbitMQ 监听数据库数据变化
*
* @author Asurplus
*/
@Slf4j
@Component
public class CanalListener {
@RabbitListener(bindings = {
@QueueBinding(
value = @Queue(value = "canal.queue", durable = "true"),
exchange = @Exchange(value = "canal.exchange"),
key = "canal.routing.key"
)
})
public void handleDataChange(String message) {
JSONObject object = JSONObject.parseObject(message);
log.info("Canal监听到数据发生变化\n库名:{}\n表名:{}\n类型:{}\n数据:{}", object.getString("database"), object.getString("table"), object.getString("type"), object.getString("data"));
/**
* TODO 同步Redis
*/
}
}
```
### 启动测试
- 启动 Canal:
```
\springboot-canal-rabbitmq\canal\bin\startup.bat
```
- 双击启动
- 启动项目:
- 修改数据库任意数据:
监听成功,我们就可以同步更新 Redis 缓存的数据了