springboot+druid+dynamic-dataSouce多数据源 配置及踩坑记录
项目先集成了阿里的 druiddruid-spring-boot-starter
,并已经配置好数据源。
突然项目有需要多数据源访问,遂集成引用dynamic-datasource-spring-boot-starter
。
但是添加 maven 引用之后,项目启动一直报错,且报错位置在业务代码处
下面是报错代码
2020-11-12 17:41:10.335 INFO 8892 --- [ main] c.a.d.s.b.a.DruidDataSourceAutoConfigure : Init DruidDataSource
2020-11-12 17:41:10.499 WARN 8892 --- [ main] ConfigServletWebServerApplicationContext : Exception encountered during context initialization - cancelling refresh attempt: org.springframework.beans.factory.
UnsatisfiedDependencyException:Error creating bean with name 'orderRecordServiceImpl': Unsatisfied dependency expressed through field 'baseMapper'; nested exception is org.springframework.beans.factory.
UnsatisfiedDependencyException: Error creating bean with name 'orderRecordMapper' defined in file [E:\workspaces\ideaProjects\frameWork\target\classes\com\easy\project\orderRecord\mapper\OrderRecordMapper.class]:
Unsatisfied dependency expressed through bean property 'sqlSessionFactory'; nested exception is org.springframework.beans.factory.
UnsatisfiedDependencyException: Error creating bean with name 'sqlSessionFactory' defined in class path resource [com/baomidou/mybatisplus/autoconfigure/MybatisPlusAutoConfiguration.class]:
Unsatisfied dependency expressed through method 'sqlSessionFactory' parameter 0; nested exception is org.springframework.beans.factory.
BeanCreationException: Error creating bean with name 'dataSource' defined in class path resource [com/alibaba/druid/spring/boot/autoconfigure/DruidDataSourceAutoConfigure.class]:
Invocation of init method failed; nested exception is org.springframework.boot.autoconfigure.jdbc.DataSourceProperties$DataSourceBeanCreationException: Failed to determine a suitable driver class
问题解决
其实很简单,是 druid 跟 dynamic-datasouce 的 jar 包冲突了,去掉了原来的 druid 的引用就可以了。因为 dynamic-datasource 里面本身已经引用了 druid
下面是集成 druid 跟 dynamic-dataSource 的方法
druid
首先 Druid 是什么?
Druid 是 Java 语言中最好的数据库连接池。Druid 能够提供强大的监控和扩展功能。
阿里 Druid 为 SpringBoot 提供专门的 start 依赖,Mybatis 使用 Druid 相对比较简单,我们只需要引入 Druid 的 starter 依赖并添加相关的一些配置即可。(自行了解)
spring:
# 配置数据库信息
datasource:
# 数据源配置
username: root
password: 123456
url: jdbc:mysql://localhost:3306/mybatis?serverTimezone=GMT%2B8&characterEncoding=UTF-8&useSSL=false
driver-class-name: com.mysql.cj.jdbc.Driver
type: com.alibaba.druid.pool.DruidDataSource
# druid相关配置
druid:
# 初始化 最小 最大
initial-size: 5
min-idle: 5
max-active: 20
# 配置获取连接等待超时的时间
max-wait: 60000
# 配置间隔多久才进行一次检测,检测需要关闭的空闲连接,单位是毫秒
time-between-eviction-runs-millis: 60000
# 配置一个连接在池中最小生存的时间,单位是毫秒
min-evictable-idle-time-millis: 300000
validation-query: SELECT 'x'
test-while-idle: true
test-on-borrow: false
test-on-return: false
# 打开PSCache,并且指定每个连接上PSCache的大小
poolPreparedStatements: true
maxPoolPreparedStatementPerConnectionSize: 20
# 配置多个英文逗号分隔
filters: stat,wall
# WebStatFilter配置,说明请参考Druid Wiki,配置_配置WebStatFilter
# 是否启用StatFilter默认值true
web-stat-filter:
enabled: true
url-pattern: /*
exclusions: "*.js,*.gif,*.jpg,*.png,*.css,*.ico,/druid/*"
session-stat-enable: false
session-stat-max-count: 1000
principal-cookie-name: admin
principal-session-name: admin
profile-enable: true
# 根据配置中的url-pattern来访问内置监控页面,如果是上面的配置,内置监控页面的首页是/druid/index.html
# http://loacalhsot:8081/druid
stat-view-servlet:
url-pattern: /druid/* # 监控页面访问路径
# 允许清空统计数据
reset-enable: true
login-username: admin
login-password: 123456
# StatViewSerlvet展示出来的监控信息比较敏感,是系统运行的内部情况,如果你需要做访问控制,可以配置allow和deny这两个参数
# deny优先于allow,如果在deny列表中,就算在allow列表中,也会被拒绝。如果allow没有配置或者为空,则允许所有访问
# 配置的格式
# <IP>
# 或者<IP>/<SUB_NET_MASK_size>其中128.242.127.1/24
# 24表示,前面24位是子网掩码,比对的时候,前面24位相同就匹配,不支持IPV6。
allow:
deny:
动态数据源
添加依赖
<!-- 动态数据源 -->
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>dynamic-datasource-spring-boot-starter</artifactId>
<version>2.5.4</version>
</dependency>
注释掉原来的数据源配置信息,替换为下面的,注意缩进
spring:
datasource:
dynamic:
primary: master
datasource:
master:
url: jdbc:oracle:thin:@XX:1521:orcl
username: XX
password: XX
driver-class-name: oracle.jdbc.driver.OracleDriver
mysql:
url: jdbc:mysql://XX:3306/yqhb?serverTimezone=GMT%2B8&characterEncoding=UTF-8&useUnicode=true&useSSL=false&tinyInt1isBit=false&useAffectedRows=true
username: XXX
password: xx.xx
driver-class-name: com.mysql.cj.jdbc.Driver