Springboot 整合Druid

Posted 鹏懿如斯

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Springboot 整合Druid相关的知识,希望对你有一定的参考价值。

导入依赖

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

配置

spring:
  datasource:
    username: admin
    password: 8098
    # 如果时区报错了,加一个时区的配置:serverTimezone=UTC
    url: jdbc:mysql://localhost:3306/mybatis?useUnicode=true&characterEncoding=utf8&useSSL=false
    driver-class-name: com.mysql.jdbc.Driver
    type: com.alibaba.druid.pool.DruidDataSource

    # druid专有配置
    initial-size: 5
    minIdle: 5
    maxActive: 20
    maxWait: 60000
    timeBetweenEvictionRunsMillis: 60000
    minEvictableIdleTimeMillis: 300000
    validationQuery: SELECT 1 FROM DUAL
    testWhileIdle: true
    testOnBorrow: false
    testOnReturn: false
    poolPreparedStatements: true

    #配置监控统计拦载的filters,stat: 监控统计、Log4j: 日志记录、wall:防御sql注入
    #如果允许时报错java.lang.ClassNotFoundException: org.apache.Log4j.Priority
    #则导入Log4j依赖即可,Maven 地址: https://mvnrepository. com/artifact/Log4j/Log4j
    filters: stat,wall,1og4j
    maxPoolPreparedStatementPerConnectionSize: 20
    useGlobalDataSourceStat: true
    connectionProperties: druid.stat.mergeSql=true;druid.stat.slowSqlMillis=500

导入一个功能,以 log4j 为例

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

后台监控

package com.peng.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.servlet.Filter;
import javax.sql.DataSource;
import java.util.HashMap;
import java.util.Map;

@Configuration
public class DruidConfig {

    @ConfigurationProperties(prefix = "spring.datasource")
    @Bean
    public DataSource druidDataSource(){
        return new DruidDataSource();
    }

    //后台监控:web.xml,ServletRegis trationBean
    //因为SpringBoot内置了servlet容器,所以没有web.xmL
    @Bean
    public ServletRegistrationBean statViewServlet(){
        //固定写法
        ServletRegistrationBean<StatViewServlet> bean = new ServletRegistrationBean<>(new StatViewServlet(), "/druid/*");

        //后台需要有人登录
        HashMap<String, String> initParameters = new HashMap<>();

        //增加配置
        initParameters.put("loginUsername","admin"); //登录的key是固定的
        initParameters.put("loginPassword","123456");

        //允许谁可以访问
        initParameters.put("allow",""); //为空代表谁都能访问

        //禁止谁访问 initParameters.put("peng","192.168.1.120");

        bean.setInitParameters(initParameters);
        return bean;
    }

    //filter 过滤器
    @Bean
    public FilterRegistrationBean webStatFilter(){
        FilterRegistrationBean<Filter> bean = new FilterRegistrationBean<>();

        bean.setFilter(new WebStatFilter());

        //过滤哪些请求
        Map<String,String> initParameters = new HashMap<>();

        //这些东西不进行统计
        initParameters.put("exclustions","*.js,*.css,/druid/**");
        bean.setInitParameters(initParameters);
        return bean;
    }
}

以上是关于Springboot 整合Druid的主要内容,如果未能解决你的问题,请参考以下文章

springboot---整合druid连接池---连接oracle数据库---整合mybatis---整合thymeleaf---日志配置

SpringBoot 整合Druid

SpringBoot整合Druid数据源

SpringBoot系列七:SpringBoot 整合 MyBatis(配置 druid 数据源配置 MyBatis事务控制druid 监控)

SpringBoot | DruidSpringBoot整合Druid

SpringBoot 整合--Druid