diff --git a/src/main/webapp/pages/modular/frame/druid.html b/src/main/webapp/pages/modular/frame/druid.html
new file mode 100644
index 00000000..d7e0a242
--- /dev/null
+++ b/src/main/webapp/pages/modular/frame/druid.html
@@ -0,0 +1,32 @@
+
+
+
+
+
+
+
${constants.getSystemName()}
+
+
+
+
+
+
+
+ @/* 加入contextPath属性和session超时的配置 */
+
+
+
+
+
\ No newline at end of file
From 37658803c8993387a5867f1cccff8c9fc691b24f Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?=E9=99=88=E7=AB=8B?= <413940119@qq.com>
Date: Fri, 8 Jan 2021 18:49:38 +0800
Subject: [PATCH 2/3] =?UTF-8?q?=E4=BF=AE=E6=94=B9=E9=85=8D=E7=BD=AE?=
=?UTF-8?q?=E5=B9=B6=E5=B0=86druid=E9=85=8D=E7=BD=AE=E6=94=BE=E5=88=B0bean?=
=?UTF-8?q?=E9=87=8C?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
---
.../guns/config/web/DruidConfiguration.java | 108 ++++++++++++++++--
.../stylefeng/guns/core/util/IpInfoUtils.java | 63 ++++++++++
.../system/controller/MonitorController.java | 13 ++-
.../system/warpper/SystemHardwareWarpper.java | 2 +-
src/main/resources/application-dev.yml | 22 +---
src/main/resources/application-local.yml | 22 +---
6 files changed, 173 insertions(+), 57 deletions(-)
create mode 100644 src/main/java/cn/stylefeng/guns/core/util/IpInfoUtils.java
diff --git a/src/main/java/cn/stylefeng/guns/config/web/DruidConfiguration.java b/src/main/java/cn/stylefeng/guns/config/web/DruidConfiguration.java
index 8c0df532..0409c48c 100644
--- a/src/main/java/cn/stylefeng/guns/config/web/DruidConfiguration.java
+++ b/src/main/java/cn/stylefeng/guns/config/web/DruidConfiguration.java
@@ -1,8 +1,18 @@
package cn.stylefeng.guns.config.web;
+import com.alibaba.druid.filter.stat.StatFilter;
+import com.alibaba.druid.pool.DruidDataSource;
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.support.spring.stat.BeanTypeAutoProxyCreator;
+import com.alibaba.druid.support.spring.stat.DruidStatInterceptor;
import com.alibaba.druid.util.Utils;
+import org.springframework.aop.Advisor;
+import org.springframework.aop.support.DefaultPointcutAdvisor;
+import org.springframework.aop.support.JdkRegexpMethodPointcut;
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;
@@ -13,22 +23,102 @@ import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import java.io.IOException;
-/***
- * 功能描述:
Druid配置
- * 创建时间: 2021/1/5 15:14
- * @author chenli
+/**
+ * Druid配置
+ *
+ * @author fengshuonan
+ * @date 2020/4/11 10:23
*/
@Configuration
public class DruidConfiguration {
- /***
- * 功能描述:
去除监控页面广告
- * 创建时间: 2021/1/4 20:57
- * @author chenli
+ /**
+ * druidServlet注册
+ */
+ @Bean
+ public ServletRegistrationBean druidServletRegistration() {
+ ServletRegistrationBean registration = new ServletRegistrationBean(new StatViewServlet());
+ registration.addUrlMappings("/druid/*");
+ return registration;
+ }
+
+ /**
+ * druid监控 配置URI拦截策略
+ */
+ @Bean
+ public FilterRegistrationBean druidStatFilter() {
+ FilterRegistrationBean filterRegistrationBean = new FilterRegistrationBean(new WebStatFilter());
+ //添加过滤规则.
+ filterRegistrationBean.addUrlPatterns("/*");
+ //添加不需要忽略的格式信息.
+ filterRegistrationBean.addInitParameter(
+ "exclusions", "/static/*,*.js,*.gif,*.jpg,*.png,*.css,*.ico,/druid,/druid/*");
+ //用于session监控页面的用户名显示 需要登录后主动将username注入到session里
+ filterRegistrationBean.addInitParameter("principalSessionName", "username");
+ filterRegistrationBean.addInitParameter("profileEnable", "true");
+ return filterRegistrationBean;
+ }
+
+ /**
+ * druid数据库连接池监控
+ */
+ @Bean
+ public DruidStatInterceptor druidStatInterceptor() {
+ return new DruidStatInterceptor();
+ }
+
+ /**
+ * druid慢sql监控
+ */
+ @Bean
+ public StatFilter druidSlowSQL(){
+ StatFilter statFilter = new StatFilter();
+ statFilter.setLogSlowSql(true);
+ statFilter.setMergeSql(true);
+ statFilter.setSlowSqlMillis(1000);
+ return statFilter;
+ }
+
+
+
+ @Bean
+ public JdkRegexpMethodPointcut druidStatPointcut() {
+ JdkRegexpMethodPointcut druidStatPointcut = new JdkRegexpMethodPointcut();
+ String patterns = "cn.stylefeng.guns.modular.*.service.*";
+ //可以set多个
+ druidStatPointcut.setPatterns(patterns);
+ return druidStatPointcut;
+ }
+
+ /**
+ * druid数据库连接池监控
+ */
+ @Bean
+ public BeanTypeAutoProxyCreator beanTypeAutoProxyCreator() {
+ BeanTypeAutoProxyCreator beanTypeAutoProxyCreator = new BeanTypeAutoProxyCreator();
+ beanTypeAutoProxyCreator.setTargetBeanType(DruidDataSource.class);
+ beanTypeAutoProxyCreator.setInterceptorNames("druidStatInterceptor");
+ return beanTypeAutoProxyCreator;
+ }
+
+ /**
+ * druid 为druidStatPointcut添加拦截
+ *
+ * @return
+ */
+ @Bean
+ public Advisor druidStatAdvisor() {
+ return new DefaultPointcutAdvisor(druidStatPointcut(), druidStatInterceptor());
+ }
+
+ /**
+ * 去除监控页面广告
+ *
+ * @author chenli
+ * @Date 2021/1/4 20:57
*/
@SuppressWarnings({"rawtypes", "unchecked"})
@Bean
-// @ConditionalOnProperty(name = "spring.datasource.druid.statViewServlet.enabled", havingValue = "true")
public FilterRegistrationBean removeDruidFilterRegistrationBean(DruidStatProperties properties) {
// 获取web监控页面的参数
DruidStatProperties.StatViewServlet config = properties.getStatViewServlet();
diff --git a/src/main/java/cn/stylefeng/guns/core/util/IpInfoUtils.java b/src/main/java/cn/stylefeng/guns/core/util/IpInfoUtils.java
new file mode 100644
index 00000000..ea5dc854
--- /dev/null
+++ b/src/main/java/cn/stylefeng/guns/core/util/IpInfoUtils.java
@@ -0,0 +1,63 @@
+package cn.stylefeng.guns.core.util;
+
+import javax.servlet.http.HttpServletRequest;
+import java.net.InetAddress;
+import java.net.UnknownHostException;
+
+/**
+ * ip工具类
+ *
+ * @author fengshuonan
+ * @Date 2018/9/27 上午10:47
+ */
+public class IpInfoUtils {
+
+ /**
+ * 获取客户端IP地址
+ */
+ public static String getIpAddr(HttpServletRequest request) {
+ String ip = request.getHeader("x-forwarded-for");
+ if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
+ ip = request.getHeader("Proxy-Client-IP");
+ }
+ if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
+ ip = request.getHeader("WL-Proxy-Client-IP");
+ }
+ if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
+ ip = request.getRemoteAddr();
+ if (ip.equals("127.0.0.1")) {
+ //根据网卡取本机配置的IP
+ InetAddress inet = null;
+ try {
+ inet = InetAddress.getLocalHost();
+ } catch (UnknownHostException e) {
+ e.printStackTrace();
+ }
+ ip = inet.getHostAddress();
+ }
+ }
+ // 对于通过多个代理的情况,第一个IP为客户端真实IP,多个IP按照','分割
+ if (ip != null && ip.length() > 15) {
+ if (ip.indexOf(",") > 0) {
+ ip = ip.substring(0, ip.indexOf(","));
+ }
+ }
+
+ if ("0:0:0:0:0:0:0:1".equals(ip)) {
+ ip = "127.0.0.1";
+ }
+
+ return ip;
+ }
+
+ /**
+ * 获取客户端主机名称
+ */
+ public static String getHostName() {
+ try {
+ return InetAddress.getLocalHost().getHostName();
+ } catch (UnknownHostException e) {
+ }
+ return "未知";
+ }
+}
diff --git a/src/main/java/cn/stylefeng/guns/modular/system/controller/MonitorController.java b/src/main/java/cn/stylefeng/guns/modular/system/controller/MonitorController.java
index bee38cd2..83e6c037 100644
--- a/src/main/java/cn/stylefeng/guns/modular/system/controller/MonitorController.java
+++ b/src/main/java/cn/stylefeng/guns/modular/system/controller/MonitorController.java
@@ -9,8 +9,10 @@ import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
/**
+ * 项目监控
+ *
* @author chenli
- * @date 2020/12/30 16:40
+ * @Date 2020/12/30 16:40
*/
@Controller
@Slf4j
@@ -36,10 +38,11 @@ public class MonitorController {
return PREFIX+"/systemInfo.html";
}
- /***
- * 功能描述:
durid sql监控页面
- * 创建时间: 2021/1/4 16:32
- * @author chenli
+ /**
+ * durid sql监控页面
+ *
+ * @author chenli
+ * @Date 2021/1/4 16:32
*/
@GetResource(name = "SQL监控", path = "/monitor/druid", requiredPermission = false,requiredLogin = false)
public String duridInfo(Model model){
diff --git a/src/main/java/cn/stylefeng/guns/modular/system/warpper/SystemHardwareWarpper.java b/src/main/java/cn/stylefeng/guns/modular/system/warpper/SystemHardwareWarpper.java
index a0497a42..82dc50d8 100755
--- a/src/main/java/cn/stylefeng/guns/modular/system/warpper/SystemHardwareWarpper.java
+++ b/src/main/java/cn/stylefeng/guns/modular/system/warpper/SystemHardwareWarpper.java
@@ -2,8 +2,8 @@ package cn.stylefeng.guns.modular.system.warpper;
import cn.hutool.core.net.NetUtil;
import cn.hutool.core.util.NumberUtil;
-import cn.stylefeng.guns.modular.system.model.*;
import cn.stylefeng.guns.core.util.IpInfoUtils;
+import cn.stylefeng.guns.modular.system.model.*;
import lombok.Data;
import oshi.SystemInfo;
import oshi.hardware.CentralProcessor;
diff --git a/src/main/resources/application-dev.yml b/src/main/resources/application-dev.yml
index c0946d9e..b0584110 100644
--- a/src/main/resources/application-dev.yml
+++ b/src/main/resources/application-dev.yml
@@ -21,24 +21,4 @@ spring:
testWhileIdle: true
testOnBorrow: false
testOnReturn: false
- poolPreparedStatements: false
- webStatFilter:
- enabled: true
- statViewServlet:
- enabled: true
- # 设置白名单,不填则允许所有访问
- allow:
- url-pattern: /druid/*
- # 控制台管理用户名和密码
- login-username:
- login-password:
- filter:
- stat:
- enabled: true
- # 慢SQL记录
- log-slow-sql: true
- slow-sql-millis: 1000
- merge-sql: true
- wall:
- config:
- multi-statement-allow: true
\ No newline at end of file
+ poolPreparedStatements: false
\ No newline at end of file
diff --git a/src/main/resources/application-local.yml b/src/main/resources/application-local.yml
index 384b045c..9c770ae6 100644
--- a/src/main/resources/application-local.yml
+++ b/src/main/resources/application-local.yml
@@ -21,24 +21,4 @@ spring:
testWhileIdle: true
testOnBorrow: false
testOnReturn: false
- poolPreparedStatements: false
- webStatFilter:
- enabled: true
- statViewServlet:
- enabled: true
- # 设置白名单,不填则允许所有访问
- allow:
- url-pattern: /druid/*
- # 控制台管理用户名和密码
- login-username:
- login-password:
- filter:
- stat:
- enabled: true
- # 慢SQL记录
- log-slow-sql: true
- slow-sql-millis: 1000
- merge-sql: true
- wall:
- config:
- multi-statement-allow: true
\ No newline at end of file
+ poolPreparedStatements: false
\ No newline at end of file
From 48b90d2b75efc8334f0d899f69d2d60eb9bedee9 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?=E9=99=88=E7=AB=8B?= <413940119@qq.com>
Date: Sat, 9 Jan 2021 10:28:47 +0800
Subject: [PATCH 3/3] =?UTF-8?q?=E4=BF=AE=E6=94=B9=E9=85=8D=E7=BD=AE?=
=?UTF-8?q?=E5=B9=B6=E5=B0=86druid=E9=85=8D=E7=BD=AE=E6=94=BE=E5=88=B0bean?=
=?UTF-8?q?=E9=87=8C?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
---
src/main/resources/application-dev.yml | 15 +--------------
src/main/resources/application-local.yml | 15 +--------------
2 files changed, 2 insertions(+), 28 deletions(-)
diff --git a/src/main/resources/application-dev.yml b/src/main/resources/application-dev.yml
index b0584110..38600473 100644
--- a/src/main/resources/application-dev.yml
+++ b/src/main/resources/application-dev.yml
@@ -8,17 +8,4 @@ spring:
# 连接池大小根据实际情况调整
max-active: 100
- max-pool-prepared-statement-per-connection-size: 100
- druid:
- initialSize: 50
- minIdle: 50
- maxActive: 200
- maxWait: 60000
- timeBetweenEvictionRunsMillis: 60000
- minEvictableIdleTimeMillis: 300000
- maxEvictableIdleTimeMillis: 900000
- validationQuery: SELECT 1 FROM DUAL
- testWhileIdle: true
- testOnBorrow: false
- testOnReturn: false
- poolPreparedStatements: false
\ No newline at end of file
+ max-pool-prepared-statement-per-connection-size: 100
\ No newline at end of file
diff --git a/src/main/resources/application-local.yml b/src/main/resources/application-local.yml
index 9c770ae6..f21c1fe6 100644
--- a/src/main/resources/application-local.yml
+++ b/src/main/resources/application-local.yml
@@ -8,17 +8,4 @@ spring:
password: p8HMTBEapAPPmXB8!
# 连接池大小根据实际情况调整
max-active: 100
- max-pool-prepared-statement-per-connection-size: 100
- druid:
- initialSize: 50
- minIdle: 50
- maxActive: 200
- maxWait: 60000
- timeBetweenEvictionRunsMillis: 60000
- minEvictableIdleTimeMillis: 300000
- maxEvictableIdleTimeMillis: 900000
- validationQuery: SELECT 1 FROM DUAL
- testWhileIdle: true
- testOnBorrow: false
- testOnReturn: false
- poolPreparedStatements: false
\ No newline at end of file
+ max-pool-prepared-statement-per-connection-size: 100
\ No newline at end of file