2016-03-04 82 views
2

這是我第一個使用spring-data-redis的應用程序,我認爲我的概念非常好(過去我多次使用JdbcTemplate與RDBMS-es )。以下是發生了什麼......Spring-data-redis連接池不工作,因爲我期望

我遇到的問題是,每次我執行get(key)操作(使用ValueOperations對象)時,打開和關閉連接會導致大約1/10秒的延遲(這是服務器代碼,因此1/10秒是大量的)。以下是Spring XML配置:

<!-- Redis DAO stuff --> 
<bean 
    id="jedisPoolConfig" 
    class="redis.clients.jedis.JedisPoolConfig" 
    p:testOnBorrow="true" 
    p:testOnReturn="true" 
    p:timeBetweenEvictionRunsMillis="60000" 
    p:minIdle="2" 
    p:maxTotal="30" 
    p:maxIdle="10" 
    /> 

<bean id="jedisConnectionFactory" 
    class="org.springframework.data.redis.connection.jedis.JedisConnectionFactory" 
    p:host-name="${redis.url}" 
    p:port="${redis.port}" 
    p:database="0" 
    p:use-pool="true" 
    p:pool-config-ref="jedisPoolConfig" 
/> 

<bean id="stringRedisSerializer" 
    class="org.springframework.data.redis.serializer.StringRedisSerializer" 
/> 

<bean id="redisTemplate" 
    class="org.springframework.data.redis.core.RedisTemplate" 
    p:connection-factory-ref="jedisConnectionFactory" 
    p:defaultSerializer-ref="stringRedisSerializer" 
/> 

這裏是相關的Java代碼:

@Autowired 
private RedisTemplate<String, String> template; 
private ValueOperations<String, String> valOps; 

@PostConstruct 
public void init() { 
    logger.debug("111111111111111111111111aaaaaaaaaaaaaaaaaaaaaaa"); 
    valOps = template.opsForValue(); 
} 

public String getTestVal() { 
    logger.debug("getTestVal() function called++++++++++++++++++++"); 
    Object testVal2 = valOps.get("akey"); 
    testVal2 = valOps.get("akey"); 
    testVal2 = valOps.get("akey"); 
    testVal2 = valOps.get("akey"); 
    testVal2 = valOps.get("akey"); 
    testVal2 = valOps.get("akey"); 

    logger.debug("TestVal2 returned from REdis: " + testVal2); 

    return null; 
} 

因此,對於相同的密鑰值被檢索到六倍。日誌輸出,我看到的是如下:

13:46:37.011 [http-nio-8080-exec-1] DEBUG com.arrow.pricing.dao.RedisDAO - getTestVal() function called++++++++++++++++++++ 
13:46:37.014 [http-nio-8080-exec-1] DEBUG o.s.d.r.core.RedisConnectionUtils - Opening RedisConnection 
13:46:37.344 [http-nio-8080-exec-1] DEBUG o.s.d.r.core.RedisConnectionUtils - Closing Redis Connection 
13:46:37.416 [http-nio-8080-exec-1] DEBUG o.s.d.r.core.RedisConnectionUtils - Opening RedisConnection 
13:46:37.543 [http-nio-8080-exec-1] DEBUG o.s.d.r.core.RedisConnectionUtils - Closing Redis Connection 
13:46:37.616 [http-nio-8080-exec-1] DEBUG o.s.d.r.core.RedisConnectionUtils - Opening RedisConnection 
13:46:37.742 [http-nio-8080-exec-1] DEBUG o.s.d.r.core.RedisConnectionUtils - Closing Redis Connection 
13:46:37.812 [http-nio-8080-exec-1] DEBUG o.s.d.r.core.RedisConnectionUtils - Opening RedisConnection 
13:46:37.940 [http-nio-8080-exec-1] DEBUG o.s.d.r.core.RedisConnectionUtils - Closing Redis Connection 
13:46:38.003 [http-nio-8080-exec-1] DEBUG o.s.d.r.core.RedisConnectionUtils - Opening RedisConnection 
13:46:38.128 [http-nio-8080-exec-1] DEBUG o.s.d.r.core.RedisConnectionUtils - Closing Redis Connection 
13:46:38.201 [http-nio-8080-exec-1] DEBUG o.s.d.r.core.RedisConnectionUtils - Opening RedisConnection 
13:46:38.337 [http-nio-8080-exec-1] DEBUG o.s.d.r.core.RedisConnectionUtils - Closing Redis Connection 
13:46:38.414 [http-nio-8080-exec-1] DEBUG com.arrow.pricing.dao.RedisDAO - TestVal2 returned from REdis: yo mama 

我以爲我已經按照如何建立一個連接池,但以績效爲導向的平臺處理,如Redis的時候,我會不是文檔預計我看到這種延遲。

非常感謝您的幫助或提示。

+0

那些打開和關閉連接日誌,在這兩個池和非池的情況將被記錄。嘗試通過刪除'testOn *'configs –

+0

來停止驗證連接感謝您的回覆@AliDehghani。恐怕我仍然會得到同樣的延遲。 –

+0

查看'RedisAutoConfiguration'從彈簧啓動它可能會幫助你 –

回答

0

基於彈簧數據redis的-1.7.2.RELEASE

if(!isConnectionTransactional(conn, factory)) { 
    if (log.isDebugEnabled()) { 
     log.debug("Closing Redis Connection"); 
    } 
    conn.close() 
} 

通過日誌,你可以看到在管線205「關閉Redis的連接」,然後連接是通過調用接近方法關閉。

我們可以發現,康恩是農具RedisConnection。在這種情況下,我們實際使用的類是JedisConnection

看到代碼開始在行256

public void close() throws DataAccessExeception { 
    super.close(); 
    // return the connection to the pool 
    if (pool != null) { 
     ...balabala 
    } 
} 

的連接返回到池中。 現在我們知道RedisConnectionUtils alaways顯示日誌「關閉Redis的連接」甚至動用池

相關問題