您当前的位置: 首页 >  ui

qq_34412985

暂无认证

  • 1浏览

    0关注

    1061博文

    0收益

  • 0浏览

    0点赞

    0打赏

    0留言

私信
关注
热门博文

druid简介及springboot中使用druid

qq_34412985 发布时间:2020-04-12 12:14:41 ,浏览量:1

“Druid连接池是阿⾥里里巴巴开源的数据库连接池项⽬目。Druid连接池为监控⽽而⽣生,内置强⼤大的监控功能,监控特性不不影响性能。功能强⼤大,能防SQL注⼊入,内置Logging能诊断Hack应⽤用⾏行行为。”    –Alibaba Druid 官⽅方介绍

Druid

经过阿⾥里里巴巴各⼤大系统的考验,值得信赖  官网地址:https://github.com/alibaba/druid/

实⽤的功能

   •详细的监控(真的是全⾯)  web的static界面,实时看到数据源的一些状态

   •ExceptionSorter,针对主流数据库的返回码都有支持 和JBoss数据源像

   •SQL 防注入

   •内置加密配置

   •众多扩展点,方便进行定制 数据库操作的各个环节都留有扩展点,方便自己定制;例如取得连接后操作,执行一条sql语句statement执行之前,之后操作,获得resultset之后操作

springboot2.x中的pom.xml中排除掉HikariCP

数据源配置

直接配置 DruidDataSource

通过 druid-spring-boot-starter

    •spring.datasource.druid.*

Filter 配置

     •spring.datasource.druid.filters=stat,config,wall,log4j (全部使⽤用默认值)密码加密

     •spring.datasource.password=

     •spring.datasource.druid.filter.config.enabled=true

     •spring.datasource.druid.connection-properties=config.decrypt=true;config.decrypt.key=

SQL 防注入

     •spring.datasource.druid.filter.wall.enabled=true

     •spring.datasource.druid.filter.wall.db-type=h2

     •spring.datasource.druid.filter.wall.config.delete-allow=false

     •spring.datasource.druid.filter.wall.config.drop-table-allow=false

慢 SQL ⽇日志 系统属性配置 • druid.stat.logSlowSql=true • druid.stat.slowSqlMillis=3000 Spring Boot • spring.datasource.druid.filter.stat.enabled=true    默认开启统计 • spring.datasource.druid.filter.stat.log-slow-sql=true • spring.datasource.druid.filter.stat.slow-sql-millis=3000  默认值

一些注意事项 • 没特殊情况,不要在⽣产环境打开监控的 Servlet • 没有连接泄露可能的情况下,不要开启 removeAbandoned  黑客和SLB切换A,B机器看到的信息不一定准确,线上以日志输出为主 • testXxx 的使用需要注意  test-while-idle可以使用,但每次取borrow和放return开销挺大的,特殊场景下使用 • 务必配置合理的超时时间  建立连接配置超时,connection timeout避免长时间卡死

DruidDataSource#  removeAbandoned  Thread.getCurrentThread.trace()性能开销大,使用orm框架后很难遇到连接泄露的情况

Druid Filter在DruidDataSource# 责任链设计模式,一步一步判断,每个filter做自己的增强,最后执行获取连接的动作;druid中扩展通过责任链实现的;

Druid Filter

    •⽤用于定制连接池操作的各种环节

    •可以继承 FilterEventAdapter 以便便⽅方便便地实现 Filter

    •修改 META-INF/druid-filter.properties 增加 Filter 配置

可靠性和性能最重要

业务操作做了哪些sql语句,服务追踪,traceId,spId,能否也在sql中加上traceId和spId?在数据库连接池在每条sql执行前加入一条注释,把traceId加进去;对sql做一些拦截或一些预处理。数据库中间件不知道业务线用的hibernate还是mybatis,使用最底层数据库连接池来做更通用些。

社区是否还活跃,是否还有更新。  

关注
打赏
1653291990
查看更多评论
立即登录/注册

微信扫码登录

0.0405s