redis集群

来源:互联网 发布:apache 代理转发 编辑:程序博客网 时间:2024/06/04 19:44

以下仅作为个人开发参考,部分内容需要根据实际情况更改,如文件路径等

1、环境配置:

系统环境:centos7

安装gcc:yum install gcc-c++

安装ruby的环境。

yum -y install ruby ruby-devel rubygems rpm-build

wget https://rubygems.org/downloads/redis-3.3.3.gem   (地址可以根据官网复制)

gem install redis-3.3.3.gem  (根据下载的版本更改)

2、redis安装

下载源码

wget http://download.redis.io/releases/redis-3.2.3.tar.gz

解压

tar -zxvf redis-3.2.3.tar.gz

编译

cd /home/wqz/redis-3.2.3

mv redis-3.2.3 redis

make

安装

创建redis-cluster目录
mkdir  /usr/local/redis-cluster

make PREFIX=/usr/local/redis-cluster/redis1 install

拷贝配置文件

拷贝源码目录里面配置文件 redis.conf,然后将其拷贝到安装路径下

cd /usr/local/redis-cluster/redis1

mkdir etc

cp /home/wqz/redis/redis.conf ./etc

编辑redis.conf 打开第721行# cluster-enabled yes 的注释

将redis1复制5个Redis到redis-cluster目录下,此时共6个redis

分别命名为redis1-6

mkdir redis2

cp -rf  redis1/* redis2

分别修改redis.conf配置文件中的第45行端口号为7001-7006

复制redis-trib.rb集群管理工具


 mv  redis* redis-cluster/

cp redis-3.0.0/src/redis-trib.rb /usr/local/redis-cluster


启动(后端启动)


1、切换到管理员权限  su root
2、错误提示
"Sorry, the cluster configuration file nodes.conf is already used by a different Redis Cluster node. Please make sure that different nodes use different cluster configuration files."
需要进入redis1-7目录下执行

/usr/local/redis-cluster/redis1/bin/redis-server /usr/local/redis-cluster/redis1/etc/redis.conf
/usr/local/redis-cluster/redis2/bin/redis-server /usr/local/redis-cluster/redis2/etc/redis.conf
/usr/local/redis-cluster/redis3/bin/redis-server /usr/local/redis-cluster/redis3/etc/redis.conf
/usr/local/redis-cluster/redis4/bin/redis-server /usr/local/redis-cluster/redis4/etc/redis.conf
/usr/local/redis-cluster/redis5/bin/redis-server /usr/local/redis-cluster/redis5/etc/redis.conf
/usr/local/redis-cluster/redis6/bin/redis-server /usr/local/redis-cluster/redis6/etc/redis.conf

创建集群

cd /usr/local/redis-cluster

./redis-trib.rb create --replicas 1  127.0.0.1:7001 127.0.0.1:7002 127.0.0.1:7003 127.0.0.1:7004 127.0.0.1:7005 127.0.0.1:7006


打印:

[OK] All nodes agree about slots configuration.

>>> Check for open slots...

>>> Check slots coverage...

[OK] All 16384 slots covered.


查看Redis运行状态

1
2
3
4
5
6
7
ps -el | grep redis
5 S 0 1999 1 0 80 0 - 34359 ep_pol ? 00:00:00 redis-server
5 S 0 2003 1 0 80 0 - 34359 ep_pol ? 00:00:00 redis-server
5 S 0 2007 1 0 80 0 - 34359 ep_pol ? 00:00:00 redis-server
5 S 0 2011 1 0 80 0 - 34359 ep_pol ? 00:00:00 redis-server
5 S 0 2017 1 0 80 0 - 34359 ep_pol ? 00:00:00 redis-server
5 S 0 2023 1 0 80 0 - 34359 ep_pol ? 00:00:00 redis-server

其他记录:

1、查看进程,通过kill命令杀死redis 服务

[root@localhost redis-cluster]# ps -aux | grep redis
root      22676  0.0  1.0 141016 10032 ?        Sl   15:15   0:02 /usr/local/redis-cluster/redis1/bin/redis-server 127.0.0.1:7001 [cluster]
wqz       22724  0.0  1.0 141016 10180 ?        Sl   15:15   0:02 /usr/local/redis-cluster/redis2/bin/redis-server 127.0.0.1:7002 [cluster]
root      23212  0.0  0.8 136920  8108 ?        Sl   15:30   0:02 ./redis-server 127.0.0.1:7004 [cluster]
root      23506  0.0  0.8 136920  8108 ?        Sl   15:34   0:02 ./redis-server 127.0.0.1:7005 [cluster]
root      23622  0.0  0.8 136920  8100 ?        Sl   15:35   0:02 /usr/local/redis-cluster/redis3/bin/redis-server 127.0.0.1:7003 [cluster]
root      23772  0.0  1.0 138968 10160 ?        Sl   15:39   0:01 /usr/local/redis-cluster/redis6/bin/redis-server 127.0.0.1:7006 [cluster]

root      25111  0.0  0.0 112664   972 pts/6    R+   16:35   0:00 grep --color=auto redis

[root@localhost redis-cluster]# kill -9 22676

2、卸载  删除redis-cluster

3、测试,执行redis1下redis-cli 

[root@localhost redis1]# ./bin/redis-cli -h 127.0.0.1 -p 7001
[root@localhost redis1]# 
127.0.0.1:7002> set hello world
-> Redirected to slot [866] located at 127.0.0.1:7006
OK
127.0.0.1:7006> set name liwei
-> Redirected to slot [5798] located at 127.0.0.1:7004
OK
127.0.0.1:7004> get name
"liwei"
127.0.0.1:7004> get hello
-> Redirected to slot [866] located at 127.0.0.1:7006
"world"

127.0.0.1:7006> exit
[root@localhost redis-cluster]# 


使用脚本,启动或停止各个节点,详细参见http://blog.csdn.net/fengyong7723131/article/details/53196382


1、创建目录结构,如下

node_7001  node_7002  node_7003  node_7004  node_7005  node_7006 

2、将原来redis.config拷贝到各个目录下,并根据各个节点设置一下内容(vi查找命令:/text  查找text,按n健查找下一个,按N健查找前一个

port 7001cluster-enabled yescluster-config-file nodes.confcluster-node-timeout 5000appendonly yes
bind 192.168.20.103 #集群如果不是在本地测试(127.0.0.1),需要设置改ip
# By default protected mode is enabled. You should disable it only if# you are sure you want clients from other hosts to connect to Redis# even if no authentication is configured, nor a specific set of interfaces# are explicitly listed using the "bind" directive.protected-mode no  //允许远程访问  3.2版本后才有

3、将redis1中redis-cli、redis-server放到当前目录。

4、创建re.sh脚本,vim re.sh,粘贴如下内容

  1. #!/bin/bash  
  2. #redis集群快速启动与关闭脚本  
  3. #条件一:当前文件夹下包括包含node开头的各个节点文件夹,节点文件夹下包含redis.conf配置文件,不可以配置成守护线程,不然启动时无法输出info到infoFile  
  4. #条件二:当前文件夹下包括redis-server,redis-cli  
  5. #条件三:请保持node文件夹后的数字与内部配置文件的端口号一致,例如node_7000文件夹的端口号为7000  
  6.   
  7. #当前位置  
  8. cluster_dir=`ls`  
  9. #node节点的个数  
  10. node_size=0  
  11. #node节点数组  
  12. nodes=()  
  13. #信息文件  
  14. infoFile="infoFile"  
  15. #pid文件  
  16. pidFile="pidFile"  
  17. #创建infoFile和pidFile文件  
  18. touch infoFile  
  19. touch pidFile  
  20.   
  21. #启动以node开头的文件夹下的redis节点,条件一:node开头,条件二:必须是文件夹  
  22. function starCluster(){  
  23. echo "" > $infoFile  
  24. echo "========================端口号:======================="  
  25. for node in $cluster_dir  
  26.  do  
  27.   if [ ${node:0:4} = "node" ]  
  28.    then  
  29.     if [ -d ${node} ]  
  30.      then  
  31.       node_size=`expr ${node_size} + 1`  
  32.       echo -e "${node:5:8},\c"  
  33.       ./redis-server ${node}/redis.conf >> $infoFile&   
  34.     fi  
  35.   fi  
  36. done  
  37. echo ""  
  38. echo "======================================================"  
  39. echo "===================有${node_size}个redis节点====================="  
  40. echo "======================================================"  
  41. }  
  42.   
  43. #关闭所有节点  
  44. function stopCluster(){  
  45. cat $infoFile | grep "PID" |  awk '{ infoSize=length($0);infoIndex=index($0,"PID");print substr($0,infoIndex+4,infoSize) }' > $pidFile  
  46. echo "===================redis集群的pid:===================="  
  47. for node in `cat $pidFile`  
  48.  do  
  49.   echo -e "${node}, \c}"   
  50.   kill -9 ${node}&  
  51. done    
  52. echo ""  
  53. echo "======================================================"  
  54. }  
  55.   
  56. case $1 in  
  57. start) starCluster  
  58. ;;  
  59. stop) stopCluster  
  60. ;;  
  61. esac  

5、最后目录情况如下:

[root@localhost redis-cluster]# ls
node_7001  node_7002  node_7003  node_7004  node_7005  node_7006  redis-cli  redis-server  redis-trib.rb  re.sh

6、启动:./re.sh start  

[root@localhost redis-cluster]# ./redis-trib.rb create --replicas 1  127.0.0.1:7001 127.0.0.1:7002 127.0.0.1:7003 127.0.0.1:7004 127.0.0.1:7005 127.0.0.1:7006

7、停止:./re.sh stop


更改ip报错:

[ERR] Node 192.168.19.33:7001 is not empty. Either the node already knows other nodes (check with CLUSTER NODES) or contains some key in database 0.

解决办法:

删除旧文件:

appendonly.aof   nodes-7001.conf  nodes-7006.conf  dump.rdb         nodes-7002.conf  

flushdb           nodes-7003.conf   infoFile       node_7005  nodes-7004.conf        node_7006  nodes-7005.conf  

重新启动,重新创建

./re.sh start

./redis-trib.rb create --replicas 1 192.168.19.33:7001 192.168.19.33:7002 192.168.19.33:7003 192.168.19.33:7004 192.168.19.33:7005 192.168.19.33:7006

注意开启防火墙:(systemctl stop firewalld.service 开发可以先关闭掉)

firewall-cmd --zone=public --add-port=7001/tcp


---


firewall-cmd --zone=public --add-port=7006/tcp



客户端测试代码:

package com.store.test;

import java.io.IOException;
import java.util.HashSet;

import redis.clients.jedis.HostAndPort;
import redis.clients.jedis.JedisCluster;


public class RedisClusterTest {

public static void main(String [] args) throws IOException {

HashSet<HostAndPort> nodes = new HashSet<HostAndPort>();
nodes.add(new HostAndPort("192.168.19.33", 7001));
nodes.add(new HostAndPort("192.168.19.33", 7002));
nodes.add(new HostAndPort("192.168.19.33", 7003));
nodes.add(new HostAndPort("192.168.19.33", 7004));
nodes.add(new HostAndPort("192.168.19.33", 7005));
nodes.add(new HostAndPort("192.168.19.33", 7006));

JedisCluster cluster = new JedisCluster(nodes);
cluster.set("hello", "hello_world");
System.out.println(cluster.get("hello"));
cluster.close();

}
}


参考文章:

1、https://rubygems.org/gems/redis

2、http://binux.cn/2017/03/03/Redis-Cluster-Install/

3、http://blog.csdn.net/zgf19930504/article/details/51850594

4、http://www.redis.cn/topics/cluster-tutorial.html

5、https://stackoverflow.com/questions/35342977/redis-nodes-conf-file-locked

6、http://blog.csdn.net/fengyong7723131/article/details/53196382

7、http://blog.csdn.net/wudalang_gd/article/details/52153571





原创粉丝点击