访次: AmazingCounters.com 次

使用consul实现分布式服务注册和发现--redis篇

安装consul client
consul 客户端检脚本


=============================================================================================
在10.86.5.3 服务器上
=============================================================================================
1、安装redis
yum intstall redis -y
2、配置redis  为主
复制代码
cat >/etc/redis.conf<<EOF
bind 10.86.5.3 127.0.0.1
protected-mode yes
port 6379
tcp-backlog 511
timeout 0
tcp-keepalive 300
daemonize yes
supervised no
pidfile /var/run/redis_6379.pid
loglevel notice
logfile /var/log/redis/redis.log
databases 16
save 900 1
save 300 10
save 60 10000
stop-writes-on-bgsave-error yes
rdbcompression yes
rdbchecksum yes
dbfilename dump.rdb
dir /var/lib/redis
slave-serve-stale-data yes
slave-read-only yes
repl-diskless-sync no
repl-diskless-sync-delay 5
repl-disable-tcp-nodelay no
slave-priority 100
requirepass password
appendonly no
appendfilename "appendonly.aof"
appendfsync everysec
no-appendfsync-on-rewrite no
auto-aof-rewrite-percentage 100
auto-aof-rewrite-min-size 64mb
aof-load-truncated yes
lua-time-limit 5000
slowlog-log-slower-than 10000
slowlog-max-len 128
latency-monitor-threshold 0
notify-keyspace-events ""
hash-max-ziplist-entries 512
hash-max-ziplist-value 64
list-max-ziplist-size -2
list-compress-depth 0
set-max-intset-entries 512
zset-max-ziplist-entries 128
zset-max-ziplist-value 64
hll-sparse-max-bytes 3000
activerehashing yes
client-output-buffer-limit normal 0 0 0
client-output-buffer-limit slave 256mb 64mb 60
client-output-buffer-limit pubsub 32mb 8mb 60
hz 10
aof-rewrite-incremental-fsync yes
EOF
复制代码
3、启动redis
service redis start
4、配置consul服务

复制代码
[root@MySQL /etc/consul]# cat >redis-master.json<<EOF 
{ 
  "service": [
  {
    "name": "6379-redis-m",
    "tags": ["master"],
    "address": "10.86.5.3",
    "port": 6379,
    "checks": [
      { 
        "args": ["/home/consul/check_master.sh","0"],
        "interval": "5s"
      }
    ]
  }
 ] 
}
EOF
复制代码

 

复制代码
[root@MySQL /etc/consul]# cat >redis-slave.json<<EOF 
{ 
  "service": [
  {
    "name": "6379-redis-s",
    "tags": ["slave"],
    "address": "10.86.5.3",
    "port": 6379,
    "checks": [
      { 
        "args": ["/data/consul/check_slave.sh","0"],
        "interval": "5s"
      }
    ]
  }
 ] 
}
EOF
复制代码
5、主从检测脚本

[root@tomcatconsul]/#cat  >/data/consul/check_master.sh<<EOF 
#!/bin/bash
/usr/bin/redis-cli -h 127.0.0.1 -p 6379 -a password info | grep role:master
a=$?
if [ $a == 1 ];then
  exit 2
fi
EOF
[root@tomcat consul]/#cat >/data/consul/check_slave.sh<<EOF
#!/bin/bash
/usr/bin/redis-cli -h 127.0.0.1 -p 6379 -a password info | grep role:slave
a=$?
if [ $a == 1 ];then
  exit 2
fi
EOF
更改启动命令

