万一主机在Redis Sentinel配置中关闭,则无法连接到从机,所有主机都在Docker容器中运行

时间:2020-05-23 01:39:48

标签: java spring-boot docker redis redis-sentinel

我有1个主机(6379),1个从属(6380)和3个在docker容器上运行的哨兵(26379、26380、26381)连接了主仆和从属兵,甚至哨兵都在提供有关主从的正确信息。

当我同时对主服务器和从服务器进行查询时,它运行良好,并且我可以通过暴露的端口看到相同的数据(主服务器和从服务器之间的同步很好)。

当我暂停master时,哨兵动作并使奴隶成为新的master,但是这一次,当我尝试通过我的代码访问redis时,它无法连接,并且Redis命令超时,嵌套异常抛出io.lettuce.core.RedisCommandTimeoutException。

docker-compose文件

services:
  db:
    image: mysql:5.7
    restart: always
    environment:
      - MYSQL_DATABASE=inward
      # So you don't have to use root, but you can if you like
      #- MYSQL_USER=test
      # You can use whatever password you like
      #- MYSQL_PASSWORD=test
      # Password for root access
      - MYSQL_ROOT_PASSWORD=unroot
    ports:
      # <Port exposed> : < MySQL Port running inside container>
      - 3309:3306
  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
    depends_on:
      - db
    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"

and application.properties文件是

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

请帮助我。

1 个答案:

答案 0 :(得分:0)

links在docker中已弃用。将所有依赖的容器保留在depends_on:中。例如:

替换

links:
      - master
      - slave

与此:

depends_on:
      - master
      - slave

它确实满足您的需求。

相关问题