Small knowledge, big challenge! This article is participating in the creation activity of “Essential Tips for Programmers”.
Druid provides a built-in StatFilter for monitoring statistics.
StatFilter configuration
The StatFilter alias is stat. The alias mapping configuration information is stored in druid-xxx.jar/ meta-INF /druid-filter.properties.
Using aliases in Spring is configured as follows:
@Bean public DruidDataSource getDataSource() { DruidDataSource druidDataSource = new DruidDataSource(); Properties properties = new Properties(); Try {/ / loading properties Read automatically classpath configuration file properties. The load (MyBatisConfig. Class. GetClassLoader () getResourceAsStream (" db. The properties ")); / / configure default monitoring filter WallFilter: prevent SQL injection filter Slf4jFileter: logging JDBC execution of SQL druidDataSource. SetFilters (" stat, wall, slf4j "); / / open slow SQL monitor, more than 1000 milliseconds for slow SQL, the default is 3 seconds druidDataSource setConnectionProperties (" druid. Stat. MergeSql = true; druid.stat.slowSqlMillis=1000"); } catch (IOException e) { e.printStackTrace(); } catch (SQLException e) { e.printStackTrace(); } / / if the Properties file attribute name in accordance with the parameter Properties configFromPropety naming rules, the automatic assignment druidDataSource. ConfigFromPropety (Properties); // return druidDataSource; }Copy the code
In the preceding configuration, StatFilter is used in combination with WallFilter and SlfFilter.
- WallFilter: a filter to prevent SQL injection
- Slf4jFileter: logs records THE SQL executed by JDBC
Use Druid’s built-in monitoring page
To access Druid’s built-in monitoring page, configure a servlet:
@WebServlet(urlPatterns = "/druid/*",initParams = { @WebInitParam(name = "loginUsername",value = "admin"), @webinitParam (name = "loginPassword",value = "123456"), // IP whitelist, not configured or empty, @webinitParam (name = "deny",value = "") @webinitParam (name = "resetEnable",value = "false")}) public class DruidStatueServlet extends StatViewServlet { }Copy the code
Or in the Spring configuration class:
@Bean public ServletRegistrationBean druidServlet() { ServletRegistrationBean reg = new ServletRegistrationBean(); reg.setServlet(new StatViewServlet()); reg.addUrlMappings(new String[]{"/druid/*"}); //IP whitelist (not configured or empty, all access allowed) reg.addinitParameter ("allow", ""); //IP blacklist reg.addInitParameter("resetEnable", "false"); Reg. AddInitParameter ("loginUsername", "admin"); // Set the account password reg.addInitParameter("loginPassword", "123456"); return reg; }Copy the code
Start the server from the address bar: IP: port number/application name /druid/login. HTML
Setting Web Monitoring
The WebStatFilter is used to collect web-JDBC association monitoring data
@WebFilter(filterName = "druidStatueFilter", urlPatterns = "/ *",
initParams = {
@WebInitParam(name = "exclusions",value = "*.js,*.jpg,*.png,*.css,/druid/*"), // Add format information to be ignored
@WebInitParam(name = "profileEnable",value = "true"), // Configure profileEnable to monitor a list of SQL calls to a single URL
@WebInitParam(name = "principalCookieName",value = "USER_COOKIE")
})
public class DruidStatueFilter extends WebStatFilter {}Copy the code
Or in the Spring configuration class:
@Bean
public FilterRegistrationBean filterRegistrationBean() {
FilterRegistrationBean filterRegistrationBean = new FilterRegistrationBean();
filterRegistrationBean.setFilter(new WebStatFilter());
// Add a filter rule
filterRegistrationBean.addUrlPatterns(new String[] {"/ *"});
// Add format information to be ignored
filterRegistrationBean.addInitParameter("exclusions"."*.js,*.gif,*.jpg,*.png,*.css,*.ico,/druid/*");
// Configure profileEnable to monitor a list of SQL calls to a single URL
filterRegistrationBean.addInitParameter("profileEnable"."true");
// If your user information is saved in cookies, you can configure it
filterRegistrationBean.addInitParameter("principalCookieName"."USER_COOKIE");
//principalCookieName, so that druid knows who the current user is USER_COOKIE is the cookie name
// Enable Druid to know who the current session user is
filterRegistrationBean.addInitParameter("principalSessionName"."USER_SESSION");
filterRegistrationBean.addInitParameter("DruidWebStatFilter"."/ *");
return filterRegistrationBean;
}
Copy the code
Setting spring Monitoring
// Set druid's AOP facet class
@Bean(name = "druidStatInterceptor")
public DruidStatInterceptor getDruidStatInterceptor() {
return new DruidStatInterceptor();
}
// Configure Spring monitoring
@Bean
public BeanNameAutoProxyCreator getAutoProxyCreator() {
BeanNameAutoProxyCreator beanNameAutoProxyCreator = new BeanNameAutoProxyCreator();
beanNameAutoProxyCreator.setProxyTargetClass(true);
beanNameAutoProxyCreator.setBeanNames(new String[] {"*Mapper"."*Service"});
beanNameAutoProxyCreator.setInterceptorNames("druidStatInterceptor");
return beanNameAutoProxyCreator;
}
Copy the code