如何使用 Jedis 库建立与 Redis Sentinel 的连接? [英] How to setup a connection to Redis Sentinel using Jedis library?

查看:82
本文介绍了如何使用 Jedis 库建立与 Redis Sentinel 的连接?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何使用 Jedis 库建立与 Redis Sentinel 服务器/集群的连接?

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

推荐答案

Jedis 库是一个很棒的解决方案,不幸的是文档很糟糕.

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

所以,

@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);
    }
}

当我使用 Spring 时,我需要:

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>

这篇关于如何使用 Jedis 库建立与 Redis Sentinel 的连接?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