SpringBoot Learning (Five) - springboot rapid integration Druid

Druid connection pool

Brief introduction

Alibaba druid by the open source connection pool is the overall strength of the most prominent database connection pool, but also provides monitoring logging capabilities to analyze SQL implementation.

Introducing druid connection pool

pom.xml added

<dependency>
    <groupId>com.alibaba</groupId>
    <artifactId>druid</artifactId>
    <version>1.1.21</version>
</dependency>

application.properties added

# druid
spring.datasource.type=com.alibaba.druid.pool.DruidDataSource

# 连接池初始化大小,最小,最大
spring.datasource.initialSize=10
spring.datasource.minIdle=10
spring.datasource.maxActive=30
# 连接等待超时时间
spring.datasource.maxWait=60000
# 多久检测需要关闭的空闲连接
spring.datasource.timeBetweenEvictionRunsMillis=60000
# 一个连接在池中最小生存的时间
spring.datasource.minEvictableIdleTimeMillis=300000
# 校验SQL,Oracle配置 spring.datasource.validationQuery=SELECT 1 FROM DUAL,如果不配validationQuery项,则下面三项配置无用
spring.datasource.validationQuery=SELECT 'x'
spring.datasource.testWhileIdle=true
spring.datasource.testOnBorrow=false
spring.datasource.testOnReturn=false
# 打开PSCache,并且指定每个连接上PSCache的大小
spring.datasource.poolPreparedStatements=true
spring.datasource.maxPoolPreparedStatementPerConnectionSize=20
# 配置监控统计拦截的filters,去掉后监控界面sql无法统计,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

Note: Before you have configured mysql, mybatis, there is no repeat wrote

Code combat

Added a DruidFilter.java to configure the built-in monitor

Here Insert Picture Description
DruidFilter.java

package com.example.config;

import com.alibaba.druid.support.http.StatViewServlet;
import com.alibaba.druid.support.http.WebStatFilter;
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;

@Configuration
public class DruidFilter {

    @Bean
    public ServletRegistrationBean druidStatView() {
        //指定路径进入内置监控页面
        ServletRegistrationBean servletRegistrationBean = new ServletRegistrationBean(new StatViewServlet(), "/druid/*");

        //IP白名单:
        servletRegistrationBean.addInitParameter("allow", "127.0.0.1");
        //IP黑名单
        servletRegistrationBean.addInitParameter("deny", "192.168.1.73");
        //登录查看信息的账号密码.
        //servletRegistrationBean.addInitParameter("loginUsername", "admin");
        //servletRegistrationBean.addInitParameter("loginPassword", "123456");
        //是否能够重置数据.
        servletRegistrationBean.addInitParameter("resetEnable", "true");
        return servletRegistrationBean;
    }

    @Bean
    public FilterRegistrationBean druidWebStatFilter() {
        FilterRegistrationBean filterRegistrationBean = new FilterRegistrationBean(new WebStatFilter());
        //添加过滤规则.
        filterRegistrationBean.addUrlPatterns("/*");
        //添加不需要忽略的格式信息.
        filterRegistrationBean.addInitParameter("exclusions", "*.js,*.gif,*.jpg,*.png,*.css,*.ico,/druid/*");
        return filterRegistrationBean;
    }

}

Note: I had already configured the spring security, you can not jump into the built-in monitor page and other tutorials if the same configuration druid account password, enter druid will lead the account password, I know there are many ways to avoid, but require additional code to bypass the judge, since spring security permissions have an account, why additional druid to have to add a set of permissions to individual accounts, the /druid/*path configuration in spring security, the public has no better thing account permissions. So I do not have configured separately.

And this is just common configuration, should the need to learn more about the detailed configuration

Built-in monitoring configuration details page , Web associated configuration details

FIG effect as

Here Insert Picture Description
Blessings, also comes with Ali cloud of advertising, it really is Ali open source products

Guess you like

Origin blog.51cto.com/14089205/2453516