yujin2010good
作者yujin2010good2019-06-10 13:45
系统工程师, 大型零售巨头

redis-3主3从1专用哨兵--程序分片配置

字数 5987阅读 445评论 0赞 3

本项目采用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. "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. "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"
    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
[root@i-e2xkard2 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

如果觉得我的文章对您有用,请点赞。您的支持将鼓励我继续创作!

3

添加新评论0 条评论

Ctrl+Enter 发表

关于TWT  使用指南  社区专家合作  厂商入驻社区  企业招聘  投诉建议  版权与免责声明  联系我们
© 2019  talkwithtrend — talk with trend,talk with technologist 京ICP备09031017号-30