Java client can't connect to Redis Sentinel which is running on localhost












1














I have setup 3 Redis Server and 3 Redis Sentinel instances in my localhost. The servers are running at:



127.0.0.1:6379 // Master
127.0.0.1:6380 // Slave
127.0.0.1:6381 // Slave


and the sentinels are running at:



127.0.0.1:5000
127.0.0.1:5001
127.0.0.1:5002


I have a (Java) client that tries to connect to one of the sentinels and set the keys in redis server:



// import statements

public class RedisPush {

private static final String MASTER_NAME = "mymaster";
private static final String PASSWORD = "foobared";
private static final Set sentinels;
static {
sentinels = new HashSet();
sentinels.add("127.0.0.1:5000");
sentinels.add("127.0.0.1:5001");
sentinels.add("127.0.0.1:5002");
}

public static void pushToRedis() {

Jedis jedis = null;

try {
JedisSentinelPool pool = new JedisSentinelPool(MASTER_NAME, sentinels);
System.out.println("Fetching connection from pool.");
jedis = pool.getResource();
jedis.auth(PASSWORD);
Socket socket = jedis.getClient().getSocket();

System.out.println("Connected to " + socket.getRemoteSocketAddress());
int i = 0;
while (true) {
jedis.set("sentinel_key" + i, "value" + i);
System.out.println(i);
i++;
Thread.sleep(1000);
}

} catch (Exception e) {
e.printStackTrace();
}
finally {
if(jedis != null)
jedis.close();
}

}

public static void main(String args) throws Exception {

while(true) {

pushToRedis();
Thread.sleep(1000);
}
}
}


Initially, my sentinel configuration is as follows (for example, the first sentinel running on port 5000):



bind 127.0.0.1
port 5000
sentinel monitor mymaster 127.0.0.1 6379 2
sentinel down-after-milliseconds mymaster 5000
sentinel failover-timeout mymaster 60000
sentinel auth-pass mymaster foobared


If I try to run my (Java) client, I get the following error:



Nov 23, 2018 1:52:57 AM redis.clients.jedis.JedisSentinelPool initSentinels
INFO: Trying to find master from available Sentinels...
Nov 23, 2018 1:52:57 AM redis.clients.jedis.JedisSentinelPool initSentinels
WARNING: Cannot get master address from sentinel running @ 192.168.0.102:5001. Reason: redis.clients.jedis.exceptions.JedisConnectionException: java.net.ConnectException: Connection refused (Connection refused). Trying next one.
Nov 23, 2018 1:52:57 AM redis.clients.jedis.JedisSentinelPool initSentinels
WARNING: Cannot get master address from sentinel running @ 192.168.0.102:5000. Reason: redis.clients.jedis.exceptions.JedisConnectionException: java.net.ConnectException: Connection refused (Connection refused). Trying next one.
Nov 23, 2018 1:52:57 AM redis.clients.jedis.JedisSentinelPool initSentinels
WARNING: Cannot get master address from sentinel running @ 192.168.0.102:5002. Reason: redis.clients.jedis.exceptions.JedisConnectionException: java.net.ConnectException: Connection refused (Connection refused). Trying next one.
redis.clients.jedis.exceptions.JedisConnectionException: All sentinels down, cannot determine where is mymaster master is running...
at redis.clients.jedis.JedisSentinelPool.initSentinels(JedisSentinelPool.java:180)
at redis.clients.jedis.JedisSentinelPool.<init>(JedisSentinelPool.java:95)
at redis.clients.jedis.JedisSentinelPool.<init>(JedisSentinelPool.java:82)
at redis.clients.jedis.JedisSentinelPool.<init>(JedisSentinelPool.java:70)
at redis.clients.jedis.JedisSentinelPool.<init>(JedisSentinelPool.java:44)
at RedisPush.pushToRedis(RedisPush.java:29)
at RedisPush.main(RedisPush.java:61)


However, if I change my sentinel configuration script to the one below:



# bind 127.0.0.1
port 5000
protected-mode no
sentinel monitor mymaster 127.0.0.1 6379 2
sentinel down-after-milliseconds mymaster 5000
sentinel failover-timeout mymaster 60000
sentinel auth-pass mymaster foobared


the client works perfectly. I don't understand why.



