redis集群创建与jedis连接

1.创建redis-cluster

     redis中文官方网站有说明,很详细地介绍了redis-cluster创建的流程。可能之前需要做些准备工作,如安装ruby,openssl等,这个就上网自己查吧,给几个链接:

   ruby: https://www.cnblogs.com/smileyes/p/7489484.html

   openssl: http://blog.csdn.net/huang930528/article/details/51027915

    几个值得注意的点

    a.用redis-server分别启动7000~7005几个redis.conf时,一定要确保当前目录在700x文件夹内,因为appendonly.aof(每执行一条命令就记录下来)和dump.rdb(save 900 1)都是默认在当前目录下生成,要确保几个server的存档分开存放。

    b.redis-trib.rb create 创建集群时,提示slots分区是否正确,一定要回复yes!yes!yes!

    c.daemonize 可以设为yes,变成后台启动

    d.关于bind的学问就非常大了,后面的问题全是bind引起的

如果一切顺利的话,应该可以很轻易的用redis-trib.rb创建出集群,也可以在本地通过redis-cli连接集群并且set、get。当你以为一切大功告成,redis-cluster so easy,跑到客户端用java程序连接cluster时,才发觉坑现在才开始出现,而且都尼玛是连环坑。

   问题一:客户端redis-cli -c -h 192.168.7.40 -p 7000可以连接上,但只要get、set就会提示

 DENIED Redis is running in protected mode because protected mode is enabled, no bind address was specified, no authentication password is requested to clients. In this mode connections are only accepted from the loopback interface. If you want to connect from external computers to Redis you may adopt one of the following solutions: 1) Just disable protected mode sending the command 'CONFIG SET protected-mode no' from the loopback interface by connecting to Redis from the same host the server is running, however MAKE SURE Redis is not publicly accessible from internet if you do so. Use CONFIG REWRITE to make this change permanent. 2) Alternatively you can just disable the protected mode by editing the Redis configuration file, and setting the protected mode option to 'no', and then restarting the server. 3) If you started the server manually just for testing, restart it with the '--protected-mode no' option. 4) Setup a bind address or an authentication password. NOTE: You only need to do one of the above things in order for the server to start accepting connections from the outside.

 原因:redis.conf中   #bind 127.0.0.1 

 问题二:客户端用jedis连接,代码测试不通过 

 redis.clients.jedis.exceptions.JedisConnectionException: java.net.ConnectException: Connection refused: connect

 原因:redis.conf中   bind 127.0.0.1

 遇到这两个问题到网上查一查就知道怎么解决,要加入redis-server的本机ip,如192.168.7.40,参考

http://blog.csdn.net/fxq8866/article/details/58238802。

 所以硬着头皮先去关闭cluster集群,妈的,怎么关呢?官方文档没说啊?

2.删除redis-cluster

 如果是删除一个节点,需要把这个节点的data移到其他slots中,然后再redis-trib移除。

 但如果是删除整个集群呢?

 a.先用redis-cli -c -p 7000 shutdown 关闭7000端口的节点,以此类推关闭其他所有节点;

 b.删除700X文件夹内的所有appendonly.aof,

  然后全部rm -rf

3.连接redis-cluster

 ok,然后修改各文件夹的redis.conf中的bind内容,改成bind 127.0.0.1 192.168.7.40。再小心翼翼地输入redis-trib.rb指令,注意的是ip地址要写成内网ip 192.168.7.40,而非教程中的127.0.0.1。

./redis-trib.rb create --replicas 1 192.168.7.40:7000 ...

 大功告成!跑到客户端用redis-cli -c -h 192.168.7.40 -p 7000连接,没问题。set name zhu,提示


  -> Redirected to slot [5798] located at 192.168.7.40:7001

  然后没反应了???!!!

  跑到java上一跑,仍然提示connection refused

  这肿么可能!!!bind 127.0.0.1 192.168.7.40 怎么会出错!!!

 然后你一顿乱找,不停地删除集群,修改redis.conf,再启动集群,头都被绕晕了,bind到底连接的是什么呢?

 首先可以肯定的是,bind连接的一定是redis-sever cluster本地的ip,绝对别傻不拉几的把你客户端的地址也添加进来了,所以有了192.168.7.40;同时要满足server端那边能用redis-cli -c -p 7000连接上,所以把127.0.0.1加上去了。

 但是顺序是有讲究的!!!

 改成bind 192.168.7.40 127.0.0.1就ojbk了!原因嘛我也不清楚,猜测是默认先取第一个。

 最终是成功用jedisCluster连接上了redis集群,期间的曲折自不必说,感觉是把该踩的坑全踩了一遍,如果你们遇到了更奇葩的坑,也不要慌,用排除法一点点解决,实在不行,就

 洗

  

   睡

    吧

     RUA!!!

猜你喜欢

转载自blog.csdn.net/qq_30905661/article/details/79577265