Commit 55b81ab3 authored by Quxl's avatar Quxl

增加Druid数据源监控

parent f57d2e18
...@@ -39,12 +39,16 @@ ...@@ -39,12 +39,16 @@
<groupId>mysql</groupId> <groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId> <artifactId>mysql-connector-java</artifactId>
</dependency> </dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid-spring-boot-starter</artifactId>
<version>1.1.20</version>
</dependency>
<dependency> <dependency>
<groupId>com.microsoft.sqlserver</groupId> <groupId>com.microsoft.sqlserver</groupId>
<artifactId>sqljdbc4</artifactId> <artifactId>sqljdbc4</artifactId>
<version>4.0</version> <version>4.0</version>
</dependency> </dependency>
<dependency> <dependency>
<groupId>com.alibaba</groupId> <groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId> <artifactId>fastjson</artifactId>
...@@ -55,11 +59,6 @@ ...@@ -55,11 +59,6 @@
<artifactId>common</artifactId> <artifactId>common</artifactId>
<version>0.0.1-RELEASE</version> <version>0.0.1-RELEASE</version>
</dependency> </dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid</artifactId>
<version>1.1.6</version>
</dependency>
<dependency> <dependency>
<groupId>io.springfox</groupId> <groupId>io.springfox</groupId>
<artifactId>springfox-swagger2</artifactId> <artifactId>springfox-swagger2</artifactId>
......
package com.egolm.shop.config;
import java.io.IOException;
import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import org.springframework.boot.autoconfigure.AutoConfigureAfter;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.boot.autoconfigure.condition.ConditionalOnWebApplication;
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 com.alibaba.druid.spring.boot.autoconfigure.DruidDataSourceAutoConfigure;
import com.alibaba.druid.spring.boot.autoconfigure.properties.DruidStatProperties;
import com.alibaba.druid.support.http.StatViewServlet;
import com.alibaba.druid.support.http.WebStatFilter;
import com.alibaba.druid.util.Utils;
@Configuration
@ConditionalOnWebApplication
@AutoConfigureAfter(DruidDataSourceAutoConfigure.class)
@ConditionalOnProperty(name = "spring.datasource.druid.stat-view-servlet.enabled", havingValue = "true", matchIfMissing = true)
public class DruidConfig {
@Bean
public FilterRegistrationBean<Filter> bannerFilterRegistrationBean(DruidStatProperties properties) {
DruidStatProperties.StatViewServlet config = properties.getStatViewServlet();
String pattern = config.getUrlPattern() != null ? config.getUrlPattern() : "/druid/*";
String commonJsPattern = pattern.replaceAll("\\*", "js/common.js");
final String filePath = "support/http/resources/js/common.js";
Filter filter = new Filter() {
@Override
public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
chain.doFilter(request, response);
response.resetBuffer();
String text = Utils.readFromResource(filePath);
text = text.replaceAll("<a.*?banner\"></a><br/>", "");
text = text.replaceAll("powered.*?shrek.wang</a>", "");
response.getWriter().write(text);
}
};
FilterRegistrationBean<Filter> registrationBean = new FilterRegistrationBean<Filter>();
registrationBean.setFilter(filter);
registrationBean.addUrlPatterns(commonJsPattern);
return registrationBean;
}
@Bean
public FilterRegistrationBean<Filter> webStatFilterRegistrationBean(DruidStatProperties properties) {
WebStatFilter filter = new WebStatFilter();
FilterRegistrationBean<Filter> registrationBean = new FilterRegistrationBean<Filter>();
registrationBean.setFilter(filter);
registrationBean.addUrlPatterns("/*");
registrationBean.addInitParameter("exclusions", "*.js, *.gif, *.jpg, *.bmp, *.png, *.css, *.ico, /druid/*");
return registrationBean;
}
@Bean
public ServletRegistrationBean<?> setvletRegistrationBean() {
ServletRegistrationBean<StatViewServlet> setvletRegistrationBean = new ServletRegistrationBean<StatViewServlet>(new StatViewServlet(), "/druid/*");
setvletRegistrationBean.addInitParameter("resetEnable", "true");
return setvletRegistrationBean;
}
}
...@@ -20,6 +20,7 @@ spring.datasource.testWhileIdle=true ...@@ -20,6 +20,7 @@ spring.datasource.testWhileIdle=true
spring.datasource.validationQueryTimeout=5 spring.datasource.validationQueryTimeout=5
spring.datasource.validationQuery=SELECT 1 spring.datasource.validationQuery=SELECT 1
spring.datasource.timeBetweenEvictionRunsMillis=3600000 spring.datasource.timeBetweenEvictionRunsMillis=3600000
spring.datasource.filters=stat,wall,log4j
spring.redis.database=0 spring.redis.database=0
......
...@@ -20,6 +20,7 @@ spring.datasource.testWhileIdle=true ...@@ -20,6 +20,7 @@ spring.datasource.testWhileIdle=true
spring.datasource.validationQueryTimeout=5 spring.datasource.validationQueryTimeout=5
spring.datasource.validationQuery=SELECT 1 spring.datasource.validationQuery=SELECT 1
spring.datasource.timeBetweenEvictionRunsMillis=3600000 spring.datasource.timeBetweenEvictionRunsMillis=3600000
spring.datasource.filters=stat,wall,log4j
spring.redis.database=0 spring.redis.database=0
......
...@@ -20,6 +20,7 @@ spring.datasource.testWhileIdle=true ...@@ -20,6 +20,7 @@ spring.datasource.testWhileIdle=true
spring.datasource.validationQueryTimeout=5 spring.datasource.validationQueryTimeout=5
spring.datasource.validationQuery=SELECT 1 spring.datasource.validationQuery=SELECT 1
spring.datasource.timeBetweenEvictionRunsMillis=3600000 spring.datasource.timeBetweenEvictionRunsMillis=3600000
spring.datasource.filters=stat,wall,log4j
spring.redis.database=0 spring.redis.database=0
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment