redis安裝,redis c++客戶端,redis cluster叢集測試
redis從2.9.9以後版本及以後開始支援cluster,而當前穩定版本2.8.5還不支援叢集,故本次使用最新開發版本redis測試驗證
1.redis安裝使用
(1)下在redis,https://github.com/antirez/redis/tree/unstable,可以用git下載,也可以直接點右下方的DownloadZip
(2)解壓unzip redis-unstable.zip
(3)編譯,進入redis目錄cdredis-unstable ,make, make完成後可以執行maketest檢視編譯是否成功,可makeinstall安裝,或者將redis-unstable/src
如:exportredis=/home/nohack/software/redis-unstable
exportPATH=$PATH:${redis}/src
(4)測試驗證是否安裝成功
開啟一個終端:輸入redis-server
本次使用預設埠6379,正常顯示如下:
[2939] 08 Feb 16:41:33.207 # Warning: no config file specified, using the default config. In order to specify a config file use redis-server /path/to/redis.conf[2939] 08 Feb 16:41:33.208 * Max number of open files set to 10032 _._ _.-``__ ''-._ _.-`` `. `_. ''-._ Redis 2.9.11 (00000000/0) 64 bit .-`` .-```. ```\/ _.,_ ''-._( ' , .-` | `, ) Running in stand alone mode |`-._`-...-` __...-.``-._|'` _.-'| Port: 6379 | `-._ `._ / _.-' | PID: 2939 `-._ `-._ `-./ _.-' _.-' |`-._`-._ `-.__.-' _.-'_.-'| | `-._`-._ _.-'_.-' | http://redis.io `-._ `-._`-.__.-'_.-' _.-' |`-._`-._ `-.__.-' _.-'_.-'| | `-._`-._ _.-'_.-' | `-._ `-._`-.__.-'_.-' _.-' `-._ `-.__.-' _.-' `-._ _.-' `-.__.-' [2939] 08 Feb 16:41:33.210 # Server started, Redis version 2.9.11 [2939] 08 Feb 16:41:33.210 # WARNING overcommit_memory is set to 0! Background save may fail under low memory condition. To fix this issue add 'vm.overcommit_memory = 1' to /etc/sysctl.conf and then reboot or run the command 'sysctl vm.overcommit_memory=1' for this to take effect. [2939] 08 Feb 16:41:33.219 * DB loaded from disk: 0.009 seconds [2939] 08 Feb 16:41:33.219 * The server is now ready to accept connections on port 6379
開啟一個終端:輸入redis-cli 127.0.0.1:6379> 安裝成功時測試如下: 127.0.0.1:6379> set a1 111 OK 127.0.0.1:6379> set a2 222 OK 127.0.0.1:6379> set a3 333 OK 127.0.0.1:6379> get a1 "111" 127.0.0.1:6379> get a2 "222" 127.0.0.1:6379> get a3 "333" 127.0.0.1:6379>
2.redisc++客戶端
(1)C++例子
#include<stdio.h> #include<stdlib.h> #include<string.h> #include"hiredis.h" intmain(void) { unsignedint j; redisContext *c; redisReply *reply; structtimeval timeout = { 1, 500000 }; // 1.5 seconds c = redisConnectWithTimeout((char*)"127.0.0.2", 6379, timeout); if (c->err) { printf("Connection error: %s\n", c->errstr); exit(1); } /* PING server */ reply = (redisReply*)redisCommand(c,"PING"); printf("PING: %s\n", reply->str); freeReplyObject(reply); /* Set a key */ reply = (redisReply*)redisCommand(c,"SET %s %s", "foo", "hello world"); printf("SET: %s\n", reply->str); freeReplyObject(reply); /* Set a key using binary safe API */ reply = (redisReply*)redisCommand(c,"SET %b %b", "bar", 3, "hello", 5); printf("SET (binary API): %s\n", reply->str); freeReplyObject(reply); /* Try a GET and two INCR */ reply = (redisReply*)redisCommand(c,"GET foo"); printf("GET foo: %s\n", reply->str); freeReplyObject(reply); reply = (redisReply*)redisCommand(c,"INCR counter"); printf("INCR counter: %lld\n", reply->integer); freeReplyObject(reply); /* again ... */ reply = (redisReply*)redisCommand(c,"INCR counter"); printf("INCR counter: %lld\n", reply->integer); freeReplyObject(reply); /* Create a list of numbers, from 0 to 9 */ reply = (redisReply*)redisCommand(c,"DEL mylist"); freeReplyObject(reply); for (j = 0; j < 10; j++) { char buf[64]; snprintf(buf,64,"%d",j); reply = (redisReply*)redisCommand(c,"LPUSH mylist element-%s", buf); freeReplyObject(reply); } /* Let's check what we have inside the list */ reply = (redisReply*)redisCommand(c,"LRANGE mylist 0 -1"); if (reply->type == REDIS_REPLY_ARRAY) { for (j = 0; j < reply->elements; j++) { printf("%u) %s\n", j, reply->element[j]->str); } } freeReplyObject(reply); return 0; }
本次使用eclipse-cdt建立的工程
在標頭檔案路徑中新增:/home/nohack/software/redis-unstable/deps/hiredis
庫目錄:/home/nohack/software/redis-unstable/deps/hiredis
庫名稱:hiredis
編譯執行後輸出內容為:
PING: PONG SET: OK SET (binary API): OK GET foo: hello world INCR counter: 5 INCR counter: 6 0) element-9 1) element-8 2) element-7 3) element-6 4) element-5 5) element-4 6) element-3 7) element-2 8) element-1 9) element-0
3.rediscluster叢集測試
(1)建立叢集配置檔案。
要讓叢集正常運作至少需要三個主節點,不過在剛開始試用叢集功能時,強烈建議使用六個節點:其中三個
為主節點,而其餘三個則是各個主節點的從節點。
首先,進入一個新目錄,並建立6個以埠號為名字的子目錄,
mkdir cluster-test
cdcluster-test
mkdir7000 7001 7002 7003 7004 7005
在檔案7000至7005中,各建立一個redis.conf檔案,檔案內容如下:
port7000
cluster-enabledyes
cluster-config-filenodes.conf
cluster-node-timeout5000
appendonlyyes
但
記得將配置中的埠號從7000改為與資料夾名字相同的號碼
。
(2)啟動多個redis例項。
cdcluster-test/7000
redis-server./redis.conf
[email protected]:/home/nohack/workspace/cluster-test/7000# redis-server ./redis.conf [2547] 08 Feb 16:19:03.069 * Max number of open files set to 10032 [2547] 08 Feb 16:19:03.071 * No cluster configuration found, I'm b80cdc41b44814c377d78d7dc93f33d72e8a00bd _._ _.-``__ ''-._ _.-`` `. `_. ''-._ Redis 2.9.11 (00000000/0) 64 bit .-`` .-```. ```\/ _.,_ ''-._ ( ' , .-` | `, ) Running in cluster mode |`-._`-...-` __...-.``-._|'` _.-'| Port: 7000 | `-._ `._ / _.-' | PID: 2547 `-._ `-._ `-./ _.-' _.-' |`-._`-._ `-.__.-' _.-'_.-'| | `-._`-._ _.-'_.-' | http://redis.io `-._ `-._`-.__.-'_.-' _.-' |`-._`-._ `-.__.-' _.-'_.-'| | `-._`-._ _.-'_.-' | `-._ `-._`-.__.-'_.-' _.-' `-._ `-.__.-' _.-' `-._ _.-' `-.__.-' [2547] 08 Feb 16:19:03.156 # Server started, Redis version 2.9.11 [2547] 08 Feb 16:19:03.156 # WARNING overcommit_memory is set to 0! Background save may fail under low memory condition. To fix this issue add 'vm.overcommit_memory = 1' to /etc/sysctl.conf and then reboot or run the command 'sysctl vm.overcommit_memory=1' for this to take effect. [2547] 08 Feb 16:19:03.157 * The server is now ready to accept connections on port 7000
cdcluster-test/7001
redis-server./redis.conf
…
cdcluster-test/7005
redis-server./redis.conf
(3)建立叢集。
redis-trib.rbcreate --replicas 1 127.0.0.1:7000 127.0.0.1:7001
127.0.0.1:7002127.0.0.1:7003 127.0.0.1:7004 127.0.0.1:7005
可能會出現錯誤:‘require’:nosuch file to load –rubygems(LoadError)
‘require’:nosuch file to load –redis(LoadError)
錯誤原因是ruby模組安裝不全,可以按照如下方法安裝:
geminstall gems
geminstall redis
再次執行redis-trib.rbcreate --replicas 1 127.0.0.1:7000 127.0.0.1:7001
127.0.0.1:7002127.0.0.1:7003 127.0.0.1:7004 127.0.0.1:7005
[email protected]:/home/nohack# redis-trib.rb create --replicas 1 127.0.0.1:7000 127.0.0.1:7001 127.0.0.1:7002 127.0.0.1:7003 127.0.0.1:7004 127.0.0.1:7005 >>> Creating cluster Connecting to node 127.0.0.1:7000: OK Connecting to node 127.0.0.1:7001: OK Connecting to node 127.0.0.1:7002: OK Connecting to node 127.0.0.1:7003: OK Connecting to node 127.0.0.1:7004: OK Connecting to node 127.0.0.1:7005: OK >>> Performing hash slots allocation on 6 nodes... Using 3 masters: 127.0.0.1:7000 127.0.0.1:7001 127.0.0.1:7002 127.0.0.1:7000 replica #1 is 127.0.0.1:7003 127.0.0.1:7001 replica #1 is 127.0.0.1:7004 127.0.0.1:7002 replica #1 is 127.0.0.1:7005 M: b80cdc41b44814c377d78d7dc93f33d72e8a00bd 127.0.0.1:7000 slots:0-5460 (5461 slots) master M: b62f5bbb4d94b550b405236662533ce52140bcaf 127.0.0.1:7001 slots:5461-10921 (5461 slots) master M: c169e4d6a6b1b5e4a05a22cb1cc2669773b03c41 127.0.0.1:7002 slots:10922-16383 (5462 slots) master S: 46f87abbd84c8e39b70be1809047d1c433796d11 127.0.0.1:7003 replicates b80cdc41b44814c377d78d7dc93f33d72e8a00bd S: 276c30e4464202ff2e65be674d779243a15d4350 127.0.0.1:7004 replicates b62f5bbb4d94b550b405236662533ce52140bcaf S: 7fa92edd3d028ae9bab81ebb6fc8e7be0fbdc106 127.0.0.1:7005 replicates c169e4d6a6b1b5e4a05a22cb1cc2669773b03c41 Can I set the above configuration? (type 'yes' to accept): yes
CanI set the above configuration?(type 'yes' to accept):填yes
>>> Nodes configuration updated >>> Sending CLUSTER MEET messages to join the cluster Waiting for the cluster to join.... >>> Performing Cluster Check (using node 127.0.0.1:7000) M: b80cdc41b44814c377d78d7dc93f33d72e8a00bd 127.0.0.1:7000 slots:0-5460 (5461 slots) master M: b62f5bbb4d94b550b405236662533ce52140bcaf 127.0.0.1:7001 slots:5461-10921 (5461 slots) master M: c169e4d6a6b1b5e4a05a22cb1cc2669773b03c41 127.0.0.1:7002 slots:10922-16383 (5462 slots) master M: 46f87abbd84c8e39b70be1809047d1c433796d11 127.0.0.1:7003 slots: (0 slots) master replicates b80cdc41b44814c377d78d7dc93f33d72e8a00bd M: 276c30e4464202ff2e65be674d779243a15d4350 127.0.0.1:7004 slots: (0 slots) master replicates b62f5bbb4d94b550b405236662533ce52140bcaf M: 7fa92edd3d028ae9bab81ebb6fc8e7be0fbdc106 127.0.0.1:7005 slots: (0 slots) master replicates c169e4d6a6b1b5e4a05a22cb1cc2669773b03c41 [OK] All nodes agree about slots configuration. >>> Check for open
All16384 slots covered說明所有槽均被叢集節點覆蓋,叢集建立成功
主節點server7000:
[2547] 08 Feb 16:21:50.236 # Cluster state changed: ok [2547] 08 Feb 16:21:55.228 * Slave asks for synchronization [2547] 08 Feb 16:21:55.228 * Full resync requested by slave. [2547] 08 Feb 16:21:55.228 * Starting BGSAVE for SYNC [2547] 08 Feb 16:21:55.229 * Background saving started by pid 2697 [2697] 08 Feb 16:21:55.314 * DB saved on disk [2697] 08 Feb 16:21:55.314 * RDB: 0 MB of memory used by copy-on-write [2547] 08 Feb 16:21:55.342 * Background saving terminated with success [2547] 08 Feb 16:21:55.342 * Synchronization with slave succeeded
從節點server7003:
[2601] 08 Feb 16:21:52.223 # Cluster state changed: ok [2601] 08 Feb 16:21:55.227 * Connecting to MASTER 127.0.0.1:7000 [2601] 08 Feb 16:21:55.228 * MASTER <-> SLAVE sync started [2601] 08 Feb 16:21:55.228 * Non blocking connect for SYNC fired the event. [2601] 08 Feb 16:21:55.228 * Master replied to PING, replication can continue... [2601] 08 Feb 16:21:55.228 * Partial resynchronization not possible (no cached master) [2601] 08 Feb 16:21:55.229 * Full resync from master: 6a57c0c36c32fa70b17a887f0c1c5a451fccc874:1 [2601] 08 Feb 16:21:55.342 * MASTER <-> SLAVE sync: receiving 18 bytes from master [2601] 08 Feb 16:21:55.342 * MASTER <-> SLAVE sync: Flushing old data [2601] 08 Feb 16:21:55.342 * MASTER <-> SLAVE sync: Loading DB in memory [2601] 08 Feb 16:21:55.342 * MASTER <-> SLAVE sync: Finished with success [2601] 08 Feb 16:21:55.343 * Background append only file rewriting started by pid 2698 [2698] 08 Feb 16:21:55.414 * SYNC append only file rewrite performed [2698] 08 Feb 16:21:55.414 * AOF rewrite: 0 MB of memory used by copy-on-write [2601] 08 Feb 16:21:55.428 * Background AOF rewrite terminated with success [2601] 08 Feb 16:21:55.428 * Parent diff successfully flushed to the rewritten AOF (0 bytes) [2601] 08 Feb 16:21:55.428 * Background AOF rewrite finished successfully
(4)測試叢集。
redis-cli -c -p 7000 127.0.0.1:7000> 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:0 cluster_stats_messages_sent:340 cluster_stats_messages_received:340 127.0.0.1:7000> set a1 111 -> Redirected to slot [7785] located at 127.0.0.1:7001 OK 127.0.0.1:7001> get a1 "111" 127.0.0.1:7001>
(5)C++客戶端測試叢集。
修改程式碼,ip改為127.0.0.1 ,埠改為7000 c = redisConnectWithTimeout((char*)"127.0.0.1", 7000, timeout);
執行結果:
PING: PONG SET: MOVED 12182 127.0.0.1:7002 SET (binary API): OK GET foo: MOVED 12182 127.0.0.1:7002 INCR counter: 0 INCR counter: 0 0) element-9 1) element-8 2) element-7 3) element-6 4) element-5 5) element-4 6) element-3 7) element-2 8) element-1 9) element-0
參考:
http://redis.io/
http://www.redisdoc.com/en/latest/index.html
http://blog.csdn.net/moxiaomomo/article/details/17540813