redis-3主3從1專用哨兵--程式分片配置
阿新 • • 發佈:2018-12-24
本專案採用redis3主3從,程式端分片。
一、redis安裝
1、安裝
vi /etc/resolv.conf
yum -y install gcc openssl-devel pcre-devel zlib-devel openssl pcre wget ruby rubygems vim tree lsof gcc-c++ libstdc++-devel tcl
yum -y install gcc gcc-c++ libstdc++-devel tcl
mkdir /data
cd /data
wget ftp://182.168.1.249/pub/redis/redis-3.0.0.tar.gz
tar xzvf redis-3.0.0.tar.gz -C /usr/local/
mv /usr/local/redis-3.0.0 /usr/local/redis
cd /usr/local/redis/src
make && make test
2、主從配置
主
cd /usr/local/redis/
vi redis.conf
./src/redis-server redis.conf
./src/redis-cli -p 6379
主配置檔案
daemonize yes
port 6379
從
cd /usr/local/redis/
vi redis.conf
./src/redis-server redis.conf
./src/redis-cli -p 16379
從配置檔案
daemonize yes
port 16379
slaveof 192.168.2.81 6379
2、手動切換測試
主
./src/redis-cli -p 6379 shutdown
從
./src/redis-cli -p 6380 slaveof NO ONE
寫入測試
啟動主、回切
./src/redis-cli -p 6380 slaveof 192.168.2.81 6379
3、rdb檔案備份
redis目錄下的dump.rdb 需要備份
二、哨兵配置
1、所有主機都配置
#MyMaster
sentinel monitor MyMaster 192.168.2.81 6379 1
sentinel down-after-milliseconds MyMaster 5000
sentinel failover-timeout MyMaster 900000
sentinel parallel-syncs MyMaster 2
2、啟動並檢視狀態
./src/redis-server sentinel.conf --sentinel
檢視master的狀態
127.0.0.1:26379> sentinel master MyMaster
1) "name"
2) "MyMaster"
3) "ip"
4) "192.168.2.81"
5) "port"
6) "6379"
7) "runid"
8) "f62bd94d135fefd5babe9fd8f29461c5d87cfe83"
9) "flags"
10) "master"
11) "pending-commands"
12) "0"
13) "last-ping-sent"
14) "0"
15) "last-ok-ping-reply"
16) "816"
17) "last-ping-reply"
18) "816"
19) "down-after-milliseconds"
20) "5000"
21) "info-refresh"
22) "7216"
23) "role-reported"
24) "master"
25) "role-reported-time"
26) "289182"
27) "config-epoch"
28) "0"
29) "num-slaves"
30) "1"
31) "num-other-sentinels"
32) "2"
33) "quorum"
34) "1"
35) "failover-timeout"
36) "900000"
37) "parallel-syncs"
38) "2"
檢視salves的狀態
127.0.0.1:26379> sentinel slaves MyMaster
1) 1) "name"
2) "192.168.2.82:16379"
3) "ip"
4) "192.168.2.82"
5) "port"
6) "16379"
7) "runid"
8) "dc32cf6346183d07d1ec2966c0c021df229c995e"
9) "flags"
10) "slave"
11) "pending-commands"
12) "0"
13) "last-ping-sent"
14) "0"
15) "last-ok-ping-reply"
16) "134"
17) "last-ping-reply"
18) "134"
19) "down-after-milliseconds"
20) "5000"
21) "info-refresh"
22) "1448"
23) "role-reported"
24) "slave"
25) "role-reported-time"
26) "372638"
27) "master-link-down-time"
28) "0"
29) "master-link-status"
30) "ok"
31) "master-host"
32) "192.168.2.81"
33) "master-port"
34) "6379"
35) "slave-priority"
36) "100"
37) "slave-repl-offset"
38) "62931"
檢視哨兵的狀態
127.0.0.1:26379> sentinel sentinels MyMaster
1) 1) "name"
2) "192.168.2.82:26379"
3) "ip"
4) "192.168.2.82"
5) "port"
6) "26379"
7) "runid"
8) "e5eed5573a17bbefbc64011383b5347ba1b8a1ef"
9) "flags"
10) "sentinel"
11) "pending-commands"
12) "0"
13) "last-ping-sent"
14) "0"
15) "last-ok-ping-reply"
16) "881"
17) "last-ping-reply"
18) "881"
19) "down-after-milliseconds"
20) "5000"
21) "last-hello-message"
22) "117"
23) "voted-leader"
24) "?"
25) "voted-leader-epoch"
26) "0"
2) 1) "name"
2) "192.168.2.89:26379"
3) "ip"
4) "192.168.2.89"
5) "port"
6) "26379"
7) "runid"
8) "3a6aa4f6a496ac430c253b04eeb8068cfb49388f"
9) "flags"
10) "sentinel"
11) "pending-commands"
12) "0"
13) "last-ping-sent"
14) "0"
15) "last-ok-ping-reply"
16) "881"
17) "last-ping-reply"
18) "881"
19) "down-after-milliseconds"
20) "5000"
21) "last-hello-message"
22) "331"
23) "voted-leader"
24) "?"
25) "voted-leader-epoch"
26) "0"
獲取當前master的地址
127.0.0.1:26379> sentinel get-master-addr-by-name MyMaster
1) "192.168.2.81"
2) "6379
當前log
[ [email protected] redis]# ./src/redis-server sentinel.conf --sentinel
_._
_.-``__ ''-._
_.-`` `. `_. ''-._ Redis 3.0.0 (00000000/0) 64 bit
.-`` .-```. ```\/ _.,_ ''-._
( ' , .-` | `, ) Running in sentinel mode
|`-._`-...-` __...-.``-._|'` _.-'| Port: 26379
| `-._ `._ / _.-' | PID: 4002
`-._ `-._ `-./ _.-' _.-'
|`-._`-._ `-.__.-' _.-'_.-'|
| `-._`-._ _.-'_.-' | http://redis.io
`-._ `-._`-.__.-'_.-' _.-'
|`-._`-._ `-.__.-' _.-'_.-'|
| `-._`-._ _.-'_.-' |
`-._ `-._`-.__.-'_.-' _.-'
`-._ `-.__.-' _.-'
`-._ _.-'
`-.__.-'
4002:X 16 Oct 11:08:00.106 # Sentinel runid is a6e45d444520407ffbbcdbab7207eac60ac8d3d7
4002:X 16 Oct 11:08:00.106 # +monitor master MyMaster 192.168.2.81 6379 quorum 1
4002:X 16 Oct 11:08:01.107 * +slave slave 192.168.2.82:16379 192.168.2.82 16379 @ MyMaster 192.168.2.81 6379
4002:X 16 Oct 11:10:00.513 * +sentinel sentinel 192.168.2.82:26379 192.168.2.82 26379 @ MyMaster 192.168.2.81 6379
4002:X 16 Oct 11:11:12.472 * +sentinel sentinel 192.168.2.89:26379 192.168.2.89 26379 @ MyMaster 192.168.2.81 6379
3、後臺啟動
nohup /usr/local/redis/src/redis-sentinel /usr/local/redis/sentinel.conf >> /var/log/redis_sentinel.log 2>&1 &
tail -f /var/log/redis_sentinel.log
nohup /usr/local/redis/src/redis-sentinel /usr/local/redis/sentinel.conf >> /var/log/redis_sentinel.log 2>&1 &
nohup /usr/local/redis/src/redis-sentinel /usr/local/redis/sentinel1.conf >> /var/log/redis_sentinel1.log 2>&1 &
nohup /usr/local/redis/src/redis-sentinel /usr/local/redis/sentinel2.conf >> /var/log/redis_sentinel2.log 2>&1 &
tail -f /var/log/redis_sentinel.log
tail -f /var/log/redis_sentinel1.log
tail -f /var/log/redis_sentinel2.log
4、切換測試
都ok
三、java連線哨兵
public static void main(String[] args) {
Set<String> sentinels = new HashSet<String>();
sentinels.add(new HostAndPort("192.168.2.81", 26379).toString());
sentinels.add(new HostAndPort("192.168.2.82", 26379).toString());
sentinels.add(new HostAndPort("192.168.2.89", 26379).toString());
JedisSentinelPool sentinelPool = new JedisSentinelPool("MyMaster", sentinels);
System.out.println("Current master: " + sentinelPool.getCurrentHostMaster().toString());
Jedis master = sentinelPool.getResource();
master.auth("pwdisadmin");
master.set("username","cczz");
Jedis master2 = sentinelPool.getResource();
master2.auth("pwdisadmin");
String value = master2.get("username");
System.out.println("username: " + value);
master2.close();
sentinelPool.close();
sentinelPool.destroy();
}
四、交付軟體測試內容
三對主從,一臺專用哨兵
第一對
192.168.2.82 (主) 埠:16379 哨兵埠:26379
192.168.2.81 (從) 埠:6379 哨兵埠:26379
192.168.2.89 (專用哨兵) 哨兵埠:26379
第二對
192.168.2.90 (主) 埠:6379 哨兵埠:26380
192.168.2.91 (從) 埠:16379 哨兵埠:26380
192.168.2.89 (專用哨兵) 哨兵埠:26380
第三對
192.168.2.92 (主) 埠:6379 哨兵埠:26381
192.168.2.93 (從) 埠:16379 哨兵埠:26381
192.168.2.89 (專用哨兵) 哨兵埠:26381
一、redis安裝
1、安裝
vi /etc/resolv.conf
yum -y install gcc openssl-devel pcre-devel zlib-devel openssl pcre wget ruby rubygems vim tree lsof gcc-c++ libstdc++-devel tcl
yum -y install gcc gcc-c++ libstdc++-devel tcl
mkdir /data
cd /data
wget ftp://182.168.1.249/pub/redis/redis-3.0.0.tar.gz
tar xzvf redis-3.0.0.tar.gz -C /usr/local/
mv /usr/local/redis-3.0.0 /usr/local/redis
cd /usr/local/redis/src
make && make test
2、主從配置
主
cd /usr/local/redis/
vi redis.conf
./src/redis-server redis.conf
./src/redis-cli -p 6379
主配置檔案
daemonize yes
port 6379
從
cd /usr/local/redis/
vi redis.conf
./src/redis-server redis.conf
./src/redis-cli -p 16379
從配置檔案
daemonize yes
port 16379
slaveof 192.168.2.81 6379
2、手動切換測試
主
./src/redis-cli -p 6379 shutdown
從
./src/redis-cli -p 6380 slaveof NO ONE
寫入測試
啟動主、回切
./src/redis-cli -p 6380 slaveof 192.168.2.81 6379
3、rdb檔案備份
redis目錄下的dump.rdb 需要備份
二、哨兵配置
1、所有主機都配置
#MyMaster
sentinel monitor MyMaster 192.168.2.81 6379 1
sentinel down-after-milliseconds MyMaster 5000
sentinel failover-timeout MyMaster 900000
sentinel parallel-syncs MyMaster 2
2、啟動並檢視狀態
./src/redis-server sentinel.conf --sentinel
檢視master的狀態
127.0.0.1:26379> sentinel master MyMaster
1) "name"
2) "MyMaster"
3) "ip"
4) "192.168.2.81"
5) "port"
6) "6379"
7) "runid"
8) "f62bd94d135fefd5babe9fd8f29461c5d87cfe83"
9) "flags"
10) "master"
11) "pending-commands"
12) "0"
13) "last-ping-sent"
14) "0"
15) "last-ok-ping-reply"
16) "816"
17) "last-ping-reply"
18) "816"
19) "down-after-milliseconds"
20) "5000"
21) "info-refresh"
22) "7216"
23) "role-reported"
24) "master"
25) "role-reported-time"
26) "289182"
27) "config-epoch"
28) "0"
29) "num-slaves"
30) "1"
31) "num-other-sentinels"
32) "2"
33) "quorum"
34) "1"
35) "failover-timeout"
36) "900000"
37) "parallel-syncs"
38) "2"
檢視salves的狀態
127.0.0.1:26379> sentinel slaves MyMaster
1) 1) "name"
2) "192.168.2.82:16379"
3) "ip"
4) "192.168.2.82"
5) "port"
6) "16379"
7) "runid"
8) "dc32cf6346183d07d1ec2966c0c021df229c995e"
9) "flags"
10) "slave"
11) "pending-commands"
12) "0"
13) "last-ping-sent"
14) "0"
15) "last-ok-ping-reply"
16) "134"
17) "last-ping-reply"
18) "134"
19) "down-after-milliseconds"
20) "5000"
21) "info-refresh"
22) "1448"
23) "role-reported"
24) "slave"
25) "role-reported-time"
26) "372638"
27) "master-link-down-time"
28) "0"
29) "master-link-status"
30) "ok"
31) "master-host"
32) "192.168.2.81"
33) "master-port"
34) "6379"
35) "slave-priority"
36) "100"
37) "slave-repl-offset"
38) "62931"
檢視哨兵的狀態
127.0.0.1:26379> sentinel sentinels MyMaster
1) 1) "name"
2) "192.168.2.82:26379"
3) "ip"
4) "192.168.2.82"
5) "port"
6) "26379"
7) "runid"
8) "e5eed5573a17bbefbc64011383b5347ba1b8a1ef"
9) "flags"
10) "sentinel"
11) "pending-commands"
12) "0"
13) "last-ping-sent"
14) "0"
15) "last-ok-ping-reply"
16) "881"
17) "last-ping-reply"
18) "881"
19) "down-after-milliseconds"
20) "5000"
21) "last-hello-message"
22) "117"
23) "voted-leader"
24) "?"
25) "voted-leader-epoch"
26) "0"
2) 1) "name"
2) "192.168.2.89:26379"
3) "ip"
4) "192.168.2.89"
5) "port"
6) "26379"
7) "runid"
8) "3a6aa4f6a496ac430c253b04eeb8068cfb49388f"
9) "flags"
10) "sentinel"
11) "pending-commands"
12) "0"
13) "last-ping-sent"
14) "0"
15) "last-ok-ping-reply"
16) "881"
17) "last-ping-reply"
18) "881"
19) "down-after-milliseconds"
20) "5000"
21) "last-hello-message"
22) "331"
23) "voted-leader"
24) "?"
25) "voted-leader-epoch"
26) "0"
獲取當前master的地址
127.0.0.1:26379> sentinel get-master-addr-by-name MyMaster
1) "192.168.2.81"
2) "6379
當前log
[
_._
_.-``__ ''-._
_.-`` `. `_. ''-._ Redis 3.0.0 (00000000/0) 64 bit
.-`` .-```. ```\/ _.,_ ''-._
( ' , .-` | `, ) Running in sentinel mode
|`-._`-...-` __...-.``-._|'` _.-'| Port: 26379
| `-._ `._ / _.-' | PID: 4002
`-._ `-._ `-./ _.-' _.-'
|`-._`-._ `-.__.-' _.-'_.-'|
| `-._`-._ _.-'_.-' | http://redis.io
`-._ `-._`-.__.-'_.-' _.-'
|`-._`-._ `-.__.-' _.-'_.-'|
| `-._`-._ _.-'_.-' |
`-._ `-._`-.__.-'_.-' _.-'
`-._ `-.__.-' _.-'
`-._ _.-'
`-.__.-'
4002:X 16 Oct 11:08:00.106 # Sentinel runid is a6e45d444520407ffbbcdbab7207eac60ac8d3d7
4002:X 16 Oct 11:08:00.106 # +monitor master MyMaster 192.168.2.81 6379 quorum 1
4002:X 16 Oct 11:08:01.107 * +slave slave 192.168.2.82:16379 192.168.2.82 16379 @ MyMaster 192.168.2.81 6379
4002:X 16 Oct 11:10:00.513 * +sentinel sentinel 192.168.2.82:26379 192.168.2.82 26379 @ MyMaster 192.168.2.81 6379
4002:X 16 Oct 11:11:12.472 * +sentinel sentinel 192.168.2.89:26379 192.168.2.89 26379 @ MyMaster 192.168.2.81 6379
3、後臺啟動
nohup /usr/local/redis/src/redis-sentinel /usr/local/redis/sentinel.conf >> /var/log/redis_sentinel.log 2>&1 &
tail -f /var/log/redis_sentinel.log
nohup /usr/local/redis/src/redis-sentinel /usr/local/redis/sentinel.conf >> /var/log/redis_sentinel.log 2>&1 &
nohup /usr/local/redis/src/redis-sentinel /usr/local/redis/sentinel1.conf >> /var/log/redis_sentinel1.log 2>&1 &
nohup /usr/local/redis/src/redis-sentinel /usr/local/redis/sentinel2.conf >> /var/log/redis_sentinel2.log 2>&1 &
tail -f /var/log/redis_sentinel.log
tail -f /var/log/redis_sentinel1.log
tail -f /var/log/redis_sentinel2.log
4、切換測試
都ok
三、java連線哨兵
public static void main(String[] args) {
Set<String> sentinels = new HashSet<String>();
sentinels.add(new HostAndPort("192.168.2.81", 26379).toString());
sentinels.add(new HostAndPort("192.168.2.82", 26379).toString());
sentinels.add(new HostAndPort("192.168.2.89", 26379).toString());
JedisSentinelPool sentinelPool = new JedisSentinelPool("MyMaster", sentinels);
System.out.println("Current master: " + sentinelPool.getCurrentHostMaster().toString());
Jedis master = sentinelPool.getResource();
master.auth("pwdisadmin");
master.set("username","cczz");
Jedis master2 = sentinelPool.getResource();
master2.auth("pwdisadmin");
String value = master2.get("username");
System.out.println("username: " + value);
master2.close();
sentinelPool.close();
sentinelPool.destroy();
}
四、交付軟體測試內容
三對主從,一臺專用哨兵
第一對
192.168.2.82 (主) 埠:16379 哨兵埠:26379
192.168.2.81 (從) 埠:6379 哨兵埠:26379
192.168.2.89 (專用哨兵) 哨兵埠:26379
第二對
192.168.2.90 (主) 埠:6379 哨兵埠:26380
192.168.2.91 (從) 埠:16379 哨兵埠:26380
192.168.2.89 (專用哨兵) 哨兵埠:26380
第三對
192.168.2.92 (主) 埠:6379 哨兵埠:26381
192.168.2.93 (從) 埠:16379 哨兵埠:26381
192.168.2.89 (專用哨兵) 哨兵埠:26381