Redis的数据同步

RedisShake使用

RedisShake可以用于数据的迁移和同步。

迁移的场景很好理解,比如原先的时候多个业务使用一个redis,然后为了避免相互影响,这个时候需要把缓存也拆分出来(特别在业务初创时期)。
同步的场景的话,可以用在主备双活。因为一般调用的链路是vip->proxy->redis集群,如果vip发生了网络抖动可能会导致redis读取失败,如果做纯缓存应用的话可以有数据库兜底还好(虽然可能预热期间会导致数据库压力增加),如果用redis当持久存储的业务来说,就会导致业务方调用失败,造成业务不可用。这个时候如果有备集群的话,可以很好的切换(最简单的直接修改vip的转发,nginx是可以无损重启的)
也可以用在异地多活,具体设计可以看下阿里云的redis灾备介绍中有一小节介绍跨地域容灾。

github地址

项目github的地址 https://github.com/alibaba/RedisShake/releases
可以下载source code进行编译,也可以直接下载二进制版。

说明文档

第一次使用,如何进行配置
通过Redis-shake将自建Redis迁移至阿里云

配置文件说明

sync模式配置以下几个内容, 包括source.type,source.address,source.password_raw,target.type,target.address,target.password_raw 6个配置。
具体配置见 第一次使用,如何进行配置

启动命令

./redis-shake -conf=redis-shake.conf -type=sync

单个节点到单个节点配置举例

  1. 下载redis源码,地址 https://github.com/redis/redis/releases/tag/6.0.9
  2. 编译源码 make

不一定需要make install

  1. 启动两个服务端
./redis-server --port 6380
./redis-server --port 6381
  1. 下载redis-shake的二进制包
wget https://github.com/alibaba/RedisShake/releases/download/release-v2.0.3-20200724/redis-shake-v2.0.3.tar.gz
  1. 配置redis-shake文件,然后启动redis-shake

没有设置密码的话不用填写

source.type = standalone
source.address = 127.0.0.1:6380
source.password_raw = 

target.type = standalone
target.address = 127.0.0.1:6381
target.password_raw =
  1. 启动redis-shake
    这边看你的环境如果是mac os就用darwin后缀的,如果是linux用linux后缀
cp redis-shake.conf redis-shake-standalone.conf
./redis-shake.darwin -conf=redis-shake-standalone.conf -type=sync
  1. 在source的redis db (端口号6380)中输入一些命令
    redis有5种类型的数据结构,都试下
redis-cli -h 127.0.0.1 -p 6380

set hello world

hset user:zihao name zihao age 28 sex male
hset user:xmz name xmz age 26 sex female

SADD phone apple sanxing huawei

lpush city hangzhou suzhou chengdu

ZADD stu_score 100 zihao 99 xmz 90 zpy
  1. 查看target的redis db中数据
edis-cli -h 127.0.0.1 -p 6381

localhost:6381> keys *
1) "city"
2) "user:xmz"
3) "stu_score"
4) "hello"
5) "phone"
6) "user:zihao"

集群cluster到集群cluster配置举例

集群直接使用docker运行,redis-cluster镜像地址
1.启动1个redis集群,命名为cluster1

docker run -p 7000-7005:7000-7005 -d --name cluster1 grokzen/redis-cluster:6.0.9

2.启动另1个redis集群,命名为cluster2

docker run -p 8000-8005:7000-7005 -d --name cluster2 grokzen/redis-cluster:6.0.9

3.获取cluster1和cluster2的集群信息

redis-cli -h localhost -p 7000 -c
localhost:7000> CLUSTER NODES

redis-cli -h localhost -p 8000 -c
localhost:7000> CLUSTER NODES

4.redis-shake文件配置
这边有两种配置一种是自动发现的,一种把所有master或者slave写上。具体看上面的github文档

source.type = cluster
source.address = [email protected]:7000
source.password_raw = 

target.type = cluster
target.address = [email protected]:8000
target.password_raw =

5.启动redis-shake

cp redis-shake.conf redis-shake-cluster.conf
./redis-shake.linux -conf=redis-shake-cluster.conf -type=sync

6.在source的redis-cluster(cluster1)输入一些命令

set hello world

hset user:zihao name zihao age 28 sex male
hset user:xmz name xmz age 26 sex female

SADD phone apple sanxing huawei

lpush city hangzhou suzhou chengdu

ZADD stu_score 100 zihao 99 xmz 90 zpy
  1. 在target的redis-cluster(2)查看
172.17.0.3:7002> get hello
-> Redirected to slot [866] located at 172.17.0.3:7000
"world"

172.17.0.3:7002> hgetall user:zihao
1) "name"
2) "zihao"
3) "age"
4) "28"
5) "sex"
6) "male"

172.17.0.3:7000> SMEMBERS phone
-> Redirected to slot [8939] located at 172.17.0.3:7001
1) "huawei"
2) "sanxing"
3) "apple"

172.17.0.3:7001> LRANGE city 0 -1
-> Redirected to slot [11479] located at 172.17.0.3:7002
1) "chengdu"
2) "suzhou"
3) "hangzhou"

172.17.0.3:7001> ZRANGE stu_score 0 -1
1) "zpy"
2) "xmz"
3) "zihao"

集群版cluster到proxy配置举例

这边proxy使用的是codis (阿里云的集群版redis也是使用proxy模式的,阿里云的redis架构)。

搭建一个codis集群环境

  1. 获取codis的二进制安装包 https://github.com/CodisLabs/codis/releases/download/3.2.2/codis3.2.2-go1.8.5-linux.tar.gz
  2. 依次启动各个组件
    具体的配置细节本文不做过多描述,详情见 https://github.com/CodisLabs/codis/blob/release3.2/doc/tutorial_zh.md
  • 启动zk
