redis---cluster集群模式搭建
一、准备集群模式的配置文件
grep -Ev "^$|#" redis.conf 复制去除注释文本 mkdir -p cluster-redis cd cluster-redis mkdir -p node1-7001 node2-7002 node3-7003 node4-7004 node5-7005 node6-7006 在每一个节点下面新建一个redis.conf文件 cd node1-7001 vim redis.conf
redis.conf配置文件的内容
bind 0.0.0.0 protected-mode no port 7001 tcp-backlog 511 timeout 0 tcp-keepalive 300 daemonize yes supervised no pidfile home/han.sun/application/cluster-redis/node1-7001/redis_7001.pid loglevel notice logfile "/home/han.sun/application/cluster-redis/node1-7001/redis.log" # 允许加入集群 cluster-enabled yes # 此文件会被生成在上面dir配置目录下, # cluster开启必须重命名指定cluster-config-file, # 不能与别的节点相同,否则会启动失败,最好按主机+端口命名 cluster-config-file node1-7001.conf # 节点宕机被发现的时间 cluster-node-timeout 15000 databases 16 always-show-logo yes save 900 1 save 300 10 save 60 10000 stop-writes-on-bgsave-error yes rdbcompression yes rdbchecksum yes dbfilename dump.rdb dir ./ replica-serve-stale-data yes replica-read-only yes repl-diskless-sync no repl-diskless-sync-delay 5 repl-disable-tcp-nodelay no replica-priority 100 # 此redis服务端密码 requirepass han.sun # 每个节点都有可能是从节点,所以如果设置了requirepass,就要设置对应的masterauth masterauth han.sun lazyfree-lazy-eviction no lazyfree-lazy-expire no lazyfree-lazy-server-del no replica-lazy-flush no appendonly no appendfilename "appendonly.aof" appendfsync everysec no-appendfsync-on-rewrite no auto-aof-rewrite-percentage 100 auto-aof-rewrite-min-size 64mb aof-load-truncated yes aof-use-rdb-preamble yes lua-time-limit 5000 slowlog-log-slower-than 10000 slowlog-max-len 128 latency-monitor-threshold 0 notify-keyspace-events "" hash-max-ziplist-entries 512 hash-max-ziplist-value 64 list-max-ziplist-size -2 list-compress-depth 0 set-max-intset-entries 512 zset-max-ziplist-entries 128 zset-max-ziplist-value 64 hll-sparse-max-bytes 3000 stream-node-max-bytes 4096 stream-node-max-entries 100 activerehashing yes client-output-buffer-limit normal 0 0 0 client-output-buffer-limit replica 256mb 64mb 60 client-output-buffer-limit pubsub 32mb 8mb 60 hz 10 dynamic-hz yes aof-rewrite-incremental-fsync yes rdb-save-incremental-fsync yes
二、启动各自的节点服务
redis-server ./redis.conf
三、执行集群命令
redis-cli --cluster create 192.168.56.11:7001 192.168.56.11:7002 192.168.56.12:7003 192.168.56.12:7004 192.168.56.13:7005 192.168.56.13:7006 --cluster-replicas 1 -a han.sun
四、查看集群节点
redis-cli -c -h ip地址 -a 密码 -p 7003
这说明cluster模式下会自动进行分配两两主从模式
五、测试set、get是否将key打在了不同的节点上
六、模拟一个主节点宕机