分享一下基于 Redis Cluster 的Redis集群解决方案。
Redis Cluster 是社区版推出的Redis分布式集群解决方案,主要解决 Redis 分布式方面的需求。比如:当遇到单机内存,并发,流量等瓶颈时,Redis Cluster 能起到很好的负载均衡的目的。
搭建环境是基于Docker,Docker Compose。

1. 创建目录,6个redis服务, 7001~7006。

  1. mkdir -p redis/node7001/conf
  2. mkdir -p redis/node7001/data
  3. mkdir -p redis/node7002/conf
  4. mkdir -p redis/node7002/data
  5. mkdir -p redis/node7003/conf
  6. mkdir -p redis/node7003/data
  7. mkdir -p redis/node7004/conf
  8. mkdir -p redis/node7004/data
  9. mkdir -p redis/node7005/conf
  10. mkdir -p redis/node7005/data
  11. mkdir -p redis/node7006/conf
  12. mkdir -p redis/node7006/data

redis目录结构如下:

  1. .
  2. ├── docker-compose.yml
  3. ├── node7001
  4. ├── conf
  5. └── redis.conf
  6. └── data
  7. ├── node7002
  8. ├── conf
  9. └── redis.conf
  10. └── data
  11. ├── node7003
  12. ├── conf
  13. └── redis.conf
  14. └── data
  15. ├── node7004
  16. ├── conf
  17. └── redis.conf
  18. └── data
  19. ├── node7005
  20. ├── conf
  21. └── redis.conf
  22. └── data
  23. └── node7006
  24. ├── conf
  25. └── redis.conf
  26. └── data

2. 创建Redis配置文件并放入conf文件夹。

redis.cnf 参考配置

  1. # 修改为实际使用端口,cluster-announce-ip相同不可重复
  2. port 7001
  3. # 开启入口master授权访问
  4. protected-mode yes
  5. masterauth 111111
  6. requirepass 111111
  7. # 开启集群
  8. cluster-enabled yes
  9. cluster-config-file nodes.conf
  10. cluster-node-timeout 5000
  11. # 集群节点IP host模式为宿主机IP 修改为服务器实际IP
  12. cluster-announce-ip 192.168.101.57
  13. # 集群节点端口 同port
  14. cluster-announce-port 7001
  15. # 集群总线端口 1+port
  16. cluster-announce-bus-port 17001
  17. # 开启 appendonly 备份模式
  18. appendonly yes
  19. # 每秒钟备份
  20. appendfsync everysec
  21. # 对aof文件进行压缩时,是否执行同步操作
  22. no-appendfsync-on-rewrite no
  23. # 当目前aof文件大小超过上一次重写时的aof文件大小的100%时会再次进行重写
  24. auto-aof-rewrite-percentage 100
  25. # 重写前AOF文件的大小最小值 默认 64mb
  26. auto-aof-rewrite-min-size 64mb

3. 创建docker-compose.yml文件并放在根目录。

