gpt4 book ai didi

java - 多数据源回退

转载 作者:太空宇宙 更新时间:2023-11-04 10:20:40 25 4
gpt4 key购买 nike

根据建议更新:

我有一个 Springboot Java 应用程序和在 application.properties 中定义的两个数据源:

spring.jpa.hibernate.ddl-auto=update

#online
spring.datasource.url=jdbc:mysql://172.17.0.2:3306/gcdb?allowPublicKeyRetrieval=true&useSSL=false
spring.datasource.username=root
spring.datasource.password=root
spring.datasource.driverClassName=com.mysql.jdbc.Driver

#offline
spring.secondDatasource.url=jdbc:h2:mem:gcdboff
spring.secondDatasource.username=root
spring.secondDatasource.password=root
spring.secondDatasource.driverClassName=org.h2.Driver

# Enabling H2 Console
spring.h2.console.enabled=true
spring.h2.console.path=/h2

手动将凭据从“datasource”更改为“secondDatasource”后,两个连接都可以正常工作。

但现在我想添加一个后备。如果“datasource”不可用,请使用“secondDatasource”。

我找到了以下解决方案。

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.boot.jdbc.DataSourceBuilder;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Primary;

import javax.sql.DataSource;

@SpringBootApplication
public class GCApplication {

public static void main(String[] args) {
SpringApplication.run(GCApplication.class, args);
}
/**/
@Bean
@Primary
@ConfigurationProperties(prefix="spring.datasource")
public DataSource primaryDataSource() {
return DataSourceBuilder.create().build();
}

@Bean
@ConfigurationProperties(prefix="spring.secondDatasource")
public DataSource secondaryDataSource() {
return DataSourceBuilder.create().build();
}
/**/
}

但现在没人会跑了。应用程序以以下错误结束,我不知道如何纠正该错误。

2018-07-06 09:52:30.805 ERROR 28073 --- [           main]     o.s.boot.SpringApplication               : Application run failed

org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'entityManagerFactory' defined in class path resource [org/springframework/boot/autoconfigure/orm/jpa/HibernateJpaConfiguration.class]: Unsatisfied dependency expressed through method 'entityManagerFactory' parameter 0; nested exception is org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'entityManagerFactoryBuilder' defined in class path resource [org/springframework/boot/autoconfigure/orm/jpa/HibernateJpaConfiguration.class]: Unsatisfied dependency expressed through method 'entityManagerFactoryBuilder' parameter 0; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'jpaVendorAdapter' defined in class path resource [org/springframework/boot/autoconfigure/orm/jpa/HibernateJpaConfiguration.class]: Bean instantiation via factory method failed; nested exception is org.springframework.beans.BeanInstantiationException: Failed to instantiate [org.springframework.orm.jpa.JpaVendorAdapter]: Factory method 'jpaVendorAdapter' threw exception; nested exception is java.lang.IllegalArgumentException: dataSource or dataSourceClassName or jdbcUrl is required.
at org.springframework.beans.factory.support.ConstructorResolver.createArgumentArray(ConstructorResolver.java:732) ~[spring-beans-5.0.7.RELEASE.jar:5.0.7.RELEASE]
[...]

感谢您的帮助!

最佳答案

我已经找到了解决我的问题的方法。我希望它也能帮助你。在第一个数据连接失败后,使用以下代码选择第二个数据连接:

在 GC 应用程序中:

import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.autoconfigure.jdbc.DataSourceProperties;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Primary;

import javax.sql.DataSource;

@SpringBootApplication
public class GCApplication
{
public static void main(String[] args)
{
SpringApplication.run(GCApplication.class, args);
}

@Primary
@Bean
public DataSource getDataSource(
@Qualifier("first") DataSourceProperties first,
@Qualifier("second") DataSourceProperties second)
{
int i = 0;
final DataSource firstDataSource = first.initializeDataSourceBuilder().build();
final DataSource secondDataSource = second.initializeDataSourceBuilder().build();
try
{
firstDataSource.getConnection();
return firstDataSource;
}
catch (Exception e)
{
return secondDataSource;
}
}

@Primary
@Bean("first")
@ConfigurationProperties(prefix = "spring.datasource")
public DataSourceProperties primaryDataSource()
{
final DataSourceProperties dataSourceProperties = new DataSourceProperties();
dataSourceProperties.setUrl("jdbc:mysql://172.17.0.2:3306/gcdb?allowPublicKeyRetrieval=true&useSSL=false");
dataSourceProperties.setDriverClassName("com.mysql.jdbc.Driver");
dataSourceProperties.setUsername("root");
dataSourceProperties.setPassword("root");
return dataSourceProperties;
}

@Bean("second")
public DataSourceProperties secondaryDataSource() {
final DataSourceProperties dataSourceProperties = new DataSourceProperties();
dataSourceProperties.setUrl("jdbc:h2:mem:gcdboff");
dataSourceProperties.setDriverClassName("org.h2.Driver");
dataSourceProperties.setUsername("root");
dataSourceProperties.setPassword("root");
return dataSourceProperties;
}
}

在application.properties中:

spring.jpa.hibernate.ddl-auto=update
spring.jpa.database=default

# Enabling H2 Console
spring.h2.console.enabled=true
spring.h2.console.path=/h2

关于java - 多数据源回退,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51205957/

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