项目场景:
需要在测试线请求其他部门的API,以验证量化指标是否能取到数据,由于该测试线机器没有做办公网映射,所以在代码中是用了@PostConstruct 注解,在jar包启动时就会自动执行跑批验证的方法。
问题描述
在验证方法中,有一个步骤需要用redisTemplate查询redis,奇怪的是,在本地能用redisTemplate查询到数据,而在测试线却查不到数据。
到测试线拉日志,看到如下报错:
2023-11-28 14:59:20,520 [pool-3-thread-5] [uid-] ERROR com.eastmoney.approboadvisor.api.cache.BkRelationCacheLoader : BkRelationCacheLoader
java.lang.IllegalStateException: LettuceConnectionFactory was destroyed and cannot be used anymore
at org.springframework.util.Assert.state(Assert.java:76)
at org.springframework.data.redis.connection.lettuce.LettuceConnectionFactory.assertInitialized(LettuceConnectionFactory.java:1263)
at org.springframework.data.redis.connection.lettuce.LettuceConnectionFactory.getConnection(LettuceConnectionFactory.java:414)
at org.springframework.data.redis.core.RedisConnectionUtils.fetchConnection(RedisConnectionUtils.java:193)
原因分析:
提示:这里填写问题的分析:
看起来是redis连接池断开了连接,所以为啥会断开连接捏,继续查日志:
2023-11-28 15:33:14,781 [main] [uid-] WARN o.s.boot.web.servlet.context.AnnotationConfigServletWebServerApplicationContext : Exception encountered during context initialization - cancelling refresh attempt: org.springframework.context.ApplicationContextException: Failed to start bean 'webServerStartStop'; nested exception is org.springframework.context.ApplicationContextException: Failed to start bean 'webServerStartStop'; nested exception is org.springframework.boot.web.server.WebServerException: Unable to start embedded Tomcat server
2023-11-28 15:33:14,782 [main] [uid-] DEBUG org.springframework.data.redis.listener.RedisMessageListenerContainer : Stopped RedisMessageListenerContainer.
可以看到在RedisMessageListenerContainer 停止前,是Tomcat启动失败了,猜测是Tomcat启动失败后使得redis连接监听器关闭,从而使得redisTemplate不可用
那么Tomcat为什么会启动失败捏,继续看日志:
2023-11-28 15:33:14,763 [main] [uid-] INFO org.apache.coyote.http11.Http11NioProtocol : Starting ProtocolHandler ["http-nio-10.195.xxx.xxx:xxxx"]
···
2023-11-28 15:33:14,764 [main] [uid-] DEBUG org.apache.tomcat.util.net.NioEndpoint : Destroy initiated for /10.195.xxx.xxx:xxxx
所以看来是ip没对,导致Tomcat启动失败,看了下配置,确实没配对···[尴尬]
重新将
LOCAL_IP改为本机地址
重启后就可以顺利跑批了
这里有两点:
1、@PostConstruct 注解的代码与Tomcat在同一时间启动,所以如果不是在@PostConstruct 中写逻辑,其实这个jar包根本不会起得来
2、Tomcat启动失败后会导致springboot启动失败,bean逐个销毁