Redis集群使用总结(一):

随着企业数据量的增多,Redis不论做为数据存储或是缓存,它的数据量也会逐渐增多,虽然Redis的速度很是可观,但随着其中的数据量的庞大,而且仅仅在一个设备或是一个Redis实例中,其存取速度也会大打折扣,因此咱们须要在不一样的设备或服务器上,搭建多个Redis实例仓库,将原来的Redis的全部的keys分发到各个服务器的Redis上,这就是如今所谓的Redis集群(Redis Cluster)。node

 

·     原理redis

·     实施缓存

·     注意ruby

·     问题服务器

 

1、原理app

一、数据共享异步

 

Redis提供多个节点实例间的数据共享,也就是Redis A,B,C,D彼此之间的数据是同步的,一样彼此之间也能够通讯,而对于客户端操做的keys是由Redis系统自行分配到各个节点中。tcp

 

二、主从复制工具

 

Redis的多个实例间通讯时,一旦其中的一个节点故障,那么Redis集群就不能继续正常工做,因此须要一种复制机制(Master-Slave)机制,作到一旦节点A故障了,那么其从节点A1和A2就能够接管并继续提供与A一样的工做服务,固然若是节点A,A1,A2节点都出现问题,那么一样这个集群不会继续保持工做,可是这种状况比较罕见,即便出现了,也会及时发现并修复使用。性能

建议:部署主从复制机制(Master-Slave)。

 

三、哈希槽值

 

Redis集群中使用哈希槽来存储客户端的keys,而在Redis中,目前存在16384个哈希槽,它们被所有分配给全部的节点,正如上图所示,全部的哈希槽值被节点A,B,C分配完成了。

 

2、实施

一、建立集群

A、辅助工具安装

在搭建Redis Cluster以前,请先确保系统已经安装了zlib和ruby(含rubygems)软件依赖包,请自行安装。

 

B、安装redis-cluster

在Redis的源码路径下,位于src目录中的redis-trib.rb文件是用来搭建和维护集群的工具,咱们须要将其放入与redis-server和redis-cli一样的指令环境下,以下:

sudo cp /redis/redis-3.0.7/src/redis-trib.rb  /redis/bin

sudo cp /redis/redis-3.0.7/src/redis-server  /redis/bin

sudo cp /redis/redis-3.0.7/src/redis-cli  /redis/bin

 

C、配置redis-cluster

文件结构:

 

通用配置:

#generate configs

daemonize no

tcp-backlog 511

timeout 2000

tcp-keepalive 0

loglevel notice

databases 16

slave-serve-stale-data yes

slave-read-only yes

repl-disable-tcp-nodelay yes

slave-priority 100

#open the aof persistence

appendonly yes

#config aof mode for everysec

appendfsync everysec

#while rewrite op then close the aof write mode

no-appendfsync-on-rewrite yes

auto-aof-rewrite-min-size 64mb

aof-rewrite-incremental-fsync yes

#limit the time of lua scripts executes

lua-time-limit 5000

#open redis cluster switch

cluster-enabled yes

#timeout of nodes connections

cluster-node-timeout 15000

cluster-migration-barrier 1

slowlog-log-slower-than 10000

slowlog-max-len 128

notify-keyspace-events ""

hash-max-ziplist-entries 512

hash-max-ziplist-value 64

list-max-ziplist-entries 512

list-max-ziplist-value 64

set-max-intset-entries 512

zset-max-ziplist-entries 128

zset-max-ziplist-value 64

#open the online rehash

activerehashing yes

 

client-output-buffer-limit normal 0 0 0

client-output-buffer-limit slave 256mb 64mb 60

client-output-buffer-limit pubsub 32mb 8mb 60

 

hz 10

 

特殊配置:

这里以6379端口为例说明,其它的特殊配置只需修改对应的端口便可:

#common configs

include /redis/etc/redis-common.conf

#listen tcp port

port 6379

#memory cahce max size

maxmemory 100m

maxmemory-policy allkeys-lru

#aof filename

appendfilename "appendonly-6379.aof"

#rdb file,only use in the slave handle

dbfilename dump-6379.rdb

