gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-03 00:19:16 25 4
gpt4 key购买 nike

我有一个 Spring Boot 应用程序,它有多个数据库。我想无论数据库的状态如何都运行该应用程序。这是我的应用程序 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

对于数据源,我有一个数据源是Primary bean,其他数据源是非主要的。即使无法连接到所有数据库,我如何运行 Spring Boot 应用程序。

最佳答案

您可以“手动”实例化数据源,然后将其提供给 Spring。例如:

@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();
}

}

@Lazy 注释将对 getDataSource() 方法的调用留到最后。这样您就可以更早地检索/填充 URL 和凭据,以便在需要时做好准备。

关于database - 即使数据库失败如何运行 Spring Boot 应用程序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/72886981/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com