- 加依赖
- 修改yml文件配置
- 排除
DruidDataSourceAutoConfigure
依赖:
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>dynamic-datasource-spring-boot-starter</artifactId>
<version>3.3.1</version>
</dependency>
yml
spring:
datasource:
druid:
stat-view-servlet:
enabled: true
dynamic:
primary: master #设置默认的数据源或者数据源组,默认值即为master
strict: false #设置严格模式,默认false不启动. 启动后在未匹配到指定数据源时候会抛出异常,不启动则使用默认数据源.
p6spy: true # 默认false,建议线上关闭。
# 配置全局druid参数,请按需配置
druid:
initial-size: 5
max-active: 8
min-idle: 3
max-wait: 1000
filters: stat
datasource:
master:
driver-class-name: com.p6spy.engine.spy.P6SpyDriver
url: jdbc:p6spy:mysql://你的地址:3306/xx?zeroDateTimeBehavior=convertToNull&useUnicode=true&characterEncoding=utf-8&useSSL=false&allowMultiQueries=true&serverTimezone=Asia/Shanghai
username: root
password: 你的密码
initialSize: 2
minIdle: 2
maxActive: 5
maxWait: 60000
timeBetweenEvictionRunsMillis: 60000
minEvictableIdleTimeMillis: 300000
validationQuery: select 1
testOnBorrow: true
testWhileIdle: true
testOnReturn: false
slave1:
driver-class-name: com.mysql.cj.jdbc.Driver
url: jdbc:mysql://你的地址:3306/xx?zeroDateTimeBehavior=convertToNull&useUnicode=true&characterEncoding=utf-8&useSSL=false&allowMultiQueries=true&serverTimezone=Asia/Shanghai
username: root
password: 密码
initialSize: 2
minIdle: 2
maxActive: 5
maxWait: 60000
timeBetweenEvictionRunsMillis: 60000
minEvictableIdleTimeMillis: 300000
validationQuery: select 1
testOnBorrow: true
testWhileIdle: true
testOnReturn: false
排除 class
启动类中@SpringBootApplication(exclude = {DruidDataSourceAutoConfigure.class})