springboot ~ integrated DataSource and Druid monitoring configuration

Posted by siric on Wed, 19 Feb 2020 04:40:15 +0100

introduce

Druid is first a database connection pool. Druid is the best database connection pool at present, surpassing other database connection pools in function, performance and scalability. Druid has deployed more than 600 applications in Alibaba, which has passed the severe test of large-scale deployment of production environment for more than one year.

function

Druid provides an efficient, powerful and scalable database connection pool. It can monitor the database access performance. Druid built-in provides a powerful StatFilter plug-in, which can count the execution performance of SQL in detail, which is helpful for online analysis of database access performance. Database password encryption. Writing the database password directly in the configuration file is a bad behavior, which is easy to cause security problems. Both DruidDruiver and DruidDataSource support PasswordCallback. Druid provides different logfilters for SQL execution logs, which can support common logging, Log4j and JdkLog. You can select corresponding logfilters as required to monitor the database access of your application. To extend JDBC, if you want to have programming requirements for JDBC layer, you can use the Filter mechanism provided by druid to easily write extensions for JDBC layer.

Add to project

Add reference

  <!--druid rely on-->
  <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>druid-spring-boot-starter</artifactId>
            <version>1.1.9</version>
  </dependency>

Add profile

spring:  
   datasource:
    druid:
      stat-view-servlet:
        enabled: true
        loginUsername: admin
        loginPassword: 123456
        allow:
      web-stat-filter:
        enabled: true
    dynamic:
      druid: # Most of the global druid parameters are consistent with the default values. (the supported parameters are as follows. Do not set them randomly if you are not clear about the meaning)
        # Connection pool configuration information
        # Initialization size, min, Max
        initial-size: 5
        min-idle: 5
        maxActive: 20
        # Configure the timeout time for getting connection waiting
        maxWait: 60000
        # Configure how often to check the interval. Check the idle connections that need to be closed, in milliseconds
        timeBetweenEvictionRunsMillis: 60000
        # Configure the minimum lifetime of a connection in the pool, in milliseconds
        minEvictableIdleTimeMillis: 300000
        validationQuery: SELECT 1 FROM DUAL
        testWhileIdle: true
        testOnBorrow: false
        testOnReturn: false
        # Open PSCache and specify the size of PSCache on each connection
        poolPreparedStatements: true
        maxPoolPreparedStatementPerConnectionSize: 20
        # Configure the filters intercepted by monitoring statistics. After the filters are removed, the monitoring interface sql cannot be counted. The 'wall' is used for the firewall
        filters: stat,wall,slf4j
        # Open mergeSql function through connectProperties property; slow SQL record
        connectionProperties: druid.stat.mergeSql\=true;druid.stat.slowSqlMillis\=5000
      datasource:
        master:
          url: jdbc:mysql://127.0.0.1:3306/jeecg-boot?characterEncoding=UTF-8&useUnicode=true&useSSL=false
          username: root
          password: 123456
          driver-class-name: com.mysql.jdbc.Driver

#mybatis plus settings
mybatis-plus:
  mapper-locations: classpath*:com/lind/jeecgdemo/modules/**/mapper/xml/*Mapper.xml
  global-config:
    # Turn off the banner of MP3.0
    banner: false
    db-config:
      #Primary key type 0: "database ID auto increment", 1: "this type is not set as primary key type", 2: "user input ID",3: "globally unique ID (number type unique ID)", 4: "globally unique ID UUID",5: "string globally unique ID (idWorker's string representation)";
      id-type: 4
      # Default database table underline naming
      table-underline: true

Access address after starting project

http://localhost:8080/lind/druid/index.html
You can see data source, SQL monitoring, Spring monitoring and many other information.

Topics: Java Druid Database SQL JDBC