上面我们虽然实现了多级缓存架构,但是问题也出现了,如果数据库中数据发生变更,如何更新Redis缓存呢?如何更新Nginx缓存呢?
我们可以使用阿里巴巴的技术解决方案Canal来实现,通过Canal监听数据库变更,并实时消费变更数据,并更新缓存。
canal [kə’næl],译意为水道/管道/沟渠,主要用途是基于 MySQL 数据库增量日志解析,提供增量数据订阅和消费
学习地址:https://github.com/alibaba/canal
早期阿里巴巴因为杭州和美国双机房部署,存在跨机房同步的业务需求,实现方式主要是基于业务 trigger 获取增量变更。从 2010 年开始,业务逐步尝试数据库日志解析获取增量变更进行同步,由此衍生出了大量的数据库增量订阅和消费业务。
基于日志增量订阅和消费的业务包括
- 数据库镜像
- 数据库实时备份
- 索引构建和实时维护(拆分异构索引、倒排索引等)
- 业务 cache 刷新
- 带业务逻辑的增量数据处理
当前的 canal 支持源端 MySQL 版本包括 5.1.x , 5.5.x , 5.6.x , 5.7.x , 8.0.x。
1 Canal原理讲解
MySQL主备复制原理
- MySQL master 将数据变更写入二进制日志( binary log, 其中记录叫做二进制日志事件binary log events,可以通过 show binlog events 进行查看)
- MySQL slave 将 master 的 binary log events 拷贝到它的中继日志(relay log)
- MySQL slave 重放 relay log 中事件,将数据变更反映它自己的数据
Canal 工作原理
- canal 模拟 MySQL slave 的交互协议,伪装自己为 MySQL slave ,向 MySQL master 发送dump 协议
- MySQL master 收到 dump 请求,开始推送 binary log 给 slave (即 canal )
- canal 解析 binary log 对象(原始为 byte 流)
手绘版
2 Canal安装
2.1 MySQL开启binlog
对于MySQL , 需要先开启 Binlog 写入功能,配置 binlog-format 为 ROW 模式,my.cnf 中配置如下
docker exec -it mysql /bin/bash
cd /etc/mysql/mysql.conf.d
vi mysqld.cnf
在最文件尾部添加如下配置:
log-bin=mysql-bin # 开启 binlog
binlog-format=ROW # 选择 ROW 模式
server_id=1 # 配置 MySQL replaction 需要定义,不要和 canal 的 slaveId 重复
**注意:**针对阿里云 RDS for MySQL , 默认打开了 binlog , 并且账号默认具有 binlog dump 权限 , 不需要任何权限或者 binlog 设置,可以直接跳过这一步。
授权 canal 链接 MySQL 账号具有作为 MySQL slave 的权限, 如果已有账户可直接 grant:
CREATE USER canal IDENTIFIED BY 'canal';
GRANT SELECT, REPLICATION SLAVE, REPLICATION CLIENT ON *.* TO 'canal'@'%';
-- GRANT ALL PRIVILEGES ON *.* TO 'canal'@'%' ;
FLUSH PRIVILEGES;
重启mysql容器
docker restart canal
查看是否开启binlog:
show variables like 'log_bin';
2.2 Canal安装
我们采用docker安装方式:
docker run -p 11111:11111 --name canal -d docker.io/canal/canal-server
进入容器,修改核心配置canal.properties 和instance.properties,canal.properties 是canal自身的配置,instance.properties是需要同步数据的数据库连接配置。
修改配置如下:
# position info
canal.instance.master.address=192.168.100.130:3306
另一处配置:
# table regex
#canal.instance.filter.regex=.*\\..* #.*表示任意,\\.表示.,此处表示任意数据库任意表
#监听配置
canal.instance.filter.regex=shop_goods.ad_items # 表示监听shop_goods数据库下的ad_items表,还可写成 mysql\\.slave_.*,表示mysql数据库下以slave_开头的表
配置完成后,重启canal
容器
docker restart canal
3 多级缓存架构缓存一致性实战
3.1 Canal微服务搭建
工程坐标:
<groupId>com.gupaoedu.vip.mall</groupId>
<version>0.0.1-SNAPSHOT</version>
<artifactId>mall-canal-service</artifactId>
pom.xml:
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<parent>
<artifactId>mall-service</artifactId>
<groupId>com.gupaoedu.vip.mall</groupId>
<version>0.0.1-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>mall-canal-service</artifactId>
<description>
Canal微服务
</description>
<dependencies>
<!--springboot-canal快速构建依赖包-->
<dependency>
<groupId>top.javatool</groupId>
<artifactId>canal-spring-boot-starter</artifactId>
<version>1.2.1-RELEASE</version>
</dependency>
<!--依赖mall-goods-api-->
<dependency>
<groupId>com.gupaoedu.vip.mall</groupId>
<artifactId>goods-api</artifactId>
<version>0.0.1-SNAPSHOT</version>
</dependency>
</dependencies>
</project>
bootstrap.yml:
server:
port: 8083
spring:
application:
name: mall-canal
cloud:
nacos:
config:
file-extension: yaml
server-addr: 192.168.100.130:8848
discovery:
#Nacos的注册地址
server-addr: 192.168.100.130:8848
#Canal配置
canal:
server: 192.168.100.130:11111
destination: example
#日志配置
logging:
pattern:
console: "%msg%n"
level:
root: error
创建监听类:com.gupaoedu.vip.canal.listener.AdItemsHandler
@CanalTable(value = "ad_items")
@Component
public class AdItemsHandler implements EntryHandler<AdItems> {
@Autowired
private SkuFeign skuFeign;
@Override
public void insert(AdItems adItems) {
//加载缓存
skuFeign.updateTypeItems(adItems.getType());
}
/***
* 修改
* @param before
* @param after
*/
@Override
public void update(AdItems before, AdItems after) {
//分类不同,则重新加载之前的缓存
if(before.getType().intValue()!=after.getType().intValue()){
//修改缓存
skuFeign.updateTypeItems(before.getType());
}
//加载缓存
skuFeign.updateTypeItems(after.getType());
}
@Override
public void delete(AdItems adItems) {
//删除缓存
skuFeign.deleteTypeItems(adItems.getType());
}
}
创建启动类:com.gupaoedu.vip.canal.MallCanalApplication
@SpringBootApplication(exclude = DataSourceAutoConfiguration.class)
@EnableFeignClients(basePackages = {"com.gupaoedu.vip.mall.goods.feign"})
public class MallCanalApplication {
public static void main(String[] args) {
SpringApplication.run(MallCanalApplication.class,args);
}
}