spring boot整合Druid和配置数据源监控

  1. 通过Spring Initializer方式创建项目,选中web依赖。
  2. pom.xml中添加依赖:
<dependency>
			<groupId>com.alibabagroupId>
			<artifactId>druidartifactId>
			<version>1.1.12version>
dependency>


<dependency>
			<groupId>log4jgroupId>
			<artifactId>log4jartifactId>
			<version>1.2.17version>
		dependency>

2.修改配置文件application.yaml

spring:
  datasource:
    username: root
    password: 1234
    url: jdbc:mysql://localhost:3306/springboot?serverTimezone=GMT%2B8&characterEncoding=utf8
    driver-class-name: com.mysql.cj.jdbc.Driver
    type: com.alibaba.druid.pool.DruidDataSource

    initialSize: 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,去掉后监控界面sql无法统计,'wall'用于防火墙
    filters: stat,wall,log4j
    maxPoolPreparedStatementPerConnectionSize: 20
    useGlobalDataSourceStat: true
    connectionProperties: druid.stat.mergeSql=true;druid.stat.slowSqlMillis=500

3.导入配置

package com.lpp.springbootjdbc.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.sql.DataSource;
import java.util.Arrays;
import java.util.Base64;
import java.util.HashMap;
import java.util.Map;

@Configuration
public class DruidConfig {

    @ConfigurationProperties(prefix = "spring.datasource")
    @Bean
    public DataSource druidDataSource(){
        return new DruidDataSource();
    }
//    配置DURID监控
//    配置管理后台的Servlet
    @Bean
    public ServletRegistrationBean statViewBean(){
        ServletRegistrationBean registrationBean = new ServletRegistrationBean(new StatViewServlet(), "/druid/*");
//        配置初始化参数,具体可以查看ResourceServlet类
        Map<String,String> map=new HashMap<String, String>();
//        登录用户名
        map.put("loginUsername","admin");
//        登录密码
        map.put("loginPassword","123456");
//        允许那个访问
        map.put("allow","localhost");
//        不允许那个访问
//        map.put("deny","192.168")
        registrationBean.setInitParameters(map);
        return registrationBean;

    }
//    配置一个监控web的filter
    @Bean
    public FilterRegistrationBean webStatBean(){
        FilterRegistrationBean bean = new FilterRegistrationBean();
        bean.setFilter(new WebStatFilter());
        Map<String,String> maps=new HashMap<String, String>();
//        不拦截的请求
        maps.put("exculsion","*.js,*.css,/druid/*");
//        设置需要拦截的请求
        bean.setUrlPatterns(Arrays.asList("/*"));
        bean.setInitParameters(maps);
        return bean;
    }
}

4.访问:http://localhost:8080/druid
spring boot整合Druid和配置数据源监控_第1张图片

你可能感兴趣的:(spring boot整合Druid和配置数据源监控)