服务器之家:专注于服务器技术及软件下载分享
分类导航

Mysql|Sql Server|Oracle|Redis|MongoDB|PostgreSQL|Sqlite|DB2|mariadb|Access|数据库技术|

服务器之家 - 数据库 - Redis - 在redhat6.4安装redis集群【教程】

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

2019-10-29 19:34wulei Redis

这篇文章主要介绍了在redhat6.4安装redis集群【教程】,需要的朋友可以参考下

  参考:

  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,跳到安装过程可能遇到的问题

  

?
1
2
3
4
5
6
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

  复制两个脚本到安装的目录

 

?
1
2
3
4
5
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

 

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
#!/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

  测试

 

?
1
2
3
4
5
<dependency>
 <groupId>redis.clients</groupId>
 <artifactId>jedis</artifactId>
 <version>2.8.1</version>
</dependency>

  声明JedisCluster Bean

 

?
1
2
3
4
5
6
7
8
@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

  

?
1
2
3
4
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,

 

?
1
2
3
4
5
6
7
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

  

?
1
2
3
4
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

延伸 · 阅读

精彩推荐
  • RedisRedis存取序列化与反序列化性能问题详解

    Redis存取序列化与反序列化性能问题详解

    这篇文章主要给大家介绍了关于Redis存取序列化与反序列化性能问题的相关资料,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参...

    这名字已经存在9742021-02-24
  • RedisRedis分布式锁升级版RedLock及SpringBoot实现方法

    Redis分布式锁升级版RedLock及SpringBoot实现方法

    这篇文章主要介绍了Redis分布式锁升级版RedLock及SpringBoot实现,本文给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以...

    等不到的口琴7802021-07-25
  • Redis聊一聊Redis与MySQL双写一致性如何保证

    聊一聊Redis与MySQL双写一致性如何保证

    一致性就是数据保持一致,在分布式系统中,可以理解为多个节点中数据的值是一致的。本文给大家分享Redis与MySQL双写一致性该如何保证,感兴趣的朋友一...

    mind_programmonkey6432021-08-12
  • Redis在ssm项目中使用redis缓存查询数据的方法

    在ssm项目中使用redis缓存查询数据的方法

    本文主要简单的使用Java代码进行redis缓存,即在查询的时候先在service层从redis缓存中获取数据。如果大家对在ssm项目中使用redis缓存查询数据的相关知识感...

    caychen8962019-11-12
  • RedisRedis数据结构之链表与字典的使用

    Redis数据结构之链表与字典的使用

    这篇文章主要介绍了Redis数据结构之链表与字典的使用,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友...

    白泽来了4052021-08-03
  • Redis就这?Redis持久化策略——AOF

    就这?Redis持久化策略——AOF

    今天为大家介绍Redis的另一种持久化策略——AOF。注意:AOF文件只会记录Redis的写操作命令,因为读命令对数据的恢复没有任何意义...

    头发茂密的刘叔4052021-12-14
  • Redisredis启动,停止,及端口占用处理方法

    redis启动,停止,及端口占用处理方法

    今天小编就为大家分享一篇redis启动,停止,及端口占用处理方法,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧 ...

    澄海单挑狂5152019-11-14
  • RedisLinux Redis 的安装步骤详解

    Linux Redis 的安装步骤详解

    这篇文章主要介绍了 Linux Redis 的安装步骤详解的相关资料,希望大家通过本文能掌握如何安装Redis,需要的朋友可以参考下 ...

    carl-zhao3822019-11-08