阿里Canal实现MySQL至Redis的数据同步

该文详细介绍了如何在CentOS系统中配置和使用Canal1.1.5版本来实现MySQL数据库的数据同步到Redis。步骤包括下载Canal、配置MySQL的binlog、修改Canal实例配置、启动Canal服务,以及创建Springboot项目并配置监听MySQL的表变更,将变更实时同步到Redis中。
摘要由CSDN通过智能技术生成

环境:

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博客

评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值