springboot druid 資料來源監控
阿新 • • 發佈:2018-12-29
首現要在配置資料來源的地方開啟要監控
# 配置監控統計攔截的filters,去掉後監控介面sql無法統計,'wall'用於防火牆 filters: stat,wall,log4j
package com.lenovo.esim.cloud.order.config.datasourcemonitor; import com.alibaba.druid.support.http.StatViewServlet; import com.alibaba.druid.support.http.WebStatFilter; import lombok.extern.slf4j.Slf4j; import org.springframework.beans.factory.annotation.Value; 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; import java.util.HashMap; import java.util.Map; /** * datasource 資料來源監控中心 */ @Slf4j @Configuration public class DruidMonitorConfig { @Value("${security.user.name}") private String userName; @Value("${security.user.password}") private String password; @Bean public ServletRegistrationBean druidServlet() { log.info("init Druid Servlet Configuration "); ServletRegistrationBean servletRegistrationBean = new ServletRegistrationBean(); servletRegistrationBean.setServlet(new StatViewServlet()); servletRegistrationBean.addUrlMappings("/druid/*"); Map<String, String> initParameters = new HashMap<String, String>(); initParameters.put("loginUsername",userName);// 使用者名稱 initParameters.put("loginPassword", password);// 密碼 initParameters.put("resetEnable", "false");// 禁用HTML頁面上的“Reset All”功能 initParameters.put("allow", ""); // IP白名單 (沒有配置或者為空,則允許所有訪問) // initParameters.put("deny", "192.168.20.38");// IP黑名單 // (存在共同時,deny優先於allow) servletRegistrationBean.setInitParameters(initParameters); return servletRegistrationBean; } @Bean public FilterRegistrationBean filterRegistrationBean() { FilterRegistrationBean filterRegistrationBean = new FilterRegistrationBean(); filterRegistrationBean.setFilter(new WebStatFilter()); // 新增過濾規則 filterRegistrationBean.addUrlPatterns("/*"); // 新增不需要忽略的格式資訊. filterRegistrationBean.addInitParameter("exclusions", "*.js,*.gif,*.jpg,*.png,*.css,*.ico,/druid/*"); return filterRegistrationBean; } }
@Bean
@ConfigurationProperties(prefix = "spring.datasource")
public DataSource druidDataSource() {
return new DruidDataSource();
}