zoukankan      html  css  js  c++  java
  • SpringBoot 配置Druid数据监控

    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
    
    
  • 相关阅读:
    UNIX 环境模拟工具Cygwin安装及使用图文教程
    转转转!SpringMVC访问静态资源的三种方式
    转!!!解释Eclipse下Tomcat项目部署路径问题(.metadata.pluginsorg.eclipse.wst.server.core mp0wtpwebapps)
    JavaWeb中读取文件资源的路径问题 -- 转自新浪博客
    springMVC学习(10)-上传图片
    springMVC学习(9)-全局异常处理
    springMVC学习(8)-数据回显
    springMVC学习(7)-springMVC校验
    springMVC学习(6)-包装pojo类型、数组、list、Map类型参数绑定
    springMVC学习(5)-参数绑定
  • 原文地址:https://www.cnblogs.com/lin127/p/13049394.html
Copyright © 2011-2022 走看看