Environment
Springboot 1.5.9.RELEASE + JDK1.8
Configuration steps
Two-step, 1 configuration data source 2 configuration monitoring
Directly on the code
1 Configuring the data source
Package Com.company.project.support.druid;import Com.alibaba.druid.pool.druiddatasource;import Org.springframework.context.annotation.bean;import Org.springframework.context.annotation.configuration;import Org.springframework.jdbc.core.jdbctemplate;import java.sql.sqlexception;/** * Pure Java Config way to configure Druid * JavaBean Method Register database connection pool * Created by Hxy on 2018/6/2. */@Configurationpublic class Datasourceconfig {@Bean public druiddatasource getdatasourceconfig () throws Sqlexcepti on {druiddatasource ds = new Druiddatasource (); Basic attribute Ds.setdriverclassname ("Com.mysql.jdbc.Driver"); Ds.seturl (the ip:3306/database name of the jdbc:mysql://database? Characterencoding=utf-8&usessl=false&useunicode=true "); Ds.setusername ("account"); Ds.setpassword ("password"); Configuration initialization size, minimum, maximum ds.setinitialsize (5); Ds.setminidle (5); Ds.setmaxactive (50); Configure the time to get the connection wait timeout ds.setmaxwait (60000); How often the configuration interval is detected, detecting idle connections that need to be closed, in milliseconds Ds.settimebetweEnevictionrunsmillis (60000); Configures the minimum time for a connection to survive in a pool, in milliseconds ds.setminevictableidletimemillis (300000); Ds.setvalidationquery ("Select ' X '"); Ds.settestwhileidle (TRUE); Ds.settestonborrow (FALSE); Ds.settestonreturn (FALSE); Open Pscache, and specify the size ds.setpoolpreparedstatements (false) of pscache on each connection; Ds.setmaxpoolpreparedstatementperconnectionsize (20); Configure monitoring statistics Interception of filters Ds.setfilters ("Stat,wall"); return DS; }/* JDBC template */@Bean public jdbctemplate jdbctemplate (Druiddatasource druiddatasource) throws Sqlexceptio n {return new JdbcTemplate (Druiddatasource); }}
2 Configuring the Monitoring page
Package Com.company.project.support.druid;import Com.alibaba.druid.support.http.statviewservlet;import Com.alibaba.druid.support.http.webstatfilter;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;/** * Configuration of the Druid Monitor page */@Configurationpublic class Druidstatviewservletconfig {@Bean public servletregistrationbean Servletr Egistrationbean () {Servletregistrationbean Servletregistrationbean = new Servletregistrationbean (New STATVIEWSERVL ET ()); Servletregistrationbean.addurlmappings ("/druid/*"); Add initialization parameters: InitParams//Whitelist://Servletregistrationbean.addinitparameter ("Allow", "127.0.0.1"); IP blacklist (when common, deny takes precedence over allow): If Deny is satisfied, prompt: Sorry, you is not permitted to view this page.//Servletregistrationbe An.addinitparameter ("Deny", "192.168.1.73"); Log in to view the account password of the information. Servletregistrationbean.addinitparameter ("Loginusername", "admin"); Servletregistrationbean.addinitparameter ("Loginpassword", "123456"); Whether the data can be reset. Servletregistrationbean.addinitparameter ("Resetenable", "false"); return Servletregistrationbean; } @Bean Public Filterregistrationbean Filterregistrationbean () {Filterregistrationbean Filterregistrationbea n = new Filterregistrationbean (new Webstatfilter ()); Add filter rules. Filterregistrationbean.addurlpatterns ("/*"); Add formatting information that you do not need to ignore. Filterregistrationbean.addinitparameter ("Exclusions", "*.js,*.gif,*.jpg,*.png,*.css,*.ico,/druid/*"); return Filterregistrationbean; }}
Pure. properties file configuration or richer configuration reference previous article
Deep use and configuration of the 2510-DRUID monitoring function-based on springboot-full use of the Java Config form