Druid打印慢sql自动配置(附带完整配置)

第一种用注解@Configuration

import com.alibaba.druid.filter.Filter;
import com.alibaba.druid.filter.stat.StatFilter;
import com.alibaba.druid.pool.DruidDataSource;
import com.alibaba.druid.support.http.StatViewServlet;
import org.assertj.core.util.Lists;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.boot.web.servlet.ServletRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * @Author Lorie_Chen
 * @Date 2019-12-12 15:08
 **/
@Configuration
public class DruidConfig {

    //跟配置文件绑定
    //spring.druid.xx xx跟DruidDataSource里的属性绑定
    @ConfigurationProperties(prefix = "spring.druid")
    //spring容器启动时,DruidDataSource调用内部init方法启动,关闭时,调用内部close方法关闭
    @Bean(initMethod = "init",destroyMethod = "close")
    public DruidDataSource dataSource(){
        DruidDataSource dataSource = new DruidDataSource();
        //设置数据库URL
        dataSource.setUrl("jdbc:mysql://ip:port/test?useUnicode=true&characterEncoding=utf8&useSSL=false&zeroDateTimeBehavior=convertToNull");
        //设置账户密码
        dataSource.setUsername("username");
        dataSource.setPassword("password");
        //设置驱动
        dataSource.setDriverClassName("com.mysql.jdbc.Driver");

        //初始化大小,最小,最大
        dataSource.setInitialSize(1);
        dataSource.setMinIdle(3);
        dataSource.setMaxActive(20);
        //配置获取连接等待超时的时间
        dataSource.setMaxWait(60000);
        //配置间隔多久才进行一次检测,检测需要关闭的空闲连接,单位是毫秒
        dataSource.setTimeBetweenEvictionRunsMillis(60000);
        //配置一个连接在池中最小生存的时间,单位是毫秒
        dataSource.setMinEvictableIdleTimeMillis(30000);
        dataSource.setTestWhileIdle(true);
        dataSource.setTestOnBorrow(false);
        dataSource.setTestOnReturn(false);
        //打开PSCache,并且指定每个连接上PSCache的大小
        dataSource.setPoolPreparedStatements(true);
        dataSource.setMaxPoolPreparedStatementPerConnectionSize(20);
        //配置监控统计拦截的filters,去掉后监控界面sql无法统计,'wall'用于防火墙,通过connectProperties属性来打开mergeSql功能;慢SQL记录
        dataSource.setProxyFilters(Lists.newArrayList(statFilter()));
        //合并多个DruidDataSource的监控数据
        dataSource.setUseGlobalDataSourceStat(true);
        return dataSource;
    }

    /**
     * 定义慢sql的Filter,出现慢sql可以打印出来
     * @return
     */
    @Bean
    public Filter statFilter(){
        StatFilter statFilter = new StatFilter();
        //设定多长时间属于慢SQL  这里设置为5秒属于慢SQL
        statFilter.setSlowSqlMillis(5000);
        //设置是否打印慢SQL日志
        statFilter.setLogSlowSql(true);
        //是否将日志合并
        statFilter.setMergeSql(true);
        return statFilter;
    }

    /**
     * 添加监控bean 用来监控慢SQL日志等等
     * 服务启动后,输入地址:http://localhost:8080/druid/index.html查看监控
     * 如果存在慢SQL,则控制台会打印出来
     * 例如:slow sql 21 millis. select id,name,phone,email from user[]
     * @return
     */
    @Bean
    public ServletRegistrationBean servletRegistrationBean(){
        return new ServletRegistrationBean(new StatViewServlet(),"/druid/*");
    }
}

第二种使用自定义注解实现

import com.fpd.order.config.DruidConfig;
import org.springframework.context.annotation.Import;

import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;

/**
 * @Desp 在启动类上开启此注解,自动配置自定义的DruidConfig配置文件
 **/
@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
@Import(DruidConfig.class)
public @interface EnableDruidConfig {
}

你可能感兴趣的:(java)