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

Error while trying to configure redis-sentinel URI #31

Open
282ori opened this issue Nov 4, 2023 · 1 comment
Open

Error while trying to configure redis-sentinel URI #31

282ori opened this issue Nov 4, 2023 · 1 comment

Comments

@282ori
Copy link

282ori commented Nov 4, 2023

Hi,
Im trying to use the redis connector in Openshift.
I need to connect to redis-sentinel through openshift Service resource. my configuration:
redis.uri=redis-sentinel://my-redis-sentinel-service:my-port/0?timeout=some-timeout&sentinelMasterId=myMasterId
when I tried that I received this error:
"Cannot build a RedisURI. One of the following must be provided Host, Socket or Sentinel"
also tried to configure:
redis.uri=redis-sentinel://my-redis-sentinel-service:my-port?timeout=some-timeout&sentinelMasterId=myMasterId (no default db)
redis.uri=redis-sentinel://my-redis-sentinel-service:my-port/0?timeout=some-timeout&sentinelMasterId=myMasterId
and also tried without the 'timeout'
Does anyone know what can cause this problem?

@rwas2505
Copy link

rwas2505 commented Feb 21, 2025

Running into this as well. The issue seems to be that sentinel connections can be a bit more nuanced. I have a redis setup where the sentinel password is different than the master redis password and the below code is needed to use the sentinel to get the master and then conduct operations:

    RedisURI uri = RedisURI.Builder.sentinel("host1.net", 26379)
      .withSentinel("host2.net", 26379)
      .withSentinel("host3.net", 26379)
      .withSentinelMasterId("mymaster")
      .withPassword("master-password") // master password, not sentinel
      .withDatabase(0)
      .build();

    // Add sentinel authentication to each sentinel node
    for (RedisURI sentinelUri : uri.getSentinels()) {
      sentinelUri.setPassword("sentinel-password");  // Sentinel node password
    }

    RedisClient client = RedisClient.create(uri);
    try {
      // First connect to sentinel to get master info
      StatefulRedisSentinelConnection<String, String> sentinelConnection = client.connectSentinel();
      RedisSentinelCommands<String, String> sentinelCommands = sentinelConnection.sync();

      // Get master info which includes host and port
      Map<String, String> masterInfo = sentinelCommands.master("mymaster");
      String masterHost = masterInfo.get("ip");
      int masterPort = Integer.parseInt(masterInfo.get("port"));

      // Create a new URI specifically for the master
      RedisURI masterUri = RedisURI.builder()
        .withHost(masterHost)
        .withPort(masterPort)
        .withPassword("master-password")  // Master password
        .withDatabase(0)
        .build();

      // Connect to master directly with authentication
      StatefulRedisConnection<String, String> masterConnection = client.connect(masterUri);
      RedisCommands<String, String> commands = masterConnection.sync();

      // Now you can execute Redis commands
      commands.set("mykey", "myvalue");
      String value = commands.get("mykey");
      System.out.println("Retrieved value: " + value);

      // Clean up connections
      masterConnection.close();
      sentinelConnection.close();

    } catch (Exception e) {
      e.printStackTrace();
    } finally {
      // Shutdown the client when done
      client.shutdown();
    }

Reference: redis/lettuce#1232

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants