【问题标题】:How to make Spring server to start even if database is down?即使数据库已关闭,如何使 Spring 服务器启动?
【发布时间】:2018-01-06 15:08:27
【问题描述】:

我正在使用 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

有什么办法吗?

谢谢

【问题讨论】:

标签: java spring-boot spring-data fail-fast


【解决方案1】:

你可以设置:

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

在您的 application.properties 中。

根据Spring Boot 2.5.5 user guide

默认情况下,Spring Boot 启用其基于脚本的数据库初始化程序的快速失败功能。这意味着,如果脚本导致异常,应用程序将无法启动。您可以通过设置 spring.sql.init.continue-on-error 来调整该行为。

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

【讨论】:

  • 这不适用于 2.1.1.RELEASE 或比这更早的版本
  • 根据 Spring Boot 2.1.1 用户指南,这应该可以工作。
  • 我正在使用这些配置:spring.datasource.url= spring.datasource.username= spring.datasource.password= spring.datasource.driverClassName=com.microsoft.sqlserver.jdbc.SQLServerDriver spring.datasource .continue-on-error=true
  • 除了初始化 entityManagerFactory 的 MSSQLDbConfig 类
  • @cristi_nmr 该属性的名称现在是spring.sql.init.continue-on-error。请参阅我的更新答案。
【解决方案2】:

我能够解决这个问题。不过,我的工作内容与问题中的代码之间的一个主要区别是,我使用 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

小于零的值将绕过任何初始连接尝试,并且池将在尝试在后台获取连接时立即启动。因此,以后获取连接的努力可能会失败。

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 Boot 依赖:

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

【讨论】:

    【解决方案3】:

    你需要添加

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

    为了让它工作

    【讨论】:

    • 这是我唯一需要添加的设置。它不需要continue-on-error就可以工作。
    【解决方案4】:

    如果以上提示没有帮助,而您使用 jpa,则设置

    spring.jpa.hibernate.ddl-auto=none
    

    它对我有用。

    【讨论】:

      【解决方案5】:

      当你构建你的应用时,你可以添加它

      mvn clean install -DskipTests
      

      它会跳过数据库连接测试

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

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2021-06-02
        • 2018-06-13
        • 2021-04-06
        • 2020-12-07
        • 1970-01-01
        • 2014-02-28
        • 2020-08-22
        • 1970-01-01
        相关资源
        最近更新 更多