17

How do I setup a connection to a Redis Sentinel server/cluster using the Jedis library?

Jens
  • 1,379
  • 12
  • 30
ERNESTO ARROYO RON
  • 922
  • 1
  • 8
  • 21

3 Answers3

20

The Jedis library is an awesome solution, unfortunately with bad documentation.

So,

@Autowired private JedisSentinelPool pool;

public void mymethod() {
    Jedis jedis = null;
    try {
        jedis = pool.getResource();
        jedis.hset(....
    } catch (JedisException je) {
        throw je;
    } finally {
        if (jedis != null) pool.returnResource(jedis);
    }
}

As I am using Spring, I need:

<bean id="redisSentinel" class="redis.clients.jedis.JedisSentinelPool">
<constructor-arg index="0" value="mymaster" />
<constructor-arg index="1">
     <set>  
         <value>hostofsentinel:26379</value>  
    </set> 
</constructor-arg>
<constructor-arg index="2" ref="jedisPoolConfig"/>
</bean>
Dale K
  • 16,372
  • 12
  • 37
  • 62
ERNESTO ARROYO RON
  • 922
  • 1
  • 8
  • 21
  • 1
    how to use this for RedisTemplate? – pavan May 06 '14 at 07:43
  • You can use RedisTemplate as the example i shared for this post – ravi ranjan Jun 10 '14 at 18:36
  • Can someone explain why JedisSentinelPool.getResource() doesn't return JedisSentinel but return Jedis instead? – Ray Wu Apr 04 '17 at 06:42
  • @Ray Wu, because you aren't interacting with the sentinel, but rather the redis master that the sentinels track for you. The sentinels just track which masters are available so you can talk to them. – absmiths Dec 11 '18 at 15:18
8

Here's an example when you aren't using Spring and need a simple connection via Jedis to a Redis master/slave set managed by Redis sentinels

public class JedisTestSentinelEndpoint {
    private static final String MASTER_NAME = "mymaster";
    public static final String PASSWORD = "foobared";
    private static final Set sentinels;
    static {
        sentinels = new HashSet();
        sentinels.add("mymaster-0.servers.example.com:26379");
        sentinels.add("mymaster-1.servers.example.com:26379");
        sentinels.add("mymaster-2.servers.example.com:26379");
    }

    public JedisTestSentinelEndpoint() {
    }

    private void runTest() throws InterruptedException {
        JedisSentinelPool pool = new JedisSentinelPool(MASTER_NAME, sentinels);
        Jedis jedis = null;
            try {
                printer("Fetching connection from pool");
                jedis = pool.getResource();
                printer("Authenticating...");
                jedis.auth(PASSWORD);
                printer("auth complete...");
                Socket socket = jedis.getClient().getSocket();
                printer("Connected to " + socket.getRemoteSocketAddress());
                printer("Writing...");
                jedis.set("java-key-999", "java-value-999");
                printer("Reading...");
                jedis.get("java-key-999");
            } catch (JedisException e) {
                printer("Connection error of some sort!");
                printer(e.getMessage());
                Thread.sleep(2 * 1000);
            } finally {
                if (jedis != null) {
                    jedis.close();
                }
            }
    }
...
}

Source: This blog post on connecting to Redis Sentinels.

mmdemirbas
  • 8,486
  • 5
  • 44
  • 51
Vaibhaw
  • 538
  • 8
  • 17
1

Have you tried Redisson? It offer sentinel automatic master/slave/sentinel discovery and topology update and you don't need to handle with connections, data encoding... it's all done by Redisson. Here is the code example:

Config config = new Config();
config.useSentinelServers()
   .setMasterName("mymaster")
   .addSentinelAddress("127.0.0.1:26389", "127.0.0.1:26379")

RedissonClient redisson = Redisson.create(config);

RMap<MyKey, MyValue> map = redisson.getMap("myMap");
map.put(new MyKey(), new MyValue());
mmdemirbas
  • 8,486
  • 5
  • 44
  • 51
Nikita Koksharov
  • 8,672
  • 52
  • 63
  • How could I apply different Redis operations using Radisson, apart from put and get? – Veswanth Aug 28 '17 at 13:46
  • 1
    @VeswanthRaju Here is a documentation how Redis command mapped to Redisson object https://github.com/redisson/redisson/wiki/11.-Redis-commands-mapping – Nikita Koksharov Aug 28 '17 at 13:56