欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

SpringBoot系列记录(六)——druid连接池及监控配置

程序员文章站 2022-04-30 13:59:04
...

一、相关POM依赖

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

        <!-- mysql-connector-java -->
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
<!--            <version>5.1.47</version>-->
            <scope>runtime</scope>
        </dependency>
        <!-- mybatis-spring-boot-starter -->
        <dependency>
            <groupId>org.mybatis.spring.boot</groupId>
            <artifactId>mybatis-spring-boot-starter</artifactId>
            <version>1.3.2</version>
        </dependency>

        <dependency>
            <groupId>log4j</groupId>
            <artifactId>log4j</artifactId>
            <version>1.2.17</version>
        </dependency>

二、properties文件对数据源信息配置

基本上每行都有注释,这里主要说一下

spring.datasource.filters 如果配置了log4j,那么依赖里一定要有log4j,我在另一个项目中使用的log4j2,那么我这里配置log4j2也是没问题的
server.port=8080

spring.datasource.druid.driver-class-name=com.mysql.jdbc.Driver
#spring.datasource.driver-class-name = com.mysql.jdbc.Driver
spring.datasource.url = jdbc:mysql://localhost:3306/数据库名?useUnicode=true&characterEncoding=UTF-8&serverTimezone=UTC
spring.datasource.username = root
spring.datasource.password = 1234

##########Druid连接池配置
# 初始化大小,最小,最大
spring.datasource.initialSize=5
spring.datasource.minIdle=5
spring.datasource.maxActive=20

# 获取数据库连接等待的超时时间
spring.datasource.maxWait=60000

# 配置多久进行一次检测,检测需要关闭的空闲连接 单位毫秒
spring.datasource.timeBetweenEvictionRunsMillis=60000

#配置连接在池中的最小生存时间
spring.datasource.minEvictableIdleTimeMillis=300000

#系统启动时通过该sql语句验证数据库是否可用,如果不配置validationQuery,则下面三项无效
spring.datasource.validationQuery=SELECT 1 FROM DUAL
#启用空闲连接检测,以便回收
spring.datasource.testWhileIdle=true
#从连接池获取连接时,是否检测连接可用性,开启性能会有些许影响
spring.datasource.testOnBorrow=false
#释放连接到连接池时,是否检测连接可用性,开启性能会有些许影响
spring.datasource.testOnReturn=false

#PSCache对支持游标的数据库性能提升巨大,比如说oracle。在mysql下建议关闭。
# 打开PSCache,并且指定每个连接上PSCache的大小
spring.datasource.poolPreparedStatements=false
spring.datasource.maxPoolPreparedStatementPerConnectionSize=0

#监控统计用的filter:stat日志用的filter:log4j防御sql注入的filter:wall  'wall'用于防火墙
spring.datasource.filters=stat,wall,log4j
# 通过connectProperties属性来打开mergeSql功能;慢SQL记录
spring.datasource.connectionProperties=druid.stat.mergeSql=true;druid.stat.slowSqlMillis=5000
#合并多个DruidDataSource的监控数据
#spring.datasource.useGlobalDataSourceStat=true

三、配置类DruidConfig

package com.example.demo.config;

import com.alibaba.druid.pool.DruidDataSource;
import com.alibaba.druid.support.http.StatViewServlet;
import com.alibaba.druid.support.http.WebStatFilter;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.boot.web.servlet.FilterRegistrationBean;
import org.springframework.boot.web.servlet.ServletRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import javax.sql.DataSource;

/**
 * User: wangy
 * Date: 2020/4/2
 * Description: No Description
 */
@Configuration
public class DruidConfig {

    /**
     * 配置读取spring数据源
     */
    @Bean
    @ConfigurationProperties(prefix="spring.datasource")
    public DataSource druidDataSource() {
        return new DruidDataSource();
    }

    /**
     * 配置Druid监控启动页面
     */
    @Bean
    public ServletRegistrationBean druidStartViewServlet(){
        //org.springframework.boot.context.embedded.ServletRegistrationBean提供类的进行注册.
        ServletRegistrationBean servletRegistrationBean = new ServletRegistrationBean(new StatViewServlet(),"/druid/*");

        //白名单:
        servletRegistrationBean.addInitParameter("allow","127.0.0.1");
        //IP黑名单 (存在共同时,deny优先于allow) : 如果满足deny的话提示:Sorry, you are not permitted to view this page.
        //servletRegistrationBean.addInitParameter("deny","192.168.1.100");

        //登录查看信息的账号密码.
        //servletRegistrationBean.addInitParameter("loginUsername","admin");
        //servletRegistrationBean.addInitParameter("loginPassword","123456");

        //是否能够重置数据.
        servletRegistrationBean.addInitParameter("resetEnable","false");
        return servletRegistrationBean;
    }

    /**
     * Druid监控过滤器配置规则
     */
    @Bean
    public FilterRegistrationBean druidStartFilter(){
        FilterRegistrationBean filterRegistrationBean = new FilterRegistrationBean(new WebStatFilter());

        //添加过滤规则.
        filterRegistrationBean.addUrlPatterns("/*");

        //添加不需要忽略的格式信息.
        filterRegistrationBean.addInitParameter("exclusions","*.js,*.gif,*.jpg,*.png,*.css,*.ico,/druid/*");
        return filterRegistrationBean;
    }
}

至此配置已经结束了,启动项目,测试也完美收官

SpringBoot系列记录(六)——druid连接池及监控配置