参考文章:Docker实战

  1. version: '3.8'
  2. services:
  3. redis7001:
  4. image: 'redis:6.2'
  5. container_name: redis7001
  6. command: ['redis-server', '/usr/local/etc/redis/redis.conf']
  7. volumes:
  8. - ./node7001/conf/redis.conf:/usr/local/etc/redis/redis.conf
  9. - ./node7001/data:/data
  10. ports:
  11. - '7001:7001'
  12. - '17001:17001'
  13. environment:
  14. # 设置时区为上海,否则时间会有问题
  15. - TZ=Asia/Shanghai
  16. redis7002:
  17. image: 'redis:6.2'
  18. container_name: redis7002
  19. command: ['redis-server', '/usr/local/etc/redis/redis.conf']
  20. volumes:
  21. - ./node7002/conf/redis.conf:/usr/local/etc/redis/redis.conf
  22. - ./node7002/data:/data
  23. ports:
  24. - '7002:7002'
  25. - '17002:17002'
  26. environment:
  27. - TZ=Asia/Shanghai
  28. redis7003:
  29. image: 'redis:6.2'
  30. container_name: redis7003
  31. command: ['redis-server', '/usr/local/etc/redis/redis.conf']
  32. volumes:
  33. - ./node7003/conf/redis.conf:/usr/local/etc/redis/redis.conf
  34. - ./node7003/data:/data
  35. ports:
  36. - '7003:7003'
  37. - '17003:17003'
  38. environment:
  39. - TZ=Asia/Shanghai
  40. redis7004:
  41. image: 'redis:6.2'
  42. container_name: redis7004
  43. command: ['redis-server', '/usr/local/etc/redis/redis.conf']
  44. volumes:
  45. - ./node7004/conf/redis.conf:/usr/local/etc/redis/redis.conf
  46. - ./node7004/data:/data
  47. ports:
  48. - '7004:7004'
  49. - '17004:17004'
  50. environment:
  51. - TZ=Asia/Shanghai
  52. redis7005:
  53. image: 'redis:6.2'
  54. container_name: redis7005
  55. command: ['redis-server', '/usr/local/etc/redis/redis.conf']
  56. volumes:
  57. - ./node7005/conf/redis.conf:/usr/local/etc/redis/redis.conf
  58. - ./node7005/data:/data
  59. ports:
  60. - '7005:7005'
  61. - '17005:17005'
  62. environment:
  63. - TZ=Asia/Shanghai
  64. redis7006:
  65. image: 'redis:6.2'
  66. container_name: redis7006
  67. command: ['redis-server', '/usr/local/etc/redis/redis.conf']
  68. volumes:
  69. - ./node7006/conf/redis.conf:/usr/local/etc/redis/redis.conf
  70. - ./node7006/data:/data
  71. ports:
  72. - '7006:7006'
  73. - '17006:17006'
  74. environment:
  75. - TZ=Asia/Shanghai

4. 以上准备工作都做好后,开始实操。

4.1 启动redis。

  1. docker-compose up

4.2 配置cluster集群。

4.2.1 创建集群。

  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,端口,密钥都需要替换为实际使用的!本文中出现的命令行都需要替换!
创建成功如下图:
image.png
如果需要解除集群,只需要删除data文件夹中的appendonly.aof和nodes.conf

  1. rm -rf node*/data/appendonly.aof
  2. rm -rf node*/data/nodes.conf

4.2.2 测试集群。

4.2.2.1 测试集群通信:

  1. docker exec -it redis7001 redis-cli -h 192.168.3.77 -p 7005 -a 111111 ping

image.png

4.2.2.2 测试集群读写:

  1. docker exec -it redis7001 redis-cli -h 192.168.3.77 -p 7003 -a 111111 -c
  2. set name test
  3. get name

image.png

4.2.3 查看集群。

4.2.3.1 查看集群状态

  1. cluster nodes

image.png

4.2.3.2 查看 slots 分片

  1. cluster slots

image.png

4.2.3.3 查看集群信息

  1. cluster info

image.png

5. Spring项目集成。

5.1 pom.xml

  1. <dependency>
  2. <groupId>org.springframework.boot</groupId>
  3. <artifactId>spring-boot-starter-data-redis</artifactId>
  4. </dependency>
  5. <dependency>
  6. <groupId>org.redisson</groupId>
  7. <artifactId>redisson-spring-boot-starter</artifactId>
  8. <version>${redisson-spring-boot-starter.version}</version>
  9. </dependency>

5.2 application.properties

  1. 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
  2. spring.redis.cluster.max-redirects=3
  3. spring.redis.timeout=5000
  4. spring.redis.password=111111

6. 附录

6.1 redis.conf example

6.2 param notify-keyspace-events

  1. K Keyspace events, published with __keyspace@<db>__ prefix.
  2. E Keyevent events, published with __keyevent@<db>__ prefix.
  3. g Generic commands (non-type specific) like DEL, EXPIRE, RENAME, ...
  4. $ String commands
  5. l List commands
  6. s Set commands
  7. h Hash commands
  8. z Sorted set commands
  9. x Expired events (events generated every time a key expires)
  10. e Evicted events (events generated when a key is evicted for maxmemory)
  11. A Alias for g$lshzxe, so that the "AKE" string means all the events.

6.3 redisson