Moment For Technology

Springboot multi-source configuration Driud monitors configuration and pits encountered

Posted on Aug. 26, 2023, 5:02 a.m. by 張婷婷
Category: The back-end Tag: The back-end

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

Copy the code

The configuration of yml

         driver-class-name: com.mysql.cj.jdbc.Driver
           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
         url: jdbc:mysql:// useSSL=falseuseUnicode=truecharacterEncoding=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 ~[druid-1.1. 5.jar:1.1. 5]
    at [druid-1.1. 5.jar:1.1. 5]
    at [druid-1.1. 5.jar:1.1. 5]
    at [druid-1.1. 5.jar:1.1. 5]
    at [druid-1.1. 5.jar:1.1. 5]
Copy the code

The configuration class

package com.gafc.comm.config;

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 */
@Profile({"dev", "sit", "uat"})
public class DruidConfiguration {
    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)
        // IP blacklist (deny takes precedence over allow if there is a common IP blacklist)
/ / servletRegistrationBean addInitParameter (" deny ", "");
        // Set the user name and password for login
        // Whether data can be reset.
        return servletRegistrationBean;

    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/*"); 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

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 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 */
public class SpringMonitoringConfig {

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

    @Bean(name = "druid-stat-pointcut")
    / / the singleton
    public JdkRegexpMethodPointcut druidStatPointcut(a) {
        final JdkRegexpMethodPointcut pointcut = new JdkRegexpMethodPointcut();
        // Here is the path to your Controller package
        return pointcut;

    public DefaultPointcutAdvisor druidStatAdvisor(@Qualifier("druid-stat-interceptor") final DruidStatInterceptor druidStatInterceptor,
                                                   @Qualifier("druid-stat-pointcut") final JdkRegexpMethodPointcut druidStatPointcut) {
        final DefaultPointcutAdvisor defaultPointAdvisor = new DefaultPointcutAdvisor();
        returndefaultPointAdvisor; }}Copy the code
About (Moment For Technology) is a global community with thousands techies from across the global hang out!Passionate technologists, be it gadget freaks, tech enthusiasts, coders, technopreneurs, or CIOs, you would find them all here.