欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

SpringBoot配置Druid数据监控代码实例

程序员文章站 2022-06-03 15:06:38
druid,一个为监控而生的数据库连接池,提供可视化界面来查看sql执行情况。1、pom文件引入druid数据源 ...

druid,一个为监控而生的数据库连接池,提供可视化界面来查看sql执行情况。

1、pom文件引入druid数据源

<!--druid数据源-->
  <dependency>
   <groupid>com.alibaba</groupid>
   <artifactid>druid</artifactid>
   <version>1.1.8</version>
 </dependency>

2、新建一个drruidconfig配置文件

package com.example.mybatisplus.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.hashmap;
import java.util.map;

/**
 * druid数据池配置
 */
@configuration
public class druidconfig {

  @configurationproperties(prefix = "spring.datasource")
  @bean
  public datasource druid(){
    return new druiddatasource();
  }
  //配置druid的监控
  //1、配置一个管理后台的servlet
  @bean
  public servletregistrationbean statviewservlet(){
    servletregistrationbean bean = new servletregistrationbean(new statviewservlet(), "/druid/*");
    map<string,string> initparams = new hashmap<>();
    //配置登录的账号密码
    initparams.put("loginusername","admin");
    initparams.put("loginpassword","123456");
//    initparams.put("deny","localhost"); //拒绝谁登录
    //是否能够重置数据.
//    bean.addinitparameter("resetenable","false");
    bean.setinitparameters(initparams);
    return bean;
  }

  //2、配置一个web监控的filter
  @bean
  public filterregistrationbean webstatfilter(){
    filterregistrationbean bean = new filterregistrationbean();
    bean.setfilter(new webstatfilter());
    map<string,string> initparams = new hashmap<>();
    initparams.put("exclusions","*.html,*.js,*.gif,*.jpg,*.png,*.css,*.ico,/druid/*");
    bean.setinitparameters(initparams);
    bean.seturlpatterns(arrays.aslist("/*"));
    return bean;
  }
}

3、yml文件配置

spring:
 datasource:
  schema:
   - classpath:schema.sql
  initialization-mode: always
  username: root
  password: root
  url: jdbc:mysql://localhost:3306/jdbc?servertimezone=utc&useunicode=true&characterencoding=utf8&usessl=false
  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
  maxpoolpreparedstatementperconnectionsize: 20
  useglobaldatasourcestat: true
  connectionproperties: druid.stat.mergesql=true;druid.stat.slowsqlmillis=500

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。