dir /redis/db-6379

#cluster config that auto create

cluster-config-file nodes-6379.conf

#log path

logfile /redis/logs/6379/log-6379.log

 

#in the same computer redis,then give the limit

#fork all redis processes done rewrite,using big memory

auto-aof-rewrite-percentage 80-100

 

D、搭建redis-cluster

在这里,咱们使用Redis自带的ruby工具redis-trib.rb来建立和管理集群。本人共建立了6个redis节点,分别启动以后,使用./redis-trib.rb create –replicas配置和生成3个主节点和对应每一个节点的3个从节点。

首先,启动6个节点实例:

$redis-server /redis/etc/redis-6379.conf

$redis-server  /redis/etc/redis-6380.conf

$redis-server  /redis/etc/redis-6381.conf

$redis-server  /redis/etc/redis-7379.conf

$redis-server  /redis/etc/redis-7380.conf

$redis-server  /redis/etc/redis-7381.conf

 

其次,建立分配主从节点:

$redis-trib.rb create --replicas 1

127.0.0.1:6379 127.0.0.1:6380 127.0.0.1:6381 127.0.0.1:7379 127.0.0.1:7380127.0.0.1:7381

 

执行的结果:

 

NOTE:

--replicas 1表明建立1个从节点,建立的从节点的顺序是按照6379-7379,6380-7380及6381-7381的顺序建立配置的。

 

从上图知道,已经建立和配置了3个Master和对应的3个Sub子节点,同时会提示您是否赞成这个配置生成,当你回复yes以后,显示以下:

 

NOTE:

从上图知道,Redis集群已经建立和配置成功了,而且redis的16384个哈希槽已经所有分配完成。

 

二、集群验证

验证可使用redis-rb-cluster或是redis-cli来验证,这里以最简单的方式redis-cli来验证使用,意在说明集群的使用和校验。

A、哈希槽分配

$redis-cli -c -p 6379

127.0.0.1:6379> set mykey "hello"

-> Redirected to slot [14687] located at 127.0.0.1:6381

 

$redis-cli -c -p 6380

127.0.0.1:6380> set mykey2 "hello world"

-> Redirected to slot [14119] located at 127.0.0.1:6381

OK

 

$redis-cli -c -p 6381

127.0.0.1:6381> set mykey3 "hello"

-> Redirected to slot [9990] located at 127.0.0.1:6380

OK

 

B、集群验证

$redis-trib.rb  127.0.0.1:7379

 

结果:

 

右上图,能够知道集群配置没问题,继续往下验证下数据的异步同步。

 

C、集群数据共享

$redis-cli -c -p 6379

127.0.0.1:6379> set mykey "hello"

-> Redirected to slot [14687] located at 127.0.0.1:6381

OK

127.0.0.1:6381> get mykey2

"hello world"

127.0.0.1:6381> get mykey3

-> Redirected to slot [9990] located at 127.0.0.1:6380

"hello"

 

NOTE:

从上面能够看出,我实例子6379中能够访问同一个集群内的节点数据,访问的机制是根据set时分配的哈希槽,例如:在6379中,使用get mykey3,那么自动定位到6380。  

 

3、注意

一、主从复制

Redis集群支持主从复制功能,也就是主节点对应的从节点,可是不须要在从节点中加入slaveof <server>:<port>,不然会报错哦。

 

二、主从配置

通常状况下,从节点的配置和对应的主节点的配置相似,可是通常从节点的大小要小于主节点的配置大小,这主要考虑内存和性能均衡方面,请在实际使用时留意下。

 

三、实例通讯

Redis集群中的节点实例间的数据共享机制是经过定位哈希槽(set时的键值分配的哈希),不会区分主从节点或是普通节点的通讯。

 

4、问题

遇到问题:

custom_require.rb:36:in `require': cannot load such file -- redis(LoadError)

from /usr/lib/ruby/1.9.1/rubygems/custom_require.rb:36:in `require'

from ./redis-trib.rb:25:in `<main>'

解决办法:

sudo gem install redis来安装ruby和redis的接口包便可

相关文章
相关标签/搜索