介绍
单机模式下Redis并发能力有限使用主从集群模式可以很好的解决性能问题,主节点负责处理写操作(如 SET、DEL、INCR 等命令),而 从节点 主要负责读取操作(如 GET、HGET、HGETALL 等命令)。
主节点负责写入
从节点负责读取
通过将读操作分担给从节点,减轻了主节点的负担,提高读取请求的处理能力。
读写分离、负载均衡、数据冗余、高可用性
DockerCompose
yaml
version: '3'
services:
redis1:
image: redis:7
container_name: redis1
ports:
- "7001:6379"
volumes:
- ./redis1.conf:/usr/local/etc/redis/redis.conf
- redis1-data:/data
command: redis-server /usr/local/etc/redis/redis.conf
networks:
- redis-cluster
redis2:
image: redis:7
container_name: redis2
ports:
- "7002:6379"
volumes:
- ./redis2.conf:/usr/local/etc/redis/redis.conf
- redis2-data:/data
command: redis-server /usr/local/etc/redis/redis.conf
networks:
- redis-cluster
redis3:
image: redis:7
container_name: redis3
ports:
- "7003:6379"
volumes:
- ./redis3.conf:/usr/local/etc/redis/redis.conf
- redis3-data:/data
command: redis-server /usr/local/etc/redis/redis.conf
networks:
- redis-cluster
networks:
redis-cluster:
driver: bridge
volumes:
redis1-data:
redis2-data:
redis3-data:
主节点配置
powershell
requirepass yourpassword #本机密码
timeout 65
maxclients 10000
databases 16 #数据库
maxmemory 1048576000
从节点配置
powershell
replicaof 103.116.247.215 7001 #主节点
masterauth yourpassword #主节点密码
requirepass yourpassword #本机密码
timeout 65
maxclients 10000
databases 16
maxmemory 1048576000
创建并启动
powershell
docker-compose up -d
验证是否集群成功
powershell
docker exec -it redis1 bash #进入容器
redis-cli -a yourpassword #连接Redis
info replication #查看节点信息
输出信息 role:master为主节点 connected_slaves:2 两个从节点
powershell
# Replication
role:master
connected_slaves:2
slave0:ip=103.116.247.215,port=6379,state=online,offset=3890,lag=1
slave1:ip=103.116.247.215,port=6379,state=online,offset=3890,lag=1
master_failover_state:no-failover
master_replid:dcc792d6c3f54a748c3b14bf43fc5b77e89bd7f9
master_replid2:737e4edbde43a4dc544d2ac7a81582b23af4e3a7
master_repl_offset:3890
second_repl_offset:239
repl_backlog_active:1
repl_backlog_size:1048576
repl_backlog_first_byte_offset:239
repl_backlog_histlen:3652