First of all, it sounds very simple, isn’t it configuration? No! No! Many of these pit dynamic data source configurations and dependencies see portal Driud configuration above to add dependencies first

 <dependency>
     <groupId>com.alibaba</groupId>
     <artifactId>druid</artifactId>
     <version>1.1.10</version>
 </dependency>
Copy the code

The configuration of yml

 spring:
 datasource:
   dynamic:
     datasource:
       master:
         driver-class-name: com.mysql.cj.jdbc.Driver
         druid:
           filters: stat,wall,log4j2
           initial-size: 5
           max-active: 20
           max-evictable-idle-time-millis: 300000
           max-wait: 60000
           min-evictable-idle-time-millis: 300000
           min-idle: 5
           pool-prepared-statements: true
           share-prepared-statements: true
           test-on-borrow: false
           test-on-return: false
           test-while-idle: true
           time-between-eviction-runs-millis: 60000
           validation-query: select 1
           validation-query-timeout: - 1
           loginUsername: root
           loginPassword: 123456
           exclusions: '*.js,*.gif,*.jpg,*.bmp,*.png,*.css,*.ico,/druid/*'
           url-pattern: / *
         password: password
         type: com.alibaba.druid.pool.DruidDataSource
         url: jdbc:mysql://xxx.xxx.xx:3306/black_cat_fish? useSSL=false&useUnicode=true&characterEncoding=UTF-8
         username: root
     primary: master
Copy the code

Database driver if choose com. P6spy. Engine. Spy. P6SpyDriver will filters: stat, wall, log4j2 changed to filters: stat, log4j2 because made the p6spy agent or you can quote the following error:

java.lang.IllegalStateException: dbType not support : null, url null
    at com.alibaba.druid.wall.WallFilter.init(WallFilter.java:159) ~[druid-1.1. 5.jar:1.1. 5]
    at com.alibaba.druid.pool.DruidDataSource.init(DruidDataSource.java:770) [druid-1.1. 5.jar:1.1. 5]
    at com.alibaba.druid.pool.DruidDataSource.getConnection(DruidDataSource.java:1215) [druid-1.1. 5.jar:1.1. 5]
    at com.alibaba.druid.pool.DruidDataSource.getConnection(DruidDataSource.java:1211) [druid-1.1. 5.jar:1.1. 5]
    at com.alibaba.druid.pool.DruidDataSource.getConnection(DruidDataSource.java:105) [druid-1.1. 5.jar:1.1. 5]
    at 
Copy the code

The configuration class

package com.gafc.comm.config;

import com.alibaba.druid.support.http.StatViewServlet;
import com.alibaba.druid.support.http.WebStatFilter;
import lombok.extern.slf4j.Slf4j;
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 org.springframework.context.annotation.Profile;

/** * Druid setup */
@Configuration
@Profile({"dev", "sit", "uat"})
@Slf4j
public class DruidConfiguration {
    @Bean
    public ServletRegistrationBean druidStatViewServle(a) {
        // Register service
        ServletRegistrationBean servletRegistrationBean = new ServletRegistrationBean(
                new StatViewServlet(), "/druid/*");
        // Whitelist (empty indicates that all IP addresses are accessible, separated by commas when multiple IP addresses are available)
        servletRegistrationBean.addInitParameter("allow"."");
        // IP blacklist (deny takes precedence over allow if there is a common IP blacklist)
/ / servletRegistrationBean addInitParameter (" deny ", "127.0.0.2");
        // Set the user name and password for login
        servletRegistrationBean.addInitParameter("loginUsername"."root");
        servletRegistrationBean.addInitParameter("loginPassword"."123456");
        // Whether data can be reset.
        servletRegistrationBean.addInitParameter("resetEnable"."false");
        return servletRegistrationBean;
    }

    @Bean
    public FilterRegistrationBean druidStatFilter(a) {
        FilterRegistrationBean filterRegistrationBean = new FilterRegistrationBean(
                new WebStatFilter());
        // Add a filter rule
        filterRegistrationBean.addUrlPatterns("/ *");
        // Add format information that does not need to be ignored
        filterRegistrationBean.addInitParameter("exclusions"."*.js,*.gif,*.jpg,*.png,*.css,*.ico,/druid/*");
        log.info(Druid successfully initialized!);
        returnfilterRegistrationBean; }}Copy the code

Usually start the project and then access localhost: 8088 / druid/index. The HTML will appear the interface

There’s no problem up here if you’re executing an interface or if you’re executing SQL, rightError Attempting to get column 'reg_time' from result setIt is true that I used Mybatis – Plus (3.3.4) and now driud version is incompatible. At the beginning, I did not directly access the official website, but directly accessed Baidu, which took a lot of time and failed to solve the problem. The reason is that the dependent version is incompatible. Have a problem or look for the official website first, depend on spectrum! The official website explains as follows

Drop mp to 3.3.0 and upgrade Driud to version 1.2.23

<dependency>
     <groupId>com.alibaba</groupId>
     <artifactId>druid</artifactId>
     <version>1.1.23</version>
 </dependency>
Copy the code

In the configuration process, there was no record at that time, and it was eventually resolved. In the future, I will record the last configuration of spring monitoring

package com.gafc.comm.config;

import com.alibaba.druid.support.spring.stat.DruidStatInterceptor;
import org.springframework.aop.support.DefaultPointcutAdvisor;
import org.springframework.aop.support.JdkRegexpMethodPointcut;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Scope;

/** * Delegate to Druid for Spring monitoring */
@Configuration
public class SpringMonitoringConfig {

    @Bean(name = "druid-stat-interceptor")
    public DruidStatInterceptor druidStatInterceptor(a) {
        return new DruidStatInterceptor();
    }

    @Bean(name = "druid-stat-pointcut")
    / / the singleton
    @Scope("prototype")
    public JdkRegexpMethodPointcut druidStatPointcut(a) {
        final JdkRegexpMethodPointcut pointcut = new JdkRegexpMethodPointcut();
        // Here is the path to your Controller package
        pointcut.setPatterns("com.xxx.controller.*"
        );
        return pointcut;
    }

    @Bean
    public DefaultPointcutAdvisor druidStatAdvisor(@Qualifier("druid-stat-interceptor") final DruidStatInterceptor druidStatInterceptor,
                                                   @Qualifier("druid-stat-pointcut") final JdkRegexpMethodPointcut druidStatPointcut) {
        final DefaultPointcutAdvisor defaultPointAdvisor = new DefaultPointcutAdvisor();
        defaultPointAdvisor.setPointcut(druidStatPointcut);
        defaultPointAdvisor.setAdvice(druidStatInterceptor);
        returndefaultPointAdvisor; }}Copy the code