1. 程式人生 > 實用技巧 >使用consul實現分散式服務註冊和發現--redis篇

使用consul實現分散式服務註冊和發現--redis篇

一、前提,請認真閱讀我上篇博文《consul 1.0 server cluster叢集配置全解密

http://412166174.blog.51cto.com/3102369/1975822

本文是上面文章的續集


安裝consulclient
consul客戶端檢指令碼
=============================================================================================
在192.168.56.122伺服器上
=============================================================================================
1、安裝redis
yumintstallredis-y
2、配置redis為主
cat>/etc/redis.conf<<EOF
bind192.168.56.122127.0.0.1
protected-modeyes
port6379
tcp-backlog511
timeout0
tcp-keepalive300
daemonizeyes
supervisedno
pidfile/var/run/redis_6379.pid
loglevelnotice
logfile/var/log/redis/redis.log
databases16
save9001
save30010
save6010000
stop-writes-on-bgsave-erroryes
rdbcompressionyes
rdbchecksumyes
dbfilenamedump.rdb
dir/var/lib/redis
slave-serve-stale-datayes
slave-read-onlyyes
repl-diskless-syncno
repl-diskless-sync-delay5
repl-disable-tcp-nodelayno
slave-priority100
requirepasspassword
appendonlyno
appendfilename"appendonly.aof"
appendfsynceverysec
no-appendfsync-on-rewriteno
auto-aof-rewrite-percentage100
auto-aof-rewrite-min-size64mb
aof-load-truncatedyes
lua-time-limit5000
slowlog-log-slower-than10000
slowlog-max-len128
latency-monitor-threshold0
notify-keyspace-events""
hash-max-ziplist-entries512
hash-max-ziplist-value64
list-max-ziplist-size-2
list-compress-depth0
set-max-intset-entries512
zset-max-ziplist-entries128
zset-max-ziplist-value64
hll-sparse-max-bytes3000
activerehashingyes
client-output-buffer-limitnormal000
client-output-buffer-limitslave256mb64mb60
client-output-buffer-limitpubsub32mb8mb60
hz10
aof-rewrite-incremental-fsyncyes
EOF


3、啟動redis
serviceredisstart

