首页

基于Redis5.0通过简单配置实现redis集群模式整理详细操作步骤并作代码示例测试验证

标签:redis集群,redis5验证,非ruby,redis高可用,JedisCluster     发布时间:2019-04-21   

一、前言

通过redis5.0版本可以通过简单配置实现redis集群模式(如下架构图所示),不需要低版本走ruby等复杂配置,之前centos没有安装gcc/gcc+导致无法正常安装redis (重新安装centos操作系统时自定义库时选择桌面平台开发/开发工具并选择安装gcc所有依赖库)。

基于Redis5.0通过简单配置实现redis集群模式整理详细操作步骤并作代码示例测试验证

二、操作步骤

1. 下载安装redis5.0及以上,详细安装步骤日志可以参加其他文章页面教程

wget http://download.redis.io/releases/redis-5.0.0.tar.gz@b@tar -zxvf  redis-5.0.0.tar.gzcd redis-5.0.0@b@make@b@cd src && make install

2. 修改安装解压后的目录,并模拟复制6台集群文件配置

[root@centos-cluster-s19423 app]#  mv  redis-5.0.0  redis5 @b@[root@centos-cluster-s19423 redis5]# mkdir   redis-cluster-conf@b@[root@centos-cluster-s19423 redis5]# cp  redis.conf   redis-cluster-conf/7001/@b@[root@centos-cluster-s19423 redis5]# cp  redis.conf   redis-cluster-conf/7002/@b@[root@centos-cluster-s19423 redis5]# cp  redis.conf   redis-cluster-conf/7003/@b@[root@centos-cluster-s19423 redis5]# cp  redis.conf   redis-cluster-conf/7004/@b@[root@centos-cluster-s19423 redis5]# cp  redis.conf   redis-cluster-conf/7005/@b@[root@centos-cluster-s19423 redis5]# cp  redis.conf   redis-cluster-conf/7006/

分配修改redis.conf配置文件

#分别修改端口为7001..7006@b@port 7001  @b@@b@@b@#修改绑定ip地址为本机实际网路ip地址@b@#bind 127.0.0.1@b@bind  192.168.1.204@b@@b@#启用集群模式@b@cluster-enabled yes  @b@# nodes-7001.conf...nodes-7006.conf@b@cluster-config-file nodes-7001.conf@b@cluster-node-timeout 5000 @b@@b@#超时时间appendonly yes@b@daemonize yes @b@@b@#后台运行@b@protected-mode no @b@@b@#非保护模式pidfile,分别修改为redis_7001.pid...7006.pid  @b@/var/run/redis_7001.pid

3. 修改防火墙配置,并重启

[root@centos-cluster-s19423 redis5]# vi /etc/sysconfig/iptables@b@@b@-A INPUT -m state --state NEW -m tcp -p tcp --dport 7001 -j ACCEPT@b@-A INPUT -m state --state NEW -m tcp -p tcp --dport 7002 -j ACCEPT@b@-A INPUT -m state --state NEW -m tcp -p tcp --dport 7003 -j ACCEPT@b@-A INPUT -m state --state NEW -m tcp -p tcp --dport 7004 -j ACCEPT@b@-A INPUT -m state --state NEW -m tcp -p tcp --dport 7005 -j ACCEPT@b@-A INPUT -m state --state NEW -m tcp -p tcp --dport 7006 -j ACCEPT@b@-A INPUT -j REJECT --reject-with icmp-host-prohibited@b@-A FORWARD -j REJECT --reject-with icmp-host-prohibited@b@COMMIT@b@~@b@~@b@~@b@~@b@~@b@~@b@~@b@~@b@~@b@"/etc/sysconfig/iptables" 20L, 879C written@b@[root@centos-cluster-s19423 redis5]# service  iptables  stop@b@iptables:将链设置为政策 ACCEPT:filter [确定]@b@iptables:清除防火墙规则:[确定]@b@iptables:正在卸载模块:[确定]@b@[root@centos-cluster-s19423 redis5]# service  iptables  start@b@iptables:应用防火墙规则:[确定]

4. 启动节点和集群服务

