Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Using Spring boot Lettuce cannot connect Redis sentinel #2786

Open
new-biee opened this issue Mar 13, 2024 · 1 comment
Open

Using Spring boot Lettuce cannot connect Redis sentinel #2786

new-biee opened this issue Mar 13, 2024 · 1 comment
Labels
status: waiting-for-feedback We need additional information before we can continue

Comments

@new-biee
Copy link

new-biee commented Mar 13, 2024

I configured Redis Sentinel using docker-compose and used spring boot to connect to redis sentinel

Docker-compose

redis-master:
    image: 'redis:latest'
    environment:
      - REDIS_REPLICATION_MODE=master
      - REDIS_PASSWORD=admin
      - ALLOW_EMPTY_PASSWORD=yes
    command: "redis-server /etc/redis.conf"
    user: root
    volumes:
      - "./master.conf:/etc/redis.conf"
    ports:
      - "6379:6379"
    networks:
      mss-dev:
        aliases:
          - redis-master
        ipv4_address: 192.168.1.100

  redis-slave:
    image: 'redis:latest'
    command: "redis-server /etc/redis.conf --slaveof redis-master 6379 --masterauth admin"
    user: root
    environment:
      - REDIS_REPLICATION_MODE=slave
      - REDIS_MASTER_HOST=127.0.0.1
    volumes:
      - "./slave.conf:/etc/redis.conf"
    depends_on:
      - redis-master
    deploy:
      replicas: 2
    networks:
      mss-dev:
        aliases:
          - redis-slave
      
  redis-sentinel-1:
    image: 'redis:latest'
    command:   bash -c 
      "echo bind 127.0.0.1 > sentinel.conf &&
      echo 'port 26379' >> sentinel.conf &&
      echo 'dir /tmp' >> sentinel.conf &&
      echo 'protected-mode no' >> sentinel.conf &&
      echo 'sentinel monitor mymaster redis-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 &&
      echo 'sentinel auth-pass mymaster admin' >> sentinel.conf &&
      echo 'sentinel resolve-hostnames yes' >> sentinel.conf &&
      cat sentinel.conf &&
      redis-server sentinel.conf --sentinel"
    environment:
      - REDIS_MASTER_HOST=127.0.0.1
    volumes:
      - "./sentinel/sentinel.conf:/etc/redis.conf"
    depends_on:
      - redis-master
      - redis-slave
    ports:
      - 26379:26379      
    networks:
      mss-dev:
        aliases:
          - redis-sentinel

  redis-sentinel-2:
    image: 'redis:latest'
    command:   bash -c 
      "echo bind 127.0.0.1 > sentinel.conf &&
      echo 'port 26379' >> sentinel.conf &&
      echo 'dir /tmp' >> sentinel.conf &&
      echo 'protected-mode no' >> sentinel.conf &&
      echo 'sentinel monitor mymaster redis-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 &&
      echo 'sentinel auth-pass mymaster admin' >> sentinel.conf &&
      echo 'sentinel resolve-hostnames yes' >> sentinel.conf &&
      cat sentinel.conf &&
      redis-server sentinel.conf --sentinel"
    environment:
      - REDIS_MASTER_HOST=127.0.0.1
    volumes:
      - "./sentinel/sentinel.conf:/etc/redis.conf"
    depends_on:
      - redis-master
      - redis-slave
    ports:
      - 26380:26379
    networks:
      mss-dev:
        aliases:
          - redis-sentinel

  redis-sentinel-3:
    image: 'redis:latest'
    command:   bash -c 
      "echo bind 127.0.0.1 > sentinel.conf &&
      echo 'port 26379' >> sentinel.conf &&
      echo 'dir /tmp' >> sentinel.conf &&
      echo 'protected-mode no' >> sentinel.conf &&
      echo 'sentinel monitor mymaster redis-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 &&
      echo 'sentinel auth-pass mymaster admin' >> sentinel.conf &&
      echo 'sentinel resolve-hostnames yes' >> sentinel.conf &&
      cat sentinel.conf &&
      redis-server sentinel.conf --sentinel"
    environment:
      - REDIS_MASTER_HOST=127.0.0.1
    volumes:
      - "./sentinel/sentinel.conf:/etc/redis.conf"
    depends_on:
      - redis-master
      - redis-slave
    ports:
      - 26381:26379
    networks:
      mss-dev:
        aliases:
          - redis-sentinel

slave.conf

bind 127.0.0.1
protected-mode no
port 6379
replicaof redis-master 6379
masterauth admin
requirepass admin

master.conf

bind 127.0.0.1
protected-mode no
requirepass admin
appendonly yes

application.yml

spring
  cache:
    type: redis
    redis:
      time-to-live: 600
  redis:
    database: 0
    sentinel:
      master: mymaster
      password: admin
      nodes:
        - 127.0.0.1:26379
        - 127.0.0.1:26380
        - 127.0.0.1:26381
    lettuce:
      shutdown-timeout: 20000ms

RedisConfig

@Bean
    public LettuceConnectionFactory connectionFactor() {
        LettuceClientConfiguration clientConfig = LettuceClientConfiguration.builder()
            .readFrom(ReadFrom.REPLICA_PREFERRED)
            .build();
        RedisSentinelConfiguration sentinelConfig = new RedisSentinelConfiguration()
            .master(redisProperties.getSentinel().getMaster());
        sentinelConfig.master("mymaster");
        sentinelConfig.sentinel("127.0.0.1", 26379);
        sentinelConfig.sentinel("127.0.0.1", 26380);
        sentinelConfig.sentinel("127.0.0.1", 26381);
        sentinelConfig.setPassword(RedisPassword.of(redisProperties.getPassword()));
        return new LettuceConnectionFactory(sentinelConfig, clientConfig);
    }

log.txt

Please help me!

@new-biee new-biee reopened this Mar 14, 2024
@tishun
Copy link
Collaborator

tishun commented May 24, 2024

Hey @new-biee ,

Cannot connect Redis Sentinel at redis://127.0.0.1:26380: java.util.concurrent.CompletionException: io.lettuce.core.RedisConnectionException: Connection closed prematurely

This message indicates there are connectivity issues with the current setup you are using.
Are you able to connect to Redis with some other client, such as redis-cli or Redis Insight?

If you can't establish a connection successfully with another client then the issue is not in Lettuce, but in your environment.

@tishun tishun added the status: waiting-for-feedback We need additional information before we can continue label May 24, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
status: waiting-for-feedback We need additional information before we can continue
Projects
None yet
Development

No branches or pull requests

2 participants