安装consul client
consul 客户端检脚本
=============================================================================================
在192.168.56.122服务器上
=============================================================================================
1、安装redis
yum intstall redis -y
2、配置redis  为主
cat >/etc/redis.conf<<EOF
bind 192.168.56.122 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-slave /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 [root@MySQL-slave /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、主从检测脚本
[root@tomcat103 17:30:04 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@tomcat103 17:30:18 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 &
====================================================================
在192.168.56.122服务器上
====================================================================
1、安装redis
yum intstall redis -y
2、配置192.168.56.132上为192.168.56.122 redis的从
cat >/etc/redis.conf<<EOF
bind 192.168.56.132 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 192.168.56.122 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@tomcat103 17:30:04 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@tomcat103 17:30:18 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": "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 [root@MySQL-slave /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 [root@MySQL-slave /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客户端服务  nohup consul agent -config-dir /etc/consul/ -data-dir /data/consul/data -enable-script-checks=true -ui &
 
6、 在122服务器上ping 域名
测试高可用性 
ping 6379-redis-m.service.consul
ping 6379-redis-s.service.consul
(推荐使用dig @192.168.56.120 -p 53 6379-redis-m.service.consul 命令测试 )
 

==================================================================================
更改两个服务器redis配置
 互换主从角色
在122上打开着两行注释
slaveof 192.168.56.132 6379
masterauth password
重启redis
service redis restart
在132上
#slaveof 192.168.56.122 6379 #masterauth password
重启服务
service redis restart
 
重新测试域名对应的数据库主从地址
 ping 6379-redis-m.service.consul
 ping 6379-redis-s.service.consul
 

至此consul通过脚本自动发现redis主从高可用完成

使用consul实现分布式服务注册和发现--redis篇的更多相关文章

  1. 分布式服务注册和发现consul 简要介绍

    Consul是HashiCorp公司推出的开源工具,用于实现分布式系统的服务发现与配置.与其他分布式服务注册与发现的方案,Consul的方案更"一站式",内置了服务注册与发现框 架 ...

  2. Docker+Consul+Registrator 实现服务注册与发现

    Docker+Consul+Registrator实现服务注册与发现 逻辑图 实现nginx节点自动化加入容器IP代理 1.三台Consul agent server作为高可用通过Consul Tem ...

  3. 温故知新,.Net Core遇见Consul(HashiCorp),实践分布式服务注册与发现

    什么是Consul 参考 https://www.consul.io https://www.hashicorp.com 使用Consul做服务发现的若干姿势 ASP.NET Core 发布之后通过命 ...

  4. .Net Core 商城微服务项目系列(二):使用Ocelot + Consul构建具备服务注册和发现功能的网关

    1.服务注册 在上一篇的鉴权和登录服务中分别通过NuGet引用Consul这个包,同时新增AppBuilderExtensions类: public static class AppBuilderEx ...

  5. SpringCloud核心教程 | 第三篇:服务注册与发现 Eureka篇

    Spring Cloud简介 Spring Cloud是一个基于Spring Boot实现的云应用开发工具,它为基于JVM的云应用开发中涉及的配置管理.服务发现.断路器.智能路由.微代理.控制总线.全 ...

  6. Consul之:服务注册与发现

    一.服务的管理(注册与发现)有三种方式: 1:通过配置文件的方式静态注册2:通过HTTP API接口来动态注册(spring cloud使用方式,spring cloud中使用的是consul api ...

  7. Spring Cloud 之 Consul 知识点:服务注册与发现(类似工具:Eureka、ZooKeeper、Etcd)

    资料 网址 springcloud(十三):注册中心 Consul 使用详解 http://ityouknow.com/springcloud/2018/07/20/spring-cloud-cons ...

  8. Spring Boot + Spring Cloud 构建微服务系统(一):服务注册和发现(Consul)

    使用Consul提供注册和发现服务 什么是 Consul Consul 是 HashiCorp 公司推出的开源工具,用于实现分布式系统的服务发现与配置.与其它分布式服务注册与发现的方案,Consul ...

  9. 服务注册和发现(Consul)

    使用Consul提供注册和发现服务 什么是 Consul Consul 是 HashiCorp 公司推出的开源工具,用于实现分布式系统的服务发现与配置.与其它分布式服务注册与发现的方案,Consul ...

随机推荐

  1. docker postgresql 数据库

    1. 使用docker 镜像 获取镜像:docker pull postgres:9.4 启动: docker run --name postgres1 -e POSTGRES_PASSWORD=pa ...

  2. Djiango-富文本编辑器

    借助富文本编辑器,网站的编辑人员能够像使用offfice一样编写出漂亮的.所见即所得的页面.此处以tinymce为例,其它富文本编辑器的使用也是类似的. 在虚拟环境中安装包. pip install ...

  3. stm32flash的读写特性

    在使用stm32自带的flash保存数据时候,如下特点必须知道: 1.必须是先擦除一个扇区,才能写入 2.读数据没有限制 3.写数据必须是2字节,同时写入地址以一定要考虑字节对齐, 4.一般都是在最后 ...

  4. volatile 关键词

    volatile 关键字指示一个字段可以由多个同时执行的线程修改. 出于性能原因,编译器,运行时系统甚至硬件都可能重新排列对存储器位置的读取和写入. 声明了 volatile 的字段不进行这些优化.这 ...

  5. 关于System.MissingMethodException异常

    什么是MissingMethodException 试图动态访问不存在的方法时引发的异常. 继承 Object Exception SystemException MemberAccessExcept ...

  6. 查看.NET应用程序中的异常(上)

    内存转储是查明托管.NET应用程序中异常的原因的一种极好的方法,特别是在生产应用程序中发生异常时.当您在无法使用Visual Studio的应用程序中跟踪异常时,cdb和sos.dll的使用技术就变成 ...

  7. linux命令之------Linux文件系统具体目录

    Linux文件系统具体目录 (1)/  Linux文件系统的入口,也是处于最高一级的目录 (2)/bin  系统所需要的那些命令处于此目录,比如Is,cp,mkdir等命令:功能和/usr/bin类似 ...

  8. 【洛谷】P1449 后缀表达式

    P1449 后缀表达式 分析: 简单的模拟题. 熟练容器stack的话很容易解决. stack,栈,有先进后出的特性. 比如你有一个箱子,你每放进第一个数时,就往箱底放,放第二个数时就在第一个数的上面 ...

  9. 可分离卷积详解及计算量 Basic Introduction to Separable Convolutions

    任何看过MobileNet架构的人都会遇到可分离卷积(separable convolutions)这个概念.但什么是“可分离卷积”,它与标准的卷积又有什么区别?可分离卷积主要有两种类型: 空间可分离 ...

  10. ueditor div style被过滤 解决办法

    上周开发中有用到开源的富文本编辑器UEditor,在使用的过程中遇到了样式被过滤无法显示问题,经过一番折腾终解决,此外,还有一些关于获取前台界面元素的一些总结. 1. UEditor样式被过滤无法显示 ...