我在Eclipse(OS WIN 10)中具有Web应用程序Spring + Spring MVC。
我有以下简化的设置:
<spring.version>5.1.2.RELEASE</spring.version>
<spring-data-redis.version>2.1.2.RELEASE</spring-data-redis.version>
<jedis.version>2.9.0</jedis.version>
我已经创建了函数:
@Override
public Map<String, Double> find(final String set, final double min, final double max) {
Set<Tuple> mostViewed = redisTemplate.execute(new RedisCallback<Set<Tuple>>() {
@Override
public Set<Tuple> doInRedis(RedisConnection con)
throws DataAccessException {
Set<Tuple> zRangeByScoreWithScore = con.zRangeByScoreWithScores(set.getBytes(), min, max);
return zRangeByScoreWithScore;
}
});
Map<String, Double> mScoreMap = new HashMap<String, Double>();
for (Tuple t : mostViewed) {
mScoreMap.put(new String(t.getValue()), t.getScore());
}
return mScoreMap;
}
我的JEDIS配置:
@Bean
public JedisPoolConfig jedisPoolConfig() {
JedisPoolConfig jedisPoolConfig = new JedisPoolConfig();
jedisPoolConfig.setMaxTotal(10);
jedisPoolConfig.setMaxIdle(5);
jedisPoolConfig.setMinIdle(2);
jedisPoolConfig.setTestOnBorrow(true);
jedisPoolConfig.setTestOnReturn(true);
jedisPoolConfig.setMaxWaitMillis(3000);
jedisPoolConfig.setTimeBetweenEvictionRunsMillis(60000);
return jedisPoolConfig;
}
@Bean
public JedisConnectionFactory jedisConnectionFactory() {
RedisStandaloneConfiguration redisStandaloneConfiguration = new RedisStandaloneConfiguration("localhost", 6379);
JedisClientConfigurationBuilder jedisClientConfiguration = JedisClientConfiguration.builder();
jedisClientConfiguration.connectTimeout(Duration.ofSeconds(60));// 60s connection timeout
jedisClientConfiguration.usePooling();
JedisConnectionFactory jedisConFactory = new JedisConnectionFactory(redisStandaloneConfiguration,
jedisClientConfiguration.build());
return jedisConFactory;
}
@Bean
public StringRedisSerializer stringRedisSerializer() {
StringRedisSerializer stringRedisSerializer = new StringRedisSerializer();
return stringRedisSerializer;
}
@Bean
RedisTemplate<String, String> redisTemplate() {
final RedisTemplate<String, String> redisTemplate = new RedisTemplate<String, String>();
redisTemplate.setConnectionFactory(jedisConnectionFactory());
redisTemplate.setDefaultSerializer(stringRedisSerializer());
redisTemplate.setKeySerializer(stringRedisSerializer());
redisTemplate.setHashKeySerializer(stringRedisSerializer());
redisTemplate.setEnableTransactionSupport(true);
return redisTemplate;
}
但是,大约1-2分钟后,我的应用程序冻结了,并在日志文件(Eclipse日志)中显示了此消息:
[http-nio-8080-exec-9] DEBUG o.s.d.r.core.RedisConnectionUtils - Opening RedisConnection
redis服务器日志(Web浏览器中的刷新应用程序无效,没有新消息):
[984] 21 Nov 11:45:00.163 * 100 changes in 300 seconds. Saving...
[984] 21 Nov 11:45:00.170 * Background saving started by pid 13692
[984] 21 Nov 11:45:00.270 # fork operation complete
[984] 21 Nov 11:45:00.270 * Background saving terminated with success
[984] 21 Nov 12:30:00.162 * 100 changes in 300 seconds. Saving...
[984] 21 Nov 12:30:00.169 * Background saving started by pid 11976
[984] 21 Nov 12:30:00.269 # fork operation complete
[984] 21 Nov 12:30:00.271 * Background saving terminated with success
当我在Eclipse中停止并启动服务器(Tomcat 8.0.41)时,在1-2分钟内一切正常。当我停止并启动REDIS服务器时没有任何作用。