一、自定义整合Druid(非Starter方式)
适用于需要完全手动控制配置的场景
- 添加依赖(pom.xml)
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid</artifactId>
<version>1.2.8</version> <!-- 使用最新版本 -->
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-jdbc</artifactId>
</dependency>
- 创建配置类
@Configuration
public class DruidConfig {
@Bean
@ConfigurationProperties(prefix = "spring.datasource")
public DataSource druidDataSource() {
return new DruidDataSource();
}
// 配置监控服务器
@Bean
public ServletRegistrationBean<StatViewServlet> statViewServlet() {
ServletRegistrationBean<StatViewServlet> bean =
new ServletRegistrationBean<>(new StatViewServlet(), "/druid/*");
Map<String, String> initParams = new HashMap<>();
initParams.put("loginUsername", "admin"); // 监控后台登录账号
initParams.put("loginPassword", "admin123");
initParams.put("allow", ""); // 允许所有访问
bean.setInitParameters(initParams);
return bean;
}
// 配置过滤器
@Bean
public FilterRegistrationBean<WebStatFilter> webStatFilter() {
FilterRegistrationBean<WebStatFilter> bean =
new FilterRegistrationBean<>(new WebStatFilter());
bean.addUrlPatterns("/*");
bean.addInitParameter("exclusions", "*.js,*.css,/druid/*");
return bean;
}
}
- application.yml配置
spring:
datasource:
url: jdbc:mysql://localhost:3306/testdb
username: root
password: root123
driver-class-name: com.mysql.cj.jdbc.Driver
type: com.alibaba.druid.pool.DruidDataSource
# 连接池配置
initialSize: 5
minIdle: 5
maxActive: 20
maxWait: 60000
timeBetweenEvictionRunsMillis: 60000
minEvictableIdleTimeMillis: 300000
validationQuery: SELECT 1
testWhileIdle: true
testOnBorrow: false
testOnReturn: false
poolPreparedStatements: true
filters: stat,wall
二、使用Starter整合Druid(推荐)
官方提供的简化方案,自动配置监控页面
- 添加Starter依赖
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid-spring-boot-starter</artifactId>
<version>1.2.8</version>
</dependency>
- application.yml完整配置
spring:
datasource:
url: jdbc:mysql://localhost:3306/testdb
username: root
password: root123
driver-class-name: com.mysql.cj.jdbc.Driver
type: com.alibaba.druid.pool.DruidDataSource
druid:
# 连接池配置
initial-size: 5
min-idle: 5
max-active: 20
max-wait: 60000
# 监控配置
stat-view-servlet:
enabled: true
url-pattern: /druid/*
login-username: admin
login-password: admin123
reset-enable: false
web-stat-filter:
enabled: true
url-pattern: /*
exclusions: "*.js,*.gif,*.jpg,*.png,*.css,*.ico,/druid/*"
# 防火墙配置
filter:
wall:
config:
multi-statement-allow: true
三、关键功能验证
监控界面访问:
- 访问
http://localhost:8080/druid
- 使用配置的用户名/密码登录
- 访问
SQL监控:
@RestController public class TestController { @Autowired private JdbcTemplate jdbcTemplate; @GetMapping("/users") public List<Map<String, Object>> getUsers() { return jdbcTemplate.queryForList("SELECT * FROM user"); } }
四、配置优化建议
生产环境安全:
druid: stat-view-servlet: allow: 192.168.1.100 # 限制访问IP deny: 192.168.1.73
性能调优参数:
druid: max-wait: 1000 validation-query: SELECT 1 test-on-borrow: false test-on-return: false test-while-idle: true
启用SQL防火墙:
druid: filter: wall: enabled: true config: drop-table-allow: false
注意:使用Starter方式时,监控页面默认路径为
/druid/*
,如需修改可在配置中调整url-pattern
参数。两种方式都支持完整的连接池参数配置,但Starter方式简化了监控功能的集成。