From a79059689774b91970a1ca55faa2a3d61789c967 Mon Sep 17 00:00:00 2001 From: lihengming <89921218@qq.com> Date: Wed, 30 Jan 2019 22:54:02 +0800 Subject: [PATCH 1/2] for #3010 --- .../autoconfigure/properties/DruidStatProperties.java | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/druid-spring-boot-starter/src/main/java/com/alibaba/druid/spring/boot/autoconfigure/properties/DruidStatProperties.java b/druid-spring-boot-starter/src/main/java/com/alibaba/druid/spring/boot/autoconfigure/properties/DruidStatProperties.java index 70bdbb7475..ad6d3da70e 100644 --- a/druid-spring-boot-starter/src/main/java/com/alibaba/druid/spring/boot/autoconfigure/properties/DruidStatProperties.java +++ b/druid-spring-boot-starter/src/main/java/com/alibaba/druid/spring/boot/autoconfigure/properties/DruidStatProperties.java @@ -53,9 +53,9 @@ public void setWebStatFilter(WebStatFilter webStatFilter) { public static class StatViewServlet { /** - * Enable StatViewServlet. + * Enable StatViewServlet, default false. */ - private boolean enabled = true; + private boolean enabled; private String urlPattern; private String allow; private String deny; @@ -122,9 +122,9 @@ public void setResetEnable(String resetEnable) { public static class WebStatFilter { /** - * Enable WebStatFilter. + * Enable WebStatFilter, default false. */ - private boolean enabled = true; + private boolean enabled; private String urlPattern; private String exclusions; private String sessionStatMaxCount; From 2e2ca86f6744b9e6440dbe799587a2da3e81c193 Mon Sep 17 00:00:00 2001 From: lihengming <89921218@qq.com> Date: Wed, 30 Jan 2019 23:17:02 +0800 Subject: [PATCH 2/2] for #3010 --- druid-spring-boot-starter/README.md | 6 +++--- druid-spring-boot-starter/README_EN.md | 7 +++---- .../src/test/resources/application.properties | 10 +++++++--- 3 files changed, 13 insertions(+), 10 deletions(-) diff --git a/druid-spring-boot-starter/README.md b/druid-spring-boot-starter/README.md index 09d3659bae..2eb6d8f34e 100644 --- a/druid-spring-boot-starter/README.md +++ b/druid-spring-boot-starter/README.md @@ -71,7 +71,7 @@ spring.datasource.druid.web-stat-filter.principal-cookie-name= spring.datasource.druid.web-stat-filter.profile-enable= # StatViewServlet配置,说明请参考Druid Wiki,配置_StatViewServlet配置 -spring.datasource.druid.stat-view-servlet.enabled= #是否启用StatViewServlet默认值false +spring.datasource.druid.stat-view-servlet.enabled= #是否启用StatViewServlet(监控页面)默认值为false(考虑到安全问题默认并未启动,如需启用建议设置密码或白名单以保障安全) spring.datasource.druid.stat-view-servlet.url-pattern= spring.datasource.druid.stat-view-servlet.reset-enable= spring.datasource.druid.stat-view-servlet.login-username= @@ -132,6 +132,7 @@ public DataSource dataSourceTwo(){ 你可以通过 ```spring.datasource.druid.filters=stat,wall,log4j ...``` 的方式来启用相应的内置Filter,不过这些Filter都是默认配置。如果默认配置不能满足你的需求,你可以放弃这种方式,通过配置文件来配置Filter,下面是例子。 ```xml # 配置StatFilter +spring.datasource.druid.filter.stat.enabled=true spring.datasource.druid.filter.stat.db-type=h2 spring.datasource.druid.filter.stat.log-slow-sql=true spring.datasource.druid.filter.stat.slow-sql-millis=2000 @@ -158,14 +159,13 @@ spring.datasource.druid.filter.wall.config.drop-table-allow=false ## 如何获取 Druid 的监控数据 -Druid 的监控数据可以通过 DruidStatManagerFacade 进行获取,获取到监控数据之后你可以将其暴露给你的监控系统进行使用。Druid 默认的监控系统数据也来源于此。下面给做一个简单的演示,在 Spring Boot 中如何通过 HTTP 接口将 Druid 监控数据以 JSON 的形式暴露出去,实际使用中你可以根据你的需要自由地对监控数据、暴露方式进行扩展。 +Druid 的监控数据可以在开启 StatFilter 后通过 DruidStatManagerFacade 进行获取,获取到监控数据之后你可以将其暴露给你的监控系统进行使用。Druid 默认的监控系统数据也来源于此。下面给做一个简单的演示,在 Spring Boot 中如何通过 HTTP 接口将 Druid 监控数据以 JSON 的形式暴露出去,实际使用中你可以根据你的需要自由地对监控数据、暴露方式进行扩展。 ```java @RestController public class DruidStatController { @GetMapping("/druid/stat") public Object druidStat(){ - // 需要启用StatFilter // DruidStatManagerFacade#getDataSourceStatDataList 该方法可以获取所有数据源的监控数据,除此之外 DruidStatManagerFacade 还提供了一些其他方法,你可以按需选择使用。 return DruidStatManagerFacade.getInstance().getDataSourceStatDataList(); } diff --git a/druid-spring-boot-starter/README_EN.md b/druid-spring-boot-starter/README_EN.md index cb1661ca6a..1a78002773 100644 --- a/druid-spring-boot-starter/README_EN.md +++ b/druid-spring-boot-starter/README_EN.md @@ -68,7 +68,7 @@ spring.datasource.druid.web-stat-filter.principal-cookie-name= spring.datasource.druid.web-stat-filter.profile-enable= # StatViewServlet properties, detail see Druid Wiki -spring.datasource.druid.stat-view-servlet.enabled= #Enable StatViewServlet, default value false. +spring.datasource.druid.stat-view-servlet.enabled= #Enable StatViewServlet (monitor console), default value false. spring.datasource.druid.stat-view-servlet.url-pattern= spring.datasource.druid.stat-view-servlet.reset-enable= spring.datasource.druid.stat-view-servlet.login-username= @@ -126,6 +126,7 @@ public DataSource dataSourceTwo(){ You can ```spring.datasource.druid.filters = stat, wall, log4j, dtc.``` way to enable the corresponding built-in Filter, but these are the default configuration Filter. If the default configuration can not meet your needs, you can give up this way, through the configuration file to configure the Filter, the following is an example. ```xml # StatFilter configuration example. +spring.datasource.druid.filter.stat.enabled=true spring.datasource.druid.filter.stat.db-type=h2 spring.datasource.druid.filter.stat.log-slow-sql=true spring.datasource.druid.filter.stat.slow-sql-millis=2000 @@ -151,14 +152,12 @@ Currently, configuration support is provided for the following filters. Please r Druid Spring Boot Starter will forbid StatFilter by default, and you can also set its enabled to true.,make the Filter configuration take effect and need to set enabled to true. ## How to get Druid monitoring(stat) data - -Druid's monitoring data can be obtained through DruidStatManagerFacade. After obtaining the monitoring data, you can expose it to your monitoring system for use. Druid's default monitoring system data also comes from this. Let's take a simple demonstration. In Spring Boot, how to expose Druid monitoring data in the form of JSON through the HTTP interface. In actual use, you can freely expand the monitoring data and exposure methods according to your needs. +Druid's monitoring data can be obtained through DruidStatManagerFacade after the StatFilter is enable. After obtaining the monitoring data, you can expose it to your monitoring system for use. Druid's default monitoring system data also comes from this. Let's take a simple demonstration. In Spring Boot, how to expose Druid monitoring data in the form of JSON through the HTTP interface. In actual use, you can freely expand the monitoring data and exposure methods according to your needs. ```java @RestController public class DruidStatController { @GetMapping("/druid/stat") public Object druidStat(){ - // Your need enable StatFilter // DruidStatManagerFacade#getDataSourceStatDataList 该方法可以获取所有数据源的监控数据,除此之外 DruidStatManagerFacade 还提供了一些其他方法,你可以按需选择使用。 return DruidStatManagerFacade.getInstance().getDataSourceStatDataList(); } diff --git a/druid-spring-boot-starter/src/test/resources/application.properties b/druid-spring-boot-starter/src/test/resources/application.properties index 31a15c2b2a..1ae3b4ac48 100644 --- a/druid-spring-boot-starter/src/test/resources/application.properties +++ b/druid-spring-boot-starter/src/test/resources/application.properties @@ -5,14 +5,14 @@ spring.datasource.password=sa # driver-class-name 非必填可根据url推断 spring.datasource.driver-class-name=org.h2.Driver -#Druid 数据源配置,继承spring.datasource.* 配置,相同则覆盖 +# Druid 数据源配置,继承spring.datasource.* 配置,相同则覆盖 spring.datasource.druid.initial-size=2 spring.datasource.druid.max-active=30 spring.datasource.druid.min-idle=2 spring.datasource.druid.max-wait=1234 spring.datasource.druid.pool-prepared-statements=true spring.datasource.druid.max-pool-prepared-statement-per-connection-size=5 -#spring.datasource.druid.max-open-prepared-statements= #等价于上面的max-pool-prepared-statement-per-connection-size +# spring.datasource.druid.max-open-prepared-statements= #等价于上面的max-pool-prepared-statement-per-connection-size spring.datasource.druid.validation-query=select 1 spring.datasource.druid.validation-query-timeout=1 spring.datasource.druid.test-on-borrow=true @@ -34,8 +34,12 @@ spring.datasource.druid.filter.stat.slow-sql-millis=2000 spring.jpa.show-sql= true spring.jpa.hibernate.ddl-auto=create-drop -# 更多配置属性见 DruidDataSource 内成员变量(只要有set方法便支持),或者根据IDE提示,或者查看官方文档 +# 配置下面参数用于启动监控页面,考虑安全问题,默认是关闭的,按需开启 +spring.datasource.druid.stat-view-servlet.enabled=true +spring.datasource.druid.filter.stat.enabled=true +spring.datasource.druid.web-stat-filter.enabled=true +# 更多配置属性见 DruidDataSource 内成员变量(只要有set方法便支持),或者根据IDE提示,或者查看官方文档