redis sentinel配置中,如果主机关闭,无法连接到从机,所有都在docker容器中运行

blpfk2vs  于 2021-06-09  发布在  Redis
关注(0)|答案(1)|浏览(454)

我有1个主(6379),1个从(6380)和3个哨兵(26379,26380,26381)在docker容器上运行主和从是连接的,甚至哨兵给出了正确的主从信息。
当我在主设备和从设备都启动的情况下进行查询时,它运行良好,我可以(通过公开的端口)看到两者中的相同数据(主设备和从设备之间的同步良好)。
当我暂停master时,sentinels动作并使slave成为新的master,但这次当我试图通过代码访问redis时,它无法连接,redis命令超时嵌套的异常是io.莴苣.core.rediscommandtimeoutexception被抛出。
docker编写文件

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"

并且application.properties文件

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

请帮帮我。

ajsxfq5m

ajsxfq5m1#

links 在码头上折旧。把所有的容器都放在里面 depends_on: . 如。:
替换

links:
      - master
      - slave

有了这个:

depends_on:
      - master
      - slave

它正是你想要的。

相关问题