前言
前提是在springboot上已經使用了druid,但是沒有監控。是以怎麼使用druid不在本文範圍内,或者如果不是通過springboot使用druid的也不在本文範圍内。
增加druid連接配接池監控
增加servlet
import javax.servlet.annotation.WebInitParam;
import javax.servlet.annotation.WebServlet;
import com.alibaba.druid.support.http.StatViewServlet;
@WebServlet(urlPatterns = "/druid/*",
initParams={
@WebInitParam(name="loginUsername",value="admin"),// 使用者名
@WebInitParam(name="loginPassword",value="druid"),// 密碼
@WebInitParam(name="resetEnable",value="false")// 禁用HTML頁面上的“Reset All”功能
})
public class DruidServlet extends StatViewServlet {
private static final long serialVersionUID = 1L;
}
增加filter
import javax.servlet.annotation.WebFilter;
import javax.servlet.annotation.WebInitParam;
import com.alibaba.druid.support.http.WebStatFilter;
@WebFilter(filterName="druidWebStatFilter",urlPatterns="/*",
initParams={
@WebInitParam(name="exclusions",value="*.js,*.gif,*.jpg,*.bmp,*.png,*.css,*.ico,/druid/*")// 忽略資源
})
public class DruidFilter extends WebStatFilter {
}
增加ServletComponentScan
...
@ServletComponentScan
public class ServerApplication{
public static void main(String[] args) throws Exception {
SpringApplication.run(ServerApplication.class, args);
}
}