环境:
centos7.9
canal 1.1.5
下载地址:Releases · alibaba/canal (github.com)
1.下载canal 1.1.5
2.创建canal文件夹,并将canal 1.1.5解压
mkdir canal
tar -zxvf canal.deployer-1.1.5.tar.gz
3./etc/my.cnf文件添加如下配置
server-id=222 # 给mysql服务创建一个唯一id
log-bin=mysql-bin # 开启 binlog
binlog-format=ROW # 选择 ROW 模式
重启mysql服务:
systemctl restart mysqld
4.进canal/conf/example目录,修改配置
vim /soft/canal/conf/example/instance.properties
配置信息:
canal.instance.master.address=10.70.20.130:3306 #mysql ip+端口
canal.instance.dbUsername=root #mysql 账号
canal.instance.dbPassword=123456 #mysql 密码
canal.instance.filter.regex=canaldb.employee #需要同步的库、表
5.启动canal
startup.sh #启动
stop.sh #停止
显示如下启动成功:
6.创建Springboot项目测试数据是否同步
6.1 依赖
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>
<!--Canal 依赖-->
<dependency>
<groupId>top.javatool</groupId>
<artifactId>canal-spring-boot-starter</artifactId>
<version>1.2.1-RELEASE</version>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>1.2.50</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
6.2 application.yaml
canal:
server: 10.70.20.130:11111
destination: example
spring:
redis:
host: 10.70.20.130
password: 123456
port: 6379
6.3 bean
注:这里的字段等与数据库保持一致
@Data
public class Employee implements Serializable {
private Integer id;
private String username;
}
6.4 RedisConfig
package com.lm.canal.conf;
import com.alibaba.fastjson.support.spring.GenericFastJsonRedisSerializer;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.serializer.StringRedisSerializer;
import javax.annotation.Resource;
/**
* @author: lm
* @createDate: 2023-02-23 17:29
* @description:
*/
@Configuration
public class RedisConfig {
@Resource
private RedisConnectionFactory factory;
//使用JSON进行序列化
@Bean
public RedisTemplate<Object, Object> redisTemplate() {
RedisTemplate<Object, Object> redisTemplate = new RedisTemplate<>();
redisTemplate.setConnectionFactory(factory);
//JSON格式序列化
GenericFastJsonRedisSerializer serializer = new GenericFastJsonRedisSerializer();
//key的序列化
redisTemplate.setKeySerializer(serializer);
//value的序列化
redisTemplate.setValueSerializer(serializer);
//hash结构key的虚拟化
redisTemplate.setHashKeySerializer(new StringRedisSerializer());
//hash结构value的虚拟化
redisTemplate.setHashValueSerializer(serializer);
return redisTemplate;
}
}
6.5 controller
package com.lm.canal;
import com.lm.canal.bean.Employee;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Component;
import top.javatool.canal.client.annotation.CanalTable;
import top.javatool.canal.client.handler.EntryHandler;
/**
* @author: lm
* @createDate: 2023-02-23 17:33
* @description:
*/
@CanalTable("employee")
@Component
@Slf4j
public class EmployeeHandler implements EntryHandler<Employee> {
//把数据往Redis同步
@Autowired
private RedisTemplate<Object,Object> redisTemplate;
@Override
public void insert(Employee employee) {
redisTemplate.opsForValue().set("EMP:"+employee.getId(),employee);
}
@Override
public void delete(Employee employee) {
redisTemplate.delete("EMP:"+employee.getId());
}
@Override
public void update(Employee before, Employee after) {
redisTemplate.opsForValue().set("EMP:"+after.getId(),after);
}
}
7.创建mysql库、表
CREATE DATABASE `canaldb` /*!40100 DEFAULT CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci */ /*!80016 DEFAULT ENCRYPTION='N' */;
-- canaldb.employee definition
CREATE TABLE `employee` (
`id` int NOT NULL AUTO_INCREMENT,
`username` varchar(100) CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci DEFAULT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=13 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci;
8.启动Springboot项目
显示如下,canal服务正在实时监控数据,有无CRUD
9.sql表新增数据
10.canal监控到数据变动,执行相应方法
11.Redis查询数据
如上图显示,则同步成功!
参考文章:(1条消息) 使用canal解决Mysql和Redis数据同步(TCP)_canal redis_墨家巨子@俏如来的博客-CSDN博客