SpringBoot集成Druid

Druid

  • 是一个阿里开源的连接池组件
  • 加⼊了日志监控,不需要额外的界面开发

详细介绍可以看官方文档:https://www.alibabacloud.com/help/zh/doc-detail/72987.htm

在springboot 2.0 以上默认都是使⽤用Hikari数据源, Hikari与Druid都是当前⽐比较优秀的数据源

com.alibaba.druid.pool.DruidDataSource 基本配置参数如下:

配置 缺省值 说明
name 配置这个属性的意义在于,如果存在多个数据源,监控的时候可以通过名字来区分开来。 如果没有配置,将会生成一个名字,格式是:“DataSource-” + System.identityHashCode(this)
jdbcUrl 连接数据库的url,不同数据库不一样。例如: mysql : jdbc:mysql://10.20.153.104:3306/druid2 oracle : jdbc:oracle:thin:@10.20.149.85:1521:ocnauto
username 连接数据库的用户名
password 连接数据库的密码。如果你不希望密码直接写在配置文件中,可以使用ConfigFilter。详细看这里:https://github.com/alibaba/druid/wiki/%E4%BD%BF%E7%94%A8ConfigFilter
driverClassName 根据url自动识别 这一项可配可不配,如果不配置druid会根据url自动识别dbType,然后选择相应的driverClassName(建议配置下)
initialSize 0 初始化时建立物理连接的个数。初始化发生在显示调用init方法,或者第一次getConnection时
maxActive 8 最大连接池数量
maxIdle 8 已经不再使用,配置了也没效果
minIdle 最小连接池数量
maxWait 获取连接时最大等待时间,单位毫秒。配置了maxWait之后,缺省启用公平锁,并发效率会有所下降,如果需要可以通过配置useUnfairLock属性为true使用非公平锁。
poolPreparedStatements false 是否缓存preparedStatement,也就是PSCache。PSCache对支持游标的数据库性能提升巨大,比如说oracle。在mysql下建议关闭。
maxOpenPreparedStatements -1 要启用PSCache,必须配置大于0,当大于0时,poolPreparedStatements自动触发修改为true。在Druid中,不会存在Oracle下PSCache占用内存过多的问题,可以把这个数值配置大一些,比如说100
validationQuery 用来检测连接是否有效的sql,要求是一个查询语句。如果validationQuery为null,testOnBorrow、testOnReturn、testWhileIdle都不会其作用。
testOnBorrow true 申请连接时执行validationQuery检测连接是否有效,做了这个配置会降低性能。
validationQueryTimeout 单位:秒,检测连接是否有效的超时时间。底层调⽤用jdbc Statement对象的void setQueryTimeout(int seconds)⽅方法
testOnReturn false 归还连接时执行validationQuery检测连接是否有效,做了这个配置会降低性能
testWhileIdle false 建议配置为true,不影响性能,并且保证安全性。申请连接的时候检测,如果空闲时间大于timeBetweenEvictionRunsMillis,执行validationQuery检测连接是否有效。
timeBetweenEvictionRunsMillis 1分钟(1.0.14) 有两个含义: 1) Destroy线程会检测连接的间隔时间2) testWhileIdle的判断依据,详细看testWhileIdle属性的说明
numTestsPerEvictionRun 不再使用,一个DruidDataSource只支持一个EvictionRun
minEvictableIdleTimeMillis 30分钟(1.0.14) 连接保持空闲⽽而不不被驱逐的最⻓长时间
connectionInitSqls 物理连接初始化的时候执行的sql
exceptionSorter 根据dbType自动识别 当数据库抛出一些不可恢复的异常时,抛弃连接
filters 属性类型是字符串,通过别名的方式配置扩展插件,常用的插件有: 监控统计用的filter:stat日志用的filter:log4j防御sql注入的filter:wall
proxyFilters 类型是List<com.alibaba.druid.filter.
maxPoolPreparedStatementPerConnectionSize 10 要启⽤用PSCache,必须配置⼤大于0,当⼤大于0时poolPreparedStatements自动触发修改为true。单个connnection独享⼀一个statement cache,也就是说maxOpenPreparedStatements是针对单个connection链接的

