redis集群搭建完整过程
1.安装一个虚拟机centOs7,安装编译环境yum -y gcc-c++(redis环境支持)
2.安装下载redis工具 yum -y intall wget
3.下载redis:wget http://download.redis.io/releases/redis-5.0.0.tar.gz(此文安装的是5.0.0)
4.解压 tar -zxvf http://download.redis.io/releases/redis-4.0.14.tar.gz(解压位置可根据自己调整,本人路径/usr/loca/redis)
5.cd redis-5.0.0 编译 make(编译成功后redis-5.0.0/src/目录下会有redis-server redis-cli 这种文件)
6.我在redis目录下创建redis-cluster-conf文件夹存放对应的redis.conf文件
7.更改每一个文件下的redis.conf文件,具体配置如下:
ps:700*表示对应的端口
**port 7001 #端口
cluster-enabled yes #启用集群模式
cluster-config-file nodes-7001.conf
cluster-node-timeout 5000 #超时时间
appendonly yes
daemonize yes #后台运行
protected-mode no #非保护模式
pidfile /var/run/redis_7001.pid**
8.开启redis服务节点
/usr/local/redis/redis-5.0.0/src/redis-server /root/software/redis/redis-cluster-conf/7001/redis.conf
/usr/local/redis/redis-5.0.0/src/redis-server /root/software/redis/redis-cluster-conf/7002/redis.conf
/usr/local/redis/redis-5.0.0/src/redis-server /root/software/redis/redis-cluster-conf/7003/redis.conf
/usr/local/redis/redis-5.0.0/src/redis-server /root/software/redis/redis-cluster-conf/7004/redis.conf
/usr/local/redis/redis-5.0.0/src/redis-server /root/software/redis/redis-cluster-conf/7005/redis.conf
/usr/local/redis/redis-5.0.0/src/redis-server /root/software/redis/redis-cluster-conf/7006/redis.conf
ps:redis5.0之后支持统一开启关闭命令:
redis-5.0.0/utils/create-cluster/create-cluster stop
redis-5.0.0/utils/create-cluster/create-cluster start
9.启动redis集群(3主3从)
/usr/local/redis/redis-5.0.0/src/redis-cli --cluster create 192.168.43.136:7001 192.168.43.136:7002 192.168.43.136:7003 192.168.43.136:7004 192.168.43.136:7005 192.168.43.136:7006 --cluster-replicas 1
10.使用jedis连接集群
Set<HostAndPort> jedisClusterNode=new HashSet<HostAndPort> ();
jedisClusterNode.add(new HostAndPort("192.168.43.136",7001));
jedisClusterNode.add(new HostAndPort("192.168.43.136",7002));
jedisClusterNode.add(new HostAndPort("192.168.43.136",7003));
jedisClusterNode.add(new HostAndPort("192.168.43.136",7004));
jedisClusterNode.add(new HostAndPort("192.168.43.136",7005));
jedisClusterNode.add(new HostAndPort("192.168.43.136",7006));
JedisPoolConfig config=new JedisPoolConfig();
config.setMaxTotal(100);
config.setMaxIdle(10);
config.setTestOnBorrow(true);
JedisCluster jedisCluster=new JedisCluster(jedisClusterNode, 6000,10,
config);
for(int i =1;i <=100; i++){
System.out.println(jedisCluster.set("num"+i,i+""));
}
for(int i =1;i <=100; i++){
System.out.println(jedisCluster.get("num"+i));
}
jedisCluster.close();
}
11.验证结果
至此,redis集群的搭建就已经完毕,不过,这其中的踩的坑还真的是多,各种奇奇怪怪的问题 还有些未知的问题自己也没去管了,先记录一下,看之后还会不会遇到吧,如果有道友遇到过,也希望能给予解答,不胜感激
1. 使用代码jedis连接集群的时候报出No reachable node in cluster异常,这个问题找了一下午没找到,有问题说是nodes.conf文件的问题,删除重启集群就能解决,但我结合了自己的原因,最终在防火墙上面找到答案了,把防火墙关闭了就行了
systemctl stop firewalld.service #停止firewall
firewall-cmd --reload #重启防火墙
firewall-cmd --state #查看防火墙状态,是否是running