物联网架构成长之路(11)-Redis缓存主从复制

1. 说明

  在我的物联网平台框架框架中,会用到Redis这个中间件。作为EMQ权限认证的缓存。https://www.cnblogs.com/think-in-java/p/5123884.html 

2. 编译&运行

1 wget http://download.redis.io/releases/redis-4.0.6.tar.gz
2 make && make test && make PREFIX=/home/user/workspace/redis install

  编译,测试,安装

1 cp ./redis.conf /home/user/workspace/redis
2 cd /home/user/workspace/redis
3 ./bin/redis-server
4 ps -c redis-server

3. 主从复制

  一台MASTER(172.16.20.229) 配置,从默认配置拷贝一份,然后修改如下配置项

1 port 6379 #这个根据需要,选择默认的6379端口即可
2 bind 0.0.0.0 #这个绑定,如果是单机测试那么就不用改,如果是多机测试,这个bind选项要注释掉,注释后表示redis监听所有网卡,或者绑定 0.0.0.0
3 requirepass 123456 #密码表示所有连接都要进行授权

  另外一台 SLAVE(172.16.20.203) 配置,同样从默认配置拷贝一份,然后修改如下配置项

1 port 6379  #
2 slaveof 172.16.20.203 6379  #
3 masterauth 123456  #
4 requirepass 123456  #

  配置后,先启动MASTER然后启动SLAVE,可以看到MASTER日志信息如下

  然后通过任意客户端 ./redis-cli -h 172.16.20.229  ./redis-cli -h 172.16.20.203 连接授权后,两者的操作就都是在操作同一份数据了。这样就可以简单的在应用层实现读写分离了。

4. HA高可用

  Redis 算是一个比较流行的中间件了。本身提供了上面的主从复制功能,同时也提供了HA高可用功能。Sentinel哨兵进程。对于普通的企业应用来说,这个功能就够了。

  Redis内置的主从复制和高可用会以来redis.confsentinel.conf 这两个配置文件,而且还会在系统主从选举等操作时修改这两个配置文件

  172.16.20.229 redis.conf MASTER

 1 protected-mode yes
 2 tcp-backlog 511
 3 timeout 0
 4 tcp-keepalive 300
 5 daemonize no
 6 supervised no
 7 pidfile "/var/run/redis_6379.pid"
 8 loglevel notice
 9 logfile ""
10 databases 16
11 always-show-logo yes
12 save 900 1
13 save 300 10
14 save 60 10000
15 stop-writes-on-bgsave-error yes
16 rdbcompression yes
17 rdbchecksum yes
18 dbfilename "dump.rdb"
19 dir "/home/user/workspace/emq/redis/bin"
20 slave-serve-stale-data yes
21 slave-read-only yes
22 repl-diskless-sync no
23 repl-diskless-sync-delay 5
24 repl-disable-tcp-nodelay no
25 slave-priority 100
26 lazyfree-lazy-eviction no
27 lazyfree-lazy-expire no
28 lazyfree-lazy-server-del no
29 slave-lazy-flush no
30 appendonly no
31 appendfilename "appendonly.aof"
32 appendfsync everysec
33 no-appendfsync-on-rewrite no
34 auto-aof-rewrite-percentage 100
35 auto-aof-rewrite-min-size 64mb
36 aof-load-truncated yes
37 aof-use-rdb-preamble no
38 lua-time-limit 5000
39 slowlog-log-slower-than 10000
40 slowlog-max-len 128
41 latency-monitor-threshold 0
42 notify-keyspace-events ""
43 hash-max-ziplist-entries 512
44 hash-max-ziplist-value 64
45 list-max-ziplist-size -2
46 list-compress-depth 0
47 set-max-intset-entries 512
48 zset-max-ziplist-entries 128
49 zset-max-ziplist-value 64
50 hll-sparse-max-bytes 3000
51 activerehashing yes
52 client-output-buffer-limit normal 0 0 0
53 client-output-buffer-limit slave 256mb 64mb 60
54 client-output-buffer-limit pubsub 32mb 8mb 60
55 hz 10
56 aof-rewrite-incremental-fsync yes
57 
58 bind 0.0.0.0
59 port 6379
60 requirepass 123456
61 masterauth 123456

  172.16.23.203 redis.conf SLAVE

 

 1 protected-mode yes
 2 tcp-backlog 511
 3 timeout 0
 4 tcp-keepalive 300
 5 daemonize no
 6 supervised no
 7 pidfile "/var/run/redis_6379.pid"
 8 loglevel notice
 9 logfile ""
