首页 > 代码库 > IX redis(2)

IX redis(2)

IX redis(2)集群

 

redis是一个开源的,使用C语言编写、支持网络交互、可基于内存也可持久化的key-value数据库,sina微博使用redis集群;

redis.io

redisdoc.com

 

redismemcached对比:


memcached

redis

类型

key-value DB

key-value DB

过期策略

支持

支持

数据类型

单一数据类型

五种数据类型

持久化

不支持

支持

主从复制

不支持

支持

虚拟内存

不支持

支持

技术分享

 

memcached用于session

redis用于购物车;

redis集群(客户端分片;proxyrediscluster(客户端缺失);codiscodisproxy-codis-serverdashboard));

 

#vim /etc/sysctl.conf

net.core.somaxconn = 2048

vm.overcommit_memory = 1

#sysctl -p

#cat /sys/kernel/mm/transparent_hugepage/enabled

[always] madvise never

#echo never > /sys/kernel/mm/transparent_hugepage/enabled   #redis3.0.7需调整,否则启动时会警告)

#cat /sys/kernel/mm/transparent_hugepage/enabled

always madvise [never]

#vim /etc/rc.local

echo never > /sys/kernel/mm/transparent_hugepage/enabled

 

 

[root@test1 ~]# cd /usr/local/src

[root@test1 src]# tar xf redis-3.0.7.tar.gz

[root@test1 src]# cd redis-3.0.7

[root@test1 redis-3.0.7]# less README

[root@test1 redis-3.0.7]# make PREFIX=/usr/local/redis install   #(生产环境中编译时要确定已安装gcc环境,#yum -y install gcc,否则会报错如下:

/bin/sh: cc: command not found

make[1]: *** [adlist.o] Error 127

