redis-py使用(2)-redis-py-cluster

一.获取redis-py-cluster

redis-py并没有支持Redis Cluster。社区中有人开发了redis-py-cluster(非同一个作者)。两种方法获取。

第一:使用pip进行安装(推荐)

1
pip install redis-py-cluster

第三:使用源码安装(以1.3.4版本为例子,可以在release列表选取需要)

1
2
3
4
wget https://github.com/Grokzen/redis-py-cluster/releases/download/1.3.4/redis-py-cluster-1.3.4.tar.gz
unzip redis-py-cluster-1.3.4.tar.gz
cd redis-py-cluster-1.3.4
python setup.py install

二.redis-py-cluster的基本使用

redis-py-cluster的使用方法也比较简单,我们逐步骤介绍:

1.导入依赖库

1
import rediscluster

2.生成客户端连接:

需要传入Redis Cluster的全部节点(其实一个节点也行,内部会通过一个节点自动获取),例如:

1
2
3
4
startup_nodes = [{"host": "127.0.0.1", "port": "7000"}, {"host": "127.0.0.1", "port": "7001"},
{"host": "127.0.0.1", "port": "7002"}, {"host": "127.0.0.1", "port": "7003"},
{"host": "127.0.0.1", "port": "7004"}, {"host": "127.0.0.1", "port": "7005"}]
clusterClient = rediscluster.StrictRedisCluster(startup_nodes=startup_nodes, decode_responses=True)

3.执行命令

redis-py的API保留了Redis API的原始风格,所以使用起来不会有不习惯的感觉

1
2
clusterClient.set(key, "python-redis")
clusterClient.get(key)

三、集群下的命令

redis-py-cluster中的命令按照执行范围的不同可以分为三种,下面只是简单说明,具体参考官方文档:http://redis-py-cluster.readthedocs.io/en/master/commands.html#

1.Fanout Commands(发送到全部节点)

(1) 一些管理命令

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
bgrewriteaof
bgsave
client_getname
client_kill
client_list
client_setname
config_get
config_resetstat
config_rewrite
config_set
dbsize
echo
info
lastsave
ping
save
slowlog_get
slowlog_len
slowlog_reset
time

(2) 数据命令

1
2
3
4
keys
flushall
flushdb
scan

2.Blocked commands

有些命令是禁止使用的:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
bitop - Currently to hard to implement a solution in python space
client_setname - Not yet implemented
move - It is not possible to move a key from one db to another in cluster mode
restore
script_kill - Not yet implemented
sentinel
sentinel_get_master_addr_by_name
sentinel_master
sentinel_masters
sentinel_monitor
sentinel_remove
sentinel_sentinels
sentinel_set
sentinel_slaves
shutdown
slaveof - Cluster management should be done via redis-trib.rb manually
unwatch - Not yet implemented
watch - Not yet implemented

3.Overridden methods(重写命令)

下面命令在集群下使用会略有不同,具体使用时候可以了解下.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
brpoplpus
mget
mset
msetnx
pfmerge
randomkey
rename
renamenx
rpoplpush
sdiff
sdiffstore
sinter
sinterstore
smove
sort
sunion
sunionstore
zinterstore
zunionstore

4.集群命令:有些命令是在全部节点执行,有些是在一个节点上执行。

(1) cluster info
1
2
3
4
5
6
7
8
9
10
11
import rediscluster
startup_nodes = [{"host": "127.0.0.1", "port": "7000"}, {"host": "127.0.0.1", "port": "7001"},
{"host": "127.0.0.1", "port": "7002"}, {"host": "127.0.0.1", "port": "7003"},
{"host": "127.0.0.1", "port": "7004"}, {"host": "127.0.0.1", "port": "7005"}]
clusterClient = rediscluster.StrictRedisCluster(startup_nodes=startup_nodes, decode_responses=True)
clusterInfos = clusterClient.cluster_info()
for (hostPort,clusterInfo) in clusterInfos.items():
print "=================={hostPort}==================".format(hostPort=hostPort)
for (configKey,configValue) in clusterInfo.items():
print configKey + ":" + str(configValue)

输出结果:

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
/System/Library/Frameworks/Python.framework/Versions/2.7/bin/python2.7 /Users/carlosfu/Desktop/py/py/redis/redis-py-first2.py
==================127.0.0.1:7000==================
cluster_state:ok
cluster_slots_assigned:16384
cluster_known_nodes:6
cluster_slots_fail:0
cluster_stats_messages_received:150148
cluster_size:3
cluster_current_epoch:7
cluster_stats_messages_sent:150146
cluster_slots_pfail:0
cluster_my_epoch:7
cluster_slots_ok:16384
==================127.0.0.1:7001==================
cluster_state:ok
cluster_slots_assigned:16384
cluster_known_nodes:6
cluster_slots_fail:0
cluster_stats_messages_received:155413
cluster_size:3
cluster_current_epoch:7
cluster_stats_messages_sent:155412
cluster_slots_pfail:0
cluster_my_epoch:2
cluster_slots_ok:16384
==================127.0.0.1:7002==================
cluster_state:ok
cluster_slots_assigned:16384
cluster_known_nodes:6
cluster_slots_fail:0
cluster_stats_messages_received:155704
cluster_size:3
cluster_current_epoch:7
cluster_stats_messages_sent:155703
cluster_slots_pfail:0
cluster_my_epoch:3
cluster_slots_ok:16384
==================127.0.0.1:7003==================
cluster_state:ok
cluster_slots_assigned:16384
cluster_known_nodes:6
cluster_slots_fail:0
cluster_stats_messages_received:152197
cluster_size:3
cluster_current_epoch:7
cluster_stats_messages_sent:152205
cluster_slots_pfail:0
cluster_my_epoch:7
cluster_slots_ok:16384
==================127.0.0.1:7004==================
cluster_state:ok
cluster_slots_assigned:16384
cluster_known_nodes:6
cluster_slots_fail:0
cluster_stats_messages_received:147089
cluster_size:3
cluster_current_epoch:7
cluster_stats_messages_sent:147087
cluster_slots_pfail:0
cluster_my_epoch:2
cluster_slots_ok:16384
==================127.0.0.1:7005==================
cluster_state:ok
cluster_slots_assigned:16384
cluster_known_nodes:6
cluster_slots_fail:0
cluster_stats_messages_received:153097
cluster_size:3
cluster_current_epoch:7
cluster_stats_messages_sent:153095
cluster_slots_pfail:0
cluster_my_epoch:3
cluster_slots_ok:16384
Process finished with exit code 0

需要注意的结果是和startup_nodes个数无关的。

(2) cluster nodes
1
2
3
clusterNodes = clusterClient.cluster_nodes();
for clusterNode in clusterNodes:
print clusterNode

输出结果: