简体   繁体   English

即使数据库失败,如何运行 Spring Boot 应用程序

[英]How to run spring boot app even if database fails

I have a spring boot app which has multiple databases.我有一个包含多个数据库的 Spring Boot 应用程序。 I want to run the app regardless of the state of the database.无论数据库的状态如何,我都想运行该应用程序。 Here is how my application yaml looks like这是我的应用程序 yaml 的样子

logging:
  level:
    org:
      hibernate:
        SQL: DEBUG
        type:
          descriptor:
            sql:
              BasicBinder: TRACE

spring:
  sql:
    init:
      continue-on-error: true # <-- didn't work
  jpa:
    properties:
      hibernate:
        format_sql: 'true'
    show-sql: 'true'
  datasource:
    azure-read-only:
      url: <azure_db1_url>
      username: '<username>'
      password: '<password>'
      driver-class-name: com.microsoft.sqlserver.jdbc.SQLServerDriver
      continue-on-error: true
      hikari: # <--- add this to bypass boot failure but it didn't work either
        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
    azure-read-write:
      url: <azure_db2_url>
      username: '<username>'
      password: '<password>'
      driver-class-name: com.microsoft.sqlserver.jdbc.SQLServerDriver
      continue-on-error: true
      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
    db2-testdb1:
      url: <another_db_not_hosted_on_azure>
      username: '<username>'
      password: '<passwor'
      driver-class-name: com.ibm.db2.jcc.DB2Driver
      continue-on-error: true
      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

For datasources, I have one datasource which is Primary bean and others are non-primary.对于数据源,我有一个数据源是Primary bean,而其他数据源是非主的。 How can i run spring boot app even if it fails to connect to ALL dbs.即使无法连接到所有数据库,我如何运行 Spring Boot 应用程序。

You can instantiate a datasource "manually" then feed it to Spring.您可以“手动”实例化数据源,然后将其提供给 Spring。 For example:例如:

@Configuration
public class MyConfigBean {

  @Lazy
  @Autowired
  private DataSource dataSource;
  
  @Bean
  public DataSource getDataSource() {
    log.debug(">>> Building the DATA-SOURCE");
    DataSourceBuilder<?> dataSourceBuilder = DataSourceBuilder.create();
    dataSourceBuilder.driverClassName(JDBC_DRIVER_CLASS_NAME);
    dataSourceBuilder.url(props.getJDBCUrl());
    dataSourceBuilder.username(props.getJDBCUsername());
    dataSourceBuilder.password(props.getJDBCPassword());
    return dataSourceBuilder.build();
  }
  
}

The @Lazy annotation leaves the call to getDataSource() method to the last. @Lazy注释将对getDataSource()方法的调用留到最后。 This way you can retrieve/populate the URL and credentials earlier, so they are ready for the moment they are needed.通过这种方式,您可以更早地检索/填充 URL 和凭据,以便在需要时准备好它们。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM