将连接池与Jedis一起使用

时间:2017-06-15 14:16:48

标签: java multithreading spring-boot connection-pooling jedis

我正在使用Jedis连接REST服务中的Redis服务器。

当我调用网络服务时,我想执行 jedis.hmget jedis.exits hgetALL 等操作。

例如:

jedis.hmget("employee:data:" + emp_user_id, "employee_id").get(0);

我用于Redis的配置是:

Jedis jedis;

    JedisShardInfo shardInfo;

    @PostConstruct
    public void init() {

        try {

            shardInfo = new JedisShardInfo(Config.getRedisHost(), Config.getRedisPort());
            shardInfo.setPassword(Config.getRedisPassword());
            jedis = new Jedis(shardInfo);
            jedis.select(2);
        //jedis.se
        } catch (Exception e) {
            logger.error("Exception in init ------- > " + e);
        }

    }

我知道Jedis不是线程安全的。当我一次使用1000个线程来调用服务时,我得到一个异常作为意外的流结束。我想知道Jedis池是线程安全的吗?无法为其找到具体的解决方案。

感谢。任何帮助将不胜感激。

2 个答案:

答案 0 :(得分:2)

JedisPool pool = new JedisPool(new JedisPoolConfig(), "localhost", portno, 10000,
            "password");

https://github.com/xetorthio/jedis/wiki/Getting-started this link can resolve the problem

答案 1 :(得分:0)

查看 Spring-data-redis

当您添加JedisConnectionFactory时,您会获得一个默认具有连接池功能的connectionFactory。

  

JedisConnectionFactory()             使用默认设置构造新的JedisConnectionFactory实例(默认连接池,没有分片信息)。 See docs

<beans xmlns="http://www.springframework.org/schema/beans"
  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xmlns:p="http://www.springframework.org/schema/p"
  xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">

    <bean id="jedisConnectionFactory" class="org.springframework.data.redis.connection.jedis.JedisConnectionFactory" p:use-pool="true" p:host-name="server" p:port="6379"/>

</beans>

有关详细信息,请see the documentation

相关问题