./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
redis集群管理工具redis-trib.rb依赖ruby环境,首先需要安装ruby环境:
安装ruby
yum install ruby
gem install redis
这里在同一台服务器用不同的端口表示不同的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
#脚本内容如下:
# 参数
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
复制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 查询集群状态信息
// 连接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);
}