本来是打算在一个机器里边全给搭了的。由于docker的特性,同一个机器搭建这个集群,sentinel给了我一个docker容器内部的ip,在windows中无法直接访问他给的内部ip,所以最后弄了两个centos环境搭建。
目前有两个centos
- centos1
192.168.32.129
放置1个redis的master节点
放置sentinel集群 - centos2
192.168.32.133
放置两个slave节点
centos1 master节点
version: '3'
services:
master:
image: redis
container_name: redis-master
command: redis-server --requirepass 123456
ports:
- "6379:6379"
networks:
- custom_net
networks:
custom_net:
external:
name: app_net
这个Master节点可以直接启动 docker-compose up -d
centos2 搭建两个slave节点
version: '3'
services:
slave1:
image: redis
container_name: redis-slave-1
ports:
- "6380:6379"
command: redis-server --slaveof 192.168.32.129 6379 --requirepass 123456 --masterauth 123456
networks:
- custom_net
slave2:
image: redis
container_name: redis-slave-2
ports:
- "6381:6379"
command: redis-server --slaveof 192.168.32.129 6379 --requirepass 123456 --masterauth 123456
networks:
- custom_net
networks:
custom_net:
external:
name: app_net
这俩slave也可以直接启动 docker-compose up -d
centos1 搭建sentinel集群
创建sentinel.conf
port 26379
dir /tmp
sentinel monitor mymaster ilove 6379 2
sentinel auth-pass mymaster 123456
sentinel down-after-milliseconds mymaster 30000
sentinel parallel-syncs mymaster 1
sentinel failover-timeout mymaster 10000
sentinel deny-scripts-reconfig yes
解释下上面的内容… ilove是192.168.32.129的主机名,直接写192.168.32.129应该也是可以的…
123456是配置的redis密码
所以关键是指定了master的访问地址和端口,指定了redis的master密码
然后将这个文件拷贝三份
cp sentinel.conf sentinel1.conf
cp sentinel.conf sentinel2.conf
cp sentinel.conf sentinel3.conf
之后准备docker-compose.yml
version: '3'
services:
sentinel1:
image: redis
container_name: redis-sentinel-1
privileged: true
ports:
- 26379:26379
command: redis-sentinel /usr/local/etc/redis/sentinel.conf
volumes:
- $PWD/sentinel1.conf:/usr/local/etc/redis/sentinel.conf
networks:
- custom_net
sentinel2:
image: redis
container_name: redis-sentinel-2
privileged: true
ports:
- 26380:26379
command: redis-sentinel /usr/local/etc/redis/sentinel.conf
volumes:
- $PWD/sentinel2.conf:/usr/local/etc/redis/sentinel.conf
networks:
- custom_net
sentinel3:
image: redis
container_name: redis-sentinel-3
privileged: true
ports:
- 26381:26379
command: redis-sentinel /usr/local/etc/redis/sentinel.conf
volumes:
- $PWD/sentinel3.conf:/usr/local/etc/redis/sentinel.conf
networks:
- custom_net
networks:
custom_net:
external:
name: app_net
注意前面这几个文件是同级目录
然后启动哨兵 docker-compose up -d
成功日志大概是介样子~
代码连接试试
引入jar
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>
spring:
redis:
timeout: 6000
password: 123456
sentinel:
nodes: ilove:26379,ilove:26380,ilove:26381
master: mymaster
jedis:
pool:
max-active: 1000 # 连接池最大连接数(使用负值表示没有限制)
max-wait: -1ms # 连接池最大阻塞等待时间(使用负值表示没有限制)
max-idle: 10 # 连接池中的最大空闲连接
min-idle: 5 # 连接池中的最小空闲连接
server:
port: 8080
单元测试…
@RunWith(SpringRunner.class)
@SpringBootTest
public class ApplicationTest {
@Autowired
RedisTemplate redisTemplate;
@Test
public void test(){
redisTemplate.opsForValue().set("name","tang");
System.out.println(redisTemplate.opsForValue().get("name"));
}
}