nohup /usr/local/bin/consul agent -config-dir=/etc/consul -rejoin -enable-script-checks=true -ui &
====================================================================
在10.86.5.4服务器上
====================================================================
1、安装redis
yum intstall redis -y
2、配置10.86.5.4上为10.86.5.3 redis的从
复制代码
cat >/etc/redis.conf<<EOF
bind 10.86.5.4 127.0.0.1
protected-mode yes
port 6379
tcp-backlog 511
timeout 0
tcp-keepalive 300
daemonize yes
supervised no
pidfile /var/run/redis_6379.pid
loglevel notice
logfile /var/log/redis/redis.log
databases 16
save 900 1
save 300 10
save 60 10000
stop-writes-on-bgsave-error yes
rdbcompression yes
rdbchecksum yes
dbfilename dump.rdb
dir /var/lib/redis
slaveof 10.86.4.3 6379
masterauth password
slave-serve-stale-data yes
slave-read-only yes
repl-diskless-sync no
repl-diskless-sync-delay 5
repl-disable-tcp-nodelay no
slave-priority 100
requirepass password
appendonly no
appendfilename "appendonly.aof"
appendfsync everysec
no-appendfsync-on-rewrite no
auto-aof-rewrite-percentage 100
auto-aof-rewrite-min-size 64mb
aof-load-truncated yes
lua-time-limit 5000
slowlog-log-slower-than 10000
slowlog-max-len 128
latency-monitor-threshold 0
notify-keyspace-events ""
hash-max-ziplist-entries 512
hash-max-ziplist-value 64
list-max-ziplist-size -2
list-compress-depth 0
set-max-intset-entries 512
zset-max-ziplist-entries 128
zset-max-ziplist-value 64
hll-sparse-max-bytes 3000
activerehashing yes
client-output-buffer-limit normal 0 0 0
client-output-buffer-limit slave 256mb 64mb 60
client-output-buffer-limit pubsub 32mb 8mb 60
hz 10
aof-rewrite-incremental-fsync yes
EOF
复制代码
3、配置consul服务
主从检测脚本

[root@tomcat consul]/#cat  >/data/consul/check_master.sh<<EOF 
#!/bin/bash
/usr/bin/redis-cli -h 127.0.0.1 -p 6379 -a password info | grep role:master
a=$?
if [ $a == 1 ];then
  exit 2
fi
EOF
[root@tomcat consul]/#cat >/data/consul/check_slave.sh<<EOF
#!/bin/bash
/usr/bin/redis-cli -h 127.0.0.1 -p 6379 -a password info | grep role:slave
a=$?
if [ $a == 1 ];then
  exit 2
fi
EOF
4、consul配置文件

复制代码
[root@MySQL-slave /etc/consul]# cat >/etc/consul/consul_config.json<<EOF 
{
    "advertise_addr": "10.86.5.4",
    "bind_addr": "10.86.5.4",
    "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": [
        "xx.xx.xx.xx",
        "xx.xx.xx.xx",
        "xx.xx.xx.xx"
    ],

    "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
复制代码
复制代码
[root@MySQL /etc/consul]# cat >/etc/consul/redis-master.json<<EOF 
{ 
  "service": [
  {
    "name": "6379-redis-m",
    "tags": ["master"],
    "address": "10.86.5.4",
    "port": 6379,
    "checks": [
      { 
        "args": ["/data/consul/check_master.sh","0"],
        "interval": "5s"
      }
    ]
  }
 ] 
}
EOF
复制代码
复制代码
[root@MySQL /etc/consul]# cat >/etc/consul/redis-slave.json<<EOF 
{ 
  "service": [
  {
    "name": "6379-redis-s",
    "tags": ["slave"],
    "address": "10.86.5.4",
    "port": 6379,
    "checks": [
      { 
        "args": ["/data/consul/check_slave.sh","0"],
        "interval": "5s"
      }
    ]
  }
 ] 
}
EOF
复制代码
5、启动consul客户端服务

nohup consul agent -config-dir /etc/consul/ -data-dir /data/consul/data -enable-script-checks=true  &

 

 
 
 
 
posted @   IT老登  阅读(7)  评论(0编辑  收藏  举报
(评论功能已被禁用)
编辑推荐:
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
· 浏览器原生「磁吸」效果!Anchor Positioning 锚点定位神器解析
· 没有源码,如何修改代码逻辑?
阅读排行:
· 分享4款.NET开源、免费、实用的商城系统
· 全程不用写代码,我用AI程序员写了一个飞机大战
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· 白话解读 Dapr 1.15:你的「微服务管家」又秀新绝活了
· 上周热点回顾(2.24-3.2)
访次: AmazingCounters.com 次
点击右上角即可分享
微信分享提示