druid 数据库连接池和监控配置(适用mysql和oracle )

1.引入配置,驱动mysql或oralce选一个

        
        
            mysql
            mysql-connector-java
            runtime
        

        

        
           com.oracle
			ojdbc6
			11.2.0.4
			runtime
		
  
        
            com.alibaba
            druid-spring-boot-starter
            1.1.10
        

 2.配置ymal文件

   driver-class-name: com.mysql.jdbc.Driver 
    url: jdbc:mysql://127.0.0.1:3306/xx?
    useUnicode=true&characterEncoding=utf8&useSSL=false
    username: root
    password: root
    initialSize: 2
    maxActive: 20
    maxIdle: 130
    maxWait: 30000
    minIdle: 20
    validationQuery: select 1 from dual
    testOnBorrow: true
    removeAbandoned: true
    timeBetweenEvictionRunsMillis: 30000
    minEvictableIdleTimeMillis: 30000
    #droid
    testWhileIdle: true
    testOnReturn: false
    poolPreparedStatements: true
    ### 配置监控统计拦截的filters,去掉后监控界面sql无法统计,'wall'用于防火墙
    filters: stat,wall,log4j
    maxPoolPreparedStatementPerConnectionSize: 20
    useGlobalDataSourceStat: true
    connectionProperties: druid.stat.mergeSql=true;druid.stat.slowSqlMillis=500

3.添加配置文件引入和实例化


import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;

import javax.sql.DataSource;

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 com.alibaba.druid.pool.DruidDataSource;
import com.alibaba.druid.support.http.StatViewServlet;
import com.alibaba.druid.support.http.WebStatFilter;
/**
 * druid 连接池 和监控配置
 * 
 * @author 12160
 *
 */
@Configuration
public class DruidConfig {
	// 将所有前缀为spring.datasource下的配置项都加载到DataSource中
    @ConfigurationProperties(prefix = "spring.datasource")
    @Bean
    public DataSource druidDataSource() {
        return new DruidDataSource();
    }

    @Bean
    public ServletRegistrationBean druidStatViewServlet() {
        ServletRegistrationBean servletRegistrationBean = new ServletRegistrationBean(new StatViewServlet(),"/druid/*");
        Map initParams = new HashMap<>();
        // 可配的属性都在 StatViewServlet 和其父类下
        initParams.put("loginUsername", "admin");
        initParams.put("loginPassword", "1992zhu");
        servletRegistrationBean.setInitParameters(initParams);
        return servletRegistrationBean;
    }

    @Bean
    public FilterRegistrationBean druidWebStatFilter() {
        FilterRegistrationBean filterRegistrationBean = new FilterRegistrationBean(new WebStatFilter());
        Map initParams = new HashMap<>();
        initParams.put("exclusions", "*.js,*.css,/druid/*");
        filterRegistrationBean.setInitParameters(initParams);
        filterRegistrationBean.setUrlPatterns(Arrays.asList("/*"));
        return filterRegistrationBean;
    }
    
}

4.输入对应地址

项目目录+/druid/sql.html 

例如:

http://127.0.0.1:8083/kouzhao/druid/sql.html 

druid 数据库连接池和监控配置(适用mysql和oracle )_第1张图片

5.部分错误和可能原因

监控sql语句不显示问题,大部分是配置未生效

可能原因有这几种:

  • 数据库驱动问题,
  • ymal文件配置错误,或者未配置filters
  • 引入包的不容可能需要别的配置文件

输入地址后ui缺失:

  • 安全框架拦截(shiro,可以配置白名单里)

 

 

 

你可能感兴趣的:(druid,监控,yml,数据库)