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,make完了後maketestを実行してコンパイルに成功したかどうか,makeinstallインストール,またはredis-unstable/srcディレクトリをpathに追加
例えば、exportredis=/home/nohack/software/redis-unstable exportPATH=$PATH:${redis}/src
(4)テスト検証が正常にインストールされたか
端末を開く:redis-serverを入力
今回はデフォルトのポート6379を使用して、通常は次のように表示されます.
2.redisc++クライアント
(1)C++の例
今回eclipse-cdtを使用して作成したプロジェクト
ヘッダファイルパスに追加:/home/nohack/software/redis-unstable/deps/hiredis
ライブラリディレクトリ:/home/nohack/software/redis-unstable/deps/hiredis
ライブラリ名:hiredis
コンパイル実行後の出力内容は次のとおりです.
3.redisclusterクラスタテスト
(1)クラスタプロファイルを作成する.
クラスタを正常に動作させるには少なくとも3つのプライマリノードが必要ですが、クラスタ機能の試用を開始したばかりのときは、6つのノードを使用することを強くお勧めします.そのうち3つ
プライマリノードであり、残りの3つは各プライマリノードのスレーブノードである.
まず、新しいディレクトリに入り、ポート番号のサブディレクトリを6つ作成します.
mkdir cluster-test
cdcluster-test
mkdir7000 7001 7002 7003 7004 7005
ファイル7000~7005には、それぞれ1つのredisが作成する.confファイル、ファイルの内容は以下の通りです.
port7000
cluster-enabledyes
cluster-config-filenodes.conf
cluster-node-timeout5000
appendonlyyes
しかし
構成中のポート番号を7000からフォルダ名と同じ番号に変更してください.
.
(2)複数のredisインスタンスを起動する.
cdcluster-test/7000
redis-server./redis.conf
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
CanI set the above configuration?(type'yes'to accept):yesを記入
All 16384 slots coveredは、すべてのスロットがクラスタノードによって上書きされ、クラスタの作成に成功したことを示しています.
プライマリノードserver 7000:
ノードserver 7003から:
(4)クラスタをテストする.
(5)C++クライアントテストクラスタ.
実行結果:
参照先:
http://redis.io/
http://www.redisdoc.com/en/latest/index.html
http://blog.csdn.net/moxiaomomo/article/details/17540813
1.redisインストール使用
(1)redisでhttps://github.com/antirez/redis/tree/unstableあ、gitでダウンロードしてもいいし、右下のDownloadZipを直接クリックしてもいいです
(2)unzip redis-unstableを解凍する.zip
(3)コンパイル,redisディレクトリcdredis-unstable,make,make,make完了後maketestを実行してコンパイルに成功したかどうか,makeinstallインストール,またはredis-unstable/srcディレクトリをpathに追加
例えば、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
#include
#include
#include "hiredis.h"
int main(void) {
unsigned int j;
redisContext *c;
redisReply *reply;
struct timeval timeout = { 1, 500000 }; // 1.5 seconds
c = redisConnectWithTimeout((char*)"127.0.0.2", 6379, timeout);
if (c->err) {
printf("Connection error: %s
", c->errstr);
exit(1);
}
/* PING server */
reply = (redisReply*)redisCommand(c,"PING");
printf("PING: %s
", reply->str);
freeReplyObject(reply);
/* Set a key */
reply = (redisReply*)redisCommand(c,"SET %s %s", "foo", "hello world");
printf("SET: %s
", 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
", reply->str);
freeReplyObject(reply);
/* Try a GET and two INCR */
reply = (redisReply*)redisCommand(c,"GET foo");
printf("GET foo: %s
", reply->str);
freeReplyObject(reply);
reply = (redisReply*)redisCommand(c,"INCR counter");
printf("INCR counter: %lld
", reply->integer);
freeReplyObject(reply);
/* again ... */
reply = (redisReply*)redisCommand(c,"INCR counter");
printf("INCR counter: %lld
", 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
", 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)クラスタプロファイルを作成する.
クラスタを正常に動作させるには少なくとも3つのプライマリノードが必要ですが、クラスタ機能の試用を開始したばかりのときは、6つのノードを使用することを強くお勧めします.そのうち3つ
プライマリノードであり、残りの3つは各プライマリノードのスレーブノードである.
まず、新しいディレクトリに入り、ポート番号のサブディレクトリを6つ作成します.
mkdir cluster-test
cdcluster-test
mkdir7000 7001 7002 7003 7004 7005
ファイル7000~7005には、それぞれ1つのredisが作成する.confファイル、ファイルの内容は以下の通りです.
port7000
cluster-enabledyes
cluster-config-filenodes.conf
cluster-node-timeout5000
appendonlyyes
しかし
構成中のポート番号を7000からフォルダ名と同じ番号に変更してください.
.
(2)複数のredisインスタンスを起動する.
cdcluster-test/7000
redis-server./redis.conf
root@inspiron:/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
root@inspiron:/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
All 16384 slots coveredは、すべてのスロットがクラスタノードによって上書きされ、クラスタの作成に成功したことを示しています.
プライマリノードserver 7000:
[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
ノードserver 7003から:
[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