springboot整合druid

1. pom.xml中引入Druid的依赖


  1.0.28



    
        com.alibaba
        druid
        ${druid.version}
    

2. application.properties 文件中的配置项

spring.datasource.type=com.alibaba.druid.pool.DruidDataSource
# 下面为连接池的补充设置,应用到上面所有数据源中
# 初始化大小,最小,最大
spring.datasource.initialSize=5
spring.datasource.minIdle=5
spring.datasource.maxActive=20
# 配置获取连接等待超时的时间
spring.datasource.maxWait=60000
# 配置间隔多久才进行一次检测,检测需要关闭的空闲连接,单位是毫秒
spring.datasource.timeBetweenEvictionRunsMillis=60000
# 配置一个连接在池中最小生存的时间,单位是毫秒
spring.datasource.minEvictableIdleTimeMillis=300000
spring.datasource.validationQuery=SELECT 1 FROM DUAL
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

# 通过connectProperties属性来打开mergeSql功能;慢SQL记录
spring.datasource.connectionProperties=druid.stat.mergeSql=true;
druid.stat.slowSqlMillis=5000

# 合并多个DruidDataSource的监控数据
spring.datasource.useGlobalDataSourceStat=true

3. 创建Druid配置类

Druid 执行批量操作的时候会失败报sql异常
Druid默认的WallFilter是wall.如果启动时有错误信息需要去查看WallFilter的配置
Druid配置的时候还有一个大坑就是,不要自定义WallFilter配置filters,你需要通过proxyFilters来配置。
批量操作的异常是Druid的WallFilter.check()抛出来的,那就是说是Druid在做预编译的时候,给抛出的异常,还没有到mysql的服务器。
需要配置multiStatementAllow的参数为true就可以避免出现这样的错误了!

mport com.alibaba.druid.filter.Filter;
import com.alibaba.druid.pool.DruidDataSource;
import com.alibaba.druid.wall.WallConfig;
import com.alibaba.druid.wall.WallFilter;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.DependsOn;
import org.springframework.context.annotation.Primary;

import javax.sql.DataSource;
import java.util.ArrayList;
import java.util.List;

@Configuration
public class DruidConfig {

    @Autowired
    WallFilter wallFilter;

    @Bean //声明为实例bean
    @Primary //在同样的DataSource中,首先使用被标注的DataSource
    @ConfigurationProperties(prefix = "spring.datasource")
    public DataSource druidDataSource(){
        DruidDataSource druidDataSource = new DruidDataSource();//创建Druid数据源
        List filters = new ArrayList<>();
        filters.add(wallFilter);
        druidDataSource.setProxyFilters(filters);
        System.out.println("druid datasource");
        return druidDataSource;
    }

  //下面的配置解决批量操作的异常
    @Bean(name = "wallConfig")
    WallConfig wallFilterConfig(){
        WallConfig wc = new WallConfig();
        wc.setMultiStatementAllow(true); //允许一次执行多条语句
        wc.setNoneBaseStatementAllow(true); //允许一次执行多条语句
        return wc;
    }

    @Bean(name = "wallFilter")
    @DependsOn("wallConfig")
    WallFilter wallFilter(WallConfig wallConfig){
        WallFilter wfilter = new WallFilter();
        wfilter.setConfig(wallConfig);
        return wfilter;
    }

}

4. 创建类配置druid的过滤规则

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

import javax.servlet.annotation.WebFilter;
import javax.servlet.annotation.WebInitParam;


/**
 * 配置druid的过滤规则,监控所有的页面,并排除以下exclusions
 */
@WebFilter(filterName = "druidWebStatFilter", urlPatterns = "/*",
    initParams = {
        @WebInitParam(name="exclusions",value="*.js,*.css,*.gif,*.jpg,*.png,*.bmp,*.ico,/druid/*")//设置忽略的资源路径
    }) // /*拦截所有的请求
//WebStatFilter druid的检测器
public class DruidStatFilter extends WebStatFilter {

}

5. 创建配置类:配置Druid的WebServlet

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

import javax.servlet.annotation.WebInitParam;
import javax.servlet.annotation.WebServlet;

@WebServlet(urlPatterns = "/druid/*",
    initParams = {
        @WebInitParam(name = "allow",value = "192.168.100.1,127.0.0.1"), //ip白名单
        @WebInitParam(name = "deny",value = "192.168.100.2"), //ip黑名单
        @WebInitParam(name = "loginUsername",value = "admin"), //配置登陆用户名
        @WebInitParam(name = "loginPassword",value = "admin"),
        @WebInitParam(name = "resetEnable",value = "false") //禁用HTML页面还是那个的清空按钮
    })
public class DruidStatViewServlet extends StatViewServlet {
    private static final long serialVersionUID = 1L;

}

6. 在springboot的Application启动类上加上注解

@ServletComponentScan //扫描servlet,filter组件

7. Druid的spring监控的配置

  • classpath下创建xml配置文件
    application-bean.xml



    
    

    
    
        
            
                com.qfedu.rongzaiboot.service.*
                com.qfedu.rongzaiboot.dao.*
            
        
    
    
        
    


  • 创建配置类
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.ImportResource;

@Configuration
@ImportResource("classpath:application-bean.xml ")
public class DruidStateInterceptor {
}

最后启动项目 登陆Druid的界面

http://localhost:8081/druid/login.html

Druid登陆界面

登陆后的主页

你可能感兴趣的:(springboot整合druid)