如何使 Spring 服务器即使在数据库已关闭的情况下也能启动?

How to make Spring server to start even if database is down?

我正在使用 Spring Boot(1.4.7) & MyBatis。

spring.main1.datasource.url=jdbc:mariadb://192.168.0.11:3306/testdb?useUnicode=true&characterEncoding=utf8&autoReconnect=true&socketTimeout=5000&connectTimeout=3000
spring.main1.datasource.username=username
spring.main1.datasource.password=password
spring.main1.datasource.driverClassName=org.mariadb.jdbc.Driver
spring.main1.datasource.tomcat.test-on-borrow=true
spring.main1.datasource.tomcat.test-while-idle=true
spring.main1.datasource.tomcat.validation-query=SELECT 1
spring.main1.datasource.tomcat.validation-query-timeout=5000
spring.main1.datasource.tomcat.validation-interval=5000
spring.main1.datasource.tomcat.max-wait=5000
spring.main1.datasource.continue-on-error=true

当 Eclipse 或 Linux 服务器上的数据库断开连接时,我无法启动程序并出现错误。 (数据库不在本地主机上。)

当我尝试使用断开连接的数据库启动程序时, 打印这个。

java.sql.SQLNonTransientConnectionException: Could not connect to address=(host=192.168.0.11)(port=3306)(type=master) : connect timed out
Cause: org.springframework.jdbc.CannotGetJdbcConnectionException: Could not get JDBC Connection; nested exception is java.sql.SQLNonTransientConnectionException: Could not connect to address=(host=192.168.0.11)(port=3306)(type=master) : connect timed out
Stopping service [Tomcat]
Application startup failed

有什么办法吗?

谢谢

您可以设置:

spring.sql.init.continue-on-error=true

在你的 application.properties.

根据Spring Boot 2.5.5 user guide

By default, Spring Boot enables the fail-fast feature of its script-based database initializer. This means that, if the scripts cause exceptions, the application fails to start. You can tune that behavior by setting spring.sql.init.continue-on-error.

P.S.: 在 Spring Boot 2.5 之前,属性 被命名为 spring.datasource.continue-on-error.

您需要添加

spring.jpa.database-platform=org.hibernate.dialect.MySQL5Dialect

为了让它发挥作用

我能够解决这个问题。不过,我的工作与问题中的代码之间的一个主要区别是,我使用 Hikari 而不是 Tomcat 作为连接池。

这些是我必须进行的关键设置:

spring.datasource.hikari.minimum-idle: 0
spring.datasource.hikari.initialization-fail-timeout: -1
spring.datasource.continue-on-error: true
spring.datasource.driver-class-name: org.postgresql.Driver
spring.jpa.database-platform: org.hibernate.dialect.PostgreSQLDialect

minimum-idle 设置为 0 可以让 Hikari 在没有任何联系的情况下快乐。

-1 的 initialization-fail-timeout 设置告诉 Hikari 我不希望它在池启动时获得连接。

来自HikariCP documentation

A value less than zero will bypass any initial connection attempt, and the pool will start immediately while trying to obtain connections in the background. Consequently, later efforts to obtain a connection may fail.

continue-on-error 设置 true 允许服务在遇到错误时继续。

driver-class-namedatabase-platform 都是必需的。否则,Hikari 会尝试通过连接到数据库(在启动期间)来计算这些值。


不过,以防万一我遗漏了什么,这是我的完整 Spring 配置:

spring:
  application:
    name: <redacted>
  datasource:
    url: <redacted>
    username: <redacted>
    password: <redacted>
    driver-class-name: org.postgresql.Driver
    hikari:
      minimum-idle: 0
      maximum-pool-size: 15
      connection-timeout: 10000 #10s
      idle-timeout: 300000 #5m
      max-lifetime: 600000 #10m
      initialization-fail-timeout: -1
      validation-timeout: 1000 #1s
    continue-on-error: true
  jpa:
    open-in-view: false
    database-platform: org.hibernate.dialect.PostgreSQLDialect

我的项目有以下 Spring 引导依赖项:

org.springframework.boot:spring-boot
org.springframework.boot:spring-boot-actuator
org.springframework.boot:spring-boot-actuator-autoconfigure
org.springframework.boot:spring-boot-autoconfigure
org.springframework.boot:spring-boot-configuration-processor
org.springframework.boot:spring-boot-devtools
org.springframework.boot:spring-boot-starter
org.springframework.boot:spring-boot-starter-actuator
org.springframework.boot:spring-boot-starter-jdbc
org.springframework.boot:spring-boot-starter-jooq
org.springframework.boot:spring-boot-starter-json
org.springframework.boot:spring-boot-starter-logging
org.springframework.boot:spring-boot-starter-security
org.springframework.boot:spring-boot-starter-test
org.springframework.boot:spring-boot-starter-tomcat
org.springframework.boot:spring-boot-starter-validation
org.springframework.boot:spring-boot-starter-web

构建应用时,您可以添加

mvn clean install -DskipTests

会跳过数据库连接测试

(-D用于定义系统属性)

如果以上提示没有帮助并且您使用的是 jpa,则设置

spring.jpa.hibernate.ddl-auto=none

对我有用。