springboot+druid连接池及监控配置

1. 问题描述

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

2. 解决方案

2.1 pom.xml

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

      <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>druid</artifactId>
            <version>1.0.26</version>
        </dependency>
        
      <!--starter有点问题,没用-->
       <!-- <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>druid-spring-boot-starter</artifactId>
            <version>1.1.10</version>
        </dependency>-->

2.2 连接池配置

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

    <!-- datasource configuration -->
    <bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource" init-method="init" destroy-method="close">
        <property name="url" value="${jdbc.url}"/>
        <property name="username" value="${jdbc.username}"/>
        <property name="password" value="${jdbc.password}"/>

        <property name="maxActive" value="${spring.datasource.maxActive}" />
        <property name="initialSize" value="${spring.datasource.initialSize}" />
        <property name="maxWait" value="${spring.datasource.maxWait}" />
        <property name="minIdle" value="${spring.datasource.minIdle}" />
        <property name="timeBetweenEvictionRunsMillis" value="${spring.datasource.timeBetweenEvictionRunsMillis}" />
        <property name="minEvictableIdleTimeMillis" value="${spring.datasource.minEvictableIdleTimeMillis}" />
        <property name="testWhileIdle" value="${spring.datasource.testWhileIdle}" />
        <property name="testOnBorrow" value="${spring.datasource.testOnBorrow}" />
        <property name="testOnReturn" value="${spring.datasource.testOnReturn}" />
        <property name="poolPreparedStatements" value="${spring.datasource.poolPreparedStatements}" />
        <property name="maxPoolPreparedStatementPerConnectionSize" value="${spring.datasource.maxPoolPreparedStatementPerConnectionSize}" />
        <property name="filters" value="${spring.datasource.filters}"  />
        <property name="connectionProperties" value="${spring.datasource.connectionProperties}"  />
    </bean>

2.3 监控配置

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

2.3.1 filter类
@WebFilter(filterName = "druidWebStatFilter", urlPatterns = "/*",
        initParams = {
                @WebInitParam(name = "exclusions", value = "*.js,*.gif,*.jpg,*.bmp,*.png,*.css,*.ico,/druid/*")
        }
)
public class DruidStatFilter extends WebStatFilter {

}
2.3.2 servlet类
@WebServlet(urlPatterns = "/druid/*",
        initParams = {
                @WebInitParam(name = "loginUsername", value = "laowang"),// 用户名
                @WebInitParam(name = "loginPassword", value = "lw123"),// 密码
                @WebInitParam(name = "resetEnable", value = "false")// 禁用HTML页面上的“Reset All”功能
        }
)
public class DruidStatViewServlet extends StatViewServlet {
    private static final long serialVersionUID = 1L;
}

2.3.3 启动类上增加标签
@ServletComponentScan
@SpringBootApplication
public class SptestApplication {

    public static void main(String[] args) {
        SpringApplication.run(SptestApplication.class, args);
    }

}

ok,配置完了。

2.4 访问界面

2.4.1 访问首页

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

2.4.2 SQL监控

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

2.4.3 URI监控

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


原文地址:https://www.cnblogs.com/ruanjianlaowang/p/11182689.html