1. 问题描述

阿里巴巴的数据库连接池Druid在效率与稳定性都很高,被很多开发团队使用,并且自带的Druid监控也很好用,本章简单介绍下springboot+druid配置连接池及监控。

2. 解决方案

2.1 pom.xml

springboot 已经有druid的starter,但是好像有点问题,不知道为什么没拿到jar包,有可能是网络问题,还是使用了原生的druid gav。

 1      <dependency>
 2            <groupId>com.alibaba</groupId>
 3            <artifactId>druid</artifactId>
 4            <version>1.0.26</version>
 5        </dependency>
 6
 7      <!--starter有点问题,没用-->
 8       <!-- <dependency>
 9            <groupId>com.alibaba</groupId>
10            <artifactId>druid-spring-boot-starter</artifactId>
11            <version>1.1.10</version>
12        </dependency>-->

2.2 连接池配置

连接池配置没啥说的,我们当时为了省事,直接将以前项目中的xml导过来了(在application启动上加个标签就好了)

 1    <!-- datasource configuration -->
 2    <bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource" init-method="init" destroy-method="close">
 3        <property name="url" value="${jdbc.url}"/>
 4        <property name="username" value="${jdbc.username}"/>
 5        <property name="password" value="${jdbc.password}"/>
 6
 7        <property name="maxActive" value="${spring.datasource.maxActive}" />
 8        <property name="initialSize" value="${spring.datasource.initialSize}" />
 9        <property name="maxWait" value="${spring.datasource.maxWait}" />
10        <property name="minIdle" value="${spring.datasource.minIdle}" />
11        <property name="timeBetweenEvictionRunsMillis" value="${spring.datasource.timeBetweenEvictionRunsMillis}" />
12        <property name="minEvictableIdleTimeMillis" value="${spring.datasource.minEvictableIdleTimeMillis}" />
13        <property name="testWhileIdle" value="${spring.datasource.testWhileIdle}" />
14        <property name="testOnBorrow" value="${spring.datasource.testOnBorrow}" />
15        <property name="testOnReturn" value="${spring.datasource.testOnReturn}" />
16        <property name="poolPreparedStatements" value="${spring.datasource.poolPreparedStatements}" />
17        <property name="maxPoolPreparedStatementPerConnectionSize" value="${spring.datasource.maxPoolPreparedStatementPerConnectionSize}" />
18        <property name="filters" value="${spring.datasource.filters}"  />
19        <property name="connectionProperties" value="${spring.datasource.connectionProperties}"  />
20    </bean>

2.3 监控配置

默认继承了一个filter,一个Servlet,使用了两个标签。

2.3.1 filter类
1@WebFilter(filterName = "druidWebStatFilter", urlPatterns = "/*",
2        initParams = {
3                @WebInitParam(name = "exclusions", value = "*.js,*.gif,*.jpg,*.bmp,*.png,*.css,*.ico,/druid/*")
4        }
5)
6public class DruidStatFilter extends WebStatFilter {
7
8}
2.3.2 servlet类
 1@WebServlet(urlPatterns = "/druid/*",
 2        initParams = {
 3                @WebInitParam(name = "loginUsername", value = "laowang"),// 用户名
 4                @WebInitParam(name = "loginPassword", value = "lw123"),// 密码
 5                @WebInitParam(name = "resetEnable", value = "false")// 禁用HTML页面上的“Reset All”功能
 6        }
 7)
 8public class DruidStatViewServlet extends StatViewServlet {
 9    private static final long serialVersionUID = 1L;
10}
2.3.3 启动类上增加标签
1@ServletComponentScan
2@SpringBootApplication
3public class SptestApplication {
4
5    public static void main(String[] args) {
6        SpringApplication.run(SptestApplication.class, args);
7    }
8
9}

ok,配置完了。

2.4 访问界面

2.4.1 访问首页

地址:http://192.168.0.10:9107/druid/login.html

2.4.2 SQL监控

可以监控具体执行的sql,时间行数等,用于分析慢SQL,优化响应时间。

springboot+druid连接池及监控配置_springboot
2.4.3 URI监控

可以看到接口的执行时间及访问次数,为系统优化提供参考。

springboot+druid连接池及监控配置_springboot_02

 

springboot+druid连接池及监控配置_springboot_03