分享一下基于 Redis Cluster 的Redis集群解决方案。
Redis Cluster 是社区版推出的Redis分布式集群解决方案,主要解决 Redis 分布式方面的需求。比如:当遇到单机内存,并发,流量等瓶颈时,Redis Cluster 能起到很好的负载均衡的目的。
搭建环境是基于Docker,Docker Compose。
1. 创建目录,6个redis服务, 7001~7006。
mkdir -p redis/node7001/conf
mkdir -p redis/node7001/data
mkdir -p redis/node7002/conf
mkdir -p redis/node7002/data
mkdir -p redis/node7003/conf
mkdir -p redis/node7003/data
mkdir -p redis/node7004/conf
mkdir -p redis/node7004/data
mkdir -p redis/node7005/conf
mkdir -p redis/node7005/data
mkdir -p redis/node7006/conf
mkdir -p redis/node7006/data
redis目录结构如下:
.
├── docker-compose.yml
├── node7001
│ ├── conf
│ │ └── redis.conf
│ └── data
├── node7002
│ ├── conf
│ │ └── redis.conf
│ └── data
├── node7003
│ ├── conf
│ │ └── redis.conf
│ └── data
├── node7004
│ ├── conf
│ │ └── redis.conf
│ └── data
├── node7005
│ ├── conf
│ │ └── redis.conf
│ └── data
└── node7006
├── conf
│ └── redis.conf
└── data
2. 创建Redis配置文件并放入conf文件夹。
redis.cnf 参考配置
# 修改为实际使用端口,cluster-announce-ip相同不可重复
port 7001
# 开启入口master授权访问
protected-mode yes
masterauth 111111
requirepass 111111
# 开启集群
cluster-enabled yes
cluster-config-file nodes.conf
cluster-node-timeout 5000
# 集群节点IP host模式为宿主机IP 修改为服务器实际IP
cluster-announce-ip 192.168.101.57
# 集群节点端口 同port
cluster-announce-port 7001
# 集群总线端口 1+port
cluster-announce-bus-port 17001
# 开启 appendonly 备份模式
appendonly yes
# 每秒钟备份
appendfsync everysec
# 对aof文件进行压缩时,是否执行同步操作
no-appendfsync-on-rewrite no
# 当目前aof文件大小超过上一次重写时的aof文件大小的100%时会再次进行重写
auto-aof-rewrite-percentage 100
# 重写前AOF文件的大小最小值 默认 64mb
auto-aof-rewrite-min-size 64mb
3. 创建docker-compose.yml文件并放在根目录。
参考文章:Docker实战。
version: '3.8'
services:
redis7001:
image: 'redis:6.2'
container_name: redis7001
command: ['redis-server', '/usr/local/etc/redis/redis.conf']
volumes:
- ./node7001/conf/redis.conf:/usr/local/etc/redis/redis.conf
- ./node7001/data:/data
ports:
- '7001:7001'
- '17001:17001'
environment:
# 设置时区为上海,否则时间会有问题
- TZ=Asia/Shanghai
redis7002:
image: 'redis:6.2'
container_name: redis7002
command: ['redis-server', '/usr/local/etc/redis/redis.conf']
volumes:
- ./node7002/conf/redis.conf:/usr/local/etc/redis/redis.conf
- ./node7002/data:/data
ports:
- '7002:7002'
- '17002:17002'
environment:
- TZ=Asia/Shanghai
redis7003:
image: 'redis:6.2'
container_name: redis7003
command: ['redis-server', '/usr/local/etc/redis/redis.conf']
volumes:
- ./node7003/conf/redis.conf:/usr/local/etc/redis/redis.conf
- ./node7003/data:/data
ports:
- '7003:7003'
- '17003:17003'
environment:
- TZ=Asia/Shanghai
redis7004:
image: 'redis:6.2'
container_name: redis7004
command: ['redis-server', '/usr/local/etc/redis/redis.conf']
volumes:
- ./node7004/conf/redis.conf:/usr/local/etc/redis/redis.conf
- ./node7004/data:/data
ports:
- '7004:7004'
- '17004:17004'
environment:
- TZ=Asia/Shanghai
redis7005:
image: 'redis:6.2'
container_name: redis7005
command: ['redis-server', '/usr/local/etc/redis/redis.conf']
volumes:
- ./node7005/conf/redis.conf:/usr/local/etc/redis/redis.conf
- ./node7005/data:/data
ports:
- '7005:7005'
- '17005:17005'
environment:
- TZ=Asia/Shanghai
redis7006:
image: 'redis:6.2'
container_name: redis7006
command: ['redis-server', '/usr/local/etc/redis/redis.conf']
volumes:
- ./node7006/conf/redis.conf:/usr/local/etc/redis/redis.conf
- ./node7006/data:/data
ports:
- '7006:7006'
- '17006:17006'
environment:
- TZ=Asia/Shanghai
4. 以上准备工作都做好后,开始实操。
4.1 启动redis。
docker-compose up
4.2 配置cluster集群。
4.2.1 创建集群。
docker exec -it redis7001 redis-cli -p 7001 -a 111111 --cluster create 192.168.3.77:7001 192.168.3.77:7002 192.168.3.77:7003 192.168.3.77:7004 192.168.3.77:7005 192.168.3.77:7006 --cluster-replicas 1
注意:IP,端口,密钥都需要替换为实际使用的!本文中出现的命令行都需要替换!
创建成功如下图:
如果需要解除集群,只需要删除data文件夹中的appendonly.aof和nodes.conf
rm -rf node*/data/appendonly.aof
rm -rf node*/data/nodes.conf
4.2.2 测试集群。
4.2.2.1 测试集群通信:
docker exec -it redis7001 redis-cli -h 192.168.3.77 -p 7005 -a 111111 ping
4.2.2.2 测试集群读写:
docker exec -it redis7001 redis-cli -h 192.168.3.77 -p 7003 -a 111111 -c
set name test
get name
4.2.3 查看集群。
4.2.3.1 查看集群状态
cluster nodes
4.2.3.2 查看 slots 分片
cluster slots
4.2.3.3 查看集群信息
cluster info
5. Spring项目集成。
5.1 pom.xml
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>
<dependency>
<groupId>org.redisson</groupId>
<artifactId>redisson-spring-boot-starter</artifactId>
<version>${redisson-spring-boot-starter.version}</version>
</dependency>
5.2 application.properties
spring.redis.cluster.nodes=192.168.3.77:7001,192.168.3.77:7002,192.168.3.77:7003,192.168.3.77:7004,192.168.3.77:7005,192.168.3.77:7006
spring.redis.cluster.max-redirects=3
spring.redis.timeout=5000
spring.redis.password=111111
6. 附录
6.1 redis.conf example
6.2 param notify-keyspace-events
K Keyspace events, published with __keyspace@<db>__ prefix.
E Keyevent events, published with __keyevent@<db>__ prefix.
g Generic commands (non-type specific) like DEL, EXPIRE, RENAME, ...
$ String commands
l List commands
s Set commands
h Hash commands
z Sorted set commands
x Expired events (events generated every time a key expires)
e Evicted events (events generated when a key is evicted for maxmemory)
A Alias for g$lshzxe, so that the "AKE" string means all the events.