[英]Use Connection pool with Jedis
我正在使用 Jedis 連接 REST 服務中的 Redis 服務器。
當我調用 Web 服務時,我想做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 池是線程安全的嗎? 無法為它找到特定的解決方案。
謝謝。 任何幫助將不勝感激。
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.