连接池运行原理

  • 连接池初始化的时候按照initialSize创建多个连接
  • 有DB操作访问的时候,就从里面取一个
  • 如果当前正在使用的连接=maxActive,就会进入等待,没有到maxActive拿一个空闲连接,没有空闲就创建一个新连接,等待超过maxWati则会报错
  • 使用完毕还回去等待其它人用,不会物理销毁
  • 每一个connection在连接池里都有空闲时长的,允许最大空闲时长:minEvictableldleTimeMillis,多久检测一次:timeBetweenEvictionRunsMillis
  • maxActive如何配置:理论上应该设置成最大并发数
  • 只要连接被前端业务拿到就算不空闲了,这个时候如果说长时间占有连接,连接数是不会崩溃,连接池主动关闭是使用中的连接,属于一个高级功能
    removeAbandoned=“true”
    removeAbandonedTimeout=“60”
    logAbandoned=“true”

知道了连接池的运行原理之后,我们就正式来用SpringBoot来集成它吧

  1. 导入依赖:
<dependency>
    <groupId>com.alibaba</groupId>
    <artifactId>druid-spring-boot-starter</artifactId>
    <version>1.1.21</version>
</dependency>

 
 
  • 1
  • 2
  • 3
  • 4
  • 5
  1. 编写配置文件:application.yaml
 spring:
datasource:
  username: root
  password: 123456
  url: jdbc:mysql://localhost:3306/ec_admin?useUnicode=true&characterEncoding=UTF-8
  driver-class-name: com.mysql.jdbc.Driver
  type: com.alibaba.druid.pool.DruidDataSource
  #Spring Boot 默认是不注入这些属性值的,需要自己绑定
  #druid 数据源专有配置
  initialSize: 5
  minIdle: 5
  maxActive: 20
  maxWait: 60000
  timeBetweenEvictionRunsMillis: 60000
  minEvictableIdleTimeMillis: 300000
  validationQuery: SELECT 1 FROM DUAL
  testWhileIdle: true
  testOnBorrow: false
  testOnReturn: false
  # 打开PSCache
  poolPreparedStatements: true
  #配置监控统计拦截的filters,stat:监控统计、log4j:日志记录、wall:防御sql注入
  #如果运行时报错  java.lang.ClassNotFoundException: org.apache.log4j.Priority
  #则导入 log4j 依赖即可,Maven 地址: https://mvnrepository.com/artifact/log4j/log4j
  filters: stat,wall,log4j,config
  #指定每个连接上PSCache的大小
  maxPoolPreparedStatementPerConnectionSize: 20
  #合并多个DruidDataSource的监控数据
  useGlobalDataSourceStat: true
  #通过connectProperties属性来打开mergeSql功能;慢SQL记录
  connectionProperties: druid.stat.mergeSql=true;druid.stat.slowSqlMillis=5000
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32

3、编写配置类:

