模拟安装redis5.0集群并通过Java代码访问redis集群
在虚拟机上模拟redis5.0的集群,由于redis的投票机制,一个集群至少需要3个redis节点,如果每个节点设置一主一备,一共需要六台虚拟机来搭建集群,此处,在一台虚拟机上使用6个redis实例来模拟搭建一个伪分布式的redis集群。
1.安装ruby
搭建redis集群需要ruby脚本,需要安装ruby的环境
(1)yum install ruby
(2) yum install rubygems
2. 创建一个集群的目录
3. 复制编译安装好的redis节点
进入redis01目录,删除快照文件
修改配置文件中的端口号,去掉集群配置cluster-enabled yes前面的#
除此之外,还需要将绑定ip注释掉,即 bind命令前面加#(以便除了linux机器之外的其他ip可以访问),关闭保护模式:protected-mode yes改为protected-mode no
(这样做是为了通过Java代码能够访问到虚拟机上的redis5集群,redis3不存在这两项设置,如果是生产环境搭建redis集群,bind应该是要绑定本机回环地址 )
4.复制其他5个节点并修改redis.conf的端口号为7002~7006
5.启动所有节点:
vim startall.sh 创建一个启动脚本:
cd redis01
./redis-server redis.conf
cd ..
cd redis02
./redis-server redis.conf
cd ..
cd redis03
./redis-server redis.conf
cd ..
cd redis04
./redis-server redis.conf
cd ..
cd redis05
./redis-server redis.conf
cd ..
cd redis06
./redis-server redis.conf
保存后赋予执行权限:
[root@localhost redis-cluster]# chmod +x startall.sh
并执行脚本:bash startall.sh:
6.创建集群:redis5创建集群的工具集成到了redis-cli命令行中,
创建集群命令如下:进入任意一个redis实例,此处进入redis01目录下:
./redis-cli --cluster create 192.168.93.135:7001 192.168.93.135:7002 192.168.93.135:7003 192.168.93.135:7004 192.168.93.135:7005 192.168.93.135:7006 --cluster-replicas 1
(以命令为准,下图仅供示例)
创建集群过程中分配槽号:
7测试集群状态:
./redis-cli -h 192.168.93.135 -p 7001 -c( -h表示host ip,-p表示端口,-c表示要连接的是集群的节点)
查看集群所有节点:
向集群中添加数据:(此时会计算槽号,并将数据保存到对应的槽)
8.停止集群所有节点:在 redis-cluster目录下创建shutdown-all.sh脚本,内容如下:
redis01/redis-cli -p 7001 shutdown
redis02/redis-cli -p 7002 shutdown
redis03/redis-cli -p 7003 shutdown
redis04/redis-cli -p 7004 shutdown
redis05/redis-cli -p 7005 shutdown
redis06/redis-cli -p 7006 shutdown
然后赋予执行权限,bash该脚本,即可停止所有redis集群节点
9.Java代码访问redis5.0 集群节点:
1 @Test 2 public void testJedisCluster() throws IOException { 3 /*redis.clients.jedis.JedisCluster.JedisCluster(Set<HostAndPort> nodes)*/ 4 Set<HostAndPort> nodes = new HashSet<>(); 5 String clusterHost = "192.168.93.135"; 6 nodes.add(new HostAndPort(clusterHost, 7001)); 7 nodes.add(new HostAndPort(clusterHost, 7002)); 8 nodes.add(new HostAndPort(clusterHost, 7003)); 9 nodes.add(new HostAndPort(clusterHost, 7004)); 10 nodes.add(new HostAndPort(clusterHost, 7005)); 11 nodes.add(new HostAndPort(clusterHost, 7006)); 12 JedisCluster cluster = new JedisCluster(nodes); 13 // 向redis集群中保存数据 14 cluster.set("name", "LBG"); 15 String name = cluster.get("name"); 16 System.out.println(name); 17 cluster.close(); 18 }
结果报如下错误:节点不可访问,这是由于redis的集群节端口没有开放,被防火墙拦截了
redis.clients.jedis.exceptions.JedisNoReachableClusterNodeException: No reachable node in cluster at redis.clients.jedis.JedisSlotBasedConnectionHandler.getConnection(JedisSlotBasedConnectionHandler.java:57) at redis.clients.jedis.JedisSlotBasedConnectionHandler.getConnectionFromSlot(JedisSlotBasedConnectionHandler.java:74) at redis.clients.jedis.JedisClusterCommand.runWithRetries(JedisClusterCommand.java:116) at redis.clients.jedis.JedisClusterCommand.run(JedisClusterCommand.java:31) at redis.clients.jedis.JedisCluster.set(JedisCluster.java:103) at com.tao.rest.jedis.JedisTest.testJedisCluster(JedisTest.java:71) at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57) at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) at java.lang.reflect.Method.invoke(Method.java:606) at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:50) at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12) at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:47) at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17) at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:325) at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:78) at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:57) at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290) at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71) at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288) at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58) at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268) at org.junit.runners.ParentRunner.run(ParentRunner.java:363) at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:86) at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38) at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:539) at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:761) at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:461) at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:207)
执行如下命令开放redis集群每个节点的端口即可正常访问redis集群了:
[root@localhost bin]# /sbin/iptables -I INPUT -p tcp --dport 7006 -j ACCEPT
[root@localhost bin]# /etc/rc.d/init.d/iptables save
参考文档:
https://redis.io/topics/cluster-tutorial redis官网集群指导
https://www.18188.org/articles/2018/10/19/1539930723215.html Redis5.0客户端redis-cli管理cluster尝试