繁体   English   中英

将连接池与 Jedis 一起使用

[英]Use Connection pool with Jedis

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

当我调用 Web 服务时,我想做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 个线程调用该服务时,我收到异常作为流的意外结束。 我想知道 Jedis 池是线程安全的吗? 无法为它找到特定的解决方案。

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

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

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

查看Spring-data-redis

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

JedisConnectionFactory()使用默认设置(默认连接池,无分片信息)构造一个新的 JedisConnectionFactory 实例。 请参阅文档

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

有关更多信息, 请参阅文档

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM