SpringBoot Ali Druid configuration monitoring and management

Foreword

Druid interface before seen, let's intuitive feel sql, method calls, as well as access to all aspects of the situation uri like. But I do not know how it comes out, feeling is tall, just recently learned a trick two formulas, then here to share-based monitoring and management SpringBoot how to configure Druid

Configuration Steps

  1. Introducing pom file in dependence druid
        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>druid</artifactId>
            <version>1.1.12</version>
        </dependency>
  1. In the resources to create a file application.yml file is used to configure a database connection
spring:
  datasource:
    username: root
    password: 123456
    url: jdbc:mysql://localhost:3306/jdbc?serverTimezone=GMT%2B8
    driver-class-name: com.mysql.cj.jdbc.Driver
    type: com.alibaba.druid.pool.DruidDataSource

    initialSize: 8
    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,logback
    maxPoolPreparedStatementPerConnectionSize: 25
    useGlobalDataSourceStat: true
    connectionProperties: druid.stat.mergeSql=true;druid.stat.slowSqlMillis=500

  1. Configuration Admin servlet and configuration druid druid of the filter (interceptors)
package com.mengxuegu.springboot.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.servlet.Filter;
import javax.sql.DataSource;
import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;

@Configuration
public class DruidConfig {


    @ConfigurationProperties(prefix="spring.datasource")
    @Bean
    public DataSource druid(){
        return new DruidDataSource();
    }


    //配置一个druid的后台管理servlet
    @Bean
    public ServletRegistrationBean statViewServlet(){
        //请求地址
        ServletRegistrationBean<StatViewServlet> bean = new ServletRegistrationBean<>(new StatViewServlet(), "/druid/*");

        Map<String,String> initParam = new HashMap<>();

        initParam.put(StatViewServlet.PARAM_NAME_USERNAME,"root");
        initParam.put(StatViewServlet.PARAM_NAME_PASSWORD,"123456");

        //所有IP都可以访问
        initParam.put(StatViewServlet.PARAM_NAME_ALLOW,"");

        //禁止那个IP不能访问
        initParam.put(StatViewServlet.PARAM_NAME_DENY,"192.168.10.1");

        bean.setInitParameters(initParam);
        return bean;
    }


    //配置一个druid的filter
    @Bean
    public FilterRegistrationBean webStatFilter(){
        FilterRegistrationBean<Filter> bean = new FilterRegistrationBean<>();
        bean.setFilter(new WebStatFilter());

        //设置哪些不拦截
        Map<String,String> initParmas = new HashMap<>();
        initParmas.put(WebStatFilter.PARAM_NAME_EXCLUSIONS,"*.js,*.css,/druid/*");
        bean.setInitParameters(initParmas);

        //设置拦截请求
        bean.setUrlPatterns(Arrays.asList("/*"));
        return bean;
    }
}

  1. Show results
    Here Insert Picture Description

to sum up

Learning is a process of accumulation, not afraid afraid I do not know do not know!

Published 141 original articles · won praise 206 · views 70000 +

Guess you like

Origin blog.csdn.net/ywq1016243402/article/details/98470085