凯发k8天生赢家一触即发

模拟安装redis5.0集群并通过java代码访问redis集群 -凯发k8天生赢家一触即发

2023-08-17,,

在虚拟机上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 集群节点:

 @test
public void testjediscluster() throws ioexception {
/*redis.clients.jedis.jediscluster.jediscluster(set nodes)*/
set nodes = new hashset<>();
string clusterhost = "192.168.93.135";
nodes.add(new hostandport(clusterhost, 7001));
nodes.add(new hostandport(clusterhost, 7002));
nodes.add(new hostandport(clusterhost, 7003));
nodes.add(new hostandport(clusterhost, 7004));
nodes.add(new hostandport(clusterhost, 7005));
nodes.add(new hostandport(clusterhost, 7006));
jediscluster cluster = new jediscluster(nodes);
// 向redis集群中保存数据
cluster.set("name", "lbg");
string name = cluster.get("name");
system.out.println(name);
cluster.close();
}

结果报如下错误:节点不可访问,这是由于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凯发k8天生赢家一触即发官网集群指导

https://www.18188.org/articles/2018/10/19/1539930723215.html  redis5.0客户端redis-cli管理cluster尝试

模拟安装redis5.0集群并通过java代码访问redis集群的相关教程结束。

网站地图