如何在码头工程中连接Redis和哨兵?

时间:2017-10-03 11:41:01

标签: docker redis spring-data-redis

我从docker设置了一个Redis master / slaves / sentinels,这是我的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

我想将Redis连接到docker之外,我使用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与哨兵连接起来。 感谢

1 个答案:

答案 0 :(得分: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"

applicationproperties文件

spring.redis.sentinel.master=mymaster
spring.redis.sentinel.nodes=sentinel:26379,sentinel1:26380,sentinel2:26381

dockerfile

FROM maven:3.5-jdk-8-alpine
COPY . /app