docker run --name "Codis-Z2181" -d --read-only -p 2181:2181 jplock/zookeeper
  • 启动dashboard
#dashboard.toml https://github.com/CodisLabs/codis/blob/release3.2/config/dashboard.toml
#coordinator_name和coordinator_addr改成zk
nohup ./codis-dashboard --ncpu=4 --config=dashboard.toml --log=dashboard.log --log-level=WARN &
  • 启动proxy
#proxy.toml https://github.com/CodisLabs/codis/blob/release3.2/config/proxy.toml
nohup ./codis-proxy --ncpu=4 --config=proxy.toml --log=proxy.log --log-level=WARN &
  • 启动codis server
    这边启动4台
nohup ./codis-server --port 6379 &
 nohup ./codis-server --port 6380 &
 nohup ./codis-server --port 6381 &
 nohup ./codis-server --port 6382 &

可以通过redis-cli分别连接测试下连接通不通

redis-cli -h localhost -p 6379
  • 启动codis fe
nohup ./codis-fe --ncpu=4 --log=fe.log --log-level=WARN --zookeeper=127.0.0.1:2181 --listen=0.0.0.0:8080 &

3.通过fe配置codis集群
打开localhost:8080的页面
• 添加proxy
通过proxy.toml的admin_addr地址添加


添加proxy.png
  • 添加group


    添加group.png
  • 添加server


    添加server.png
  • 分配slot


    分配slot.png

codis连接测试

[root@JD codis3.2.2-go1.8.5-linux]# redis-cli -h localhost -p 19000
localhost:19000> set hello world
OK
localhost:19000> get hello
"world"

启动redis-shake将redis-cluster的数据同步到codis

  • 配置redis-shake
source.type = cluster
source.address = [email protected]:7000
source.password_raw = 

target.type = proxy
target.address = 127.0.0.1:19000
target.password_raw =

#源端的redis版本是6.0.9,target版本的redis版本比较低 所以需要设置这个 不然会报restore的错误 
big_key_threshold=1
  • 启动redis-shake
cp redis-shake.conf redis-shake-proxy.conf
./redis-shake.linux -conf=redis-shake-proxy.conf -type=sync

RedisFullCheck

github地址

https://github.com/alibaba/RedisFullCheck

说明文档

  • 第一次使用,如何进行配置
  • 阿里云RedisFullCheck文档

使用

./redis-full-check -s "172.17.0.2:7000;172.17.0.2:7001;172.17.0.2:7002" -t "172.17.0.4:7000;172.17.0.4:7001;172.17.0.4:7002" --comparemode=1 --comparetimes=1 --qps=10 --batchcount=100 --sourcedbtype=1 --targetdbtype=1 --targetdbfilterlist=0

比对结果输出

[root@JD redis-full-check-1.4.8]# ./redis-full-check -s "172.17.0.2:7000;172.17.0.2:7001;172.17.0.2:7002" -t "172.17.0.4:7000;172.17.0.4:7001;172.17.0.4:7002" --comparemode=1 --comparetimes=1 --qps=10 --batchcount=100 --sourcedbtype=1 --targetdbtype=1 --targetdbfilterlist=0
[INFO 2021-04-08-17:45:41 main.go:65]: init log success
[INFO 2021-04-08-17:45:41 main.go:168]: configuration: {172.17.0.2:7000;172.17.0.2:7001;172.17.0.2:7002  auth 1 -1 172.17.0.4:7000;172.17.0.4:7001;172.17.0.4:7002  auth 1 0 result.db  1 1 unknown unknown unknown 10 5 100 5   false 16384  20445 false}
[INFO 2021-04-08-17:45:41 main.go:170]: ---------
[INFO 2021-04-08-17:45:41 full_check.go:238]: sourceDbType=1, p.sourcePhysicalDBList=[172.17.0.2:7000 172.17.0.2:7001 172.17.0.2:7002]
[INFO 2021-04-08-17:45:41 full_check.go:241]: db=0:keys=0(inaccurate for type cluster)
[INFO 2021-04-08-17:45:41 full_check.go:253]: ---------------- start 1th time compare
[INFO 2021-04-08-17:45:41 full_check.go:278]: start compare db 0
[INFO 2021-04-08-17:45:41 scan.go:20]: build connection[source redis addr: [172.17.0.2:7000]]
[INFO 2021-04-08-17:45:41 scan.go:20]: build connection[source redis addr: [172.17.0.2:7001]]
[INFO 2021-04-08-17:45:41 scan.go:20]: build connection[source redis addr: [172.17.0.2:7002]]
[INFO 2021-04-08-17:45:42 full_check.go:203]: stat:
times:1, db:0, dbkeys:0, finish:-1%, finished:true
KeyScan:{6 6 0}
KeyEqualAtLast|string|equal|{1 1 0}
KeyEqualAtLast|hash|equal|{2 2 0}
KeyEqualAtLast|list|equal|{1 1 0}
KeyEqualAtLast|set|equal|{1 1 0}
KeyEqualAtLast|zset|equal|{1 1 0}
FieldEqualAtLast|hash|equal|{6 6 0}
FieldEqualAtLast|set|equal|{3 3 0}
FieldEqualAtLast|zset|equal|{3 3 0}

[INFO 2021-04-08-17:45:42 full_check.go:328]: --------------- finished! ----------------
all finish successfully, totally 0 key(s) and 0 field(s) conflict

你可能感兴趣的:(Redis的数据同步)