make[1]: Leaving directory`/ane/redis-3.0.7/src‘

make: *** [install] Error 2

 

[root@test1 redis-3.0.7]# cp utils/redis_init_script /etc/init.d/redis

[root@test1 redis-3.0.7]# chmod 755 !$

chmod 755 /etc/init.d/redis

[root@test1 redis-3.0.7]# vim !$

vim /etc/init.d/redis

# chkconfig: 2345  56 44

# description: redis daemon

EXEC=/usr/local/redis/bin/redis-server

CLIEXEC=/usr/local/redis/bin/redis-cli

[root@test1 redis-3.0.7]# chkconfig --add redis

[root@test1 redis-3.0.7]# chkconfig --list redis

redis               0:off 1:off 2:on 3:on 4:on 5:on 6:off

 

[root@test1 redis-3.0.7]# mkdir /etc/redis

[root@test1 redis-3.0.7]# cp redis.conf/etc/redis/6379.conf

[root@test1 redis-3.0.7]# vim /etc/redis/6379.conf

daemonize yes

pidfile /var/run/redis_6379.pid   #(与启动脚本/etc/init.d/redis相同)

dbfilename dump_6379.rdb

dir /usr/local/redis/6379/

[root@test1 redis-3.0.7]# mkdir /usr/local/redis/6379

 

[root@test1 redis-3.0.7]# service redis start

Starting Redis server...

[root@test1 redis-3.0.7]# netstat -tnulp | grep redis

tcp       0      0 0.0.0.0:6379                0.0.0.0:*                   LISTEN      119100/redis-server

tcp       0      0 :::6379                     :::*                        LISTEN      119100/redis-server

 

[root@test1 redis-3.0.7]# /usr/local/redis/bin/redis-cli

127.0.0.1:6379> exit

[root@test1 redis-3.0.7]# /etc/init.d/redis stop

Stopping ...

Waiting for Redis to shutdown ...

Redis stopped

 

 

redis数据类型:

类型不能嵌套,不能混;各类型有各自的命令;

 

string字符串类型(SETGETDELAPPENDSTRLENMGETMSETINCRINCRBYDECRDECRBYINCRBYFLOAT),SET是王炸,任何类型只要用SET都会变为string类型;

 

hash散列类型:

HSET key field value

HGET key field

HMSET key field value [field values……]

HMGET key field [field……]

HGETALL key

HDEL key field

HEXISTS key field

EXISTS key

 

list列表类型(时间复杂度O(1)):

LPUSH key value [value……]   #left

RPUSH key value [value……]   #right

LPOP key

RPOP key

LRANGE key START STOP

LREM key count value   #remove

LLEN key

LINDEX key INDEX

LTRIM key INDEX_START INDEX_STOP   #(只保留指定的范围,常用于写日志,一边写一边出)

 

SET集合类型:

SADD key member [member……]

SREM key member [member……]

SMEMBERS key

SISMEMBER key member

SDIFF key [key……]   #(差集)

SINTER key [key……]   #(交集)

SUNION key [key……]   #(并集)

 

sortedset有序集合:

ZADD key score member   #(增加元素)

ZSCORE key member   #(获得元素的分数)

ZRANGE key START STOP [WITH SCORES]

ZRANGEBYSCORE key min max

 

redispub/sub

#redis-cli

>SUBSCRIBE channel1

>PUBLISH channel1 test_string1   #(在另一终端上执行)

 

redistransaction

>MULTI

> execute sentence……

>……

>……

>EXEC

 

redis,在线改配置:

>CONFIG SET ……

 

 

redis持久化(RDBAOF):

类似oracle的逻辑备库,mysqlbinlog

 

RDB

save 900 1

save 300 10

save 60 10000

dbfilename dump_6379.rdb

dir /usr/local/redis/6379/

 

AOF

appendonly yes

appendfilename "appendonly.aof"

 

 

redis主从复制(6379为主,6380为从):

[root@test1 ~]# cd /etc/redis/

[root@test1 redis]# cp 6379.conf 6380.conf

[root@test1 redis]# ll

total 88

-rw-r--r--. 1 root root 41592 Nov 14 19:266379.conf

-rw-r--r--. 1 root root 41592 Nov 14 21:426380.conf

[root@test1 redis]# vim 6380.conf

:%s/6379/6380/g

[root@test1 redis]# mkdir /usr/local/redis/6380/

[root@test1 redis]# /usr/local/redis/bin/redis-server 6379.conf

[root@test1 redis]# netstat -tnulp | grep redis

tcp       0      0 0.0.0.0:6379                0.0.0.0:*                   LISTEN      126880/redis-server

tcp       0      0 0.0.0.0:6380                0.0.0.0:*                   LISTEN      126967/redis-server

tcp       0      0 :::6379                     :::*                        LISTEN      126880/redis-server

tcp       0      0 :::6380                     :::*                        LISTEN      126967/redis-server

 

[root@test1 redis]# /usr/local/redis/bin/redis-cli -h 192.168.23.129 -p 6379

192.168.23.129:6379> info

……

# Replication

role:master

connected_slaves:0

master_repl_offset:0

repl_backlog_active:0

repl_backlog_size:1048576

repl_backlog_first_byte_offset:0

repl_backlog_histlen:0

……

192.168.23.129:6379> exit

[root@test1 redis]# /usr/local/redis/bin/redis-cli -h 192.168.23.129 -p 6380

192.168.23.129:6380> info

……

# Replication

role:master

connected_slaves:0

master_repl_offset:0

repl_backlog_active:0

repl_backlog_size:1048576

repl_backlog_first_byte_offset:0

repl_backlog_histlen:0

……

192.168.23.129:6380> slaveof 192.168.23.129 6379   #(在作为从的服务器上执行此条命令,或直接写到6380的配置文件中)

OK

192.168.23.129:6380> info

……

# Replication

role:slave

master_host:192.168.23.129

master_port:6379

master_link_status:up

master_last_io_seconds_ago:3

master_sync_in_progress:0

slave_repl_offset:1

slave_priority:100

slave_read_only:1

connected_slaves:0

master_repl_offset:0

repl_backlog_active:0

repl_backlog_size:1048576

repl_backlog_first_byte_offset:0

repl_backlog_histlen:0

……

 

测试:

[root@test1 ~]# /usr/local/redis/bin/redis-cli -h 192.168.23.129 -p 6379

192.168.23.129:6379> keys *

(empty list or set)

192.168.23.129:6379> set key1 value1   #(在主上创建)

OK

192.168.23.129:6379> keys *

1) "key1"

 

192.168.23.129:6380> keys *   #(在从上查看)

1) "key1"

192.168.23.129:6380> get key1

"value1"

 

[root@test1 ~]# /usr/local/redis/bin/redis-cli -p 6379 shutdown

[root@test1 ~]# /usr/local/redis/bin/redis-cli -p 6380 shutdown

 

 

 

redis集群:

http://redisdoc.com/topic/cluster-tutorial.html

方案有:

客户端分片;

proxytwemproxy

redis cluster,无中心;

豌豆荚codis

Redis 集群是一个可以在多个 Redis 节点之间进行数据共享的设施(installation);Redis 集群不支持那些需要同时处理多个键的 Redis 命令,因为执行这些命令需要在多个 Redis 节点之间移动数据,并且在高负载的情况下,这些命令将降低 Redis集群的性能,并导致不可预测的行为;Redis 集群通过分区(partition)来提供一定程度的可用性(availability):即使集群中有一部分节点失效或者无法进行通讯,集群也可以继续处理命令请求;

Redis 集群提供了以下两个好处:将数据自动切分(split)到多个节点的能力;当集群中的一部分节点失效或者无法进行通讯时,仍然可以继续处理命令请求的能力;

 

Redis 集群使用数据分片(sharding)而非一致性哈希(consistency hashing)来实现:一个 Redis 集群包含 16384 个哈希槽(hash slot),数据库中的每个键都属于这 16384 个哈希槽的其中一个,集群使用公式 CRC16(key) % 16384 来计算键key属于哪个槽,其中 CRC16(key) 语句用于计算键keyCRC16 校验和;

集群中的每个节点负责处理一部分哈希槽,举个例子,一个集群可以有三个哈希槽,其中:

节点 A 负责处理 0 号至 5500 号哈希槽,

节点 B 负责处理 5501 号至 11000 号哈希槽,

节点 C 负责处理 11001 号至 16384 号哈希槽,

这种将哈希槽分布到不同节点的做法使得用户可以很容易地向集群中添加或者删除节点,比如说:如果用户将新节点D添加到集群中,那么集群只需要将节点ABC中的某些槽移动到节点 D 就可以了;与此类似,如果用户要从集群中移除节点A,那么集群只需要将节点A中的所有哈希槽移动到节点B和节点C,然后再移除空白(不包含任何哈希槽)的节点 A 就可以了;因为将一个哈希槽从一个节点移动到另一个节点不会造成节点阻塞,所以无论是添加新节点还是移除已存在节点,又或者改变某个节点包含的哈希槽数量,都不会造成集群下线;

 

为了使得集群在一部分节点下线或者无法与集群的大多数(majority)节点进行通讯的情况下,仍然可以正常运作,Redis集群对节点使用了主从复制功能:集群中的每个节点都有1个至N个复制品(replica),其中一个复制品为主节点(master),而其余的 N-1 个复制品为从节点(slave);

在之前列举的节点ABC的例子中,如果节点B下线了,那么集群将无法正常运行,因为集群找不到节点来处理 5501号至11000号的哈希槽;另一方面,假如在创建集群的时候(或者至少在节点B下线之前),我们为主节点B添加了从节点B1那么当主节点B下线的时候,集群就会将B1设置为新的主节点,并让它代替下线的主节点B继续处理 5501 号至11000号的哈希槽,这样集群就不会因为主节点B的下线而无法正常运作了;不过如果节点BB1 都下线的话,Redis 集群还是会停止运作;

 

Redis集群不保证数据的强一致性(strong consistency):在特定条件下,Redis 集群可能会丢失已经被执行过的写命令;

使用异步复制(asynchronous replication)是Redis集群可能会丢失写命令的其中一个原因,考虑以下这个写命令的例子:

客户端向主节点B发送一条写命令,

主节点B执行写命令,并向客户端返回命令回复,

主节点B将刚刚执行的写命令复制给它的从节点B1B2B3

如你所见,主节点对命令的复制工作发生在返回命令回复之后,因为如果每次处理命令请求都需要等待复制操作完成的话,那么主节点处理命令请求的速度将极大地降低,我们必须在性能和一致性之间做出权衡;

如果真的有必要的话,Redi集群可能会在将来提供同步地(synchronous)执行写命令的方法,Redis集群另外一种可能会丢失命令的情况是,集群出现网络分裂(network partition),并且一个客户端与至少包括一个主节点在内的少数(minority)实例被孤立;

举个例子,假设集群包含ABCA1B1C1六个节点,其中ABC为主节点,而A1B1C1分别为三个主节点的从节点,另外还有一个客户端 Z1,假设集群中发生网络分裂,那么集群可能会分裂为两方,大多数(majority)的一方包含节点ACA1B1C1,而少数(minority)的一方则包含节点B和客户端Z1,在网络分裂期间,主节点B仍然会接受 Z1发送的写命令,如果网络分裂出现的时间很短,那么集群会继续正常运行;但是,如果网络分裂出现的时间足够长,使得大多数一方将从节点B1设置为新的主节点,并使用B1来代替原来的主节点B,那么Z1发送给主节点 B的写命令将丢失;

注意,在网络分裂出现期间,客户端Z1可以向主节点B发送写命令的最大时间是有限制的,这一时间限制称为节点超时时间(node timeout),是Redis集群的一个重要的配置选项:对于大多数一方来说,如果一个主节点未能在节点超时时间所设定的时限内重新联系上集群,那么集群会将这个主节点视为下线,并使用从节点来代替这个主节点继续工作;

对于少数一方,如果一个主节点未能在节点超时时间所设定的时限内重新联系上集群,那么它将停止处理写命令,并向客户端报告错误;

 

操作:

集群中有700170027003700470057006,其中700470057006分别为700170027003的从;

 

[root@test1 ~]# cd /opt

[root@test1 opt]# mkdir `seq 7001 7008`

[root@test1 opt]# ls

7001 7002  7003  7004 7005  7006  7007 7008  rh

[root@test1 opt]# cp /etc/redis/6379.conf /opt/7001/7001.conf

[root@test1 opt]# vim /opt/7001/7001.conf

daemonize yes

pidfile /var/run/redis_7001.pid

port 7001

logfile "/opt/7001/7001.log"

dbfilename dump_7001.rdb

dir /opt/7001/

cluster-enabled yes

cluster-config-filenodes-7001.conf

cluster-node-timeout 15000

[root@test1 opt]# sed ‘s/7001/7002/g‘/opt/7001/7001.conf >> /opt/7002/7002.conf

[root@test1 opt]# sed ‘s/7001/7003/g‘/opt/7001/7001.conf >> /opt/7003/7003.conf

[root@test1 opt]# sed ‘s/7001/7004/g‘/opt/7001/7001.conf >> /opt/7004/7004.conf

[root@test1 opt]# sed ‘s/7001/7005/g‘/opt/7001/7001.conf >> /opt/7005/7005.conf

[root@test1 opt]# sed ‘s/7001/7006/g‘/opt/7001/7001.conf >> /opt/7006/7006.conf

[root@test1 opt]# fori in `seq 7001 7006` ; do /usr/local/redis/bin/redis-server /opt/$i/$i.conf ;done

[root@test1 opt]# netstat -tnulp | grep redis

tcp       0      0 0.0.0.0:17003               0.0.0.0:*                   LISTEN      129644/redis-server

tcp       0      0 0.0.0.0:17004               0.0.0.0:*                   LISTEN      129646/redis-server

tcp       0      0 0.0.0.0:17005               0.0.0.0:*                   LISTEN      129654/redis-server

tcp       0      0 0.0.0.0:17006               0.0.0.0:*                   LISTEN      129658/redis-server

tcp       0      0 0.0.0.0:7001                0.0.0.0:*                   LISTEN      129640/redis-server

tcp       0      0 0.0.0.0:7002                0.0.0.0:*                   LISTEN      129642/redis-server

tcp       0      0 0.0.0.0:7003                0.0.0.0:*                   LISTEN      129644/redis-server

tcp       0      0 0.0.0.0:7004                0.0.0.0:*                   LISTEN     129646/redis-server

tcp       0      0 0.0.0.0:7005                0.0.0.0:*                   LISTEN      129654/redis-server

tcp       0      0 0.0.0.0:7006                0.0.0.0:*                   LISTEN      129658/redis-server

tcp       0      0 0.0.0.0:17001               0.0.0.0:*                   LISTEN      129640/redis-server

tcp       0      0 0.0.0.0:17002               0.0.0.0:*                   LISTEN      129642/redis-server

tcp       0      0 :::17003                    :::*                        LISTEN      129644/redis-server

tcp       0      0 :::17004                    :::*                        LISTEN      129646/redis-server

tcp       0      0 :::17005                    :::*                        LISTEN      129654/redis-server

tcp       0      0 :::17006                    :::*                        LISTEN      129658/redis-server

tcp       0      0 :::7001                     :::*                        LISTEN      129640/redis-server

tcp       0      0 :::7002                     :::*                        LISTEN      129642/redis-server

tcp       0      0 :::7003                     :::*                        LISTEN      129644/redis-server

tcp        0     0 :::7004                    :::*                        LISTEN      129646/redis-server

tcp       0      0 :::7005                     :::*                        LISTEN      129654/redis-server

tcp       0      0 :::7006                     :::*                        LISTEN      129658/redis-server

tcp       0      0 :::17001                    :::*                        LISTEN      129640/redis-server

tcp       0      0 :::17002                    :::*                        LISTEN      129642/redis-server

 

使用redis-trib.rbruby脚本管理集群,要先安装ruby环境,再安装rubyredis的接口;

[root@test1 opt]# yum -y install ruby rubygems [ruby-devel] #(安装ruby环境;RubyGems,简称 gems,是一个用于对 Ruby组件进行打包的Ruby打包系统,它提供一个分发Ruby程序和库的标准格式,还提供一个管理程序包安装的工具,RubyGems的功能类似于Linux下的apt-get,使用它可以方便第从远程服务器下载并安装Rails

[root@test1 opt]# gem install redis   #(安装rubyredis的接口)

Successfully installed redis-3.3.1

1 gem installed

Installing ri documentation forredis-3.3.1...

Installing RDoc documentation forredis-3.3.1...

[root@test1 opt]# ln -sv /usr/local/src/redis-3.0.7/src/redis-trib.rb /usr/local/bin/

`/usr/local/bin/redis-trib.rb‘ ->`/usr/local/src/redis-3.0.7/src/redis-trib.rb‘

 

注:

#gem list

#gem sources -l

#gem sources --add https://gems.ruby-china.org/  --remove http://rubygems.org/

#gem uninstall redis --version 3.3.1

#gem install redis --version 3.0.7

 

 

[root@test1 opt]# redis-trib.rb help

Usage: redis-trib <command><options> <arguments ...>

  add-node        new_host:new_portexisting_host:existing_port

                 --master-id<arg>

                  --slave

 check           host:port

 help            (show this help)

 call            host:port commandarg arg .. arg

  del-node        host:port node_id

  reshard         host:port

                  --from <arg>

                  --timeout <arg>

                  --pipeline <arg>

                  --yes

                  --slots <arg>

                  --to <arg>

  create          host1:port1 ... hostN:portN

                  --replicas <arg>

 import          host:port

                  --from <arg>

                  --copy

                  --replace

 set-timeout     host:portmilliseconds

 rebalance       host:port

                  --timeout <arg>

                  --pipeline <arg>

                  --simulate

                  --use-empty-masters

                  --weight <arg>

                  --threshold <arg>

                  --auto-weights

 fix             host:port

                  --timeout <arg>

 info            host:port

For check, fix, reshard, del-node,set-timeout you can specify the host and port of any working node in thecluster.

 

[root@test1 opt]# redis-trib.rb create --replicas 1 192.168.23.129:7001 192.168.23.129:7002 192.168.23.129:7003192.168.23.129:7004 192.168.23.129:7005 192.168.23.129:7006   #(依次按顺序创建,主为700170027003700470057006分别为700170027003的从,注意分配的ID,靠ID来识别)

>>> Creating cluster

>>> Performing hash slotsallocation on 6 nodes...

Using 3 masters:

192.168.23.129:7001

192.168.23.129:7002

192.168.23.129:7003

Adding replica 192.168.23.129:7004 to192.168.23.129:7001

Adding replica 192.168.23.129:7005 to192.168.23.129:7002

Adding replica 192.168.23.129:7006 to192.168.23.129:7003

M: 908ff036d9ff445b74098ed9870566292c3dddeb192.168.23.129:7001

  slots:0-5460 (5461 slots) master

M: 204b6b8a7fb15f000a0e75c5224b6d4aa774ba6c192.168.23.129:7002

  slots:5461-10922 (5462 slots) master

M: 9d0f64162132dc02416f1217e60ae35b28d71274192.168.23.129:7003

  slots:10923-16383 (5461 slots) master

S: 56d6adfd7eda1049ef1ba79d6d505df61b979d45192.168.23.129:7004

  replicates 908ff036d9ff445b74098ed9870566292c3dddeb

S: e4a3c90127e6573200a55448d122b457839e5d51192.168.23.129:7005

  replicates 204b6b8a7fb15f000a0e75c5224b6d4aa774ba6c

S: 84f715fdf90dcd6a060dc63387204eb9396e3470192.168.23.129:7006

  replicates 9d0f64162132dc02416f1217e60ae35b28d71274

Can I set the above configuration? (type‘yes‘ to accept): yes

>>> Nodes configuration updated

>>> Assign a different configepoch to each node

>>> Sending CLUSTER MEET messagesto join the cluster

Waiting for the cluster to join.....

>>> Performing Cluster Check(using node 192.168.23.129:7001)

M: 908ff036d9ff445b74098ed9870566292c3dddeb192.168.23.129:7001

  slots:0-5460 (5461 slots) master

M: 204b6b8a7fb15f000a0e75c5224b6d4aa774ba6c192.168.23.129:7002

  slots:5461-10922 (5462 slots) master

M: 9d0f64162132dc02416f1217e60ae35b28d71274192.168.23.129:7003

  slots:10923-16383 (5461 slots) master

M: 56d6adfd7eda1049ef1ba79d6d505df61b979d45192.168.23.129:7004

  slots: (0 slots) master

  replicates 908ff036d9ff445b74098ed9870566292c3dddeb

M: e4a3c90127e6573200a55448d122b457839e5d51192.168.23.129:7005

  slots: (0 slots) master

  replicates 204b6b8a7fb15f000a0e75c5224b6d4aa774ba6c

M: 84f715fdf90dcd6a060dc63387204eb9396e3470192.168.23.129:7006

  slots: (0 slots) master

  replicates 9d0f64162132dc02416f1217e60ae35b28d71274

[OK] All nodes agree about slotsconfiguration.

>>> Check for open slots...

>>> Check slots coverage...

[OK] All 16384 slots covered.

 

[root@test1 opt]# /usr/local/redis/bin/redis-cli -c -h 192.168.23.129 -p 7001

192.168.23.129:7001> info

……

# Replication

role:master

connected_slaves:1

slave0:ip=192.168.23.129,port=7004,state=online,offset=449,lag=0

master_repl_offset:449

repl_backlog_active:1

repl_backlog_size:1048576

repl_backlog_first_byte_offset:2

repl_backlog_histlen:448

……

192.168.23.129:7001> cluster nodes

204b6b8a7fb15f000a0e75c5224b6d4aa774ba6c192.168.23.129:7002 master - 0 1479195195214 2 connected 5461-10922

9d0f64162132dc02416f1217e60ae35b28d71274192.168.23.129:7003 master - 0 1479195197228 3 connected 10923-16383

56d6adfd7eda1049ef1ba79d6d505df61b979d45192.168.23.129:7004 slave 908ff036d9ff445b74098ed9870566292c3dddeb 01479195191182 4 connected

908ff036d9ff445b74098ed9870566292c3dddeb192.168.23.129:7001 myself,master - 0 0 1connected 0-5460

84f715fdf90dcd6a060dc63387204eb9396e3470192.168.23.129:7006 slave 9d0f64162132dc02416f1217e60ae35b28d71274 01479195194205 6 connected

e4a3c90127e6573200a55448d122b457839e5d51192.168.23.129:7005 slave 204b6b8a7fb15f000a0e75c5224b6d4aa774ba6c 01479195196221 5 connected

192.168.23.129:7001> cluster info

cluster_state:ok

cluster_slots_assigned:16384

cluster_slots_ok:16384

cluster_slots_pfail:0

cluster_slots_fail:0

cluster_known_nodes:6

cluster_size:3

cluster_current_epoch:6

cluster_my_epoch:1

cluster_stats_messages_sent:850

cluster_stats_messages_received:850

 

添加新node(主为700770087007的从):

[root@test1 opt]# sed ‘s/7001/7007/g‘/opt/7001/7001.conf > /opt/7007/7007.conf

[root@test1 opt]# sed ‘s/7001/7008/g‘/opt/7001/7001.conf > /opt/7008/7008.conf

[root@test1 opt]# for i in `seq 7007 7008`; do /usr/local/redis/bin/redis-server /opt/$i/$i.conf ; done

[root@test1 opt]# netstat -tnlp | egrep ‘7007|7008‘

tcp       0      0 0.0.0.0:17007               0.0.0.0:*                   LISTEN      1442/redis-server *

tcp       0      0 0.0.0.0:17008               0.0.0.0:*                   LISTEN      1444/redis-server *

tcp       0      0 0.0.0.0:7007                0.0.0.0:*                   LISTEN      1442/redis-server *

tcp       0      0 0.0.0.0:7008                0.0.0.0:*                   LISTEN      1444/redis-server *

tcp       0      0 :::17007                    :::*                        LISTEN      1442/redis-server *

tcp       0      0 :::17008                    :::*                        LISTEN      1444/redis-server *

tcp       0      0 :::7007                     :::*                        LISTEN      1442/redis-server *

tcp       0      0 :::7008                     :::*                        LISTEN      1444/redis-server *

 

[root@test1 opt]# redis-trib.rb add-node 192.168.23.129:7007 192.168.23.129:7001   #192.168.23.129:7007为新node192.168.23.129:7001为集群中存在的node可以是7001-7006中的任何一个)

>>> Adding node 192.168.23.129:7007to cluster 192.168.23.129:7001

>>> Performing Cluster Check(using node 192.168.23.129:7001)

M: 908ff036d9ff445b74098ed9870566292c3dddeb192.168.23.129:7001

  slots:0-5460 (5461 slots) master

   1additional replica(s)

M: 204b6b8a7fb15f000a0e75c5224b6d4aa774ba6c192.168.23.129:7002

  slots:5461-10922 (5462 slots) master

   1additional replica(s)

M: 9d0f64162132dc02416f1217e60ae35b28d71274192.168.23.129:7003

  slots:10923-16383 (5461 slots) master

   1additional replica(s)

S: 56d6adfd7eda1049ef1ba79d6d505df61b979d45192.168.23.129:7004

  slots: (0 slots) slave

  replicates 908ff036d9ff445b74098ed9870566292c3dddeb

S: 84f715fdf90dcd6a060dc63387204eb9396e3470192.168.23.129:7006

  slots: (0 slots) slave

  replicates 9d0f64162132dc02416f1217e60ae35b28d71274

S: e4a3c90127e6573200a55448d122b457839e5d51192.168.23.129:7005

  slots: (0 slots) slave

  replicates 204b6b8a7fb15f000a0e75c5224b6d4aa774ba6c

[OK] All nodes agree about slotsconfiguration.

>>> Check for open slots...

>>> Check slots coverage...

[OK] All 16384 slots covered.

>>> Send CLUSTER MEET to node192.168.23.129:7007 to make it join the cluster.

[OK] New node added correctly.

 

[root@test1 opt]# /usr/local/redis/bin/redis-cli -h 192.168.23.129 -p 7001

192.168.23.129:7001> cluster nodes   #(新添的node7007没有slots将无法写入)

204b6b8a7fb15f000a0e75c5224b6d4aa774ba6c192.168.23.129:7002 master - 0 1479195859314 2 connected 5461-10922

9d0f64162132dc02416f1217e60ae35b28d71274192.168.23.129:7003 master - 0 1479195855276 3 connected 10923-16383

56d6adfd7eda1049ef1ba79d6d505df61b979d45192.168.23.129:7004 slave 908ff036d9ff445b74098ed9870566292c3dddeb 01479195860323 4 connected

908ff036d9ff445b74098ed9870566292c3dddeb192.168.23.129:7001 myself,master - 0 0 1 connected 0-5460

84f715fdf90dcd6a060dc63387204eb9396e3470192.168.23.129:7006 slave 9d0f64162132dc02416f1217e60ae35b28d71274 01479195861332 6 connected

c1b66f955d9831a30516b1ad7dc5529b5e37ea8d192.168.23.129:7007 master - 0 1479195860827 0 connected

e4a3c90127e6573200a55448d122b457839e5d51192.168.23.129:7005 slave 204b6b8a7fb15f000a0e75c5224b6d4aa774ba6c 01479195858303 5 connected

192.168.23.129:7001> exit

 

[root@test1 opt]# /usr/local/redis/bin/redis-cli -h 192.168.23.129 -p 7001

192.168.23.129:7001> cluster nodes

204b6b8a7fb15f000a0e75c5224b6d4aa774ba6c192.168.23.129:7002 master - 0 1479195859314 2 connected 5461-10922

9d0f64162132dc02416f1217e60ae35b28d71274192.168.23.129:7003 master - 0 1479195855276 3 connected 10923-16383

56d6adfd7eda1049ef1ba79d6d505df61b979d45 192.168.23.129:7004 slave 908ff036d9ff445b74098ed9870566292c3dddeb 0 1479195860323 4 connected

908ff036d9ff445b74098ed9870566292c3dddeb192.168.23.129:7001 myself,master - 0 0 1 connected 0-5460

84f715fdf90dcd6a060dc63387204eb9396e3470192.168.23.129:7006 slave 9d0f64162132dc02416f1217e60ae35b28d71274 01479195861332 6 connected

c1b66f955d9831a30516b1ad7dc5529b5e37ea8d192.168.23.129:7007 master - 0 1479195860827 0 connected

e4a3c90127e6573200a55448d122b457839e5d51192.168.23.129:7005 slave 204b6b8a7fb15f000a0e75c5224b6d4aa774ba6c 01479195858303 5 connected

192.168.23.129:7001> exit

 

[root@test1 opt]# redis-trib.rb reshard 192.168.23.129:7007   #(输入要迁出的哈希槽的数目;输入迁入哈希槽的目的节点ID;输入迁出哈希槽的源节点ID,可以是all;脚本会根据填入的信息自动执行迁移计划)

>>> Performing Cluster Check(using node 192.168.23.129:7007)

M: c1b66f955d9831a30516b1ad7dc5529b5e37ea8d192.168.23.129:7007

  slots: (0 slots) master

   0additional replica(s)

M: 908ff036d9ff445b74098ed9870566292c3dddeb192.168.23.129:7001

  slots:0-5460 (5461 slots) master

   1additional replica(s)

S: 84f715fdf90dcd6a060dc63387204eb9396e3470192.168.23.129:7006

  slots: (0 slots) slave

  replicates 9d0f64162132dc02416f1217e60ae35b28d71274

M: 204b6b8a7fb15f000a0e75c5224b6d4aa774ba6c192.168.23.129:7002

  slots:5461-10922 (5462 slots) master

   1additional replica(s)

S: 56d6adfd7eda1049ef1ba79d6d505df61b979d45192.168.23.129:7004

  slots: (0 slots) slave

  replicates 908ff036d9ff445b74098ed9870566292c3dddeb

M: 9d0f64162132dc02416f1217e60ae35b28d71274192.168.23.129:7003

  slots:10923-16383 (5461 slots) master

   1additional replica(s)

S: e4a3c90127e6573200a55448d122b457839e5d51192.168.23.129:7005

  slots: (0 slots) slave

  replicates 204b6b8a7fb15f000a0e75c5224b6d4aa774ba6c

[OK] All nodes agree about slotsconfiguration.

>>> Check for open slots...

>>> Check slots coverage...

[OK] All 16384 slots covered.

How many slots do you want to move (from 1to 16384)? 4000

What is the receiving node ID? c1b66f955d9831a30516b1ad7dc5529b5e37ea8d

Please enter all the source node IDs.

 Type ‘all‘ to use all the nodes as source nodes for the hash slots.

 Type ‘done‘ once you entered all the source nodes IDs.

Source node #1:all

……

   Moving slot 12250 from 9d0f64162132dc02416f1217e60ae35b28d71274

   Moving slot 12251 from 9d0f64162132dc02416f1217e60ae35b28d71274

   Moving slot 12252 from 9d0f64162132dc02416f1217e60ae35b28d71274

   Moving slot 12253 from 9d0f64162132dc02416f1217e60ae35b28d71274

   Moving slot 12254 from 9d0f64162132dc02416f1217e60ae35b28d71274

   Moving slot 12255 from 9d0f64162132dc02416f1217e60ae35b28d71274

Do you want to proceed with the proposedreshard plan (yes/no)? yes

……

 

[root@test1 opt]# redis-trib.rb add-node 192.168.23.129:7008 192.168.23.129:7001   #(先将7008加入到集群中)

……

 

[root@test1 opt]#/usr/local/redis/bin/redis-cli -h 192.168.23.129 -p 7001  

192.168.23.129:7001> cluster nodes

204b6b8a7fb15f000a0e75c5224b6d4aa774ba6c192.168.23.129:7002 master - 0 1479197010157 2 connected 6795-10922

9d0f64162132dc02416f1217e60ae35b28d71274192.168.23.129:7003 master - 0 1479197011167 3 connected 12256-16383

56d6adfd7eda1049ef1ba79d6d505df61b979d45192.168.23.129:7004 slave 908ff036d9ff445b74098ed9870566292c3dddeb 01479197009148 4 connected

908ff036d9ff445b74098ed9870566292c3dddeb192.168.23.129:7001 myself,master - 0 0 1 connected 1333-5460

84f715fdf90dcd6a060dc63387204eb9396e3470192.168.23.129:7006 slave 9d0f64162132dc02416f1217e60ae35b28d71274 01479197008138 6 connected

932fe08667f7d1e2c3c2062721cdfdd467a87200192.168.23.129:7008 master - 0 1479197012177 0 connected

c1b66f955d9831a30516b1ad7dc5529b5e37ea8d192.168.23.129:7007 master - 0 1479197009148 7 connected 0-1332 5461-679410923-12255

e4a3c90127e6573200a55448d122b457839e5d51192.168.23.129:7005 slave 204b6b8a7fb15f000a0e75c5224b6d4aa774ba6c 01479197007633 5 connected

192.168.23.129:7001> exit

 

[root@test1 opt]# /usr/local/redis/bin/redis-cli -h 192.168.23.129 -p 7008   #(将7008设为7007的从,clusterreplicate 7007‘s ID

192.168.23.129:7008> cluster nodes

c1b66f955d9831a30516b1ad7dc5529b5e37ea8d 192.168.23.129:7007 master - 0 1479197224856 7 connected 0-1332 5461-6794 10923-12255

204b6b8a7fb15f000a0e75c5224b6d4aa774ba6c192.168.23.129:7002 master - 0 1479197228888 2 connected 6795-10922

84f715fdf90dcd6a060dc63387204eb9396e3470192.168.23.129:7006 slave 9d0f64162132dc02416f1217e60ae35b28d71274 01479197227881 3 connected

932fe08667f7d1e2c3c2062721cdfdd467a87200192.168.23.129:7008 myself,master - 0 0 0 connected

9d0f64162132dc02416f1217e60ae35b28d71274192.168.23.129:7003 master - 0 1479197229896 3 connected 12256-16383

56d6adfd7eda1049ef1ba79d6d505df61b979d45192.168.23.129:7004 slave 908ff036d9ff445b74098ed9870566292c3dddeb 01479197225866 1 connected

e4a3c90127e6573200a55448d122b457839e5d51192.168.23.129:7005 slave 204b6b8a7fb15f000a0e75c5224b6d4aa774ba6c 01479197228888 2 connected

908ff036d9ff445b74098ed9870566292c3dddeb192.168.23.129:7001 master - 0 1479197226872 1 connected 1333-5460

192.168.23.129:7008> cluster replicate c1b66f955d9831a30516b1ad7dc5529b5e37ea8d

OK

192.168.23.129:7008> cluster nodes

c1b66f955d9831a30516b1ad7dc5529b5e37ea8d192.168.23.129:7007 master - 0 1479197446839 7 connected 0-1332 5461-679410923-12255

204b6b8a7fb15f000a0e75c5224b6d4aa774ba6c192.168.23.129:7002 master - 0 1479197442801 2 connected 6795-10922

84f715fdf90dcd6a060dc63387204eb9396e3470192.168.23.129:7006 slave 9d0f64162132dc02416f1217e60ae35b28d71274 01479197441790 3 connected

932fe08667f7d1e2c3c2062721cdfdd467a87200192.168.23.129:7008 myself,slave c1b66f955d9831a30516b1ad7dc5529b5e37ea8d 0 0 0connected

9d0f64162132dc02416f1217e60ae35b28d71274192.168.23.129:7003 master - 0 1479197447849 3 connected 12256-16383

56d6adfd7eda1049ef1ba79d6d505df61b979d45192.168.23.129:7004 slave 908ff036d9ff445b74098ed9870566292c3dddeb 0 14791974488591 connected

e4a3c90127e6573200a55448d122b457839e5d51192.168.23.129:7005 slave 204b6b8a7fb15f000a0e75c5224b6d4aa774ba6c 01479197445829 2 connected

908ff036d9ff445b74098ed9870566292c3dddeb192.168.23.129:7001 master - 0 1479197443810 1 connected 1333-5460

192.168.23.129:7008> exit

 

[root@test1 ~]# /usr/local/redis/bin/redis-cli -c -h 192.168.23.129 -p 7001   #(要使用-c,支持集群模式,Enablecluster mode (follow -ASK and -MOVED redirections)

192.168.23.129:7001> keys *

(empty list or set)

 


注:

#./redis-trib.rb check 192.168.23.129:7001   #(检查集群)

#./redis-cli -c -h 172.17.101.60 -p 7008

>cluster replicate c1b66f955d9831a30516b1ad7dc5529b5e37ea8d   #(将7008设为7007的从)

>cluster saveconfig


 

 

 

redis工具:

phpredisadmin

[root@test1 ~]# yum -y install httpd php php-pecl-redis  #Package php-redis is obsoleted by php-pecl-redis

[root@test1 ~]# cd /var/www/html

[root@test1 html]# which git

/usr/bin/git

[root@test1 html]# git clone https://github.com/ErikDubbelboer/phpRedisAdmin.git

Initialized empty Git repository in/var/www/html/phpRedisAdmin/.git/

remote: Counting objects: 598, done.

remote: Total 598 (delta 0), reused 0(delta 0), pack-reused 598

Receiving objects: 100% (598/598), 176.34KiB | 163 KiB/s, done.

Resolving deltas: 100% (362/362), done.

[root@test1 html]# cd phpRedisAdmin/

[root@test1 phpRedisAdmin]# git clone https://github.com/nrk/predis.git vendor

Initialized empty Git repository in/var/www/html/phpRedisAdmin/vendor/.git/

remote: Counting objects: 22351, done.

remote: Total 22351 (delta 0), reused 0(delta 0), pack-reused 22351

Receiving objects: 100% (22351/22351), 4.84MiB | 275 KiB/s, done.

Resolving deltas: 100% (15366/15366), done.

[root@test1 phpRedisAdmin]# cd includes/

[root@test1 includes]# ls

common.inc.php              config.sample.inc.php  functions.inc.php  login.inc.php

config.environment.inc.php  footer.inc.php         header.inc.php     page.inc.php

[root@test1 includes]# cp config.sample.inc.php config.inc.php

[root@test1 includes]# vim config.inc.php

<?php

//Copy this file to config.inc.php and makechanges to that file to customize your configuration.

$config = array(

 ‘servers‘ => array(

   array(

      ‘name‘  => ‘7001‘, // Optional name.

      ‘host‘  => ‘192.168.23.129‘,

      ‘port‘  => 7001,

     ‘filter‘ => ‘*‘,

     ‘scheme‘ => ‘tcp‘, // Optional. Connection scheme. ‘tcp‘ - for TCPconnection, ‘unix‘ - for connection by unix domain socket

     ‘path‘   => ‘‘ // Optional.Path to unix domain socket. Uses only if ‘scheme‘ => ‘unix‘. Example:‘/var/run/redis/redis.sock‘

 

     // Optional Redis authentication.

     //‘auth‘ => ‘redispasswordhere‘ // Warning: The password is sent inplain-text to the Redis server.

   ),

   array(

      ‘name‘ => ‘7002‘,

      ‘host‘ => ‘192.168.23.129‘,

      ‘port‘ => 7002,

   ),

……

[root@test1 includes]# cd ../..

[root@test1 html]# chown -R apache:apache phpRedisAdmin/

[root@test1 html]# service httpd start

Starting httpd: httpd: Could not reliablydetermine the server‘s fully qualified domain name, using 192.168.23.129 forServerName

                                                          [  OK  ]

 

http://192.168.23.129/phpRedisAdmin/

技术分享

 

 

rdbtools(分析redis内存):

[root@test1 ~]# yum -y install zlib-devel bzip2-devel openssl-devel ncurses-devel sqlite-devel readline-devel tk-devel

[root@test1 ~]# tar xf setuptools-19.6.1.tar.gz

[root@test1 ~]# cd setuptools-19.6.1

[root@test1 setuptools-19.6.1]# python setup.py install

[root@test1 setuptools-19.6.1]# cd

[root@test1 ~]# tar xf pip-9.0.1.tar.gz

[root@test1 ~]# cd pip-9.0.1

[root@test1 pip-9.0.1]# chmod +x setup.py

[root@test1 pip-9.0.1]# python setup.py install

……

Installed/usr/lib/python2.6/site-packages/pip-9.0.1-py2.6.egg

Processing dependencies for pip==9.0.1

Finished processing dependencies forpip==9.0.1

[root@test1 ~]# which pip

/usr/bin/pip

 

[root@test1 ~]# pip install rdbtools

……

[root@test1 ~]# which rdb

/usr/bin/rdb

 

[root@test1 ~]# rdb --help

Usage: rdb [options] /path/to/dump.rdb

Example : rdb --command json -k"user.*" /var/redis/6379/dump.rdb

Options:

  -h,--help            show this help messageand exit

  -c FILE, --command=FILE

                        Command to execute.Valid commands are json, diff, and

                        protocol

  -f FILE, --file=FILE  Output file

  -n DBS, --db=DBS      Database Number.Multiple databases can be provided.

                        If not specified, alldatabases will be included.

  -k KEYS, --key=KEYS   Keys to export. This can be a regular expression

  -t TYPES, --type=TYPES

                       Data types toinclude. Possible values are string,

                        hash, set, sortedset,list. Multiple typees can be

                        provided.                      If not specified, all

                        data types will bereturned

 

[root@test1 ~]# ll /opt/7001

total 56

-rw-r--r--. 1 root root 41681 Nov 14 22:477001.conf

-rw-r--r--. 1 root root  2662 Nov 14 23:26 7001.log

-rw-r--r--. 1 root root     0 Nov 14 22:53 appendonly.aof

-rw-r--r--. 1 root root    18 Nov 14 23:26 dump_7001.rdb

-rw-r--r--. 1 root root  1029 Nov 15 00:09 nodes-7001.conf

 

[root@test1 ~]# rdb -c memory /opt/7001/dump_7001.rdb > memory.csv   #(生成内存报告)

[root@test1 ~]# sz memory.csv

技术分享

 

转换dump文件为json

#rdb --command json/data/redis_data/6379/dump.rdb

#rdb --command json --key "sences_2.*" /data/redis_data/6379/dump.rdb   #(只解析符合正则的key

#rdb --command json --db 2 --type hash--key "a.*" /data/redis_data/6379/dump.rdb   #(解析DB ID2,类型为hash,以a开头)

 

 

https://docs.saltstack.com   #(远程执行,redismod模块)

 

 

redis主从切换:

一主一从:

#redis-cli -p 6379 shutdown   #(停主;或先在从上执行#redis-cli -p 6380 slaveof NO ONE,再停主)

#redis-cli -p 6380 slaveof NO ONE   #(将从设为主)

#redis-cli -p 6380   #(若之前的主正常了,要切回去,在当前的主上save保存后将dump.rdb文件拷贝覆盖至即将要成为主的主机上)

>save

#redis-server /opt/6379/redis.conf

#redis-cli -p 6380 salveof 192.168.23.1296379

 


 

codis

技术分享

CodisWandoujia Infrastructure Team开发的一个分布式Redis服务,用户可以看成是一个无限内存的Redis服务,有动态扩/缩容的能力.对偏存储型的业务更实用,如果你需要SUBPUB之类的指令,Codis 是不支持的.时刻记住 Codis 是一个分布式存储的项目.对于海量的key, value不太大( <= 1M ),随着业务扩展缓存也要随之扩展的业务场景有特效;Redis获得动态扩容/缩容的能力,增减redis实例对client完全透明、不需要重启服务,不需要业务方担心Redis 内存爆掉的问题.也不用担心申请太大,造成浪费.业务方也不需要自己维护 RedisCodis支持水平扩容/缩容,扩容可以直接界面的"Auto Rebalance"按钮,缩容只需要将要下线的实例拥有的slot迁移到其它实例,然后在界面上删除下线的group即可;

 

我的服务能直接迁移到 Codis 上吗?分两种情况:

1) 原来使用 twemproxy 的用户:可以,使用codis项目内的redis-por工具,可以实时的同步twemproxy底下的redis 数据到你的codis 集群上.搞定了以后,只需要你修改一下你的配置, twemproxy的地址改成codis的地址就好了.除此之外,你什么事情都不用做.

2) 原来使用 Redis 的用户:如果你使用了doc/unsupported_cmds中提到的命令,是无法直接迁移到Codis上的.你需要修改你的代码,用其他的方式实现.

 

codistwemproxy最大的区别有两个:一个是codis支持动态水平扩展,对client完全透明不影响服务的情况下可以完成增减redis实例的操作;一个是codis是用go语言写的并支持多线程,而twemproxyC并只用单线程,意味着:codis在多核机器上的性能会好于twemproxycodis的最坏响应时间可能会因为GCSTW而变大,不过go1.5发布后会显著降低STW的时间;如果只用一个CPU的话go语言的性能不如C,因此在一些短连接而非长连接的场景中,整个系统的瓶颈可能变成accepttcp连接的速度,这时codis的性能可能会差于twemproxy

redis cluster基于smart client和无中心的设计,client必须按key的哈希将请求直接发送到对应的节点,这意味着:使用官方cluster必须要等对应语言的redis drivercluster支持的开发和不断成熟;client不能直接像单机一样使用pipeline来提高效率,想同时执行多个请求来提速必须在client自行实现异步逻辑,而codis因其有中心节点、基于proxy的设计,对client来说可以像对单机redis一样去操作proxy(除了一些命令不支持),还可以继续使用pipeline并且如果后台redis有多个的话速度会显著快于单redispipeline,同时codis使用zookeeper来作为辅助,这意味着单纯对于redis集群来说需要额外的机器搭zk,不过对于很多已经在其他服务上用了zk的公司来说这不是问题;

 

Codis采用Pre-sharding 的技术来实现数据的分片,默认分成1024slots(0-1023),对于每个key来说,通过以下公式确定所属的Slot Id : SlotId = crc32(key) % 1024

每一个slot都会有一个且必须有一个特定的server group id来表示这个slot的数据由哪个 server group来提供,数据的迁移也是以slot为单位的;

Codis还支持LPUSH LPOP这样的指令,但是,并不是说你用了Codis就有了一个分布式队列.对于单个key,它的 value还是会在一台机器上,所以,如果你的队列特别大,而且整个业务就用了几个key,那么就几乎退化成了单个redis,所以,如果你是希望有一个队列服务,那么我建议你:Listkey里存放的是任务id,用另一个key-value pair来存放具体任务信息;使用 Pull 而不是Push,由消费者主动拉取数据,而不是生产者推送数据;监控好你的消费者,当队列过长的时候,及时报警;可以将一个大队列拆分成多个小的队列,放在不同的key中;

 

codis在比较早的版本中MSET/MGET的性能较差,甚至可能差过单机redis,但2.0开始会因为后台并发执行而比单机的mset/mget快很多;

虽然Redis是单线程的,但Codis proxy 是多线程的(严格来说是goroutine),启动的线程数是 CPU的核数,是可以充分利用起多核的性能的;

codisCAS 暂时不支持,目前只支持eval的方式来跑lua脚本,需要配合TAG使用;

codis安全:首先, 安全的定义有很多个级别,Codis 并不是一个多副本的系统(用纯内存来做多副本还是很贵的),如果Codis底下的redis机器没有配从,也没开 bgsave,如果挂了,那么最坏情况下会丢失这部分的数据,但是集群的数据不会全失效(即使这样的,也比以前单点故障,一下全丢的好;如果上一种情况下配了从,这种情况,主挂了,到从切上来这段时间,客户端的部分写入会失败.主从之前没来得及同步的小部分数据会丢失;第二种情况,业务短时间内爆炸性增长,内存短时间内不可预见的暴涨(就和你用数据库磁盘满了一样),Codis还没来得及扩容, 同时数据迁移的速度小于暴涨的速度,此时会触发RedisLRU策略,会淘汰老的Key.这种情况也是无解...不过按照现在的运维经验,我们会尽量预分配一些buffer,内存使用量大概 80% 的时候,我们就会开始扩容;除此之外,正常的数据迁移,扩容缩容,数据都是安全的;

 

Codis是一个分布式Redis解决方案,对于上层的应用来说,连接到CodisProxy和连接原生的 Redis Server没有显著区别(不支持的命令列表),上层应用可以像使用单机的Redis一样使用, Codis底层会处理请求的转发,不停机的数据迁移等工作,所有后边的一切事情,对于前面的客户端来说是透明的,可以简单的认为后边连接的是一个内存无限大的Redis服务;

 

Codis 3.x由以下组件组成:

Codis Server:基于redis-2.8.21分支开发,增加了额外的数据结构,以支持 slot 有关的操作以及数据迁移指令,具体的修改可以参考文档redis的修改;

Codis Proxy:客户端连接的Redis代理服务,实现了Redis协议,除部分命令不支持以外(不支持的命令列表),表现的和原生的Redis没有区别(就像Twemproxy);对于同一个业务集群而言,可以同时部署多个codis-proxy 实例;不同codis-proxy之间由codis-dashboard保证状态同步;

Codis Dashboard:集群管理工具,支持codis-proxycodis-server的添加、删除,以及据迁移等操作,在集群状态发生改变时,codis-dashboard维护集群下所有codis-proxy的状态的一致性;对于同一个业务集群而言,同一个时刻codis-dashboard只能有0个或者1个;所有对集群的修改都必须通过codis-dashboard完成;

Codis Admin:集群管理的命令行工具;可用于控制codis-proxycodis-dashboard状态以及访问外部存储;

Codis FE:集群管理界面;多个集群实例共享可以共享同一个前端展示页面;通过配置文件管理后端codis-dashboard列表,配置文件可自动更新;

Codis HA:为集群提供高可用;依赖codis-dashboard实例,自动抓取集群各个组件的状态;会根据当前集群状态自动生成主从切换策略,并在需要时通过 codis-dashboard 完成主从切换;

Storage:为集群状态提供外部存储;提供Namespace 概念,不同集群的会按照不同product name 进行组织;目前仅提供了ZookeeperEtcd两种实现,但是提供了抽象的interface可自行扩展;

 

操作(codis):

先安装jdk并配置环境变量,zookeeper要用到java环境;

安装go语言环境并配置环境变量,codisgo语言编写的;

笔记140

 

 

 


附:

[root@server1 ~]# vim redis.py

 

#!/usr/bin/python

#

from wsgiref.simple_server importmake_server

import redis

def get_redis():

       r=redis.Redis(host=‘10.96.20.113‘,port=6379,password=‘test‘,db=0)

       r.set(‘key10‘,‘value10‘)

       return r.get(‘key10‘)

defhello_world_app(environ,start_response):

       status=‘200 OK‘

       headers=[(‘Content-type‘,‘text/plain‘)]

       start_response(status,headers)

       retun get_redis()

httpd=make_server(‘‘,8000,hello_world_app)

print ‘Serving on port 8000‘

httpd.server_forever()

 


使用方法

集群 

CLUSTER INFO 打印集群的信息 

CLUSTER NODES 列出集群当前已知的所有节点(node),以及这些节点的相关信息。 

节点 

CLUSTER MEET <ip> <port> ip port 所指定的节点添加到集群当中,让它成为集群的一份子。 

CLUSTER FORGET <node_id> 从集群中移除node_id 指定的节点。 

CLUSTER REPLICATE <node_id> 将当前节点设置为node_id 指定的节点的从节点。 

CLUSTER SAVECONFIG 将节点的配置文件保存到硬盘里面。 

(slot) 

CLUSTER ADDSLOTS <slot> [slot ...] 将一个或多个槽(slot)指派(assign)给当前节点。 

CLUSTER DELSLOTS <slot> [slot ...] 移除一个或多个槽对当前节点的指派。 

CLUSTER FLUSHSLOTS 移除指派给当前节点的所有槽,让当前节点变成一个没有指派任何槽的节点。 

CLUSTER SETSLOT <slot> NODE<node_id> 将槽 slot 指派给 node_id 指定的节点,如果槽已经指派给另一个节点,那么先让另一个节点删除该槽>,然后再进行指派。 

CLUSTER SETSLOT <slot> MIGRATING<node_id> 将本节点的槽 slot 迁移到 node_id 指定的节点中。 

CLUSTER SETSLOT <slot> IMPORTING<node_id> node_id 指定的节点中导入槽 slot 到本节点。 

CLUSTER SETSLOT <slot> STABLE 取消对槽 slot 的导入(import)或者迁移(migrate)。 

 

CLUSTER KEYSLOT <key> 计算键 key 应该被放置在哪个槽上。 

CLUSTER COUNTKEYSINSLOT <slot> 返回槽 slot 目前包含的键值对数量。 

CLUSTER GETKEYSINSLOT <slot><count> 返回 count slot 槽中的键。


本文出自 “Linux运维重难点学习笔记” 博客,请务必保留此出处http://jowin.blog.51cto.com/10090021/1884146

IX redis(2)