10 databases 16
11 always-show-logo yes
12 save 900 1
13 save 300 10
14 save 60 10000
15 stop-writes-on-bgsave-error yes
16 rdbcompression yes
17 rdbchecksum yes
18 dbfilename "dump.rdb"
19 dir "/root/workspace/emq/redis/bin"
20 slave-serve-stale-data yes
21 slave-read-only yes
22 repl-diskless-sync no
23 repl-diskless-sync-delay 5
24 repl-disable-tcp-nodelay no
25 slave-priority 100
26 lazyfree-lazy-eviction no
27 lazyfree-lazy-expire no
28 lazyfree-lazy-server-del no
29 slave-lazy-flush no
30 appendonly no
31 appendfilename "appendonly.aof"
32 appendfsync everysec
33 no-appendfsync-on-rewrite no
34 auto-aof-rewrite-percentage 100
35 auto-aof-rewrite-min-size 64mb
36 aof-load-truncated yes
37 aof-use-rdb-preamble no
38 lua-time-limit 5000
39 slowlog-log-slower-than 10000
40 slowlog-max-len 128
41 latency-monitor-threshold 0
42 notify-keyspace-events ""
43 hash-max-ziplist-entries 512
44 hash-max-ziplist-value 64
45 list-max-ziplist-size -2
46 list-compress-depth 0
47 set-max-intset-entries 512
48 zset-max-ziplist-entries 128
49 zset-max-ziplist-value 64
50 hll-sparse-max-bytes 3000
51 activerehashing yes
52 client-output-buffer-limit normal 0 0 0
53 client-output-buffer-limit slave 256mb 64mb 60
54 client-output-buffer-limit pubsub 32mb 8mb 60
55 hz 10
56 aof-rewrite-incremental-fsync yes
57 
58 bind 0.0.0.0
59 port 6379
60 slaveof 172.16.20.229 6379
61 masterauth 123456
62 requirepass 123456
View Code

  172.16.23.205 redis.conf SLAVE

 1 protected-mode yes
 2 tcp-backlog 511
 3 timeout 0
 4 tcp-keepalive 300
 5 daemonize no
 6 supervised no
 7 pidfile "/var/run/redis_6379.pid"
 8 loglevel notice
 9 logfile ""
10 databases 16
11 always-show-logo yes
12 save 900 1
13 save 300 10
14 save 60 10000
15 stop-writes-on-bgsave-error yes
16 rdbcompression yes
17 rdbchecksum yes
18 dbfilename "dump.rdb"
19 dir "/root/workspace/emq/redis/bin"
20 slave-serve-stale-data yes
21 slave-read-only yes
22 repl-diskless-sync no
23 repl-diskless-sync-delay 5
24 repl-disable-tcp-nodelay no
25 slave-priority 100
26 lazyfree-lazy-eviction no
27 lazyfree-lazy-expire no
28 lazyfree-lazy-server-del no
29 slave-lazy-flush no
30 appendonly no
31 appendfilename "appendonly.aof"
32 appendfsync everysec
33 no-appendfsync-on-rewrite no
34 auto-aof-rewrite-percentage 100
35 auto-aof-rewrite-min-size 64mb
36 aof-load-truncated yes
37 aof-use-rdb-preamble no
38 lua-time-limit 5000
39 slowlog-log-slower-than 10000
40 slowlog-max-len 128
41 latency-monitor-threshold 0
42 notify-keyspace-events ""
43 hash-max-ziplist-entries 512
44 hash-max-ziplist-value 64
45 list-max-ziplist-size -2
46 list-compress-depth 0
47 set-max-intset-entries 512
48 zset-max-ziplist-entries 128
49 zset-max-ziplist-value 64
50 hll-sparse-max-bytes 3000
51 activerehashing yes
52 client-output-buffer-limit normal 0 0 0
53 client-output-buffer-limit slave 256mb 64mb 60
54 client-output-buffer-limit pubsub 32mb 8mb 60
55 hz 10
56 aof-rewrite-incremental-fsync yes
57 
58 bind 0.0.0.0
59 port 6379
60 slaveof 172.16.20.229 6379
61 masterauth 123456
62 requirepass 123456
View Code

  172.16.20.229 sentinel.conf SENTINEL1

 

1 port 26379
2 
3 sentinel monitor mymaster 172.16.20.229 6379 1
4 sentinel auth-pass mymaster 123456
5 sentinel down-after-milliseconds mymaster 11000
6 sentinel failover-timeout mymaster 10000

  172.16.23.204 sentinel.conf SENTINEL2

1 port 26379
2 
3 sentinel monitor mymaster 172.16.20.229 6379 1
4 sentinel auth-pass mymaster 123456

  创建一个MASTER两个SLAVE两个SENTINEL

5. 实际测试

  (1) 启动 172.16.20.229 redis.conf MASTER

  (2) 启动 172.16.23.203 redis.conf SLAVE

  (3) 启动 172.16.23.205 redis.conf SLAVE

  (4) 启动 172.16.23.204 sentinel.conf SENTINEL

  (5) 启动 172.16.20.229 sentinel.conf SENTINEL

  (6) 上面所有服务都启动后,各个服务的日志信息

  172.16.20.229 redis master

  172.16.23.203 redis slave

  172.16.23.205 redis slave

  172.16.23.204 sentinel

  172.16.23.229 sentinel

  (7) 查看状态 ./redis-cli -p 26379

  (8) 然后可以模拟各种故障情况,看一下效果,一顿操作过后,所有服务都关闭了。再去看各个服务下的配置文件,都会发现,每个服务conf配置文件最下面都会增加一些节点信息。

  (9) 例如关闭172.16.23.229 redis Master ,这个时候

6. 关于HA说明

  好像这个sentinel模式可以做到简单的主从恢复,和主从切换。但是对于HA来说,好像不是那么回事,该挂的节点,还是挂了,对于客户端来说,不是很透明。

  有些人的做法是通过DNS进行解决。这个问题先放着,我觉得还是要等到后面Spring Cloud 学了之后,弄个全局配置中心,来动态获取这些数据,只对配置中心和Nginx负载均衡器做HA,其他的再说。

posted @ 2018-01-20 00:06  无脑仔的小明  阅读(2297)  评论(0编辑  收藏  举报