1. Add dependency, add custom configuration (thread pool size, etc.) in the configuration file
2. Then add a configuration class, use these custom configurations, configure the monitoring background Servlet, and configure the filter
@Configuration public class DruidConfig { @ConfigurationProperties(prefix = "spring.datasource") @Bean public DruidDataSource druidDataSource(){ return new DruidDataSource(); } //Configure monitoring servlet @Bean public ServletRegistrationBean statViewServlet(){ ServletRegistrationBean bean = new ServletRegistrationBean(new StatViewServlet () , "/druid/*"); Map<String,String> initParameter= new HashMap<> (); initParameter.put("loginUsername" ,"admin"); initParameter.put("loginPassword" ,"123456"); initParameter.put("allow" ,"");//Allow all access by default // Add IP whitelist //servletRegistrationBean.addInitParameter("allow", "127.0.0.1"); // Add IP blacklist, when the whitelist and blacklist are duplicated, the priority of the blacklist is higher //servletRegistrationBean.addInitParameter("deny", "127.0.0.1"); // Can the data be reset bean.addInitParameter("resetEnable", "false"); bean.setInitParameters (initParameter); return bean; } //Configure monitoring filter @Bean public FilterRegistrationBean webStatFilter(){ FilterRegistrationBean bean = new FilterRegistrationBean(new WebStatFilter ()); // Add filter rules bean .addUrlPatterns("/*"); // Ignore the filter format bean .addInitParameter("exclusions", "*.js,*.gif,*.jpg,*.png,*.css,*.ico,/druid/*,"); return bean; } }
After configuration, we start the SpringBoot program to visit:
http://localhost:8081/druid/ You can come to our login page, which is the console management user we added above. We can see the running status and Sql execution well on it.