我在springboot应用程序中使用Redis。当redis服务器关闭时,它将阻塞很多时间并抛出PoolException。我该如何减少封锁时间?谢谢!
我尝试将属性超时和 maxWait 设置为100ms,但是没有用。
服务:
@Service
public class TestService {
@Cacheable(cacheNames = "test:hello")
public String test(String name) {
System.out.println("invoke...");
return "hello " + name;
}
}
控制器:
@RestController
public class TestController {
@Autowired
private TestService testService;
@GetMapping("/test")
public Object test(@RequestParam String name) {
return testService.test(name);
}
}
配置:
@Configuration
public class RedisConfiguration extends CachingConfigurerSupport {
public static final Logger LOG = LoggerFactory.getLogger(RedisConfiguration.class);
@Bean
public RedisCacheManager cacheManager(RedisConnectionFactory redisConnectionFactory) {
RedisCacheManager redisCacheManager = RedisCacheManager.RedisCacheManagerBuilder
.fromConnectionFactory(redisConnectionFactory).build();
return redisCacheManager;
}
@Override
public CacheErrorHandler errorHandler() {
return new CacheErrorHandler() {
@Override
public void handleCacheGetError(RuntimeException exception, Cache cache, Object key) {
if (exception instanceof PoolException) {
LOG.error(exception.getMessage());
} else {
throw exception;
}
}
@Override
public void handleCachePutError(RuntimeException exception, Cache cache, Object key, Object value) {
if (exception instanceof PoolException) {
LOG.error(exception.getMessage());
} else {
throw exception;
}
}
@Override
public void handleCacheEvictError(RuntimeException exception, Cache cache, Object key) {
if (exception instanceof PoolException) {
LOG.error(exception.getMessage());
} else {
throw exception;
}
}
@Override
public void handleCacheClearError(RuntimeException exception, Cache cache) {
if (exception instanceof PoolException) {
LOG.error(exception.getMessage());
} else {
throw exception;
}
}
};
}
}
application.properties:
spring.application.name = test
spring.redis.host = 127.0.0.1 | 127.0.0.2 | 213.12.51.11
spring.redis.port = 6379
spring.redis.timeout = 100ms
spring.redis.lettuce.pool.max-active = 8
spring.redis.lettuce.pool.max-idle = 8
spring.redis.lettuce.pool.min-idle = 0
spring.redis.lettuce.pool.max-wait = 100ms
2019-04-09 17:11:02.146 INFO 13852 --- [nio-8080-exec-1] o.a.c.c.C.[Tomcat].[localhost].[/] : Initializing Spring DispatcherServlet 'dispatcherServlet'
2019-04-09 17:11:02.146 INFO 13852 --- [nio-8080-exec-1] o.s.web.servlet.DispatcherServlet : Initializing Servlet 'dispatcherServlet'
2019-04-09 17:11:02.159 INFO 13852 --- [nio-8080-exec-1] o.s.web.servlet.DispatcherServlet : Completed initialization in 13 ms
2019-04-09 17:11:02.351 INFO 13852 --- [nio-8080-exec-1] io.lettuce.core.EpollProvider : Starting without optional epoll library
2019-04-09 17:11:02.353 INFO 13852 --- [nio-8080-exec-1] io.lettuce.core.KqueueProvider : Starting without optional kqueue library
invoke...
2019-04-09 17:15:37.715 INFO 24528 --- [nio-8080-exec-1] o.a.c.c.C.[Tomcat].[localhost].[/] : Initializing Spring DispatcherServlet 'dispatcherServlet'
2019-04-09 17:15:37.715 INFO 24528 --- [nio-8080-exec-1] o.s.web.servlet.DispatcherServlet : Initializing Servlet 'dispatcherServlet'
2019-04-09 17:15:37.725 INFO 24528 --- [nio-8080-exec-1] o.s.web.servlet.DispatcherServlet : Completed initialization in 10 ms
2019-04-09 17:15:37.896 INFO 24528 --- [nio-8080-exec-1] io.lettuce.core.EpollProvider : Starting without optional epoll library
2019-04-09 17:15:37.897 INFO 24528 --- [nio-8080-exec-1] io.lettuce.core.KqueueProvider : Starting without optional kqueue library
2019-04-09 17:15:39.170 ERROR 24528 --- [nio-8080-exec-1] n.guolanren.config.RedisConfiguration : Could not get a resource from the pool; nested exception is io.lettuce.core.RedisConnectionException: Unable to connect to 127.0.0.2:6379asdasdasd
invoke...
2019-04-09 17:15:40.188 ERROR 24528 --- [nio-8080-exec-1] n.guolanren.config.RedisConfiguration : Could not get a resource from the pool; nested exception is io.lettuce.core.RedisConnectionException: Unable to connect to 127.0.0.2:6379asdasdasd
2019-04-09 17:17:35.173 INFO 27304 --- [nio-8080-exec-1] o.a.c.c.C.[Tomcat].[localhost].[/] : Initializing Spring DispatcherServlet 'dispatcherServlet'
2019-04-09 17:17:35.174 INFO 27304 --- [nio-8080-exec-1] o.s.web.servlet.DispatcherServlet : Initializing Servlet 'dispatcherServlet'
2019-04-09 17:17:35.189 INFO 27304 --- [nio-8080-exec-1] o.s.web.servlet.DispatcherServlet : Completed initialization in 15 ms
2019-04-09 17:17:35.484 INFO 27304 --- [nio-8080-exec-1] io.lettuce.core.EpollProvider : Starting without optional epoll library
2019-04-09 17:17:35.497 INFO 27304 --- [nio-8080-exec-1] io.lettuce.core.KqueueProvider : Starting without optional kqueue library
2019-04-09 17:17:45.735 ERROR 27304 --- [nio-8080-exec-1] n.guolanren.config.RedisConfiguration : Could not get a resource from the pool; nested exception is io.lettuce.core.RedisConnectionException: Unable to connect to 213.12.51.11:6379asdasdasd
invoke...
2019-04-09 17:17:55.760 ERROR 27304 --- [nio-8080-exec-1] n.guolanren.config.RedisConfiguration : Could not get a resource from the pool; nested exception is io.lettuce.core.RedisConnectionException: Unable to connect to 213.12.51.11:6379asdasdasd