From 02b42bb2afcc292b6786faf9df12fcba672fdcbb Mon Sep 17 00:00:00 2001
From: lihengming <89921218@qq.com>
Date: Sun, 3 Dec 2017 22:47:55 +0800
Subject: [PATCH] add > spring boot actuator endpoints
---
druid-spring-boot-starter/pom.xml | 20 +++++++--
.../endpoint/DruidDataSourceMvcEndpoint.java | 44 +++++++++++++++++++
.../src/test/resources/application.properties | 7 +++
3 files changed, 68 insertions(+), 3 deletions(-)
create mode 100644 druid-spring-boot-starter/src/main/java/com/alibaba/druid/spring/boot/autoconfigure/actuator/endpoint/DruidDataSourceMvcEndpoint.java
diff --git a/druid-spring-boot-starter/pom.xml b/druid-spring-boot-starter/pom.xml
index 9a8dc857a4..1475541227 100644
--- a/druid-spring-boot-starter/pom.xml
+++ b/druid-spring-boot-starter/pom.xml
@@ -4,15 +4,15 @@
4.0.0
com.alibaba
druid-spring-boot-starter
- 1.1.4
+ 1.1.6
jar
druid-spring-boot-starter
Spring Boot with Druid support,help you simplify Druid config in Spring Boot.
https://github.com/alibaba/druid
- 1.1.4
- 1.5.6.RELEASE
+ 1.1.6
+ 1.5.9.RELEASE
@@ -44,6 +44,10 @@
druid
${druid.version}
+
+ org.slf4j
+ slf4j-api
+
org.springframework.boot
spring-boot-autoconfigure
@@ -54,6 +58,16 @@
spring-boot-configuration-processor
true
+
+ org.springframework.boot
+ spring-boot-actuator
+ true
+
+
+ org.springframework
+ spring-webmvc
+ true
+
javax.servlet
javax.servlet-api
diff --git a/druid-spring-boot-starter/src/main/java/com/alibaba/druid/spring/boot/autoconfigure/actuator/endpoint/DruidDataSourceMvcEndpoint.java b/druid-spring-boot-starter/src/main/java/com/alibaba/druid/spring/boot/autoconfigure/actuator/endpoint/DruidDataSourceMvcEndpoint.java
new file mode 100644
index 0000000000..054d303af9
--- /dev/null
+++ b/druid-spring-boot-starter/src/main/java/com/alibaba/druid/spring/boot/autoconfigure/actuator/endpoint/DruidDataSourceMvcEndpoint.java
@@ -0,0 +1,44 @@
+/*
+ * Copyright 1999-2017 Alibaba Group Holding Ltd.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package com.alibaba.druid.spring.boot.autoconfigure.actuator.endpoint;
+
+import com.alibaba.druid.stat.DruidStatManagerFacade;
+
+import org.springframework.boot.actuate.endpoint.mvc.AbstractMvcEndpoint;
+import org.springframework.boot.actuate.endpoint.mvc.ActuatorMediaTypes;
+import org.springframework.boot.context.properties.ConfigurationProperties;
+import org.springframework.http.MediaType;
+import org.springframework.web.bind.annotation.GetMapping;
+import org.springframework.web.bind.annotation.ResponseBody;
+
+/**
+ * @author lihengming [89921218@qq.com]
+ */
+@ConfigurationProperties(prefix = "endpoints.druid")
+public class DruidDataSourceMvcEndpoint extends AbstractMvcEndpoint {
+
+ public DruidDataSourceMvcEndpoint() {
+ super("/druid-endpoint", true);
+ }
+
+ @ResponseBody
+ @GetMapping( produces = {ActuatorMediaTypes.APPLICATION_ACTUATOR_V1_JSON_VALUE,
+ MediaType.APPLICATION_JSON_VALUE})
+ public Object invoke() {
+ return DruidStatManagerFacade.getInstance().getDataSourceStatDataList();
+ }
+
+}
\ No newline at end of file
diff --git a/druid-spring-boot-starter/src/test/resources/application.properties b/druid-spring-boot-starter/src/test/resources/application.properties
index 6f583300f2..2ebca26911 100644
--- a/druid-spring-boot-starter/src/test/resources/application.properties
+++ b/druid-spring-boot-starter/src/test/resources/application.properties
@@ -25,4 +25,11 @@ spring.jpa.hibernate.ddl-auto=create-drop
# 更多配置属性见 DruidDataSource 内成员变量(只要有set方法便支持),或者根据IDE提示,或者查看官方文档
+# 启用endpoint,默认访问地址:/druid-endpoint,可以使用endpoints.druid.path=自定义终端地址
+endpoints.druid.enabled=true
+# 默认需要安全认证才可以访问,可以用下面两种方法之一来禁用安全认证,详情见 Spring Boot 官方文档
+endpoints.druid.sensitive=true
+# management.security.enabled=false
+
# 注:配置文件可以根据习惯更换为 .yml
+