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]
复制代码
百度了一下找到的解决方法:
- url 换成了 jdbc-url;
- 增加 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
springboot 2 Hikari 多数据源配置问题(dataSourceClassName or jdbcUrl is required)的更多相关文章
- spring boot :error querying database. Cause: java.lang.IllegalArgumentException: dataSource or dataSourceClassName or jdbcUrl is required
配置多个数据源启动报错,error querying database. Cause: java.lang.IllegalArgumentException: dataSource or dataSo ...
- 记springboot + MP +Hikari动态数据源配置
环境准备: springboot 2.1.6 mybatis-plus 数据库驱动 boot 自带hikari驱动 步骤1: 导入多数据源启动工具类 <!-- 多数据源支持 -->< ...
- springboot 多数据源之错误 HikariPool-1 - jdbcUrl is required with driverClassName.
数据源连接报错: 之前在1.5.7的版本的时候用该数据源配置没问题,看如下所示 springboot1.5.7配置多数据源: datasource.master.url=jdbc:mysql://lo ...
- 记录一下自己搭建springboot+mybatis+druid 多数据源的过程
前言 上次的一个项目(springboot+mybatis+vue),做到后面的时间发现需要用到多数据源.当时没有思路..后来直接用了jdbc来实现.这几天不是很忙,所以决定自己再搭建一次.不多说, ...
- SpringBoot | 3.1 配置数据源
目录 前言 1. 数据源的自动配置 2. *数据源自动配置源码分析 2.1 DataSourceAutoConfiguration:数据源自动配置类 2.2 JdbcTemplateAutoConfi ...
- SpringBoot+MyBatis配置多数据源
SpringBoot 可以支持多数据源,这是一个非常值得学习的功能,但是从现在主流的微服务的架构模式中,每个应用都具有唯一且准确的功能,多数据源的需求很难用到,考虑到实际情况远远比理论复杂的多,这里还 ...
- springboot中实现多数据源
springboot中实现多数据源 1.什么场景需要多数据源 业务读写分离 业务分库 业务功能模块拆分多库 2.常见的多数据源的方案 按照数据源分别把mapper和entity放到不同的package ...
- SpringBoot整合阿里Druid数据源及Spring-Data-Jpa
SpringBoot整合阿里Druid数据源及Spring-Data-Jpa https://mp.weixin.qq.com/s?__biz=MzU0MDEwMjgwNA==&mid=224 ...
- springboot 中使用Druid 数据源提供数据库监控
一.springboot 中注册 Servlet/Filter/Listener 的方式有两种,1 通过代码注册 ServletRegistrationBean. FilterRegistration ...
随机推荐
- hexo next中遇到的bug,引发出的关于jquery中click()函数和on("click",function())的区别
个人博客:https://mmmmmm.me 源码:https://github.com/dataiyangu/dataiyangu.github.io 背景: 本人在维护博客的时候加入了aplaye ...
- idea从github中pull或者push成功之后ssm项目全部controller报红色下划线的解决方案
某次从github上pull下来之后,报出如下一堆红色波浪线错误 解决方法: 在随便一个红色波浪线处,按下alt+enter,点击add maven dependency, 选中所有,点击添加即可,此 ...
- Tomcat点击项目名称,加载一个action请求
<meta http-equiv="refresh" content="0;url=index.action">
- Java性能优化的50个细节,我必须分享给你!
来源:blog.csdn.net/dongnan591172113/article/details/51790428 ;i<list.size();i++) ,len=list.size();i ...
- js 实时监听textarea输入
html: <textarea class="area" name="" id="text1" maxlength="100 ...
- java_IO流(输出流)
** * io流: * 输入流:硬盘输入到内存 字节/字符输入流 * 输出流:内存输出到硬盘 字节/字符输入流 * 字节流:一切数据都是字节存储(二进制) * 字节输出流(OutputStream): ...
- LOJ#6075. 「2017 山东一轮集训 Day6」重建
题目描述: 给定一个 n个点m 条边的带权无向连通图 ,以及一个大小为k 的关键点集合S .有个人要从点s走到点t,现在可以对所有边加上一个非负整数a,问最大的a,使得加上a后,满足:s到t的最短路长 ...
- Leetcode166. Fraction to Recurring Decimal分数到小数
给定两个整数,分别表示分数的分子 numerator 和分母 denominator,以字符串形式返回小数. 如果小数部分为循环小数,则将循环的部分括在括号内. 示例 1: 输入: numerator ...
- Error:Execution failed for task ':app:validateSigningDebug'.
今天出差回来 第一天 把项目重新移植到新的电脑上 一运行 给我报错了 ! 这个是签名的路径错误 我们需要重新导一下路径就可以了 点击左上角 File -> project structu ...
- 使用openSUSE Leap 42.2小记
闪存记录 在2017年04月10日开始想用openSuSE. 2017年04月10日开始找资料制作U盘安装openSUSE. 是在windows 7中用imageWrite.exe 软件制作的.安装的 ...