./redis-trib.rb create --replicas 1 172.20.10.8:7001 172.20.10.8:7002 172.20.10.8:7003 172.20.10.8:7004 172.20.10.8:7005 172.20.10.8:7006
1.安裝ruby環境
redis集群管理工具redis-trib.rb依賴ruby環境,首先需要安裝ruby環境:
安裝ruby
yum install ruby
gem install redis
2.創建集群
這里在同一臺服務器用不同的端口表示不同的redis服務器,如下:
主節點:172.20.10.8:7001 172.20.10.8:7002 172.20.10.8:7003
從節點:172.20.10.8:7004 172.20.10.8:7005 172.20.10.8:7006
在/下創建redis-cluster目錄,其下創建7001、7002...7006目錄,如下:
創建文件夾命令
mkdir -p /redis-cluster/700{1..6}/{logs,data} && cd /redis-cluster
生成配置文件命令(一起復制,不要單行復制)
IP=`ip a|grep 'inet' |grep -v '127.0.0.1'|grep -v 'inet6'|awk '{print $2}'|awk -F'/' '{print $1}'`
for i in {1..6}
do
cat > 700${i}/redis.conf <<EOF
daemonize yes
port 700${i}
cluster-enabled yes
cluster-config-file cluster-nodes-700${i}.conf
cluster-node-timeout 15000
Appendonly yes
bind ${IP}
protected-mode no
dbfilename dump-700${i}.rdb
logfile /redis-cluster/700${i}/logs/redis.log
pidfile /redis-cluster/700${i}/data/redis.pid
dir /redis-cluster/700${i}/data
appendfilename "appendonly-700${i}.aof"
EOF
done
3./redis-cluster文件夾下編寫cluster.sh腳本
#腳本內容如下:
# 參數
IP=`ip a|grep 'inet' |grep -v '127.0.0.1'|grep -v 'inet6'|awk '{print $2}'|awk -F'/' '{print $1}'`
port=$2
if [ ! $# -eq 2 ];then
echo "Warning! 請輸入兩個參數!"
exit 1
fi
if [ -z ${port} ];then
echo "Warning! 請輸入端口號"
exit 1
fi
# start函數
start(){
IS_PORT_EXISTS=$( ps -ef | grep redis| grep ${port} | grep -v grep | wc -l )
if [ $IS_PORT_EXISTS -ge 1 ];then
echo "Warning! 端口號已存在!"
exit 1
else
/redis-cluster/redis-server /redis-cluster/${port}/redis.conf
fi
}
# stop函數
stop(){
IS_PORT_EXISTS=$(ps -ef |grep redis |grep ${port} |grep -v grep |wc -l)
if [ $IS_PORT_EXISTS -lt 1 ];then
echo "Warning! 端口不存在!"
exit 1
else
/redis-cluster/redis-cli -h ${IP} -p ${port} shutdown
fi
}
# status函數
status(){
/redis-cluster/redis-trib.rb check ${IP}:${port}
}
case $1 in
start)
start
;;
stop)
stop
;;
status)
status
;;
?|help)
echo $"Usage: 'redis' {start|status|stop|help|?}"
;;
*)
echo $"Usage: 'redis' {start|status|stop|help|?}"
esac
4.啟動所有節點redis
復制redis的src目錄下的客戶端,服務端和集群腳本至/redis-cluster文件夾
cp /usr/local /redis-4.0.14/src/redis-cli /redis-cluster/redis-cli
cp /usr/local/redis-4.0.14/src/redis-server /redis-cluster/redis-server
cp /usr/local/redis-4.0.14/src/redis-trib.rb /redis-cluster/redis-trib.rb
至此/redis-cluster文件夾下的目錄結構如圖:
為cluster.sh腳本賦值
chmod 777 cluster.sh
運行腳本啟動6個redis進程
./cluster.sh start 7001
查看啟動的redis進程: ps -ef | grep redis
4.客戶端集群方式訪問redis
./redis-cli -c -h 172.20.10.8 -p 7001 ,其中-c表示以集群方式連接redis,-h指定ip地址,-p指定端口號
cluster nodes 查詢集群結點信息
cluster info 查詢集群狀態信息
5. jedis 連接redis集群
// 連接redis集群
@Test
publicvoid testJedisCluster() {
JedisPoolConfig config = new JedisPoolConfig();
// 最大連接數
config.setMaxTotal(30);
// 最大連接空閑數
config.setMaxIdle(2);
//集群結點
Set<HostAndPort> jedisClusterNode = new HashSet<HostAndPort>();
jedisClusterNode.add(new HostAndPort("172.20.0.8", 7001));
jedisClusterNode.add(new HostAndPort("172.20.0.8", 7002));
jedisClusterNode.add(new HostAndPort("172.20.0.8", 7003));
jedisClusterNode.add(new HostAndPort("172.20.0.8", 7004));
jedisClusterNode.add(new HostAndPort("172.20.0.8", 7005));
jedisClusterNode.add(new HostAndPort("172.20.0.8", 7006));
JedisCluster jc = new JedisCluster(jedisClusterNode, config);
JedisCluster jcd = new JedisCluster(jedisClusterNode);
jcd.set("test", "111");
String value = jcd.get("test");
System.out.println(value);
}