欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

在redhat6.4安装redis集群【教程】

程序员文章站 2022-03-14 17:45:38
  参考:   http://redis.io/topics/cluster-tutorial(主要是creating a redis cluster using the...

  参考:

  http://redis.io/topics/cluster-tutorial(主要是creating a redis cluster using the create-cluster script部分)

  https://ruby.taobao.org/

  安装一款不熟悉的软件前先看install,readme,这是习惯,生产上要建立普通用户并调节适当参数,下面是以root身份安装运行.

  下载解压并安装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/1212

 

  根据实际修改/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 -- run tail -f of instance at base port + id."

  echo "clean -- remove all instances data, logs, configs."123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102

  不要忘了创建数据目录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