4、配置consul服務
[
[email protected]
/etc/consul]#cat>redis-master.json<<EOF { "service":[ { "name":"6379-redis-m", "tags":["master"], "address":"192.168.56.132", "port":6379, "checks":[ { "args":["/data/consul/check_master.sh","0"], "interval":"5s" } ] } ] } EOF [
[email protected]
/etc/consul]#cat>redis-slave.json<<EOF { "service":[ { "name":"6379-redis-s", "tags":["slave"], "address":"192.168.56.132", "port":6379, "checks":[ { "args":["/data/consul/check_slave.sh","0"], "interval":"5s" } ] } ] } EOF 5、主從檢測指令碼 [
[email protected]
17:30:04consul]/#cat>/data/consul/check_master.sh<<EOF #!/bin/bash /usr/bin/redis-cli-h127.0.0.1-p6379-apasswordinfo|greprole:master a=$? if[$a==1];then exit2 fi EOF [[email protected]17:30:18consul]/#cat>/data/consul/check_slave.sh<<EOF #!/bin/bash /usr/bin/redis-cli-h127.0.0.1-p6379-apasswordinfo|greprole:slave a=$? if[$a==1];then exit2 fi EOF 更改啟動命令 nohup/usr/local/bin/consulagent-config-dir=/etc/consul-rejoin-enable-script-checks=true& ==================================================================== 在192.168.56.122伺服器上 ==================================================================== 1、安裝redis yumintstallredis-y 2、配置192.168.56.132上為192.168.56.122redis的從 cat>/etc/redis.conf<<EOF bind192.168.56.132127.0.0.1 protected-modeyes port6379 tcp-backlog511 timeout0 tcp-keepalive300 daemonizeyes supervisedno pidfile/var/run/redis_6379.pid loglevelnotice logfile/var/log/redis/redis.log databases16 save9001 save30010 save6010000 stop-writes-on-bgsave-erroryes rdbcompressionyes rdbchecksumyes dbfilenamedump.rdb dir/var/lib/redis slaveof192.168.56.1226379 masterauthpassword slave-serve-stale-datayes slave-read-onlyyes repl-diskless-syncno repl-diskless-sync-delay5 repl-disable-tcp-nodelayno slave-priority100 requirepasspassword appendonlyno appendfilename"appendonly.aof" appendfsynceverysec no-appendfsync-on-rewriteno auto-aof-rewrite-percentage100 auto-aof-rewrite-min-size64mb aof-load-truncatedyes lua-time-limit5000 slowlog-log-slower-than10000 slowlog-max-len128 latency-monitor-threshold0 notify-keyspace-events"" hash-max-ziplist-entries512 hash-max-ziplist-value64 list-max-ziplist-size-2 list-compress-depth0 set-max-intset-entries512 zset-max-ziplist-entries128 zset-max-ziplist-value64 hll-sparse-max-bytes3000 activerehashingyes client-output-buffer-limitnormal000 client-output-buffer-limitslave256mb64mb60 client-output-buffer-limitpubsub32mb8mb60 hz10 aof-rewrite-incremental-fsyncyes EOF 3、配置consul服務 主從檢測指令碼 [[email protected]17:30:04consul]/#cat>/data/consul/check_master.sh<<EOF #!/bin/bash /usr/bin/redis-cli-h127.0.0.1-p6379-apasswordinfo|greprole:master a=$? if[$a==1];then exit2 fi EOF [[email protected]17:30:18consul]/#cat>/data/consul/check_slave.sh<<EOF #!/bin/bash /usr/bin/redis-cli-h127.0.0.1-p6379-apasswordinfo|greprole:slave a=$? if[$a==1];then exit2 fi EOF 4、consul配置檔案 [[email protected]/etc/consul]#cat>/etc/consul/consul_config.json<<EOF { "advertise_addr":"192.168.56.132", "bind_addr":"192.168.56.132", "domain":"consul", "datacenter":"consul-cluster", "data_dir":"/data/consul/data", "enable_syslog":true, "performance":{ "raft_multiplier":1 }, "dns_config":{ "allow_stale":true, "max_stale":"15s" }, "retry_join":[ "192.168.56.120", "192.168.56.121", "192.168.56.122" ], "retry_interval":"10s", "skip_leave_on_interrupt":true, "leave_on_terminate":false, "ports":{ "dns":53, "http":80 }, "rejoin_after_leave":true, "addresses":{ "http":"0.0.0.0", "dns":"0.0.0.0" } } EOF [[email protected]/etc/consul]#cat>/etc/consul/redis-master.json<<EOF { "service":[ { "name":"6379-redis-m", "tags":["master"], "address":"192.168.56.132", "port":6379, "checks":[ { "args":["/data/consul/check_master.sh","0"], "interval":"5s" } ] } ] } EOF [[email protected]/etc/consul]#cat>/etc/consul/redis-slave.json<<EOF { "service":[ { "name":"6379-redis-s", "tags":["slave"], "address":"192.168.56.132", "port":6379, "checks":[ { "args":["/data/consul/check_slave.sh","0"], "interval":"5s" } ] } ] } EOF 5、啟動consul客戶端服務 nohupconsulagent-config-dir/etc/consul/-data-dir/data/consul/data-enable-script-checks=true-ui& 6、在122伺服器上ping域名 測試高可用性 ping6379-redis-m.service.consul ping6379-redis-s.service.consul (推薦使用[email protected]命令測試)

2f670e6f262e826e8094a7553e33f286.png

==================================================================================
更改兩個伺服器redis配置
互換主從角色
在122上開啟著兩行註釋
slaveof192.168.56.1326379
masterauthpassword
重啟redis
serviceredisrestart
在132上
#slaveof192.168.56.1226379

#masterauthpassword
重啟服務
serviceredisrestart

重新測試域名對應的資料庫主從地址
ping6379-redis-m.service.consul
ping6379-redis-s.service.consul

2f8586cc3d8c713a9d25f1447bfb8061.png

至此consul通過指令碼自動發現redis主從高可用完成

特別建議使用sentinel 或者redis cluster 實現故障切換,本文利用主從結構只是為了達到實驗目的


轉載於:https://blog.51cto.com/412166174/1976520