springboot 2 Hikari 多數據源配置問題(dataSourceClassName or jdbcUrl is required)


springboot 2 Hikari 多數據源配置問題(dataSourceClassName or jdbcUrl is required)

最近在項目中想試一下使用 Hikari 連接池,以前用的是阿里的 Druid,框架是 Spring MVC,xml配置文件方式注入的 Bean,現在換成 Spring Boot 之后,總遇到一些奇怪的問題,問題的根源是在於自己是個半桶水。

好了,先來看看 application.yml 配置文件:

spring:
  jpa:
    show-sql: true
  datasource:
    url: jdbc:mysql://localhost:3306/test?characterEncoding=utf8&useSSL=true
    username: root
    password: root
    type: com.zaxxer.hikari.HikariDataSource
    hikari:
      maximum-pool-size: 20
      max-lifetime: 30000
      idle-timeout: 30000
      data-source-properties:
        prepStmtCacheSize: 250
        prepStmtCacheSqlLimit: 2048
        cachePrepStmts: true
        useServerPrepStmts: true
    slave:
      url: jdbc:mysql://localhost:3306/test?characterEncoding=utf8&useSSL=true
      username: root
      password: root
      type: com.zaxxer.hikari.HikariDataSource
      hikari:
        maximum-pool-size: 20
        max-lifetime: 30000
        idle-timeout: 30000
        data-source-properties:
          prepStmtCacheSize: 250
          prepStmtCacheSqlLimit: 2048
          cachePrepStmts: true
          useServerPrepStmts: true
復制代碼

數據源配置文件:

package org.seckill.config;

import com.zaxxer.hikari.HikariDataSource;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.boot.autoconfigure.jdbc.DataSourceProperties;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.boot.jdbc.DataSourceBuilder;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;
import org.springframework.jdbc.core.JdbcTemplate;

import javax.sql.DataSource;
import java.util.HashMap;
import java.util.Map;

/**

  • 數據源配置

  • @author jeftom

  • @date 2019-04-14 12:03

  • @since 1.0.0
    */
    @Configuration
    public class DataSourceConfig {
    private final static Logger LOGGER = LoggerFactory.getLogger(DataSourceConfig.class);

    @Bean(name = "masterDataSource")
    @ConfigurationProperties(prefix = "spring.datasource")
    public DataSource masterDataSource(DataSourceProperties properties) {
    LOGGER.info("init master data source:{}", properties);
    return DataSourceBuilder.create().build();
    }

    @Bean(name = "slaveDataSource")
    @ConfigurationProperties(prefix = "spring.datasource.slave")
    public DataSource slaveDataSource(DataSourceProperties properties) {
    LOGGER.info("init slave data source:{}", properties);
    return DataSourceBuilder.create().build();
    }

    @Bean
    @Primary
    public DynamicDataSource dataSource(DataSource masterDataSource, DataSource slaveDataSource) {
    Map<String, DataSource> targetDataSources = new HashMap<>();
    targetDataSources.put(DataSourceEnum.MASTER.getName(), masterDataSource);
    targetDataSources.put(DataSourceEnum.SLAVE.getName(), slaveDataSource);

     <span class="hljs-built_in"><span class="hljs-built_in">return</span></span> new DynamicDataSource(masterDataSource, targetDataSources);
    

    }
    }
    復制代碼

報錯信息:

com.zaxxer.hikari.HikariConfig           : HikariPool-1 - dataSource or dataSourceClassName or jdbcUrl is required.

java.lang.IllegalArgumentException: dataSource or dataSourceClassName or jdbcUrl is required.
    at com.zaxxer.hikari.HikariConfig.validate(HikariConfig.java:955) ~[HikariCP-3.2.0.jar:na]
復制代碼

百度了一下找到的解決方法:

  1. url 換成了 jdbc-url;
  2. 增加 driver-class-name: com.mysql.cj.jdbc.Driver

試了一下,果然真的可以。但是,沒有使用多數據源時,原來的配置文件也是能正常使用的啊,為什么呢?

問題肯定是出在增加了 DataSourceConfig 這個配置文件之后。

於是試着把配置文件還原回去,再把

return DataSourceBuilder.create().build();
復制代碼

改為如下:

      return DataSourceBuilder.create(properties.getClassLoader())
                .type(HikariDataSource.class)
                .driverClassName(properties.determineDriverClassName())
                .url(properties.determineUrl())
                .username(properties.determineUsername())
                .password(properties.determinePassword())
                .build();
復制代碼

也就是從配置文件拿到配置值之后重新賦值一下,再次啟動項目,居然好了~

原因就是出在 DataSourceBuilder 創建數據源這個類上,而單數據源自動裝載時不會出現這樣的問題。

原文地址;https://juejin.im/post/5cb2f9f26fb9a068b748ab33


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM