linux环境下redis-cluster集群部署 / jedis连接redis集群超时的问题 / 主从不同步的问题

环境:

 redis版本:5.0.7 

阿里云服务器:centos7.5

jedis 2.9.0

借鉴博客:https://blog.csdn.net/baidu_38558076/article/details/90707045

但是他这个有问题,就是修改启动脚本ip的问题!

看图:

修改完你ip之后,执行 ./create-cluster start 命令,你会看到你的redis实例.

这里ip就是 你的linux系统本身的 ip .

之后执行 ./create-cluster create 命令.在执行这个命令之前,你需要在阿里云服务器上开辟你的端口:

我的示例 redis集群端口:30001-30020  redis集群通信端口是在基础端口上加10000,所以就是 40001-40020.

集群创建成功


使用jedis连接redis集群代码示例:

在new JedisCluster();时候 jedis会获取 redis集群的节点信息(节点 对应的 slots虚拟槽信息) 保存在缓存中.

在脚本执行 redis-cli -a test1234 --cluster create ip:port --cluster-replicas 1 --no-auth-warning 的时候,

如果这里不使用公网ip,那么在jedis获取节点信息的时候,是获取不到你的公网ip,获取的是你 定义的那个ip,所以这样你是连接超时的.

附图成功插入数据:


补充: 按照以上配置,主从节点监听会有问题,需要更改配置


需要加上 `masterauth 主节点密码`  要不然不会做主从替换,也不会做数据同步    重点

#!/bin/bash

# Settings
PORT=30000
TIMEOUT=2000
NODES=6
REPLICAS=1

# You may want to put the above config parameters into config.sh in order to
# override the defaults without modifying this script.

if [ -a config.sh ]
then
    source "config.sh"
fi

# Computed vars
ENDPORT=$((PORT+NODES))

if [ "$1" == "start" ]
then
    while [ $((PORT < ENDPORT)) != "0" ]; do
        PORT=$((PORT+1))
        echo "Starting $PORT"
        ../../bin/redis-server --port $PORT --bind 0.0.0.0  --requirepass test1234 --masterauth test1234  --protected-mode no  --cluster-enabled yes --cluster-config-file nodes-${PORT}.conf --cluster-node-timeout $TIMEOUT --appendonly yes --appendfilename appendonly-${PORT}.aof --dbfilename dump-${PORT}.rdb --logfile ${PORT}.log --daemonize yes --pidfile redis-${PORT}.pid
    done
    exit 0
fi

if [ "$1" == "create" ]
then
    HOSTS=""
    while [ $((PORT < ENDPORT)) != "0" ]; do
        PORT=$((PORT+1))
        HOSTS="$HOSTS 公网ip:$PORT"
    done
    ../../bin/redis-cli -a test1234  --cluster create $HOSTS --cluster-replicas $REPLICAS 
    exit 0
fi

if [ "$1" == "stop" ]
then
    while [ $((PORT < ENDPORT)) != "0" ]; do
        PORT=$((PORT+1))
        echo "Stopping $PORT"
        ../../bin/redis-cli -h 公网ip -p $PORT -a test1234  shutdown nosave
    done
    exit 0
fi

if [ "$1" == "watch" ]
then
    PORT=$((PORT+1))
    while [ 1 ]; do
        clear
        date
        ../../bin/redis-cli -p $PORT cluster nodes | head -30
        sleep 1
    done
    exit 0
fi

if [ "$1" == "tail" ]
then
    INSTANCE=$2
    PORT=$((PORT+INSTANCE))
    tail -f ${PORT}.log
    exit 0
fi

if [ "$1" == "call" ]
then
    while [ $((PORT < ENDPORT)) != "0" ]; do
        PORT=$((PORT+1))
        ../../bin/redis-cli -p $PORT $2 $3 $4 $5 $6 $7 $8 $9
    done
    exit 0
fi

if [ "$1" == "clean" ]
then
    rm -rf *.log
    rm -rf appendonly*.aof
    rm -rf dump*.rdb
    rm -rf nodes*.conf
    exit 0
fi

if [ "$1" == "clean-logs" ]
then
    rm -rf *.log
    exit 0
fi

echo "Usage: $0 [start|create|stop|watch|tail|clean]"
echo "start       -- Launch Redis Cluster instances."
echo "create      -- Create a cluster using redis-cli --cluster create."
echo "stop        -- Stop Redis Cluster instances."
echo "watch       -- Show CLUSTER NODES output (first 30 lines) of first node."
echo "tail <id>   -- Run tail -f of instance at base port + ID."
echo "clean       -- Remove all instances data, logs, configs."
echo "clean-logs  -- Remove just instances logs."

 

发布了4 篇原创文章 · 获赞 4 · 访问量 165

猜你喜欢

转载自blog.csdn.net/qq_42378705/article/details/104538786