密码错误springboot也正常启动了

1 项目背景

在日常开发中,开发有时候拿不到数据库,依赖组件的密码,例如由devops进行配置,甚至放到一些密码管理组件中。这样就会出现密码错误的情况。项目发布成功,那么依赖项例如Mysql, 访问别的系统的ak, sk真的准备好了吗?配置错误,他们启动会报错吗?直到提测被打回才发现连接依赖组件出现问题。

2 mysql启动依赖检查

2.1 配置错误的密码启动springboot

(1)配置错误的mysql密码

(2)启动springboot,观察日志

(3)发现异常日志并且springboot启动了

(4)访问健康检查Http端口

返回正常相应200

也就是说,项目启动了,并且不依赖mysql的api是可以正常处理的。依赖mysql的api访问报错。

java 复制代码
2024-07-24 06:45:13.806  INFO 13712 --- [           main] com.zaxxer.hikari.HikariDataSource       : HikariPool-1 - Starting...
2024-07-24 06:45:13.898  WARN 13712 --- [           main] com.zaxxer.hikari.util.DriverDataSource  : Registered driver with driverClassName=com.mysql.jdbc.Driver was not found, trying direct instantiation.
2024-07-24 06:45:16.100 ERROR 13712 --- [           main] com.zaxxer.hikari.pool.HikariPool        : HikariPool-1 - Exception during pool initialization.

java.sql.SQLException: Access denied for user 'root'@'localhost' (using password: YES)
2024-07-24 06:45:18.164  INFO 13712 --- [           main] o.s.b.w.embedded.tomcat.TomcatWebServer  : Tomcat started on port(s): 9099 (http) with context path ''
2024-07-24 06:45:18.566  INFO 13712 --- [           main] c.study.config.server.ServerApplication  : Started ServerApplication in 14.224 seconds (JVM running for 15.546)

2.2 优化

在项目启动过程中,查询mysql(例如缓存,或者专门写一个检测类),若报错,抛出runtimeException,springboot会自动停止启动

java 复制代码
@Slf4j
@Component
public class InitCheckUtils {

    @Autowired
    private TestMapper testMapper;

    @PostConstruct
    public void init() {
        try {
            Long cnt = testMapper.selectCount(null);
            log.info("dbTableCnt:{}", cnt);
        } catch (Exception e) {
            log.error("projectInitMysqlConnectError", e);
            throw new RuntimeException("projectInitMysqlConnectError");
        }
    }
}
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'initCheckUtils': Invocation of init method failed; nested exception is java.lang.RuntimeException: projectInitMysqlConnectError
	at org.springframework.beans.factory.annotation.InitDestroyAnnotationBeanPostProcessor.postProcessBeforeInitialization(InitDestroyAnnotationBeanPostProcessor.java:160) ~[spring-beans-5.3.31.jar:5.3.31]
	at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.applyBeanPostProcessorsBeforeInitialization(AbstractAutowireCapableBeanFactory.java:440) ~[spring-beans-5.3.31.jar:5.3.31]
	at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1796) ~[spring-beans-5.3.31.jar:5.3.31]
	at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:620) ~[spring-beans-5.3.31.jar:5.3.31]
	at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:542) ~[spring-beans-5.3.31.jar:5.3.31]
	at org.springframework.beans.factory.support.AbstractBeanFactory.lambda$doGetBean$0(AbstractBeanFactory.java:335) ~[spring-beans-5.3.31.jar:5.3.31]
	at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:234) ~[spring-beans-5.3.31.jar:5.3.31]
	at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:333) ~[spring-beans-5.3.31.jar:5.3.31]
	at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:208) ~[spring-beans-5.3.31.jar:5.3.31]
	at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:955) ~[spring-beans-5.3.31.jar:5.3.31]
	at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:929) ~[spring-context-5.3.31.jar:5.3.31]
	at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:591) ~[spring-context-5.3.31.jar:5.3.31]
	at org.springframework.boot.web.servlet.context.ServletWebServerApplicationContext.refresh(ServletWebServerApplicationContext.java:147) ~[spring-boot-2.7.18.jar:2.7.18]
	at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:732) ~[spring-boot-2.7.18.jar:2.7.18]
	at org.springframework.boot.SpringApplication.refreshContext(SpringApplication.java:409) ~[spring-boot-2.7.18.jar:2.7.18]
	at org.springframework.boot.SpringApplication.run(SpringApplication.java:308) ~[spring-boot-2.7.18.jar:2.7.18]
	at org.springframework.boot.SpringApplication.run(SpringApplication.java:1300) ~[spring-boot-2.7.18.jar:2.7.18]
	at org.springframework.boot.SpringApplication.run(SpringApplication.java:1289) ~[spring-boot-2.7.18.jar:2.7.18]
	at com.study.config.server.ServerApplication.main(ServerApplication.java:13) ~[classes/:na]
Caused by: java.lang.RuntimeException: projectInitMysqlConnectError
	at com.study.config.server.utils.InitCheckUtils.init(InitCheckUtils.java:24) ~[classes/:na]
	at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[na:na]
	at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:77) ~[na:na]
	at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[na:na]
	at java.base/java.lang.reflect.Method.invoke(Method.java:568) ~[na:na]
	at org.springframework.beans.factory.annotation.InitDestroyAnnotationBeanPostProcessor$LifecycleElement.invoke(InitDestroyAnnotationBeanPostProcessor.java:389) ~[spring-beans-5.3.31.jar:5.3.31]
	at org.springframework.beans.factory.annotation.InitDestroyAnnotationBeanPostProcessor$LifecycleMetadata.invokeInitMethods(InitDestroyAnnotationBeanPostProcessor.java:333) ~[spring-beans-5.3.31.jar:5.3.31]
	at org.springframework.beans.factory.annotation.InitDestroyAnnotationBeanPostProcessor.postProcessBeforeInitialization(InitDestroyAnnotationBeanPostProcessor.java:157) ~[spring-beans-5.3.31.jar:5.3.31]
	... 18 common frames omitted

3 总结

(1)项目启动过程中,若依赖项没有准备好,可能不会报错,项目正常启动,启动后接受请求,就会产生错误并且影响业务。

(2)对此,项目启动的时候做一些检查,保证访问依赖组件正常。

(3)若项目启动检测到依赖组件不正常,那么阻止程序启动

相关推荐
白露与泡影23 分钟前
Spring Boot中的 6 种API请求参数读取方式
java·spring boot·后端
CodeClimb23 分钟前
【华为OD-E卷 - 服务失效判断 100分(python、java、c++、js、c)】
java·javascript·c++·python·华为od
CodeClimb25 分钟前
【华为OD-E卷 - 九宫格按键输入 100分(python、java、c++、js、c)】
java·javascript·c++·python·华为od
_Soy_Milk31 分钟前
Golang,Let‘s GO!
开发语言·后端·golang
豪宇刘32 分钟前
MyBatis 与 MyBatis-Plus 的区别
java·tomcat
1-programmer35 分钟前
【Go研究】Go语言脚本化的可行性——yaegi项目体验
开发语言·后端·golang
一个儒雅随和的男子40 分钟前
Spring为什么要用三级缓存解决循环依赖?
java·spring·缓存
梦想是成为Java高手40 分钟前
ThreadLocal的介绍与使用规范,初学者必看
java
StevenGerrad41 分钟前
【读书笔记/源码】How Tomcat Works 笔记 - c1~c10
java·笔记·tomcat
数据小小爬虫1 小时前
淘宝商品详情API返回值说明:Python爬虫代码示例
java·爬虫·python