Spring Boot-配置第二个数据源以及gloud gcp postgresql主数据源

时间:2019-05-24 09:28:13

标签: java spring hibernate spring-boot google-cloud-sql

设置我们的Spring Boot项目,我们在Google Cloud Gcp上托管了一个数据库。我们使用Spring Data JPA来操作我们的数据库对象,并且效果很好。

  

application.yaml

spring:
  jpa:
database-platform: org.hibernate.dialect.PostgreSQL95Dialect
properties:
  hibernate:
    default_schema: {schema}
    dialect : org.hibernate.dialect.PostgreSQL95Dialect
datasource:
  username: {username}
  password: {pwd}
cloud:
  gcp:
    sql:
      database-name: {dbname}
      instance-connection-name: {connection-name}
  

pom.xml

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-gcp-starter-sql-postgresql</artifactId>
</dependency>

使用此Spring Boot能够自动配置数据库连接,我们正在使用Spring Data JPA来操纵数据库对象。

此数据源仅由我们的项目使用,但是我们公司有一个我需要使用的ERP数据库使用的Oracle数据库。我想我会使用JdbcTemplate。

所以我设置了一个数据源和一个链接到它的JdbcTemplate:

  

DatasourceConfiguration.java

@Bean(name = "dataSourceGenerix")
public DataSource dataSourceGenerix() {
    final DriverManagerDataSource dataSource = new DriverManagerDataSource();

    dataSource.setDriverClassName(generixDatasourceDriver);
    dataSource.setUrl(generixDatasourceUrl);
    dataSource.setUsername(generixDatasourceUsername);
    dataSource.setPassword(generixDatasourcePassword);
    return dataSource;
}

@Bean
public NamedParameterJdbcTemplate jdbcTemplateGenerix(@Qualifier("dataSourceGenerix") DataSource dataSourceGenerix) {
    NamedParameterJdbcTemplate jdbcTemplate = null;

    try(Connection conn = DataSourceUtils.getConnection(dataSourceGenerix)) {
        jdbcTemplate = new NamedParameterJdbcTemplate(dataSourceGenerix);
    } catch (SQLException | CannotGetJdbcConnectionException e) {
        log.error("{} {} : {}", Constantes.NO_DB_CONNECTION_GENERIX, generixDatasourceUrl, e.getMessage());
    }
    return jdbcTemplate;
}

通过该设置,我可以在此数据源上执行请求。

但是,由于我明确配置了数据源,因此JPA现在在该数据源上执行请求,并且不再自动配置我的CLoud SQL数据源。

我尝试设置@Primary数据源并显式配置参数:

@Bean
@Primary
DataSource dataSource() {
    DriverManagerDataSource dataSource = new DriverManagerDataSource();

    dataSource.setUrl("jdbc:postgresql://google/{dbname}?cloudSqlInstance={instancename}&socketFactory=com.google.cloud.sql.postgres.SocketFactory");
    dataSource.setUsername({username});
    dataSource.setPassword({pwd});

    return dataSource;
}

这是启动时的日志:

c.g.cloud.sql.core.SslSocketFactory : Obtaining ephemeral certificate for Cloud SQL instance [{instancename}].
o.s.b.a.orm.jpa.DatabaseLookup      : Unable to determine jdbc url from datasource
org.springframework.jdbc.support.MetaDataAccessException: Could not get Connection for extracting meta-data; nested exception is org.springframework.jdbc.CannotGetJdbcConnectionException:
Caused by: java.lang.RuntimeException: Unable to retrieve information about Cloud SQL instance
Caused by: java.net.SocketTimeoutException: connect timed out

因此,似乎无法连接到Cloud SQL实例。我使用的是与application.yaml中使用的参数完全相同的参数。

那么我如何才能将自动配置保留到Cloud GCP以满足我的JPA需求,并为jdbc模板添加第二个数据源?

2 个答案:

答案 0 :(得分:0)

您可以参考以下文章-> spring-data-jpa-multiple-databasesusing-multiple-datasources-with-spring-boot

基本上,在定义和初始化数据源时,您可以指定将使用该特定数据源的存储库的基本软件包名称。

例如在oracle的配置类中,可以指定将使用oracle db的存储库的基本软件包,在Cloud GCP中,可以指定包含将与Cloud GCP联系的存储库的软件包。

答案 1 :(得分:0)

相关问题