Spring Boot Sping Boot Starter数据Redis -捕获连接异常

4xrmg8kj  于 2022-12-29  发布在  Spring
关注(0)|答案(1)|浏览(282)

在我的Spring Boot 3.0.0项目中,我有一个spring-boot-starter-data-redis依赖项。
如果我在redis服务器未启动时启动应用程序,应用程序也会启动,但有一些例外

2022-12-05T11:11:27.284+01:00  WARN 24032 --- [oundedElastic-1] o.s.b.a.d.r.RedisReactiveHealthIndicator : Redis health check failed
org.springframework.data.redis.RedisConnectionFailureException: Unable to connect to Redis
at org.springframework.data.redis.connection.lettuce.LettuceConnectionFactory$ExceptionTranslatingConnectionProvider.translateException(LettuceConnectionFactory.java:1602) ~[spring-data-redis-3.0.0.jar:3.0.0]
at org.springframework.data.redis.connection.lettuce.LettuceConnectionFactory$ExceptionTranslatingConnectionProvider.getConnection(LettuceConnectionFactory.java:1533) ~[spring-data-redis-3.0.0.jar:3.0.0]
at org.springframework.data.redis.connection.lettuce.LettuceConnectionFactory$SharedConnection.getNativeConnection(LettuceConnectionFactory.java:1358) ~[spring-data-redis-3.0.0.jar:3.0.0]
at org.springframework.data.redis.connection.lettuce.LettuceConnectionFactory$SharedConnection.getConnection(LettuceConnectionFactory.java:1341) ~[spring-data-redis-3.0.0.jar:3.0.0]
at org.springframework.data.redis.connection.lettuce.LettuceConnectionFactory.getSharedReactiveConnection(LettuceConnectionFactory.java:1083) ~[spring-data-redis-3.0.0.jar:3.0.0]
at org.springframework.data.redis.connection.lettuce.LettuceConnectionFactory.getReactiveConnection(LettuceConnectionFactory.java:479) ~[spring-data-redis-3.0.0.jar:3.0.0]
at org.springframework.data.redis.connection.lettuce.LettuceConnectionFactory.getReactiveConnection(LettuceConnectionFactory.java:105) ~[spring-data-redis-3.0.0.jar:3.0.0]
at reactor.core.publisher.MonoSupplier.call(MonoSupplier.java:67) ~[reactor-core-3.5.0.jar:3.5.0]
at reactor.core.publisher.FluxSubscribeOnCallable$CallableSubscribeOnSubscription.run(FluxSubscribeOnCallable.java:227) ~[reactor-core-3.5.0.jar:3.5.0]
at reactor.core.scheduler.SchedulerTask.call(SchedulerTask.java:68) ~[reactor-core-3.5.0.jar:3.5.0]
at reactor.core.scheduler.SchedulerTask.call(SchedulerTask.java:28) ~[reactor-core-3.5.0.jar:3.5.0]
at java.base/java.util.concurrent.FutureTask.run(FutureTask.java:264) ~[na:na]
at java.base/java.util.concurrent.ScheduledThreadPoolExecutor$ScheduledFutureTask.run(ScheduledThreadPoolExecutor.java:304) ~[na:na]
at java.base/java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1136) ~[na:na]
at java.base/java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:635) ~[na:na]
at java.base/java.lang.Thread.run(Thread.java:833) ~[na:na]
Caused by: io.lettuce.core.RedisConnectionException: Unable to connect to localhost/<unresolved>:6379

有没有一种方法可以“捕获”连接异常并“干净地”记录它们,而不需要堆栈跟踪?

ev7lccsx

ev7lccsx1#

可以在@ControllerAdvice类中捕获异常。

@org.springframework.web.bind.annotation.ExceptionHandler(RedisConnectionFailureException.class)
  public ResponseEntity<Void> redisHandleDataAccessException(RedisConnectionFailureException e) {
    log.error("Failed to establish a connection to the Redis database.", e);
    return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).build();
  }

相关问题