1.mysql 配置
1.1 vi /etc/my.cnf
## 设置server_id,同一局域网中需要唯一
server-id=101
log-bin=/var/lib/mysql/mysql-bin
binlog-do-db=canal
## 开启二进制日志功能
log-bin=mall-mysql-bin
## 设置二进制日志使用内存大小(事务)
binlog_cache_size=1M
## 设置使用的二进制日志格式(mixed,statement,row)
binlog_format=row
## 二进制日志过期清理时间。默认值为0,表示不自动清理。
expire_logs_days=7
## 跳过主从复制中遇到的所有错误或指定类型的错误,避免slave端复制中断。
## 如:1062错误是指一些主键重复,1032错误是因为主从数据库数据不一致
slave_skip_errors=1062
1.2 重启mysql
service mysqld restart
1.3 登录mysql
mysql -h 127.0.0.1 -u admin -p
1.4 查看binlog是否启用
show variables like '%log_bin%';
1.5.查看日志格式
show variables like '%binlog_format%';
1.6创建用户并且赋权限
CREATE USER canal IDENTIFIED BY 'sjJUB%rX5mq$t9p8';
GRANT SELECT, REPLICATION SLAVE, REPLICATION CLIENT ON *.* TO 'canal'@'%';
FLUSH PRIVILEGES;
2.canal.deployer 配置
2.1 conf/example/instance.properties 配置
# 需要同步数据的MySQL地址
canal.instance.master.address=127.0.0.1:3306
canal.instance.master.journal.name=
canal.instance.master.position=
canal.instance.master.timestamp=
canal.instance.master.gtid=
# 用于同步数据的数据库账号
canal.instance.dbUsername=canal
# 用于同步数据的数据库密码
canal.instance.dbPassword=canal
# 数据库连接编码
canal.instance.connectionCharset = UTF-8
# 需要订阅binlog的表过滤正则表达式
canal.instance.filter.regex=.*\\..*
2.2 启动
./usr/local/canal/bin/startup.sh
2.3 查看日志
tail -1000f /usr/local/canal/logs/canal/canal.log
3.client 配置
3.1 pom.xml
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>top.javatool</groupId>
<artifactId>canal-spring-boot-starter</artifactId>
<version>1.2.1-RELEASE</version>
</dependency>
3.2 实体类
import lombok.Data;
import lombok.ToString;
import java.util.Date;
@Data
@ToString
public class Student {
private Long id;//商品id
private String name;//商品名称
private String address;//商品标题
private Date birthdat;//更新时间
private Integer stock;
}
3.3 监听类
mport com.test.application.entity.Student;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;
import top.javatool.canal.client.annotation.CanalTable;
import top.javatool.canal.client.handler.EntryHandler;
@CanalTable("student")
@Component
@Slf4j
public class StudentHandler implements EntryHandler<Student> {
@Override
public void insert(Student item) {
// 写数据到JVM进程缓存
// 写数据到redis
log.info("insert item:{}",item);
}
@Override
public void update(Student before, Student after) {
log.info("update before:{}",before);
log.info("update after:{}",after);
}
@Override
public void delete(Student item) {
log.info("delete item:{}",item);
}
}
版权声明:本文为hu_cheng_min_原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明。