当前位置: 首页 > news >正文

Spring Boot 2整合Druid的两种方式

一、自定义整合Druid(非Starter方式)

适用于需要完全手动控制配置的场景

  1. 添加依赖(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>

  1. 创建配置类
@Configuration
public class DruidConfig {@Bean@ConfigurationProperties(prefix = "spring.datasource")public DataSource druidDataSource() {return new DruidDataSource();}// 配置监控服务器@Beanpublic 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;}// 配置过滤器@Beanpublic FilterRegistrationBean<WebStatFilter> webStatFilter() {FilterRegistrationBean<WebStatFilter> bean = new FilterRegistrationBean<>(new WebStatFilter());bean.addUrlPatterns("/*");bean.addInitParameter("exclusions", "*.js,*.css,/druid/*");return bean;}
}

  1. application.yml配置
spring:datasource:url: jdbc:mysql://localhost:3306/testdbusername: rootpassword: root123driver-class-name: com.mysql.cj.jdbc.Drivertype: com.alibaba.druid.pool.DruidDataSource# 连接池配置initialSize: 5minIdle: 5maxActive: 20maxWait: 60000timeBetweenEvictionRunsMillis: 60000minEvictableIdleTimeMillis: 300000validationQuery: SELECT 1testWhileIdle: truetestOnBorrow: falsetestOnReturn: falsepoolPreparedStatements: truefilters: stat,wall

二、使用Starter整合Druid(推荐)

官方提供的简化方案,自动配置监控页面

  1. 添加Starter依赖
<dependency><groupId>com.alibaba</groupId><artifactId>druid-spring-boot-starter</artifactId><version>1.2.8</version>
</dependency>

  1. application.yml完整配置
spring:datasource:url: jdbc:mysql://localhost:3306/testdbusername: rootpassword: root123driver-class-name: com.mysql.cj.jdbc.Drivertype: com.alibaba.druid.pool.DruidDataSourcedruid:# 连接池配置initial-size: 5min-idle: 5max-active: 20max-wait: 60000# 监控配置stat-view-servlet:enabled: trueurl-pattern: /druid/*login-username: adminlogin-password: admin123reset-enable: falseweb-stat-filter:enabled: trueurl-pattern: /*exclusions: "*.js,*.gif,*.jpg,*.png,*.css,*.ico,/druid/*"# 防火墙配置filter:wall:config:multi-statement-allow: true

三、关键功能验证
  1. 监控界面访问

    • 访问 http://localhost:8080/druid
    • 使用配置的用户名/密码登录
  2. SQL监控

    @RestController
    public class TestController {@Autowiredprivate JdbcTemplate jdbcTemplate;@GetMapping("/users")public List<Map<String, Object>> getUsers() {return jdbcTemplate.queryForList("SELECT * FROM user");}
    }
    

四、配置优化建议
  1. 生产环境安全

    druid:stat-view-servlet:allow: 192.168.1.100  # 限制访问IPdeny: 192.168.1.73
    

  2. 性能调优参数

    druid:max-wait: 1000validation-query: SELECT 1test-on-borrow: falsetest-on-return: falsetest-while-idle: true
    

  3. 启用SQL防火墙

    druid:filter:wall:enabled: trueconfig:drop-table-allow: false
    

注意:使用Starter方式时,监控页面默认路径为/druid/*,如需修改可在配置中调整url-pattern参数。两种方式都支持完整的连接池参数配置,但Starter方式简化了监控功能的集成。

http://www.dtcms.com/a/302950.html

相关文章:

  • shell学习从入门到精通(第二部分)
  • 第六届物联网、人工智能与机械自动化国际学术会议 (IoTAIMA 2025)
  • 暑期自学嵌入式——Day10(C语言阶段)
  • springboot校园外卖配送系统
  • Stm32中USB 对时钟的要求
  • 使用 Scrapy 框架定制爬虫中间件接入淘宝 API 采集商品数据
  • 案例开发 - 日程管理 - 第三期
  • HOT100——链表篇Leetcode206. 反转链表
  • IP核乘法器NCO的使用
  • 多目标优化分解方法:加权和与罚函数边界交叉
  • 数据分析入门,深入浅出的数据分析
  • 基于 JWT 的登录验证功能实现详解
  • (多线程)等待一个线程-join() 获取当前线程的引用 线程的六种状态 线程休眠 线程的调度执行中的细节
  • 【边缘填充】——图像预处理(OpenCV)
  • 边缘计算+前端实时性:本地化数据处理在设备监控中的响应优化实践
  • MOEA/D(Multi-Objective Evolutionary Algorithm based on Decomposition)简介
  • 互信息:理论框架、跨学科应用与前沿进展
  • 从卷积到ResNet
  • Light Sci. Appl.:基于结构激发的方解石ghost极化激元红外光电子应用
  • flutter使用firebase集成谷歌,苹果登录
  • 什么是3DVR?VR技术有哪些应用场景?
  • 数学建模——蒙特卡罗法
  • 【优秀案例源码】劳务人力招聘平台系统
  • 深入理解单点登录(SSO
  • 第二十一天(shell俗称“脚本”的初学)练习答案见下一章
  • 完整复现cacti的RCE
  • 【C++进阶】---- 二叉搜索树
  • Pycaita二次开发基础代码解析:点距测量、对象层级关系与选择机制深度剖析
  • c++内联函数的执行原理,内联函数和宏函数的区别
  • Anaconda安装时的几个操作