@Configuration
public class DruidConfig {
<span class="token annotation punctuation">@ConfigurationProperties</span><span class="token punctuation">(</span>prefix <span class="token operator">=</span> <span class="token string">"spring.datasource"</span><span class="token punctuation">)</span>
<span class="token annotation punctuation">@Bean</span>
<span class="token keyword">public</span> DataSource <span class="token function">druidDataSource</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">{<!-- --></span>
    <span class="token keyword">return</span> <span class="token keyword">new</span> <span class="token class-name">DruidDataSource</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span>
<span class="token comment">//因为Springboot内置了servlet容器,所以没有web.xml,替代方法就是将ServletRegistrationBean注册进去</span>
<span class="token comment">//加入后台监控</span>
<span class="token annotation punctuation">@Bean</span>  <span class="token comment">//这里其实就相当于servlet的web.xml</span>
<span class="token keyword">public</span> ServletRegistrationBean <span class="token function">statViewServlet</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">{<!-- --></span>
    ServletRegistrationBean<span class="token generics function"><span class="token punctuation">&lt;</span>StatViewServlet<span class="token punctuation">&gt;</span></span> bean <span class="token operator">=</span>
            <span class="token keyword">new</span> <span class="token class-name">ServletRegistrationBean</span><span class="token generics function"><span class="token punctuation">&lt;</span>StatViewServlet<span class="token punctuation">&gt;</span></span><span class="token punctuation">(</span><span class="token keyword">new</span> <span class="token class-name">StatViewServlet</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">,</span><span class="token string">"/druid/*"</span><span class="token punctuation">)</span><span class="token punctuation">;</span>

    <span class="token comment">//后台需要有人登录,进行配置</span>
    <span class="token operator">/</span><span class="token operator">/</span>bean<span class="token punctuation">.</span><span class="token function">addUrlMappings</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span> 这个可以添加映射,我们在构造里已经写了
    <span class="token operator">/</span><span class="token operator">/</span>设置一些初始化参数
    Map<span class="token generics function"><span class="token punctuation">&lt;</span>String<span class="token punctuation">,</span>String<span class="token punctuation">&gt;</span></span> initParas <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">HashMap</span><span class="token generics function"><span class="token punctuation">&lt;</span>String<span class="token punctuation">,</span>String<span class="token punctuation">&gt;</span></span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    initParas<span class="token punctuation">.</span><span class="token function">put</span><span class="token punctuation">(</span><span class="token string">"loginUsername"</span><span class="token punctuation">,</span><span class="token string">"admin"</span><span class="token punctuation">)</span><span class="token punctuation">;</span><span class="token operator">/</span><span class="token operator">/</span>它这个账户密码是固定的
    initParas<span class="token punctuation">.</span><span class="token function">put</span><span class="token punctuation">(</span><span class="token string">"loginPassword"</span><span class="token punctuation">,</span><span class="token string">"123456"</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token operator">/</span><span class="token operator">/</span>允许谁能防伪
    initParas<span class="token punctuation">.</span><span class="token function">put</span><span class="token punctuation">(</span><span class="token string">"allow"</span><span class="token punctuation">,</span><span class="token string">""</span><span class="token punctuation">)</span><span class="token punctuation">;</span><span class="token operator">/</span><span class="token operator">/</span>这个值为空或没有就允许所有人访问,ip白名单
    <span class="token operator">/</span><span class="token operator">/</span>initParas<span class="token punctuation">.</span><span class="token function">put</span><span class="token punctuation">(</span><span class="token string">"allow"</span><span class="token punctuation">,</span><span class="token string">"localhost"</span><span class="token punctuation">)</span><span class="token punctuation">;</span><span class="token operator">/</span><span class="token operator">/</span>只允许本机访问,多个ip用逗号<span class="token punctuation">,</span>隔开
    <span class="token operator">/</span><span class="token operator">/</span>initParas<span class="token punctuation">.</span><span class="token function">put</span><span class="token punctuation">(</span><span class="token string">"deny"</span><span class="token punctuation">,</span><span class="token string">""</span><span class="token punctuation">)</span><span class="token punctuation">;</span><span class="token operator">/</span><span class="token operator">/</span>ip黑名单,拒绝谁访问 deny和allow同时存在优先deny
    initParas<span class="token punctuation">.</span><span class="token function">put</span><span class="token punctuation">(</span><span class="token string">"resetEnable"</span><span class="token punctuation">,</span><span class="token string">"false"</span><span class="token punctuation">)</span><span class="token punctuation">;</span><span class="token operator">/</span><span class="token operator">/</span>禁用HTML页面的Reset按钮
    bean<span class="token punctuation">.</span><span class="token function">setInitParameters</span><span class="token punctuation">(</span>initParas<span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token keyword">return</span> bean<span class="token punctuation">;</span>
<span class="token punctuation">}</span>

<span class="token operator">/</span><span class="token operator">/</span>再配置一个过滤器,Servlet按上面的方式注册Filter也只能这样
<span class="token annotation punctuation">@Bean</span>
<span class="token keyword">public</span> FilterRegistrationBean <span class="token function">webStatFilter</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">{<!-- --></span>
    FilterRegistrationBean bean <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">FilterRegistrationBean</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token operator">/</span><span class="token operator">/</span>可以设置也可以获取<span class="token punctuation">,</span>设置一个阿里巴巴的过滤器
    bean<span class="token punctuation">.</span><span class="token function">setFilter</span><span class="token punctuation">(</span><span class="token keyword">new</span> <span class="token class-name">WebStatFilter</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    bean<span class="token punctuation">.</span><span class="token function">addUrlPatterns</span><span class="token punctuation">(</span><span class="token string">"/*"</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token comment">//可以过滤和排除哪些东西</span>
    Map<span class="token generics function"><span class="token punctuation">&lt;</span>String<span class="token punctuation">,</span>String<span class="token punctuation">&gt;</span></span> initParams <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">HashMap</span><span class="token generics function"><span class="token punctuation">&lt;</span>String<span class="token punctuation">,</span>String<span class="token punctuation">&gt;</span></span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token operator">/</span><span class="token operator">/</span>把不需要监控的过滤掉<span class="token punctuation">,</span>这些不进行统计
    initParams<span class="token punctuation">.</span><span class="token function">put</span><span class="token punctuation">(</span><span class="token string">"exclusions"</span><span class="token punctuation">,</span><span class="token string">"*.js,*.css,/druid/*"</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    bean<span class="token punctuation">.</span><span class="token function">setInitParameters</span><span class="token punctuation">(</span>initParams<span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token keyword">return</span> bean<span class="token punctuation">;</span>
<span class="token punctuation">}</span>
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44

编写完之后我们启动项目,访问http://localhost:8080/druid/login.html
就会到Druid的监控登录界面
SpringBoot集成Druid
输入我们配置的登录用户名和密码进行登录即可访问
SpringBoot集成Druid
使用这个我们可以很好的监控我们的数据库了
SpringBoot集成Druid
SpringBoot集成Druid
这里要注意一个参数:PSCache

PSCache

  • PreparedStatement Cache

  • 就要从MySQL的SQL执⾏行行过程来讲
    SpringBoot集成Druid

  • 连接器器

    • 进⾏行行数据库连接,⽤用户名密码验证
    • show processlist
  • 查询缓存

    • key-value, key是SQL, value就是结果集
    • ⼤大多数情况下不不建议使⽤用查询缓存
      • testOnBorrow: false
      • testOnReturn: false
      • poolPreparedStatements: false
    • query_cache_type : DEMAND //默认的SQL都不不使⽤用查询缓存
    • mysql> select SQL_CACHE * from student where id=1; //声明使⽤用
  • 分析器器

    • 词法分析
      • SQL的关键词进⾏行行识别
    • 语法分析
      • 对语法进⾏行行分析 “use near”
  • 优化器器

    • 决定我们SQL使⽤用那个⼈人索引
  • 执⾏器

    • 会判断你是否对这个表有查询/更更新权限
  • 存储引擎

    • 获取数据的具体⽂文件地址

SQL的写法
select * from userinfo where id=? //PreparedStatement id=1 id=2 硬解析1次
select * from userinfo where id=1 / select * from user info id=2(这个在数据库都认为是两个不不同的SQL)硬解析2次

词法、语法、 语义、优化器器(执⾏计划)、执⾏
硬解析:词法、语法
结论: prepare语法适⽤用于执⾏行行频繁且复杂的SQL
prepare+execute两次

数据库密码加密
执行以下命令,会生成加密的密码和private key,public key

java -cp /Users/gavin/Documents/maven-repo/com/alibaba/druid/1.1.21/druid-
1.1.21.jar com.alibaba.druid.filter.config.ConfigTools 你的密码

 
 
  • 1
  • 2

生成的public key:

druid.stat.mergeSql=true;druid.stat.slowSqlMillis=5000;config.decrypt=true;con
fig.decrypt.key=MFwwDQYJKoZIhvcNAQEBBQADSwAwSAJBAM9l5I7/YAFtLPr2Ep1G70ovzMX8Ncd9jcuKUWaWEiJ/3SLu90v/K0IPxqYGDYaI+QmulYgecmyQ+XJIdpimtqsCAwEAAQ==

#通过connectProperties属性来打开mergeSql功能;慢SQL记录,这⾥里里⽅方publickey
connectionProperties:
druid.stat.mergeSql=true;druid.stat.slowSqlMillis=5000;config.decrypt=true;con
fig.decrypt.key=MFwwDQYJKoZIhvcNAQEBBQADSwAwSAJBAM9l5I7/YAFtLPr2Ep1G70ovzMX8Nc
d9jcuKUWaWEiJ/3SLu90v/K0IPxqYGDYaI+QmulYgecmyQ+XJIdpimtqsCAwEAAQ

 
 
  • 1
  • 2
  • 3
  • 4
  • 5

使用spring框架配置,可以参考这篇文章:https://www.cnblogs.com/wuyun-blog/p/5679073.html

</article>
上一篇:领域建模笔记


下一篇:JAVA后端如何解决跨域问题