简体   繁体   English

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

[英]Spring boot - Configure second datasource along with gloud gcp postgresql primary datasource

Setting up our Spring Boot project we have a database hosted on Google Cloud Gcp. 设置我们的Spring Boot项目,我们在Google Cloud Gcp上托管了一个数据库。 We use Spring Data JPA to manipulate our database objects and it works great. 我们使用Spring Data JPA来操作我们的数据库对象,它工作得很好。

application.yaml 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 的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>

Using this Spring boot is able to autoconfigure the database connection and we are using Spring Data JPA to manipulate database objects. 使用这个Spring引导能够自动配置数据库连接,我们使用Spring Data JPA来操作数据库对象。

This datasource is only used by our project, but our company has an Oracle database used by our ERP that I need to use. 此数据源仅供我们的项目使用,但我们公司有一个我需要使用的ERP使用的Oracle数据库。 I figured I would use JdbcTemplate. 我想我会使用JdbcTemplate。

So I setup a Datasource and a JdbcTemplate linked to it : 所以我设置了一个数据源和一个链接到它的JdbcTemplate:

DatasourceConfiguration.java 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;
}

With that setup, I can perform requests on this datasource. 通过该设置,我可以对此数据源执行请求。

But since I configures a Datasource explicitely, JPA now executes requests on that one and does not Autoconfigure my CLoud SQL datasource anymore. 但是由于我明确地配置了数据源,JPA现在在那个上执行请求,并且不再自动配置我的CLoud SQL数据源。

I tried setting up a @Primary Datasource and explicitly configure parameters : 我尝试设置@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;
}

Here's the logs at startup : 这是启动时的日志:

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

So it seems like it cannot get to connect to Cloud SQL instance. 所以它似乎无法连接到Cloud SQL实例。 I'm using the exact same parameters that I use in the application.yaml. 我使用的是与application.yaml中使用的完全相同的参数。

So how can I keep the autoconfiguration to Cloud GCP for my JPA needs, and add a second datasource for jdbc template ? 那么如何根据我的JPA需求为Cloud GCP保留自动配置,并为jdbc模板添加第二个数据源?

You can refer to these articles -> spring-data-jpa-multiple-databases or using-multiple-datasources-with-spring-boot 您可以参考这些文章 - > spring-data-jpa-multiple-databasesusing-multiple-datasources-with-spring-boot

Basically while defining and initializing data source, you can specify the base package name of repositories which will use that particular data source. 基本上,在定义和初始化数据源时,您可以指定将使用该特定数据源的存储库的基本包名称。

Eg in configuration class of oracle, you can specify base package of repository which will use oracle db and in Cloud GCP you can specify package containing repositories which will contact to Cloud GCP. 例如,在oracle的配置类中,您可以指定将使用oracle db的存储库的基本包,并且在Cloud GCP中,您可以指定包含将与Cloud GCP联系的存储库的包。

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

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