Files
testmd/关于使用dynamic+druid的配置说明.md
2026-01-10 14:58:56 +08:00

116 lines
3.9 KiB
Markdown
Raw Blame History

This file contains invisible Unicode characters
This file contains invisible Unicode characters that are indistinguishable to humans but may be processed differently by a computer. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
---
date: 2026-1-10 14:54:51
updated: 2026-1-10 14:54:51
title: Druid配置文件说明
---
2025年2月5日安全智能应用发生服务器不可用情况初步分析为数据库连接池过少访问量过大导致的线程等待。经查看安全智能应用的配置文件的Druid的数据库连接池参数未生效经分析是由于配置不规范导致的。下面针对问题进行了说明并给出了配置规范。
# 以安全智能应用原有的配置示例(错误的)
```yaml
spring:
  datasource:
    dynamic:
      primary: master
      strict: true
      datasource:
        master:
          url: jdbc:mysql://xxxxxxx
          driver-class-name: com.mysql.cj.jdbc.Driver
          username: xxxxx
          password: xxxx
        slave:
          url: jdbc:mysql://XXXXXX
          driver-class-name: com.mysql.cj.jdbc.Driver
          username: xxxx
          password: xxx
    druid:
      max-pool-prepared-statement-per-connection-size: 50
      min-idle: 1
      max-active: 100
```
**问题分析**
- `spring.datasource.dynamic.master`下没有配置`type:com.alibaba.druid.pool.DruidDataSource` 属性,这样的结果是不可预测的,会导致一些异常问题。如果没有在`spring.datasource.dynamic.master`下配置`type`属性,会出现两种情况:
- 使用的是动态数据源(`dynamic-datasource-spring-boot-starter`这个starter可能会有自己的配置和自动配置逻辑。不会使用自定义配置的Druid数据库连接池。
- 如果`dynamic-datasource-spring-boot-starter`没有默认的连接池类型设置这取决于该starter的具体实现和版本那么它可能会回退到Spring Boot默认的连接池如HikariCP这是Spring Boot 2.x的默认连接池
- 在提供的配置中,`druid`配置是放在`spring.datasource`下的,这通常意味着这些配置将应用于默认的数据源,由于使用的是动态数据源,这些配置不会应用到所有数据源。
- 如果想要为所有数据源应用相同的Druid配置应该将这些配置放在`spring.datasource.dynamic.druid`下或者每个数据源下单独配置Druid参数。
# 正确的配置
## 方式一: 全局配置
```yaml
spring:
datasource:
dynamic:
primary: master
strict: false
druid:
initial-size: 5
min-idle: 5
max-active: 100
max-wait: 60000
validation-query: SELECT 1
max-pool-prepared-statement-per-connection-size: 20
datasource:
master:
url: jdbc:mysql://xxxx
username: root
password: root
driver-class-name: com.mysql.cj.jdbc.Driver
type: com.alibaba.druid.pool.DruidDataSource
slave:
url: xxxx
username: root
password: root
driver-class-name: com.mysql.cj.jdbc.Driver
type: com.alibaba.druid.pool.DruidDataSource
```
## 方式二: 单独配置
```yaml
spring:
datasource:
dynamic:
primary: master
datasource:
master:
type: com.alibaba.druid.pool.DruidDataSource
driver-class-name: com.mysql.cj.jdbc.Driver
url: jdbc:mysql://xxxx
username: xxxx
password: xxx
druid:
initial-size: 5
min-idle: 5
max-active: 100
max-wait: 60000
max-pool-prepared-statement-per-connection-size: 20
slave:
type: com.alibaba.druid.pool.DruidDataSource
driver-class-name: com.mysql.cj.jdbc.Driver
url: jdbc:mysql://xxxxx
username: xxxx
password: xxxx
druid:
initial-size: 5
min-idle: 5
max-active: 100
max-wait: 60000
max-pool-prepared-statement-per-connection-size: 20
```