Druid: Database connection pool for monitoring. This article first understand its simple use, the next article tries to use it to do a long data source configuration. Main reference: https://github.com/alibaba/druid/wiki/FAQ https://github.com/alibaba/druid/tree/master/druid-spring-boot-starter
First, the introduction of dependency
Here to see other blogs are quoted Druid, as is the use of Springboot integration, here Reference Druid official documents, with the Druid-spring-boot-starter.
<Dependency> <groupId>Com.alibaba</groupId> <Artifactid>Druid-spring-boot-starter</Artifactid> <version>1.1.10</version> </Dependency>
Second, set the properties
The demo used here is modified on the basis of the previous blog, so the database connection and MyBatis have been configured.
spring.mvc.view.prefix=/view/spring.mvc.view.suffix=.jspmybatis.type-aliases-package= com.example.modelmybatis.config-location=classpath:mybatis/mybatis-config.xmlmybatis.mapper-locations= Classpath:mybatis/mapper/*.xml#spring.datasource.driverclassname = com.mysql.cj.jdbc.driver# Spring.datasource.url = Jdbc:mysql://localhost:3306/mybatisspring.datasource.url =jdbc:mysql://127.0.0.1:3306/ Mybatis?useunicode=true&characterencoding=utf-8&servertimezone=utcspring.datasource.username = Rootspring.datasource.password = 123456#spring.datasource.driver-class-name= Com.mysql.cj.jdbc.driverspring.datasource.type= Com.alibaba.druid.pool.druiddatasourcespring.datasource.druid.max-active= 20spring.datasource.druid.initial-size=1spring.datasource.druid.max-wait= 60000spring.datasource.druid.pool-prepared-statements= Truespring.datasource.druid.max-pool-prepared-statement-per-connection-size= 20spring.datasource.druid.connection-properties=druid.stat.mergesql=true;druid.stat.slowsqlmillis= 5000spring.datasource.druid.min-idle=1spring.datasource.druid.time-between-eviction-runs-millis= 60000spring.datasource.druid.min-evictable-idle-time-millis=300000spring.datasource.druid.validation-query= Select 1 from dualspring.datasource.druid.test-while-idle=truespring.datasource.druid.test-on-borrow= Truespring.datasource.druid.test-on-return=true
The configuration here is not quite the same as other blogs, Spring.datasource has a druid behind it, and there are some changes to the property name.
Iii. Configuring connection pool monitoring and slow SQL processing
It is only necessary to add a class class configuration under the Startup class sibling directory. This creates the Druidconfiguration class, where the Webstatfilter configuration and Statviewservlet configuration are configured.
PackageCom.example.demo;ImportOrg.springframework.boot.web.servlet.FilterRegistrationBean;ImportOrg.springframework.boot.web.servlet.ServletRegistrationBean;Importorg.springframework.context.annotation.*;ImportCom.alibaba.druid.support.http.StatViewServlet;ImportCom.alibaba.druid.support.http.WebStatFilter; @Configuration Public classdruidconfiguration {@Bean PublicServletregistrationbean Statviewservlet () {//Create a servlet registration entityServletregistrationbean Servletregistrationbean =NewServletregistrationbean (NewStatviewservlet (), "/druid/*"); //Set IP WhitelistServletregistrationbean.addinitparameter ("Allow", "127.0.0.1"); //Set IP blacklist, if allow and deny exist together, deny takes precedence over allowServletregistrationbean.addinitparameter ("Deny", "192.168.0.19"); //setting up the console administrative userServletregistrationbean.addinitparameter ("Loginusername", "Druid"); Servletregistrationbean.addinitparameter ("Loginpassword", "123456"); //whether the data can be resetServletregistrationbean.addinitparameter ("Resetenable", "false"); returnServletregistrationbean; } @Bean PublicFilterregistrationbean Statfilter () {//Creating FiltersFilterregistrationbean Filterregistrationbean =NewFilterregistrationbean (NewWebstatfilter ()); //Set Filter Filter PathFilterregistrationbean.addurlpatterns ("/*"); //ignore the form of filteringFilterregistrationbean.addinitparameter ("Exclusions", "*.js,*.gif,*.jpg,*.png,*.css,*.ico,/druid/*"); returnFilterregistrationbean; }}
Iv. Testing
Start the application here, then enter http://127.0.0.1:8080/druid/index.html in the browser, the login page will be displayed, enter the above class configuration password to log in.
Then enter the previous blog to display the url:http://localhost:8080/user/of the user list Alluser.do, see the Druid page, see SQL monitoring will have just executed SQL information, URL monitoring and session monitoring also has corresponding information, because this is not configured spring monitoring so spring monitoring information, for spring monitoring will be added later , the next preparation is to use Druid to configure multiple data sources.
Integrated Druid of Springboot entry