The default data source for Spring boot is: Org.apache.tomcat.jdbc.pool.DataSource
Druid is the best database connection pool in the Java language and provides powerful monitoring and scaling capabilities.
Here's how to configure the use of Druid in Spring Boot
(1) Add maven dependency (or jar package) \
<Dependency> <groupId>Com.alibaba</groupId> <Artifactid>Druid</Artifactid> <version>1.0.18</version></Dependency>
2), configure Data source related information
# Database Access configuration # Primary data source, default spring.datasource.type= Com.alibaba.druid.pool.druiddatasourcespring.datasource.driver-class-name= Com.mysql.jdbc.driverspring.datasource.url=jdbc:mysql://localhost:3306/testspring.datasource.username= rootspring.datasource.password=123456 # Below is a supplemental setting for connection pooling, applied to all data sources above # initialization size, minimum, maximum spring.datasource.initialsize= 5spring.datasource.minidle=5spring.datasource.maxactive=20# configuration Gets the time of the connection wait timeout spring.datasource.maxwait=60000# How long does the configuration interval take to detect the idle connection that needs to be closed, in milliseconds spring.datasource.timebetweenevictionrunsmillis=60000# to configure the minimum time for a connection to survive in the pool, Units are in milliseconds Spring.datasource.minevictableidletimemillis=300000spring.datasource.validationquery=select 1 from dualspring.datasource.testwhileidle=truespring.datasource.testonborrow=falsespring.datasource.testonreturn= false# opens Pscache and specifies the size of the Pscache on each connection spring.datasource.poolpreparedstatements= truespring.datasource.maxpoolpreparedstatementperconnectionsize=20# Configuration Monitoring Statistics interception filters, removed after the monitoring interface SQL can not be counted, ' wall ' Used for firewall spring.datasource.filters=stat,wall,log4j# to open m with Connectproperties propertyergesql function; Slow SQL record spring.datasource.connectionproperties=druid.stat.mergesql=true;druid.stat.slowsqlmillis=5000# Consolidate monitoring data for multiple Druiddatasource #spring.datasource.useglobaldatasourcestat=true
(3) Configure the Monitoring statistics function
Configuring the Servlet
The following is a note-based configuration in the Springboot project, which, if it is a Web. config, is configured as a rule.
Druidstatviewservlet:
ImportCom.alibaba.druid.support.http.StatViewServlet;ImportJavax.servlet.annotation.WebInitParam;ImportJavax.servlet.annotation.WebServlet;/*** Druid Data source status monitoring. * Created by winner_0715 on 2017/4/19.*/@WebServlet (Urlpatterns= "/druid/*", InitParams= { //IP Whitelist (no configuration or null, all access is allowed)@WebInitParam (name = "Allow", value = "192.168.1.72,127.0.0.1"), //IP blacklist (deny takes precedence over allow when there is a common)@WebInitParam (name = "Deny", value = "192.168.1.73"), //User name@WebInitParam (name = "Loginusername", value = "admin"), //Password@WebInitParam (name = "Loginpassword", value = "123456"), //disable the "Reset All" feature on HTML pages@WebInitParam (name = "Resetenable", value = "false") }) Public classDruidstatviewservletextendsStatviewservlet {Private Static Final LongSerialversionuid = 1L;}
Druidstatfilter:
ImportCom.alibaba.druid.support.http.WebStatFilter;ImportJavax.servlet.annotation.WebFilter;ImportJavax.servlet.annotation.WebInitParam;/*** Druid Filter. * Created by winner_0715 on 2017/4/19.*/@WebFilter (FilterName= "Druidwebstatfilter", Urlpatterns = "/*", InitParams= { //Ignore Resources@WebInitParam (name = "Exclusions", value = "*.js,*.gif,*.jpg,*.bmp,*.png,*.css,*.ico,/druid/*") }) Public classDruidstatfilterextendsWebstatfilter {}
Finally, add annotations on the Startup class: @ServletComponentScan allows spring to scan the servlet and filter that we write ourselves.
Be careful not to forget to add @ServletComponentScan annotations on the Springbootsampleapplication.java, or 404.
Then start the project and access http://127.0.0.1:8080/druid/index.html to View data sources and SQL statistics.
(4) Configuration Monitoring System mode two:
The above configuration is monitored using the native Servlet,filter method and then processed by @servletcomponentscan to start the scan package, and you will find that our servlet,filter does not have any coding at all.
Here we'll use a different approach: registering the servlet with code:
Druidconfiguration:
ImportCom.alibaba.druid.support.http.StatViewServlet;ImportCom.alibaba.druid.support.http.WebStatFilter;ImportOrg.springframework.boot.web.servlet.FilterRegistrationBean;ImportOrg.springframework.boot.web.servlet.ServletRegistrationBean;ImportOrg.springframework.context.annotation.Bean;Importorg.springframework.context.annotation.Configuration;/*** There is no need to add annotations in this way: @ServletComponentScan * Created by winner_0715 on 2017/4/19.*/@Configuration Public classdruidconfiguration {/*** Register a statviewservlet * *@return */@Bean PublicServletregistrationbean DruidStatViewServle2 () {//Org.springframework.boot.context.embedded.ServletRegistrationBean provides the registration of the class.Servletregistrationbean Servletregistrationbean =NewServletregistrationbean (NewStatviewservlet (), "/druid2/*"); //Add initialization parameters: InitParams//White list:Servletregistrationbean.addinitparameter ("Allow", "127.0.0.1"); //IP blacklist (when common, deny takes precedence over allow): If Deny is satisfied: Sorry, you is not permitted to view the this page.Servletregistrationbean.addinitparameter ("Deny", "192.168.1.73"); //Log in to view the account password of the information.Servletregistrationbean.addinitparameter ("Loginusername", "admin2"); Servletregistrationbean.addinitparameter ("Loginpassword", "123456"); //whether the data can be reset.Servletregistrationbean.addinitparameter ("Resetenable", "false"); returnServletregistrationbean; } /*** Register ONE: Filterregistrationbean * *@return */@Bean PublicFilterregistrationbean DruidStatFilter2 () {Filterregistrationbean Filterregistrationbean=NewFilterregistrationbean (NewWebstatfilter ()); //add filter rules.Filterregistrationbean.addurlpatterns ("/*"); //Add formatting information that you do not need to ignore.Filterregistrationbean.addinitparameter ("Exclusions", "*.js,*.gif,*.jpg,*.png,*.css,*.ico,/druid2/*"); returnFilterregistrationbean; }}
Launch the app to access: Http://127.0.0.1:8080/druid2/index.html Enter your account and password: admin2/123456 can access it.
Spring Boot uses Druid and monitoring configuration