Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

for #3010 #3056

Merged
merged 4 commits into from
Jan 31, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions druid-spring-boot-starter/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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=
Expand Down Expand Up @@ -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
Expand All @@ -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();
}
Expand Down
7 changes: 3 additions & 4 deletions druid-spring-boot-starter/README_EN.md
Original file line number Diff line number Diff line change
Expand Up @@ -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=
Expand Down Expand Up @@ -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
Expand All @@ -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();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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提示,或者查看官方文档