報錯信息
Spring Boot項目中訪問redis cluster報錯:
redis.clients.jedis.exceptions.JedisClusterMaxRedirectionsException: Too many Cluster redirections?(集群重定向過多)
原因分析
redis 綁定了多個ip
eg:
- redis.conf 文件中bind 127.0.0.1 192.168.186.1
- 在創建集群時host使用127.0.0.1 eg: redis-cli --cluster create 127.0.0.1:7000 127.0.0.1:7001 127.0.0.1:7002 127.0.0.1:7003 127.0.0.1:7004 127.0.0.1:7005 --cluster-replicas 1
問題修復
bind配置為主機ip(不要配置多個ip,也不要配置為回環地址127.0.0.1,配置為回環地址只有本機能訪問,其他主機無法訪問),重建集群 eg:redis-cli --cluster create 192.168.186.1:7000 192.168.186.1:7001 192.168.186.1:7002 192.168.186.1:7003 192.168.186.1:7004 192.168.186.1:7005 --cluster-replicas 1
- 所有節點redi.conf文件中bind配置為主機ip
- 刪除所有節點中的nodes.conf 、rdb文件
- kill所有redis進程
- 重新啟動所有redis實例
- 重建集群 redis-cli --cluster create 192.168.186.1:7000 192.168.186.1:7001 192.168.186.1:7002 192.168.186.1:7003 192.168.186.1:7004 192.168.186.1:7005 --cluster-replicas 1
- 重啟項目即可正常訪問
如果重建集群時報錯 :ERR] Node 192.168.186.1:7001 is not empty. Either the node already knows other nodes (check with CLUSTER NODES) or contains some key in database 0.
可參考edis-cli --cluster create 創建redis集群報錯
