歡迎來到Linux教程網
Linux教程網
Linux教程網
Linux教程網
Linux教程網 >> Linux基礎 >> Linux教程 >> 在RedHat 6.4安裝Redis集群

在RedHat 6.4安裝Redis集群

日期:2017/2/28 13:49:02   编辑:Linux教程

在RedHat 6.4安裝Redis集群

下載解壓並安裝redis

make test提示需要更高版本的tcl,跳到安裝過程可能遇到的問題

wget http://download.redis.io/releases/redis-3.0.7.tar.gz
tar xf redis-3.0.7.tar.gz 
cd redis-3.0.7
mkdir -p /opt/redis
make test
make PREFIX=/opt/redis install

復制兩個腳本到安裝的目錄

cp ~/redis-3.0.7/src/redis-trib.rb /opt/redis/
cp ~/redis-3.0.7/utils/create-cluster/create-cluster /opt/redis/

根據實際修改/opt/redis/create-cluster.改動的地方有幾處
a.增加了三個變量BASEDIR,BINDIR和DATADIR,
b.修改相關命令路徑,
c.start前,先進入DATADIR,start後,返回原目錄
d.clean前,先進入DATADIR,start後,返回原目錄
e.create的host由127.0.0.1改為192.168.1.194(不改有時會報Too many Cluster redirections)

下面是修改後的shell

#!/bin/bash

# Settings
PORT=30000
TIMEOUT=2000
NODES=6
REPLICAS=1
BASEDIR=/opt/redis
BINDIR=$BASEDIR/bin
DATADIR=$BASEDIR/data

# 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
    cd $DATADIR
    while [ $((PORT < ENDPORT)) != "0" ]; do
        PORT=$((PORT+1))
        echo "Starting $PORT"
        $BINDIR/redis-server --port $PORT --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
    done
    cd -
    exit 0
fi

if [ "$1" == "create" ]
then
    HOSTS=""
    while [ $((PORT < ENDPORT)) != "0" ]; do
        PORT=$((PORT+1))
        HOSTS="$HOSTS 192.168.1.194:$PORT"
    done
    $BASEDIR/redis-trib.rb create --replicas $REPLICAS $HOSTS
    exit 0
fi

if [ "$1" == "stop" ]
then
    while [ $((PORT < ENDPORT)) != "0" ]; do
        PORT=$((PORT+1))
        echo "Stopping $PORT"
        $BINDIR/redis-cli -p $PORT shutdown nosave
    done
    exit 0
fi

if [ "$1" == "watch" ]
then
    PORT=$((PORT+1))
    while [ 1 ]; do
        clear
        date
        $BINDIR/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))
        $BINDIR/redis-cli -p $PORT $2 $3 $4 $5 $6 $7 $8 $9
    done
    exit 0
fi

if [ "$1" == "clean" ]
then
    cd $DATADIR
    rm -rf *.log
    rm -rf appendonly*.aof
    rm -rf dump*.rdb
    rm -rf nodes*.conf
    cd -
    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-trib 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."

不要忘了創建數據目錄mkdir -p /opt/redis/data

根據上面的參考,啟動集群和停止集群

啟動集群:先敲入/opt/redis/create-cluster start回車,再敲入/opt/redis/create-cluster create回車,再輸入yes回車
停止集群:敲入/opt/redis/create-cluster stop回車
如果以前啟動過,造成不一致數據,create時就會報錯,可先/opt/redis/create-cluster clean

測試

主要依賴

<dependency>
    <groupId>redis.clients</groupId>
    <artifactId>jedis</artifactId>
    <version>2.8.1</version>
</dependency>

聲明JedisCluster Bean

 @Bean
    public JedisCluster jedisCluster(){
        Set<HostAndPort> nodes=new HashSet<>(3);
        nodes.add(new HostAndPort("192.168.1.194",30001));
        nodes.add(new HostAndPort("192.168.1.194",30002));
        nodes.add(new HostAndPort("192.168.1.194",30003));
        return new JedisCluster(nodes,2000,5);
    }

測試set和get

        AnnotationConfigApplicationContext context= new AnnotationConfigApplicationContext(AppConfig.class);
        JedisCluster jedisCluster = (JedisCluster) context.getBean("jedisCluster");
        jedisCluster.set("xxx","123");
        System.out.println("jedisCluster.get = " + jedisCluster.get("xxx"));

安裝過程可能遇到的問題:

make test時,提醒You need tcl 8.5 or newer in order to run the Redis test.到http://www.tcl.tk/software/tcltk/download.html下載Tcl,

wget http://prdownloads.sourceforge.net/tcl/tcl8.5.19-src.tar.gz
tar xf tcl8.5.19-src.tar.gz
cd  tcl8.5.19/unix
./configure
make
make test
make install

因為create-cluster create會調用redis-trib.rb,它是一個ruby腳本,所以提示沒有安裝ruby,就先安裝yum install -y ruby
如果提示加載rubygems錯誤,使用以下辦法安裝rubygems
a.https://rubygems.org/pages/download下載tgz格式的安裝包(wget可能不通,在windows用旋風或迅雷下載)
b.mount -t cifs -o username=xiejx618,password=123456 //192.168.1.115/share /share

cp /share/rubygems-2.6.4.tgz ./
tar xf rubygems-2.6.4.tgz
cd rubygems-2.6.4
ruby setup.rb

如果再提示no such file to load – rdoc/rdoc,就先安裝yum install -y rdoc
如果再提示 no such file to load – redis,就使用gem install redis -v 3.0.7
gem又是因為牆原因無法使用默認源,就修改為淘寶源
可能用到的幾個命令
幫助:gem sources --help
查看源:gem sources -l
刪除源:gem sources -r https://rubygems.org/
添加源:gem sources -a https://ruby.taobao.org/
更新源緩存:gem sources -u

參考:
http://redis.io/topics/cluster-tutorial(主要是Creating a Redis Cluster using the create-cluster script部分)
https://ruby.taobao.org/

下面關於Redis的文章您也可能喜歡,不妨參考下:

Ubuntu 14.04下Redis安裝及簡單測試 http://www.linuxidc.com/Linux/2014-05/101544.htm

Redis主從復制基本配置 http://www.linuxidc.com/Linux/2015-03/115610.htm

Redis集群明細文檔 http://www.linuxidc.com/Linux/2013-09/90118.htm

Ubuntu 12.10下安裝Redis(圖文詳解)+ Jedis連接Redis http://www.linuxidc.com/Linux/2013-06/85816.htm

Redis系列-安裝部署維護篇 http://www.linuxidc.com/Linux/2012-12/75627.htm

CentOS 6.3安裝Redis http://www.linuxidc.com/Linux/2012-12/75314.htm

Redis安裝部署學習筆記 http://www.linuxidc.com/Linux/2014-07/104306.htm

Redis配置文件redis.conf 詳解 http://www.linuxidc.com/Linux/2013-11/92524.htm

Redis 的詳細介紹:請點這裡
Redis 的下載地址:請點這裡

Copyright © Linux教程網 All Rights Reserved