模拟安装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 集群节点:
@Test
public void testJedisCluster() throws IOException {
/*redis.clients.jedis.JedisCluster.JedisCluster(Set<HostAndPort> nodes)*/
Set<HostAndPort> 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官网集群指导
https://www.18188.org/articles/2018/10/19/1539930723215.html Redis5.0客户端redis-cli管理cluster尝试
模拟安装redis5.0集群并通过Java代码访问redis集群的更多相关文章
- CentOS6.10安装redis5.0
1.以安装redis5.0.0为例 下载安装包:http://redis.io 安装非常简单! [root@centos6 ~]#yum install gcc #需要先安装GCC,如果已安装请忽略 ...
- Springboot2.0访问Redis集群
Redis 是一个开源(BSD许可)的,内存中的数据结构存储系统,它可以用作高性能的key-value数据库.缓存和消息中间件,掌握它是程序员的必备技能,下面是一个springboot访问redis的 ...
- Centos7安装redis5.0.7
1. 安装依赖包 yum install -y gcc gcc-c++ 2. 下载最新版redis安装包并解压安装 cd /usr/local/src wget http://download.red ...
- CentOS7源码安装Redis5.0.4非关系型数据库
源码安装redis-5.0.4 一. 下载redis 1. 需要连接网络 二. 案例(另一种安装方法) [root@localhost ~]# wget http://download.redis.i ...
- CentOS7下编译安装redis-5.0.9
CentOS7下编译安装redis-5.0.9 本文地址http://yangjianyong.cn/?p=171转载无需经过作者本人授权 下载redis #code start wget https ...
- 安装Redis5.0.8教程图解
文档:安装Redis5.0.8教程图解.note 链接:http://note.youdao.com/noteshare?id=737620a0441724783c3f8ef14ab8a453& ...
- CentOS7.6下安装Redis5.0.7
此次安装是在CentOS7下安装Redis5.0.7 一.首先准备Redis安装包 这里下载的是 redis-5.0.7.tar.gz 安装包,并将其直接放在了 root ⽬录下 压缩包下载地址:ht ...
- 通过jedis连接redis单机成功,使用redis客户端可以连接集群,但使用JedisCluster连接redis集群一直报Could not get a resource from the pool
一,问题描述: (如题目)通过jedis连接redis单机成功,使用JedisCluster连接redis集群一直报Could not get a resource from the pool 但是使 ...
- 编译安装redis-5.0.4
编译安装为redis官方推荐安装方式. 本例中使用linux版本为:CentOS Linux release 7.0.1406 (Core),Basic Web Server 一.安装依赖包 yum ...
随机推荐
- 测开之路一百二十三:快速搭建python虚拟环境
前提:已装好python3.4+且环境可正常运行 一:手动搭建: 准备好一个工作目录 管理员运行cmd,进入到准备的目录里面 执行命令:python -m venv 虚拟环境名 激活虚拟环境(在ven ...
- IDEA基本设置和快捷键大全
# IDEA基本设置 ## 设置编码格式 1. Configure - Settings - Editor - File Encodings 2. 将三个编码全部设置为UTF-8 ## 启用Ctrl+ ...
- 【ABAP系列】SAP 获取工单和工序的状态
公众号:SAP Technical 本文作者:matinal 原文出处:http://www.cnblogs.com/SAPmatinal/ 原文链接:[ABAP系列]SAP 获取工单和工序的状态 ...
- Tensorflow实战 手写数字识别(Tensorboard可视化)
一.前言 为了更好的理解Neural Network,本文使用Tensorflow实现一个最简单的神经网络,然后使用MNIST数据集进行测试.同时使用Tensorboard对训练过程进行可视化,算是打 ...
- Spring 容器的基本用法
容器的基本用法 bean 是 Spring 中最核心的东西,因为 Spring 就像是个大水桶,而 bean 就像是容器中的水,水桶脱离了水也没什么用处了,来看看 bean 的定义. public c ...
- maven build失败 (Failure to find io.renren:renren-security:pom:3.2.0 in http://maven.aliyun.com/nexus/content/groups/public/ was cached in the local repository...)
今天mvn clean package一个子工程(renren-admin)时报错: Failed to execute goal on project renren-admin: Could n ...
- MySQL-第十二篇管理结果集
1.ResultSet 2.可更新的结果集,使用ResultSet的updateRow()方法.
- html5实现拖拽上传
<html><head> <meta http-equiv="Content-Type" content="text/html; chars ...
- k3 cloud单据体首行过滤功能
#实现单据体首行过滤 clr.AddReference('System') clr.AddReference('Kingdee.BOS.Core') from Kingdee.BOS.Core.Dy ...
- vue防抖节流函数---组件封装,防止按钮多次点击
1.vue 封装utils.js /** * @param {function} func 执行函数 * @param {number} time 防抖节流时间 * @param {boolean} ...