Spring boot + hikari - dataSource or dataSourceClassName or jdbcUrl is required 问题

use*_*431 6 java spring datasource spring-boot hikaricp

尝试启动 Spring 应用程序时出现以下错误

ERROR 5908 --- [ main] com.zaxxer.hikari.HikariConfig : HikariPool-1 - dataSource or dataSourceClassName or jdbcUrl is required.

我的application.properties文件看起来像这样:

spring.datasource.one.jdbc-url = jdbc:postgresql://10.x.x.x:y/sampledb1
spring.datasource.one.username = someuser
spring.datasource.one.password = somepasswd
spring.datasource.one.driver-class-name = org.postgresql.Driver

spring.datasource.two.jdbc-url = jdbc:postgresql://10.x.x.x:z/sampledb2
spring.datasource.two.username = someuser
spring.datasource.two.password = somepassword
spring.datasource.two.driver-class-name = org.postgresql.Driver
Run Code Online (Sandbox Code Playgroud)

我正在使用 DataSourceBuilder 类,如下所示:

@Configuration
public class DataSourceConfig
{
    @Bean(name = "one")
    @Primary
    @ConfigurationProperties(prefix = "spring.datasource.one") 
    public DataSource dataSource1()
    {
        return DataSourceBuilder.create().build();
    }

    @Bean(name = "two")
    @ConfigurationProperties(prefix = "spring.datasource.two") 
    public DataSource dataSource2()
    {
        return DataSourceBuilder.create().build();
    }
}
Run Code Online (Sandbox Code Playgroud)

我的 pom 看起来像这样。

    <parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>2.0.4.RELEASE</version>
    <relativePath />
</parent>

<properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    <java.version>1.8</java.version>
    <avro.version>1.8.2</avro.version>
    <maven.compiler.source>1.8</maven.compiler.source>
    <maven.compiler.target>1.8</maven.compiler.target>
    <version.powermock>1.6.2</version.powermock>
</properties>

<dependencies>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
        <exclusions>
            <exclusion>
                <artifactId>log4j-over-slf4j</artifactId>
                <groupId>org.slf4j</groupId>
            </exclusion>
        </exclusions>
    </dependency>
    <dependency>
        <groupId>org.mybatis.spring.boot</groupId>
        <artifactId>mybatis-spring-boot-starter</artifactId>
        <version>1.3.2</version>
    </dependency>

    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-test</artifactId>
        <scope>test</scope>
    </dependency>
    <!-- actuator -->
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-actuator</artifactId>
    </dependency>
    <!-- eureka -->
    <dependency>
        <groupId>org.springframework.cloud</groupId>
        <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
    </dependency>
    <!-- hystrix -->
    <dependency>
        <groupId>org.springframework.cloud</groupId>
        <artifactId>spring-cloud-starter-netflix-hystrix</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-context</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-web</artifactId>
        <exclusions>
            <exclusion>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-logging</artifactId>
            </exclusion>
        </exclusions>
    </dependency>
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-context-support</artifactId>
    </dependency>
     </dependencies> 
Run Code Online (Sandbox Code Playgroud)

这在早期工作正常,但现在导致了一些问题。并且发生错误intermittently,有时它开始时没有错误,有时它会因错误而失败。

我尝试了链接中建议的解决方案。它们似乎对我不起作用。

use*_*900 7

改变jdbc-urljdbcUrl使光速可以找到每个URL合适的驱动程序。

jdbcUrl 此属性指示 HikariCP 使用“基于驱动程序管理器”的配置。我们认为基于 DataSource 的配置(上图)由于各种原因(见下图)而更胜一筹,但对于许多部署而言,几乎没有显着差异。将此属性与“旧”驱动程序一起使用时,您可能还需要设置 driverClassName 属性,但请先尝试不使用。请注意,如果使用此属性,您仍然可以使用 DataSource 属性来配置您的驱动程序,实际上建议优先于 URL 本身中指定的驱动程序参数。默认值:无