如何将 Redis 与 docker 中的哨兵连接起来?
Posted
技术标签:
【中文标题】如何将 Redis 与 docker 中的哨兵连接起来?【英文标题】:How to connect Redis with sentinels in docker? 【发布时间】:2018-03-14 14:25:48 【问题描述】:我从 docker 设置了一个 Redis 主/从/哨兵,这是我的 docker-compose.yml
redis-master:
image: redis:3
ports:
- 6380:6379
redis-slave:
image: redis:3
ports:
- 6381:6379
command: redis-server --slaveof redis-master 6379
deploy:
replicas: 2
redis-sentinel:
image: mengli/redis-sentinel
ports:
- 26379:26379
deploy:
replicas: 3
environment:
- MASTER_HOST=redis-mater
- SENTINEL_PORT=26379
- SENTINEL_QUORUM=2
我想从docker连接Redis,我使用spring-data-redis,这是我的配置:
redis:
sentinel:
master: mymaster
nodes: 127.0.0.1:26379
但是在连接Redis的时候,发现ip地址为10.0.0.*,是docker中的ip地址,所以抛出了连接异常。
Cannot get Jedis connection; nested exception is redis.clients.jedis.exceptions.JedisConnectionException: Could not get a resource from the pool
请告诉我如何将 Redis 与 docker 外的哨兵连接起来。 谢谢
【问题讨论】:
我也有类似的问题。解决方法是什么? 你有办法解决这个问题吗? 【参考方案1】:码头工人撰写
services:
master:
image: redis
ports:
- 6379:6379
slave:
image: redis
command: >
bash -c "echo 'port 6380' > slave.conf &&
echo 'replicaof master 6379' >> slave.conf &&
cat slave.conf &&
redis-server slave.conf"
links:
- master
ports:
- 6380:6380
sentinel:
image: redis
command: >
bash -c "echo 'port 26379' > sentinel.conf &&
echo 'dir /tmp' >> sentinel.conf &&
echo 'sentinel monitor mymaster master 6379 2' >> sentinel.conf &&
echo 'sentinel down-after-milliseconds mymaster 5000' >> sentinel.conf &&
echo 'sentinel parallel-syncs mymaster 1' >> sentinel.conf &&
echo 'sentinel failover-timeout mymaster 5000' >> sentinel.conf &&
cat sentinel.conf &&
redis-server sentinel.conf --sentinel"
links:
- master
- slave
ports:
- 26379:26379
sentinel1:
image: redis
command: >
bash -c "echo 'port 26380' > sentinel.conf &&
echo 'dir /tmp' >> sentinel.conf &&
echo 'sentinel monitor mymaster master 6379 2' >> sentinel.conf &&
echo 'sentinel down-after-milliseconds mymaster 5000' >> sentinel.conf &&
echo 'sentinel parallel-syncs mymaster 1' >> sentinel.conf &&
echo 'sentinel failover-timeout mymaster 5000' >> sentinel.conf &&
cat sentinel.conf &&
redis-server sentinel.conf --sentinel"
links:
- master
- slave
ports:
- 26380:26380
sentinel2:
image: redis
command: >
bash -c "echo 'port 26381' > sentinel.conf &&
echo 'dir /tmp' >> sentinel.conf &&
echo 'sentinel monitor mymaster master 6379 2' >> sentinel.conf &&
echo 'sentinel down-after-milliseconds mymaster 5000' >> sentinel.conf &&
echo 'sentinel parallel-syncs mymaster 1' >> sentinel.conf &&
echo 'sentinel failover-timeout mymaster 5000' >> sentinel.conf &&
cat sentinel.conf &&
redis-server sentinel.conf --sentinel"
links:
- master
- slave
ports:
- 26381:26381
app:
build:
context: ./
dockerfile: Dockerfile
links:
- master
- slave
- sentinel
- sentinel1
- sentinel2
working_dir: /app
command: [sh, -c, 'mkdir -p ~/logs/; cd /src ; mvn clean spring-boot:run -Dspring.profiles.active=local -DLOG_DIR=/root/logs/ -DLOG_FILE=hubstamper.log']
ports:
- 8080:8080
volumes:
- "$HOME/.m2:/root/.m2"
应用程序属性文件
spring.redis.sentinel.master=mymaster
spring.redis.sentinel.nodes=sentinel:26379,sentinel1:26380,sentinel2:26381
码头文件
FROM maven:3.5-jdk-8-alpine
COPY . /app
【讨论】:
以上是关于如何将 Redis 与 docker 中的哨兵连接起来?的主要内容,如果未能解决你的问题,请参考以下文章
多服务器使用Docker设置一主一从三哨兵redis(完整)