ERNESTO ARROYO RON
ERNESTO ARROYO RON

Reputation: 1012

How can I set up a connection to Redis Sentinel using Jedis library?

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

Upvotes: 18

Views: 29353

Answers (4)

promzeus
promzeus

Reputation: 302

You might not need to integrate Sentinel support into your application at all. With Redis Sentinel Gateway, failovers are handled externally, so no need for complex refactoring or dealing with libraries that poorly support Sentinel. If your code runs in Kubernetes, this solution is perfect as it provides a single entry point for the master node, ensuring smooth failovers.

Upvotes: 0

ERNESTO ARROYO RON
ERNESTO ARROYO RON

Reputation: 1012

The Jedis library is an awesome solution, but 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>

Upvotes: 20

Nikita Koksharov
Nikita Koksharov

Reputation: 10763

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());

Upvotes: 0

Vaibhaw
Vaibhaw

Reputation: 646

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.

Upvotes: 7

Related Questions