How to setup a connection to Redis Sentinel using Jedis library?

ERNESTO ARROYO RON picture ERNESTO ARROYO RON · Oct 15, 2013 · Viewed 19.8k times · Source

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

Answer

ERNESTO ARROYO RON picture ERNESTO ARROYO RON · Oct 18, 2013

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>