【问题标题】:Use Connection pool with Jedis将连接池与 Jedis 一起使用
【发布时间】:2017-11-18 02:08:23
【问题描述】:

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

当我调用网络服务时,我想做 jedis.hmgetjedis.exitshgetALL 等操作。

例如:

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 个线程调用该服务时,我收到一个异常,即 Unexpected end of stream。我想知道 Jedis 池是线程安全的吗?无法找到具体的解决方案。

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

【问题讨论】:

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


    【解决方案1】:
    JedisPool pool = new JedisPool(new JedisPoolConfig(), "localhost", portno, 10000,
                "password");
    

    请看这里:https://github.com/xetorthio/jedis/wiki/Getting-started

    【讨论】:

      【解决方案2】:

      查看 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

      【讨论】:

      • 使用 JedisConnectionFactory 我们必须使用 RedisTemplate 并且 Redistemplate 不支持我正在执行的操作:jedis.hmget("employee:data:" + emp_user_id, "employee_id").get(0) ;和 Jedis.exits(字符串键);如何做到这一点?
      • 我们如何实现jedis.hmget("employee:data:" + emp_user_id, "employee_id").get(0);和 Jedis.exits(字符串键);在java中使用redistemplate
      • 完全可以同时使用JedisConnectionFactoryRedisTemplate。事实上,您的RedisTemplate 可能已经配置了JedisConnectionFactory,为什么不使用它呢?为什么使用JedisConnectionFactory 比使用xetorthio/jedis 内部类解决您最初的问题更糟糕?
      • 我按要求提供了您应该搜索的方向。我不会为您编写代码,因为 Stack Overflow 不是编码服务。
      猜你喜欢
      • 1970-01-01
      • 2018-09-06
      • 2018-10-03
      • 1970-01-01
      • 1970-01-01
      • 2012-01-08
      • 2016-02-03
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多