Druid connection pool data monitoring

The introduction of dependence:

 

<dependency>
  <groupId>com.alibaba</groupId>
  <artifactId>druid</artifactId>
  <version>1.1.6</version>
</dependency>

 

Write configuration class: DuridConfig

 

package com.frost.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 the java.util.HashMap; 

/ ** 
 * @Auther: Frost GG 
 * @date: 2019/9/19 09:27 
 * @Description: 
 * / 
@Configuration 
public  class DruidConfig { 

    // configuration database connection pool Druid information
     // the file attributes injection yml come 
    @ConfigurationProperties (prefix = "spring.datasource" ) 
    @Bean 
    public the DataSource Druid () {
         return    new new DruidDataSource (); 
    } 
    
    // 1. Configuring the servlet 
    @Bean
     public ServletRegistrationBean statViewServlet () {
         // into the network address war 
        ServletRegistrationBean bean =new new ServletRegistrationBean ( new new StatViewServlet (), "/ Druid / *" ); 
        the HashMap <Object, Object> = hashMap ' new new the HashMap <> ();
         // configuration information of the connection
         // username 
        hashMap.put ( "loginUsername", " ADMIN " );
         // password 
        hashMap.put (" loginPassword "," 1234 " );
         // allow access to all 
        hashMap.put (" the allow "," " ); 
        bean.setInitParameters (hashMap '); 
        return the bean; 
    } 

    // 2. configure the Filter 
    @Bean
     publicWebStatFilter FilterRegistrationBean () {
         // Configure intercept information 
        FilterRegistrationBean the bean = new new FilterRegistrationBean ( new new WebStatFilter ()); 
        the HashMap <Object, Object> = hashMap ' new new the HashMap <> ();
         // that information does not intercept 
        hashMap.put ( "exclusions "," * .js, CSS *, / Druid / * " ); 
        bean.setInitParameters (hashMap '); 
        // to filter those properties 
        bean.setUrlPatterns (Arrays.asList (" / * " ));
         return the bean; 
    } 
}

 

Configuration database information:

# 配置数据库连接信息
  datasource:
    driver-class-name: com.mysql.jdbc.Driver
    url: jdbc:mysql://localhost:3306/exam?useUnicode=true&characterEncoding=utf8&serverTimezone=GMT%2B8
    username: root
    password: root
    type: com.alibaba.druid.pool.DruidDataSource
    initialSize: 5
    minIdle: 5
    maxActive: 20
    maxWait: 60000
    timeBetweenEvictionRunsMillis: 60000
    minEvictableIdleTimeMillis: 300000
    validationQuery: SELECT 1 FROM DUAL
    testWhileIdle: to true 
    testOnBorrow: false 
    testOnReturn: false 
    poolPreparedStatements: to true 
    # configure the monitoring statistics intercepted filters, after removing the monitoring interface sql not statistics, 'Wall' for Firewall 
    Filters: STAT, Wall, log4j 
    maxPoolPreparedStatementPerConnectionSize: 20 
    useGlobalDataSourceStat: to true 
    ConnectionProperties: druid.stat .mergeSql = to true ; druid.stat.slowSqlMillis = 500

The last time was in the back is the port number of projects plus durid / login.html then the account is: admain password is: 1234

 

 

 

Guess you like

Origin www.cnblogs.com/xdtg/p/11717714.html