#启动redis服务节点@b@[root@centos-cluster-s19423 redis5]# /home/app/redis5/src/redis-server   /home/app/redis5/redis-cluster-conf/7001/redis.conf @b@[root@centos-cluster-s19423 redis5]# /home/app/redis5/src/redis-server   /home/app/redis5/redis-cluster-conf/7002/redis.conf @b@[root@centos-cluster-s19423 redis5]# /home/app/redis5/src/redis-server   /home/app/redis5/redis-cluster-conf/7003/redis.conf @b@[root@centos-cluster-s19423 redis5]# /home/app/redis5/src/redis-server   /home/app/redis5/redis-cluster-conf/7004/redis.conf @b@[root@centos-cluster-s19423 redis5]# /home/app/redis5/src/redis-server   /home/app/redis5/redis-cluster-conf/7005/redis.conf @b@[root@centos-cluster-s19423 redis5]# /home/app/redis5/src/redis-server   /home/app/redis5/redis-cluster-conf/7006/redis.conf
[root@centos-cluster-s19423 redis-cluster-conf]#  /home/app/redis5/src/redis-cli   --cluster   create   192.168.1.204:7001   192.168.1.204:7002    192.168.1.204:7003   192.168.1.204:7004   192.168.1.204:7005   192.168.1.204:7006  --cluster-replicas 1@b@>>> Performing hash slots allocation on 6 nodes...@b@Master[0] -> Slots 0 - 5460@b@Master[1] -> Slots 5461 - 10922@b@Master[2] -> Slots 10923 - 16383@b@Adding replica 192.168.1.204:7004 to 192.168.1.204:7001@b@Adding replica 192.168.1.204:7005 to 192.168.1.204:7002@b@Adding replica 192.168.1.204:7006 to 192.168.1.204:7003@b@>>> Trying to optimize slaves allocation for anti-affinity@b@[WARNING] Some slaves are in the same host as their master@b@M: 075a4724233a382182d369ddd9c5cfff1936f8cb 192.168.1.204:7001@b@   slots:[0-5460] (5461 slots) master@b@M: 70fadf578fe98d5a2a1194e2eecd9fc96e238bd2 192.168.1.204:7002@b@   slots:[5461-10922] (5462 slots) master@b@M: 739076a173ecd494e03e8dc0a04b6fc9a3691b86 192.168.1.204:7003@b@   slots:[10923-16383] (5461 slots) master@b@S: e4e4cebd9f830c3b524cb1e6a8dea8850245c9c1 192.168.1.204:7004@b@   replicates 739076a173ecd494e03e8dc0a04b6fc9a3691b86@b@S: 7b1844c6f5f187a95b7b3e3950b62461f36b091e 192.168.1.204:7005@b@   replicates 075a4724233a382182d369ddd9c5cfff1936f8cb@b@S: 127b7434c2f2707bce4185866fac4bd896f28a6b 192.168.1.204:7006@b@   replicates 70fadf578fe98d5a2a1194e2eecd9fc96e238bd2@b@Can I set the above configuration? (type 'yes' to accept): yes@b@>>> Nodes configuration updated@b@>>> Assign a different config epoch to each node@b@>>> Sending CLUSTER MEET messages to join the cluster@b@Waiting for the cluster to join@b@...@b@>>> Performing Cluster Check (using node 192.168.1.204:7001)@b@M: 075a4724233a382182d369ddd9c5cfff1936f8cb 192.168.1.204:7001@b@   slots:[0-5460] (5461 slots) master@b@   1 additional replica(s)@b@S: e4e4cebd9f830c3b524cb1e6a8dea8850245c9c1 192.168.1.204:7004@b@   slots: (0 slots) slave@b@   replicates 739076a173ecd494e03e8dc0a04b6fc9a3691b86@b@S: 127b7434c2f2707bce4185866fac4bd896f28a6b 192.168.1.204:7006@b@   slots: (0 slots) slave@b@   replicates 70fadf578fe98d5a2a1194e2eecd9fc96e238bd2@b@S: 7b1844c6f5f187a95b7b3e3950b62461f36b091e 192.168.1.204:7005@b@   slots: (0 slots) slave@b@   replicates 075a4724233a382182d369ddd9c5cfff1936f8cb@b@M: 70fadf578fe98d5a2a1194e2eecd9fc96e238bd2 192.168.1.204:7002@b@   slots:[5461-10922] (5462 slots) master@b@   1 additional replica(s)@b@M: 739076a173ecd494e03e8dc0a04b6fc9a3691b86 192.168.1.204:7003@b@   slots:[10923-16383] (5461 slots) master@b@   1 additional replica(s)@b@[OK] All nodes agree about slots configuration.@b@>>> Check for open slots...@b@>>> Check slots coverage...@b@[OK] All 16384 slots covered.

三、代码测试验证

下面代码 依赖jedis-3.0.1.jar(低版本会报java.lang.NumberFormatException: For input string: "7003@17003"异常),完整的项目点击"关于redis四种不同模式(单例/主从/哨兵/集群)java示例代码项目下载(含完整依赖包)"进行下载

package com.xwood.redis.cluster;@b@@b@import java.util.HashSet;@b@import java.util.Set;@b@@b@import redis.clients.jedis.HostAndPort;@b@import redis.clients.jedis.JedisCluster;@b@@b@public class JedisClusterTest {@b@@b@ @b@	public static void main(String[] args) {@b@		@b@		Set<HostAndPort> nodes = new HashSet<HostAndPort>();@b@		@b@		nodes.add(new HostAndPort("192.168.1.204", 7001));@b@		nodes.add(new HostAndPort("192.168.1.204", 7002));@b@		nodes.add(new HostAndPort("192.168.1.204", 7003));@b@		nodes.add(new HostAndPort("192.168.1.204", 7004));@b@		nodes.add(new HostAndPort("192.168.1.204", 7005));@b@		nodes.add(new HostAndPort("192.168.1.204", 7006));@b@		@b@		JedisCluster cluster = new JedisCluster(nodes);@b@		@b@		cluster.set("key424", "123456");@b@		@b@		String result = cluster.get("key424");@b@		@b@		System.out.println("cluster=========>"+result);@b@		cluster.close();@b@@b@	}@b@@b@}

控制台结果

cluster=========>123456
@b@