Canal实现MySql和redis的数据同步
一、canal简介canal是阿里开源的数据同步工具,基于bin log可以将数据库同步到其他各类数据库中,目标数据库支持mysql,postgresql,oracle,redis,MQ,ES等。
1、canal工作原理
工作原理:把自己伪装成Mysql slave,模拟Mysql slave的交互协议,向Mysql master发送请求,master收到canal发送的请求后,开始推送binary log二进制日志给canal,然后canal解析binary log,再发送到存储目的地(Redis、MQ、ES等)。
2、canal作用
数据库镜像
数据库实时备份
索引构建和实施维护
业务缓存
贷业务逻辑的增量数据处理
二、通过canal实现Mysql和Redis数据同步(Linux环境)
在Centos7完成了Mysql的主从复制,采用中间件 canal 完成 MySQL 向 Redis & ES 同步数据。
1、下载canal1.1.5下载地址: https://github.com/alibaba/canal/releases
2、将下载好的canal上传到创建好的canal文件夹中并解压,解压后如下图所示
3、/etc/my.cnf添加如下配置server-id=140 # 给mysql服务创建一个唯一id
log-bin=mysql-bin # 开启 binlog
binlog-format=ROW # 选择 ROW 模式
注意:如果Mysql已经主从复制过了,以上配置中的log-bin=master-bin(这个不影响)重启mysql服务service mysql restart
4、/soft/canal/conf/example/instance.properties修改配置canal.instance.master.address=10.70.20.140:3306 #mysql ip+端口
canal.instance.dbUsername=root #mysql 账号
canal.instance.dbPassword=123456 #mysql 密码
canal.instance.filter.regex=canaldb.canaltable #需要同步的库、表
5、bin目录下startup.sh启动canal
# bin目录下启动canal
startup.sh
显示如下启动成功!
6、创建Springboot测试数据是否同步6.1、依赖 <dependencies>
<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>
<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>
<!--德鲁伊-->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid-spring-boot-starter</artifactId>
<version>1.1.17</version>
</dependency>
<!--导入jdbc场景-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-jdbc</artifactId>
</dependency>
<!--数据库连接-->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.29</version>
</dependency>
</dependencies>
6.2、application.yamlcanal:
server: 10.70.20.140:11111
destination: example
spring:
redis:
host: 10.70.20.140
password: 123456
port: 6379
datasource:
druid:
driver-class-name: com.mysql.cj.jdbc.Driver
url: jdbc:mysql://10.70.20.140:3306/canaldb
username: root
password: fe781212
6.3、创建表和实体类(注意数据库表和实体类字段一致)
6.4、配置类RedisConfig@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、handler@CanalTable("canaltable")
@Component
@Slf4j
public class UserHandler implements EntryHandler<User> {
@Autowired
private RedisTemplate<Object,Object> redisTemplate;
@Override
public void insert(User user) {
redisTemplate.opsForValue().set("USER:"+user.getId(),user);
}
@Override
public void update(User before, User after) {
redisTemplate.opsForValue().set("USER:"+before.getId(),after);
}
@Override
public void delete(User user) {
redisTemplate.delete("USER:"+user.getId());
}
}
7、启动项目测试数据是否同步
8、表新增数据,查看redis数据
数据同步成功!
本文转载自:https://blog.csdn.net/qq_57821542/article/details/129217882
页:
[1]