如何在docker中连接Redis与sentinels?

问题描述 投票:5回答:1

我在docker中设置了一个Redis masterslavessentinels,这是我的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时,发现ip地址为10.0.0.0*,这是docker中的ip地址,所以出现了连接异常。

  redis:
    sentinel:
      master: mymaster
      nodes: 127.0.0.1:26379

但是当连接到Redis时,发现ip地址为10.0.0.0.*,这是docker中的ip地址,所以出现了连接异常。

Cannot get Jedis connection; nested exception is redis.clients.jedis.exceptions.JedisConnectionException: Could not get a resource from the pool

请告诉我如何用docker中的sentinels连接Redis.谢谢。

docker redis spring-data-redis
1个回答
0
投票

docker-compose

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

docker文件

FROM maven:3.5-jdk-8-alpine
COPY . /app
© www.soinside.com 2019 - 2024. All rights reserved.