社会主义接班人 阅读(10) 评论(0)

Druid:为监控而生的数据库连接池.这篇先了解下它的简单使用,下篇尝试用它做多数据源配置。主要参考:https://github.com/alibaba/druid/wiki/常见问题 https://github.com/alibaba/druid/tree/master/druid-spring-boot-starter

一、引入依赖

这里看其他博客都是引用的Druid,由于是使用springboot集成,这里参考druid官方文档,用的是druid-spring-boot-starter。

        <dependency>
           <groupId>com.alibaba</groupId>
           <artifactId>druid-spring-boot-starter</artifactId>
           <version>1.1.10</version>
        </dependency>

二、设置属性

这里用的demo还是在上一博客基础上进行的修改,所以已经配置上了数据库连接和mybatis。

spring.mvc.view.prefix=/view/

spring.mvc.view.suffix=.jsp
mybatis.type-aliases-package=com.example.model
mybatis.config-location=classpath:mybatis/mybatis-config.xml
mybatis.mapper-locations=classpath:mybatis/mapper/*.xml

#spring.datasource.driverClassName = com.mysql.cj.jdbc.Driver
#spring.datasource.url = jdbc:mysql://localhost:3306/mybatis
spring.datasource.url =jdbc:mysql://127.0.0.1:3306/mybatis?useUnicode=true&characterEncoding=UTF-8&serverTimezone=UTC
spring.datasource.username = root
spring.datasource.password = 123456
#spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
spring.datasource.type=com.alibaba.druid.pool.DruidDataSource


spring.datasource.druid.max-active=20

spring.datasource.druid.initial-size=1

spring.datasource.druid.max-wait=60000

spring.datasource.druid.pool-prepared-statements=true
spring.datasource.druid.max-pool-prepared-statement-per-connection-size=20
spring.datasource.druid.connection-properties=druid.stat.mergeSql=true;druid.stat.slowSqlMillis=5000
spring.datasource.druid.min-idle=1
spring.datasource.druid.time-between-eviction-runs-millis=60000
spring.datasource.druid.min-evictable-idle-time-millis=300000
spring.datasource.druid.validation-query=select 1 from dual
spring.datasource.druid.test-while-idle=true
spring.datasource.druid.test-on-borrow=true
spring.datasource.druid.test-on-return=true

这里配置与其他有的博客不太一样,spring.datasource后面多了个druid,属性名称也有一些变化。

三、配置连接池的监控和慢sql处理

这里只需要在启动类同级目录下增加一个class类配置即可。这里创建了DruidConfiguration类,这里配置了WebStatFilter配置和StatViewServlet配置.

package com.example.demo;

import org.springframework.boot.web.servlet.FilterRegistrationBean;
import org.springframework.boot.web.servlet.ServletRegistrationBean;
import org.springframework.context.annotation.*;

import com.alibaba.druid.support.http.StatViewServlet;
import com.alibaba.druid.support.http.WebStatFilter;

@Configuration
public class DruidConfiguration {

    @Bean
    public ServletRegistrationBean statViewServlet(){
        //创建servlet注册实体
        ServletRegistrationBean servletRegistrationBean = new ServletRegistrationBean(new StatViewServlet(),"/druid/*");
        //设置ip白名单
        servletRegistrationBean.addInitParameter("allow","127.0.0.1");
        //设置ip黑名单,如果allow与deny共同存在时,deny优先于allow
        servletRegistrationBean.addInitParameter("deny","192.168.0.19");
        //设置控制台管理用户
        servletRegistrationBean.addInitParameter("loginUsername","druid");
        servletRegistrationBean.addInitParameter("loginPassword","123456");
        //是否可以重置数据
        servletRegistrationBean.addInitParameter("resetEnable","false");
        return servletRegistrationBean;
    }

    @Bean
    public FilterRegistrationBean statFilter(){
        //创建过滤器
        FilterRegistrationBean filterRegistrationBean = new FilterRegistrationBean(new WebStatFilter());
        //设置过滤器过滤路径
        filterRegistrationBean.addUrlPatterns("/*");
        //忽略过滤的形式
        filterRegistrationBean.addInitParameter("exclusions","*.js,*.gif,*.jpg,*.png,*.css,*.ico,/druid/*");
        return filterRegistrationBean;
    }
}

四、测试

这里先启动应用,然后在浏览器输入http://127.0.0.1:8080/druid/index.html,就会显示登录页面,输入上面类配置的密码即可登录.

然后输入上一博客显示用户列表的url:http://localhost:8080/user/alluser.do,再看到druid页面,查看SQL监控就会有刚才执行的sql信息,URL监控和Session监控也有对应的信息,由于这没配置Spring监控所以Spring监控的信息,对于Spring监控会在以后补充,下一篇准备是使用druid配置多数据源。