SpringBoot 对接redis哨兵集群

Redis哨兵集群

使用Redis哨兵集群,主要是为了高可用,这次部署是,一主,二从,三哨兵。主可写可读,从可读,哨兵不提供读写服务,只是监控

一主


  • 主要配置密码,端口,和可访问的ip
1
2
3
4
5
6
7
8
#任何ip
bind 0.0.0.0
#端口 6666
port 6666
#作为主节点的密码 是 redis
masterauth redis
#密码 redis
requirepass redis

upload successful

二从


  • ip,只读,主信息等
1
2
3
4
5
6
7
8
9
10
11
12
13
bind 0.0.0.0
port 6664
#port 6665
#主节点的ip和端口
slaveof host.docker.internal 6666
# 从节点只读 当被选取为主节点是这个是停止的
slave-read-only yes
# 从节点在处于快照同步期间是否对外提供服务
slave-serve-stale-data yes
# master的认证口令
masterauth redis
# 密码
requirepass redis

upload successful

三哨兵


  • ip,主信息,选举等
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
bind 0.0.0.0
port 26661
# port 26661 port 26662 port 26663
#开启域名解析
sentinel resolve-hostnames yes
#保留域名(程序可以获取到域名)
sentinel announce-hostnames yes
# 主服务信息 ip 端口 以及几个哨兵认为主服务挂了
sentinel monitor mymaster host.docker.internal 6666 2
# 主 密码
sentinel auth-pass mymaster redis
sentinel down-after-milliseconds mymaster 15000
sentinel parallel-syncs mymaster 1
sentinel failover-timeout mymaster 80000
#密码
requirepass redis

upload successful

docker-compose启动

  • 结合docker 启动
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
version: "3"
services:
masterRedis:
container_name: "masterRedis"
image: redis
restart: always
ports:
- 6666:6666
volumes:
- "D:\\env\\study\\redis\\master-6666\\redis.conf:/etc/redis/redis.conf"
command: redis-server /etc/redis/redis.conf
extra_hosts:
- "host.docker.internal:host-gateway"
#network_mode: host
slave:
container_name: "slave"
image: redis
restart: always
ports:
- 6665:6665
volumes:
- "D:\\env\\study\\redis\\slave-6665\\redis-6665.conf:/etc/redis/redis.conf"
command: redis-server /etc/redis/redis.conf
depends_on:
- masterRedis
extra_hosts:
- "host.docker.internal:host-gateway"
slave2:
container_name: "slave2"
image: redis
restart: always
ports:
- 6664:6664
volumes:
- "D:\\env\\study\\redis\\slave-6664\\redis-6664.conf:/etc/redis/redis.conf"
command: redis-server /etc/redis/redis.conf
depends_on:
- masterRedis
extra_hosts:
- "host.docker.internal:host-gateway"
sentinel:
container_name: "sentinel"
image: redis
restart: always
ports:
- 26663:26663
volumes:
- "D:\\env\\study\\redis\\sentinel-26663\\sentinel-26663.conf:/etc/redis/redis.conf"
command: redis-server /etc/redis/redis.conf --sentinel
depends_on:
- masterRedis
- slave
- slave2
extra_hosts:
- "host.docker.internal:host-gateway"
sentinel2:
container_name: "sentinel2"
image: redis
restart: always
ports:
- 26662:26662
volumes:
- "D:\\env\\study\\redis\\sentinel-26662\\sentinel-26662.conf:/etc/redis/redis.conf"
command: redis-server /etc/redis/redis.conf --sentinel
depends_on:
- masterRedis
- slave
- slave2
extra_hosts:
- "host.docker.internal:host-gateway"
sentinel3:
container_name: "sentinel3"
image: redis
restart: always
ports:
- 26661:26661
volumes:
- "D:\\env\\study\\redis\\sentinel-26661\\sentinel-26661.conf:/etc/redis/redis.conf"
command: redis-server /etc/redis/redis.conf --sentinel
#networks:
# extnetwork:
# ipv4_address: 10.2.21.6
depends_on:
- masterRedis
- slave
- slave2
extra_hosts:
- "host.docker.internal:host-gateway"
#networks:
# extnetwork:
# driver: bridge
# ipam:
# config:
# - subnet: 10.2.21.0/16

SpringBoot介入

  • 配置文件
1
2
3
4
5
6
7
8
#主节点的名称
spring.redis.sentinel.master=mymaster
# 三个哨兵的地址
spring.redis.sentinel.nodes=127.0.0.1:26663,127.0.0.1:26662,127.0.0.1:26661
# 哨兵如果有密码,配置哨兵密码
spring.redis.sentinel.password=redis
# 主节点的密码
spring.redis.password=redis
  • 一些坑
  • docker 部署的服务,给程序返回的主节点ip是虚拟ip,所以我这里用了域名,防止给程序一个根本连接不上的地址
  • 为什么我用了docker,我还是要我本地的端口和容器端口一致,也是为了,如果主节点挂掉,哨兵给的从节点地址和端口,我spring程序是宿主机上的服务,想要连接上,尽量保证一致(虽然没什么卵用,因为主关了,哨兵给程序的新的redis的ip是 docker的内网地址)
  • 哨兵上配置主的时候,竟然默认不支持域名的解析,需要增加如下两个配置
    1
    2
    3
    4
    #开启域名解析
    sentinel resolve-hostnames yes
    #保留域名(程序可以获取到域名)
    sentinel announce-hostnames yes