spring boot 添加Druid監控
1、添加jar文件
<dependency> <groupId>com.alibaba</groupId> <artifactId>druid</artifactId> <version>1.1.9</version></dependency>
2、在spring啟動文件中,掃描filter和servlet
@SpringBootApplication@ServletComponentScan
3、添加配置文件(監控sql,spring)
@Configurationpublic class DruidMonitorConfig { @Bean public DruidStatInterceptor druidStatInterceptor() { DruidStatInterceptor dsInterceptor = new DruidStatInterceptor(); return dsInterceptor; } @Bean @Scope("prototype") public JdkRegexpMethodPointcut druidStatPointcut() { JdkRegexpMethodPointcut pointcut = new JdkRegexpMethodPointcut(); pointcut.setPattern("com.oa.*"); return pointcut; } @Bean public DefaultPointcutAdvisor druidStatAdvisor(DruidStatInterceptor druidStatInterceptor, JdkRegexpMethodPointcut druidStatPointcut) { DefaultPointcutAdvisor defaultPointAdvisor = new DefaultPointcutAdvisor(); defaultPointAdvisor.setPointcut(druidStatPointcut); defaultPointAdvisor.setAdvice(druidStatInterceptor); return defaultPointAdvisor; } /** * 註冊ServletRegistrationBean * * @return */ @Bean public ServletRegistrationBean registrationBean() { ServletRegistrationBean bean = new ServletRegistrationBean(new StatViewServlet(), "/druid/*"); /** 初始化參數配置,initParams **/ // 白名單 bean.addInitParameter("allow", "127.0.0.1");// 多個ip逗號隔開 // IP黑名單 (存在共同時,deny優先於allow) : 如果滿足deny的話提示:Sorry, you are not // permitted to view this page. // bean.addInitParameter("deny", "192.168.1.73"); // 登錄查看信息的賬號密碼. // bean.addInitParameter("loginUsername", "root"); // bean.addInitParameter("loginPassword", "root"); // bean.addInitParameter("filters", "stat,wall,log4j"); // 是否能夠重置數據. bean.addInitParameter("resetEnable", "false"); return bean; } /** * 註冊FilterRegistrationBean * * @return */ @Bean public FilterRegistrationBean druidStatFilter() { FilterRegistrationBean bean = new FilterRegistrationBean(new WebStatFilter()); // 添加過濾規則. bean.addUrlPatterns("/*"); // 添加不需要忽略的格式信息. bean.addInitParameter("exclusions", "*.js,*.gif,*.jpg,*.png,*.css,*.ico,/druid/*"); return bean; } @Bean @ConfigurationProperties(prefix = "spring.datasource") public DataSource druidDataSource() { return new DruidDataSource(); }
4、在application.yml文件中配置
spring: profiles: active: dev datasource: type: com.alibaba.druid.pool.DruidDataSource driver-class-name: com.mysql.jdbc.Driver url: jdbc:mysql://localhost:3306/***?characterEncoding=utf-8&useSSL=true password: *** username: ***# 配置監控統計攔截的filters,去掉後監控界面sql無法統計,wall用於防火牆 filters: stat,wall,log4j
4、訪問路徑/druid即可查看監控的相應信息,下圖內容均可監控到
作者:盈盈原創,分享時請分享此鏈接
推薦閱讀:
※關於Spring MVC的教程和例子?
※Spring boot與Spring cloud 是什麼關係?
※springboot怎麼學?
※spring-jdbc 目前還是一個主流的廣泛使用的持久化框架嗎?
TAG:SpringBoot | 監控 |