Java 与绝地使用连接池
我正在使用绝地武士连接REST服务中的Redis服务器 当我调用web服务时,我想执行一些操作,如绝地.hmget,绝地.exits和hgetALL 例如:Java 与绝地使用连接池,java,multithreading,spring-boot,connection-pooling,jedis,Java,Multithreading,Spring Boot,Connection Pooling,Jedis,我正在使用绝地武士连接REST服务中的Redis服务器 当我调用web服务时,我想执行一些操作,如绝地.hmget,绝地.exits和hgetALL 例如: jedis.hmget("employee:data:" + emp_user_id, "employee_id").get(0); 我用于Redis的配置是: Jedis jedis; JedisShardInfo shardInfo; @PostConstruct public void init() {
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);
}
}
我知道绝地武士不是线程安全的。当我同时使用1000个线程来调用服务时,我得到了一个异常作为流的意外结束。我想知道绝地武士池是线程安全的?无法找到它的特定解决方案
谢谢。任何帮助都将不胜感激。请查看Spring data redis 添加
JedisConnectionFactory
时,会得到一个默认具有连接池功能的connectionFactory
JedisConnectionFactory()
使用默认设置(默认连接池,无碎片信息)构造新的JedisConnectionFactory实例
欲了解更多信息
请参见此处:对于JedisConnectionFactory,我们必须使用RedisTemplate,RedisTemplate不支持我正在执行的操作,如:jedis.hmget(“员工:数据:”+emp\u用户id,“员工id”).get(0);和绝地武士。退出(字符串键);如何做到这一点?检查并确认。我们如何实现jedis.hmget(“雇员:数据:”+emp\u用户id,“雇员id”).get(0);和绝地武士。退出(字符串键);在Java中使用redistemplate完全可以同时使用
JedisConnectionFactory
和redistemplate
。事实上,您的RedisTemplate
可能已经配置了JedisConnectionFactory
,为什么不使用它呢??为什么使用JedisConnectionFactory
内部类来解决您最初的问题会比使用xetorthio/jedis
内部类更糟糕?我提供了您应该搜索的方向。我不会为您编写代码,因为堆栈溢出不是一种编码服务。
<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>
JedisPool pool = new JedisPool(new JedisPoolConfig(), "localhost", portno, 10000,
"password");