AFAIK, if requirepass is not set in sentinel.conf file AND bind is commented in sentinel.conf file, ONLY then protected-mode will be yes to avoid any client connecting to the sentinel apart from localhost. In my first sentinel configuration, I had the bind command but still it didn't work.



Why does commenting out bind and explicitly setting protected-mode to no works?



P.S. I also tried having both bind 127.0.0.1 and protected-mode no but even that didn't work.










share|improve this question





























    1














    I have setup 3 Redis Server and 3 Redis Sentinel instances in my localhost. The servers are running at:



    127.0.0.1:6379 // Master
    127.0.0.1:6380 // Slave
    127.0.0.1:6381 // Slave


    and the sentinels are running at:



    127.0.0.1:5000
    127.0.0.1:5001
    127.0.0.1:5002


    I have a (Java) client that tries to connect to one of the sentinels and set the keys in redis server:



    // import statements

    public class RedisPush {

    private static final String MASTER_NAME = "mymaster";
    private static final String PASSWORD = "foobared";
    private static final Set sentinels;
    static {
    sentinels = new HashSet();
    sentinels.add("127.0.0.1:5000");
    sentinels.add("127.0.0.1:5001");
    sentinels.add("127.0.0.1:5002");
    }

    public static void pushToRedis() {

    Jedis jedis = null;

    try {
    JedisSentinelPool pool = new JedisSentinelPool(MASTER_NAME, sentinels);
    System.out.println("Fetching connection from pool.");
    jedis = pool.getResource();
    jedis.auth(PASSWORD);
    Socket socket = jedis.getClient().getSocket();

    System.out.println("Connected to " + socket.getRemoteSocketAddress());
    int i = 0;
    while (true) {
    jedis.set("sentinel_key" + i, "value" + i);
    System.out.println(i);
    i++;
    Thread.sleep(1000);
    }

    } catch (Exception e) {
    e.printStackTrace();
    }
    finally {
    if(jedis != null)
    jedis.close();
    }

    }

    public static void main(String args) throws Exception {

    while(true) {

    pushToRedis();
    Thread.sleep(1000);
    }
    }
    }


    Initially, my sentinel configuration is as follows (for example, the first sentinel running on port 5000):



    bind 127.0.0.1
    port 5000
    sentinel monitor mymaster 127.0.0.1 6379 2
    sentinel down-after-milliseconds mymaster 5000
    sentinel failover-timeout mymaster 60000
    sentinel auth-pass mymaster foobared


    If I try to run my (Java) client, I get the following error:



    Nov 23, 2018 1:52:57 AM redis.clients.jedis.JedisSentinelPool initSentinels
    INFO: Trying to find master from available Sentinels...
    Nov 23, 2018 1:52:57 AM redis.clients.jedis.JedisSentinelPool initSentinels
    WARNING: Cannot get master address from sentinel running @ 192.168.0.102:5001. Reason: redis.clients.jedis.exceptions.JedisConnectionException: java.net.ConnectException: Connection refused (Connection refused). Trying next one.
    Nov 23, 2018 1:52:57 AM redis.clients.jedis.JedisSentinelPool initSentinels
    WARNING: Cannot get master address from sentinel running @ 192.168.0.102:5000. Reason: redis.clients.jedis.exceptions.JedisConnectionException: java.net.ConnectException: Connection refused (Connection refused). Trying next one.
    Nov 23, 2018 1:52:57 AM redis.clients.jedis.JedisSentinelPool initSentinels
    WARNING: Cannot get master address from sentinel running @ 192.168.0.102:5002. Reason: redis.clients.jedis.exceptions.JedisConnectionException: java.net.ConnectException: Connection refused (Connection refused). Trying next one.
    redis.clients.jedis.exceptions.JedisConnectionException: All sentinels down, cannot determine where is mymaster master is running...
    at redis.clients.jedis.JedisSentinelPool.initSentinels(JedisSentinelPool.java:180)
    at redis.clients.jedis.JedisSentinelPool.<init>(JedisSentinelPool.java:95)
    at redis.clients.jedis.JedisSentinelPool.<init>(JedisSentinelPool.java:82)
    at redis.clients.jedis.JedisSentinelPool.<init>(JedisSentinelPool.java:70)
    at redis.clients.jedis.JedisSentinelPool.<init>(JedisSentinelPool.java:44)
    at RedisPush.pushToRedis(RedisPush.java:29)
    at RedisPush.main(RedisPush.java:61)


    However, if I change my sentinel configuration script to the one below:



    # bind 127.0.0.1
    port 5000
    protected-mode no
    sentinel monitor mymaster 127.0.0.1 6379 2
    sentinel down-after-milliseconds mymaster 5000
    sentinel failover-timeout mymaster 60000
    sentinel auth-pass mymaster foobared


    the client works perfectly. I don't understand why.



    AFAIK, if requirepass is not set in sentinel.conf file AND bind is commented in sentinel.conf file, ONLY then protected-mode will be yes to avoid any client connecting to the sentinel apart from localhost. In my first sentinel configuration, I had the bind command but still it didn't work.



    Why does commenting out bind and explicitly setting protected-mode to no works?



    P.S. I also tried having both bind 127.0.0.1 and protected-mode no but even that didn't work.










    share|improve this question



























      1












      1








      1







      I have setup 3 Redis Server and 3 Redis Sentinel instances in my localhost. The servers are running at:



      127.0.0.1:6379 // Master
      127.0.0.1:6380 // Slave
      127.0.0.1:6381 // Slave


      and the sentinels are running at:



      127.0.0.1:5000
      127.0.0.1:5001
      127.0.0.1:5002


      I have a (Java) client that tries to connect to one of the sentinels and set the keys in redis server:



      // import statements

      public class RedisPush {

      private static final String MASTER_NAME = "mymaster";
      private static final String PASSWORD = "foobared";
      private static final Set sentinels;
      static {
      sentinels = new HashSet();
      sentinels.add("127.0.0.1:5000");
      sentinels.add("127.0.0.1:5001");
      sentinels.add("127.0.0.1:5002");
      }

      public static void pushToRedis() {

      Jedis jedis = null;

      try {
      JedisSentinelPool pool = new JedisSentinelPool(MASTER_NAME, sentinels);
      System.out.println("Fetching connection from pool.");
      jedis = pool.getResource();
      jedis.auth(PASSWORD);
      Socket socket = jedis.getClient().getSocket();

      System.out.println("Connected to " + socket.getRemoteSocketAddress());
      int i = 0;
      while (true) {
      jedis.set("sentinel_key" + i, "value" + i);
      System.out.println(i);
      i++;
      Thread.sleep(1000);
      }

      } catch (Exception e) {
      e.printStackTrace();
      }
      finally {
      if(jedis != null)
      jedis.close();
      }

      }

      public static void main(String args) throws Exception {

      while(true) {

      pushToRedis();
      Thread.sleep(1000);
      }
      }
      }


      Initially, my sentinel configuration is as follows (for example, the first sentinel running on port 5000):



      bind 127.0.0.1
      port 5000
      sentinel monitor mymaster 127.0.0.1 6379 2
      sentinel down-after-milliseconds mymaster 5000
      sentinel failover-timeout mymaster 60000
      sentinel auth-pass mymaster foobared


      If I try to run my (Java) client, I get the following error:



      Nov 23, 2018 1:52:57 AM redis.clients.jedis.JedisSentinelPool initSentinels
      INFO: Trying to find master from available Sentinels...
      Nov 23, 2018 1:52:57 AM redis.clients.jedis.JedisSentinelPool initSentinels
      WARNING: Cannot get master address from sentinel running @ 192.168.0.102:5001. Reason: redis.clients.jedis.exceptions.JedisConnectionException: java.net.ConnectException: Connection refused (Connection refused). Trying next one.
      Nov 23, 2018 1:52:57 AM redis.clients.jedis.JedisSentinelPool initSentinels
      WARNING: Cannot get master address from sentinel running @ 192.168.0.102:5000. Reason: redis.clients.jedis.exceptions.JedisConnectionException: java.net.ConnectException: Connection refused (Connection refused). Trying next one.
      Nov 23, 2018 1:52:57 AM redis.clients.jedis.JedisSentinelPool initSentinels
      WARNING: Cannot get master address from sentinel running @ 192.168.0.102:5002. Reason: redis.clients.jedis.exceptions.JedisConnectionException: java.net.ConnectException: Connection refused (Connection refused). Trying next one.
      redis.clients.jedis.exceptions.JedisConnectionException: All sentinels down, cannot determine where is mymaster master is running...
      at redis.clients.jedis.JedisSentinelPool.initSentinels(JedisSentinelPool.java:180)
      at redis.clients.jedis.JedisSentinelPool.<init>(JedisSentinelPool.java:95)
      at redis.clients.jedis.JedisSentinelPool.<init>(JedisSentinelPool.java:82)
      at redis.clients.jedis.JedisSentinelPool.<init>(JedisSentinelPool.java:70)
      at redis.clients.jedis.JedisSentinelPool.<init>(JedisSentinelPool.java:44)
      at RedisPush.pushToRedis(RedisPush.java:29)
      at RedisPush.main(RedisPush.java:61)


      However, if I change my sentinel configuration script to the one below:



      # bind 127.0.0.1
      port 5000
      protected-mode no
      sentinel monitor mymaster 127.0.0.1 6379 2
      sentinel down-after-milliseconds mymaster 5000
      sentinel failover-timeout mymaster 60000
      sentinel auth-pass mymaster foobared


      the client works perfectly. I don't understand why.



      AFAIK, if requirepass is not set in sentinel.conf file AND bind is commented in sentinel.conf file, ONLY then protected-mode will be yes to avoid any client connecting to the sentinel apart from localhost. In my first sentinel configuration, I had the bind command but still it didn't work.



      Why does commenting out bind and explicitly setting protected-mode to no works?



      P.S. I also tried having both bind 127.0.0.1 and protected-mode no but even that didn't work.










      share|improve this question















      I have setup 3 Redis Server and 3 Redis Sentinel instances in my localhost. The servers are running at:



      127.0.0.1:6379 // Master
      127.0.0.1:6380 // Slave
      127.0.0.1:6381 // Slave


      and the sentinels are running at:



      127.0.0.1:5000
      127.0.0.1:5001
      127.0.0.1:5002


      I have a (Java) client that tries to connect to one of the sentinels and set the keys in redis server:



      // import statements

      public class RedisPush {

      private static final String MASTER_NAME = "mymaster";
      private static final String PASSWORD = "foobared";
      private static final Set sentinels;
      static {
      sentinels = new HashSet();
      sentinels.add("127.0.0.1:5000");
      sentinels.add("127.0.0.1:5001");
      sentinels.add("127.0.0.1:5002");
      }

      public static void pushToRedis() {

      Jedis jedis = null;

      try {
      JedisSentinelPool pool = new JedisSentinelPool(MASTER_NAME, sentinels);
      System.out.println("Fetching connection from pool.");
      jedis = pool.getResource();
      jedis.auth(PASSWORD);
      Socket socket = jedis.getClient().getSocket();

      System.out.println("Connected to " + socket.getRemoteSocketAddress());
      int i = 0;
      while (true) {
      jedis.set("sentinel_key" + i, "value" + i);
      System.out.println(i);
      i++;
      Thread.sleep(1000);
      }

      } catch (Exception e) {
      e.printStackTrace();
      }
      finally {
      if(jedis != null)
      jedis.close();
      }

      }

      public static void main(String args) throws Exception {

      while(true) {

      pushToRedis();
      Thread.sleep(1000);
      }
      }
      }


      Initially, my sentinel configuration is as follows (for example, the first sentinel running on port 5000):



      bind 127.0.0.1
      port 5000
      sentinel monitor mymaster 127.0.0.1 6379 2
      sentinel down-after-milliseconds mymaster 5000
      sentinel failover-timeout mymaster 60000
      sentinel auth-pass mymaster foobared


      If I try to run my (Java) client, I get the following error:



      Nov 23, 2018 1:52:57 AM redis.clients.jedis.JedisSentinelPool initSentinels
      INFO: Trying to find master from available Sentinels...
      Nov 23, 2018 1:52:57 AM redis.clients.jedis.JedisSentinelPool initSentinels
      WARNING: Cannot get master address from sentinel running @ 192.168.0.102:5001. Reason: redis.clients.jedis.exceptions.JedisConnectionException: java.net.ConnectException: Connection refused (Connection refused). Trying next one.
      Nov 23, 2018 1:52:57 AM redis.clients.jedis.JedisSentinelPool initSentinels
      WARNING: Cannot get master address from sentinel running @ 192.168.0.102:5000. Reason: redis.clients.jedis.exceptions.JedisConnectionException: java.net.ConnectException: Connection refused (Connection refused). Trying next one.
      Nov 23, 2018 1:52:57 AM redis.clients.jedis.JedisSentinelPool initSentinels
      WARNING: Cannot get master address from sentinel running @ 192.168.0.102:5002. Reason: redis.clients.jedis.exceptions.JedisConnectionException: java.net.ConnectException: Connection refused (Connection refused). Trying next one.
      redis.clients.jedis.exceptions.JedisConnectionException: All sentinels down, cannot determine where is mymaster master is running...
      at redis.clients.jedis.JedisSentinelPool.initSentinels(JedisSentinelPool.java:180)
      at redis.clients.jedis.JedisSentinelPool.<init>(JedisSentinelPool.java:95)
      at redis.clients.jedis.JedisSentinelPool.<init>(JedisSentinelPool.java:82)
      at redis.clients.jedis.JedisSentinelPool.<init>(JedisSentinelPool.java:70)
      at redis.clients.jedis.JedisSentinelPool.<init>(JedisSentinelPool.java:44)
      at RedisPush.pushToRedis(RedisPush.java:29)
      at RedisPush.main(RedisPush.java:61)


      However, if I change my sentinel configuration script to the one below:



      # bind 127.0.0.1
      port 5000
      protected-mode no
      sentinel monitor mymaster 127.0.0.1 6379 2
      sentinel down-after-milliseconds mymaster 5000
      sentinel failover-timeout mymaster 60000
      sentinel auth-pass mymaster foobared


      the client works perfectly. I don't understand why.



      AFAIK, if requirepass is not set in sentinel.conf file AND bind is commented in sentinel.conf file, ONLY then protected-mode will be yes to avoid any client connecting to the sentinel apart from localhost. In my first sentinel configuration, I had the bind command but still it didn't work.



      Why does commenting out bind and explicitly setting protected-mode to no works?



      P.S. I also tried having both bind 127.0.0.1 and protected-mode no but even that didn't work.







      java redis jedis redis-sentinel redis-server






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Nov 25 '18 at 10:35

























      asked Nov 22 '18 at 20:36









      Shubham

      1,97221427




      1,97221427





























          active

          oldest

          votes











          Your Answer






          StackExchange.ifUsing("editor", function () {
          StackExchange.using("externalEditor", function () {
          StackExchange.using("snippets", function () {
          StackExchange.snippets.init();
          });
          });
          }, "code-snippets");

          StackExchange.ready(function() {
          var channelOptions = {
          tags: "".split(" "),
          id: "1"
          };
          initTagRenderer("".split(" "), "".split(" "), channelOptions);

          StackExchange.using("externalEditor", function() {
          // Have to fire editor after snippets, if snippets enabled
          if (StackExchange.settings.snippets.snippetsEnabled) {
          StackExchange.using("snippets", function() {
          createEditor();
          });
          }
          else {
          createEditor();
          }
          });

          function createEditor() {
          StackExchange.prepareEditor({
          heartbeatType: 'answer',
          autoActivateHeartbeat: false,
          convertImagesToLinks: true,
          noModals: true,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: 10,
          bindNavPrevention: true,
          postfix: "",
          imageUploader: {
          brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
          contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
          allowUrls: true
          },
          onDemand: true,
          discardSelector: ".discard-answer"
          ,immediatelyShowMarkdownHelp:true
          });


          }
          });














          draft saved

          draft discarded


















          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53437726%2fjava-client-cant-connect-to-redis-sentinel-which-is-running-on-localhost%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown






























          active

          oldest

          votes













          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes
















          draft saved

          draft discarded




















































          Thanks for contributing an answer to Stack Overflow!


          • Please be sure to answer the question. Provide details and share your research!

          But avoid



          • Asking for help, clarification, or responding to other answers.

          • Making statements based on opinion; back them up with references or personal experience.


          To learn more, see our tips on writing great answers.





          Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


          Please pay close attention to the following guidance:


          • Please be sure to answer the question. Provide details and share your research!

          But avoid



          • Asking for help, clarification, or responding to other answers.

          • Making statements based on opinion; back them up with references or personal experience.


          To learn more, see our tips on writing great answers.




          draft saved


          draft discarded














          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53437726%2fjava-client-cant-connect-to-redis-sentinel-which-is-running-on-localhost%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown





















































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown

































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown







          Popular posts from this blog

          Berounka

          Sphinx de Gizeh

          Different font size/position of beamer's navigation symbols template's content depending on regular/plain...