This is an automated email from the ASF dual-hosted git repository.

liujun pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/dubbo-samples.git


The following commit(s) were added to refs/heads/master by this push:
     new 792db132 metrics to spring boot endpoint (#711)
792db132 is described below

commit 792db13253bfd47e3b2dbeb98944bde2c5931a70
Author: songxiaosheng <[email protected]>
AuthorDate: Wed Feb 1 09:45:37 2023 +0800

    metrics to spring boot endpoint (#711)
---
 .../dubbo-samples-metrics-prometheus/pom.xml       |  31 +++
 .../prometheus/StartProviderAndConsumer.java       |  72 ++++++
 .../dubbo-samples-metrics-spring-boot/README.md    |  16 ++
 .../case-configuration.yml                         |  24 ++
 .../case-versions.conf                             |  25 ++
 .../pom.xml                                        |  85 +++++--
 .../metrics/springboot/EmbeddedZooKeeper.java      | 256 +++++++++++++++++++++
 .../metrics/springboot/MetricsApplication.java     |  71 ++++++
 .../metrics/springboot/api/DemoService.java        |  51 ++++
 .../metrics/springboot/impl/DemoServiceImpl.java   |  88 +++++++
 .../samples/metrics/springboot/model/Result.java   |  69 ++++++
 .../samples/metrics/springboot/model/User.java     |  66 ++++++
 .../src/main/resources/application.properties      |   9 +
 .../src/main/resources/log4j.properties            |  26 +++
 .../main/resources/spring/dubbo-demo-context.xml   |  44 ++++
 .../metrics/prometheus/ProviderMetricsIT.java      |  55 +++++
 4-governance/pom.xml                               |   1 +
 17 files changed, 976 insertions(+), 13 deletions(-)

diff --git a/4-governance/dubbo-samples-metrics-prometheus/pom.xml 
b/4-governance/dubbo-samples-metrics-prometheus/pom.xml
index c924bdba..4e1e748a 100644
--- a/4-governance/dubbo-samples-metrics-prometheus/pom.xml
+++ b/4-governance/dubbo-samples-metrics-prometheus/pom.xml
@@ -167,6 +167,37 @@
                     <target>${target.level}</target>
                 </configuration>
             </plugin>
+            <plugin>
+                <groupId>org.apache.maven.plugins</groupId>
+                <artifactId>maven-dependency-plugin</artifactId>
+                <executions>
+                    <execution>
+                        <id>copy</id>
+                        <phase>package</phase>
+                        <goals>
+                            <goal>copy-dependencies</goal>
+                        </goals>
+                        <configuration>
+                            <outputDirectory>
+                                ${project.build.directory}/lib
+                            </outputDirectory>
+                        </configuration>
+                    </execution>
+                </executions>
+            </plugin>
+            <plugin>
+                <groupId>org.apache.maven.plugins</groupId>
+                <artifactId>maven-jar-plugin</artifactId>
+                <configuration>
+                    <archive>
+                        <manifest>
+                            <addClasspath>true</addClasspath>
+                            <classpathPrefix>lib/</classpathPrefix>
+                            
<mainClass>org.apache.dubbo.samples.metrics.prometheus.StartProviderAndConsumer</mainClass>
 <!-- 你的主类名 -->
+                        </manifest>
+                    </archive>
+                </configuration>
+            </plugin>
         </plugins>
     </build>
 
diff --git 
a/4-governance/dubbo-samples-metrics-prometheus/src/main/java/org/apache/dubbo/samples/metrics/prometheus/StartProviderAndConsumer.java
 
b/4-governance/dubbo-samples-metrics-prometheus/src/main/java/org/apache/dubbo/samples/metrics/prometheus/StartProviderAndConsumer.java
new file mode 100644
index 00000000..02c0ccec
--- /dev/null
+++ 
b/4-governance/dubbo-samples-metrics-prometheus/src/main/java/org/apache/dubbo/samples/metrics/prometheus/StartProviderAndConsumer.java
@@ -0,0 +1,72 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You 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.
+ */
+
+/*
+ * Copyright 2014 the original author or authors.
+ *
+ * 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 org.apache.dubbo.samples.metrics.prometheus;
+
+import org.apache.dubbo.samples.metrics.prometheus.api.DemoService;
+import org.apache.dubbo.samples.metrics.prometheus.model.Result;
+import org.springframework.context.support.ClassPathXmlApplicationContext;
+
+
+public class StartProviderAndConsumer {
+    public static void main(String[] args) {
+        new EmbeddedZooKeeper(2181, false).start();
+
+        startProvider();
+        startConsumer();
+    }
+
+    private static void startConsumer() {
+        ClassPathXmlApplicationContext context = new 
ClassPathXmlApplicationContext("spring/dubbo-demo-consumer.xml");
+        context.start();
+
+        DemoService demoService = context.getBean("demoService", 
DemoService.class);
+
+        while (true) {
+            try {
+                Thread.sleep(3000);
+                Result hello = demoService.sayHello("world");
+                System.out.println(hello.getMsg());
+            } catch (InterruptedException e) {
+                e.printStackTrace();
+            }
+        }
+    }
+
+    private static void startProvider() {
+        ClassPathXmlApplicationContext context = new 
ClassPathXmlApplicationContext("spring/dubbo-demo-provider.xml");
+        context.start();
+
+        System.out.println("dubbo service started");
+    }
+}
diff --git a/4-governance/dubbo-samples-metrics-spring-boot/README.md 
b/4-governance/dubbo-samples-metrics-spring-boot/README.md
new file mode 100644
index 00000000..d09a31bc
--- /dev/null
+++ b/4-governance/dubbo-samples-metrics-spring-boot/README.md
@@ -0,0 +1,16 @@
+# 使用Metrics模块进行数据采集然后暴漏数据给普罗米修斯监控
+ 
+* 服务端
+```xml
+ <dubbo:metrics protocol="prometheus" enable-jvm-metrics="true">
+  <dubbo:aggregation enabled="true"/>
+  <dubbo:prometheus-exporter enabled="false"  metrics-port="20888"/>
+</dubbo:metrics>
+
+```
+
+  
+启动MetricsApplication  访问监控指标:http://localhost:8081/management/prometheus
+
+可观测性文档如下链接:
+  
[https://cn.dubbo.apache.org/zh/docs3-v2/java-sdk/advanced-features-and-usage/observability/](https://cn.dubbo.apache.org/zh/docs3-v2/java-sdk/advanced-features-and-usage/observability/)
\ No newline at end of file
diff --git 
a/4-governance/dubbo-samples-metrics-spring-boot/case-configuration.yml 
b/4-governance/dubbo-samples-metrics-spring-boot/case-configuration.yml
new file mode 100644
index 00000000..0ad86889
--- /dev/null
+++ b/4-governance/dubbo-samples-metrics-spring-boot/case-configuration.yml
@@ -0,0 +1,24 @@
+# Licensed to the Apache Software Foundation (ASF) under one
+# or more contributor license agreements.  See the NOTICE file
+# distributed with this work for additional information
+# regarding copyright ownership.  The ASF licenses this file
+# to you 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.
+
+from: app-builtin-zookeeper.yml
+
+props:
+  project_name: dubbo-samples-metrics-spring-boot
+  main_class: org.apache.dubbo.samples.metrics.springboot.MetricsApplication
+  zookeeper_port: 2181
+  dubbo_port: 20880
+
diff --git a/4-governance/dubbo-samples-metrics-spring-boot/case-versions.conf 
b/4-governance/dubbo-samples-metrics-spring-boot/case-versions.conf
new file mode 100644
index 00000000..a370983f
--- /dev/null
+++ b/4-governance/dubbo-samples-metrics-spring-boot/case-versions.conf
@@ -0,0 +1,25 @@
+#
+#
+#   Licensed to the Apache Software Foundation (ASF) under one or more
+#   contributor license agreements.  See the NOTICE file distributed with
+#   this work for additional information regarding copyright ownership.
+#   The ASF licenses this file to You 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.
+#
+
+
+# Supported component versions of the test case
+
+# Spring app
+dubbo.version=[ >=3.2.0 ]
+spring.version=4.*, 5.*
+java.version= [<= 11]
diff --git a/4-governance/dubbo-samples-metrics-prometheus/pom.xml 
b/4-governance/dubbo-samples-metrics-spring-boot/pom.xml
similarity index 69%
copy from 4-governance/dubbo-samples-metrics-prometheus/pom.xml
copy to 4-governance/dubbo-samples-metrics-spring-boot/pom.xml
index c924bdba..7f9312c5 100644
--- a/4-governance/dubbo-samples-metrics-prometheus/pom.xml
+++ b/4-governance/dubbo-samples-metrics-spring-boot/pom.xml
@@ -27,10 +27,10 @@
     <modelVersion>4.0.0</modelVersion>
 
     <groupId>org.apache.dubbo</groupId>
-    <artifactId>dubbo-samples-metrics-prometheus</artifactId>
+    <artifactId>dubbo-samples-metrics-springboot</artifactId>
     <version>1.0-SNAPSHOT</version>
 
-    <name>Dubbo Samples Metrics</name>
+    <name>Dubbo Samples Metrics SpringBoot</name>
     <description>Dubbo Samples Metrics</description>
 
     <properties>
@@ -38,9 +38,10 @@
         <target.level>1.8</target.level>
         <dubbo.version>3.2.0-beta.4-SNAPSHOT</dubbo.version>
         <junit.version>4.13.1</junit.version>
-        <spring.version>4.3.29.RELEASE</spring.version>
+        <spring.version>5.3.18</spring.version>
         <httpclient.version>4.5.13</httpclient.version>
         <junit.version>4.13.1</junit.version>
+        <spring-boot.version>2.6.6</spring-boot.version>
         <maven-compiler-plugin.version>3.7.0</maven-compiler-plugin.version>
     </properties>
 
@@ -53,6 +54,13 @@
                 <type>pom</type>
                 <scope>import</scope>
             </dependency>
+            <dependency>
+                <groupId>org.springframework.boot</groupId>
+                <artifactId>spring-boot-dependencies</artifactId>
+                <version>${spring-boot.version}</version>
+                <type>pom</type>
+                <scope>import</scope>
+            </dependency>
             <dependency>
                 <groupId>org.apache.dubbo</groupId>
                 <artifactId>dubbo-bom</artifactId>
@@ -86,10 +94,6 @@
             <artifactId>dubbo</artifactId>
         </dependency>
 
-        <dependency>
-            <groupId>org.apache.dubbo</groupId>
-            <artifactId>dubbo-spring-boot-actuator</artifactId>
-        </dependency>
 
         <!-- 
https://mvnrepository.com/artifact/org.apache.dubbo/dubbo-metrics-prometheus -->
         <dependency>
@@ -103,6 +107,40 @@
             <type>pom</type>
         </dependency>
 
+        <dependency>
+            <groupId>org.apache.dubbo</groupId>
+            <artifactId>dubbo-spring-boot-starter</artifactId>
+        </dependency>
+        <dependency>
+            <groupId>org.apache.dubbo</groupId>
+            <artifactId>dubbo-spring-boot-actuator</artifactId>
+        </dependency>
+        <dependency>
+            <groupId>org.springframework.boot</groupId>
+            <artifactId>spring-boot-starter</artifactId>
+            <exclusions>
+                <exclusion>
+                    <artifactId>logback-classic</artifactId>
+                    <groupId>ch.qos.logback</groupId>
+                </exclusion>
+                <exclusion>
+                    <artifactId>log4j-to-slf4j</artifactId>
+                    <groupId>org.apache.logging.log4j</groupId>
+                </exclusion>
+                <exclusion>
+                    <artifactId>jul-to-slf4j</artifactId>
+                    <groupId>org.slf4j</groupId>
+                </exclusion>
+            </exclusions>
+        </dependency>
+        <dependency>
+            <groupId>org.springframework.boot</groupId>
+            <artifactId>spring-boot-starter-actuator</artifactId>
+        </dependency>
+        <dependency>
+            <groupId>org.springframework.boot</groupId>
+            <artifactId>spring-boot-starter-web</artifactId>
+        </dependency>
         <dependency>
             <groupId>junit</groupId>
             <artifactId>junit</artifactId>
@@ -110,16 +148,11 @@
             <scope>test</scope>
         </dependency>
 
-        <dependency>
-            <groupId>org.springframework</groupId>
-            <artifactId>spring-test</artifactId>
-            <scope>test</scope>
-        </dependency>
 
         <dependency>
             <groupId>com.alibaba</groupId>
             <artifactId>fastjson</artifactId>
-            <version>1.2.83</version>
+            <version>1.2.83_noneautotype</version>
         </dependency>
         <dependency>
             <groupId>org.apache.httpcomponents</groupId>
@@ -129,6 +162,7 @@
         <dependency>
             <groupId>junit</groupId>
             <artifactId>junit</artifactId>
+            <version>${junit.version}</version>
             <scope>test</scope>
         </dependency>
 
@@ -137,6 +171,26 @@
             <artifactId>spring-test</artifactId>
             <scope>test</scope>
         </dependency>
+        <dependency>
+            <groupId>org.springframework.boot</groupId>
+            <artifactId>spring-boot-test</artifactId>
+            <scope>test</scope>
+        </dependency>
+        <dependency>
+            <groupId>io.swagger</groupId>
+            <artifactId>swagger-jaxrs</artifactId>
+            <version>1.5.19</version>
+            <exclusions>
+                <exclusion>
+                    <groupId>javax.validation</groupId>
+                    <artifactId>validation-api</artifactId>
+                </exclusion>
+            </exclusions>
+        </dependency>
+        <dependency>
+            <groupId>org.springframework</groupId>
+            <artifactId>spring-core</artifactId>
+        </dependency>
     </dependencies>
 
     <profiles>
@@ -167,6 +221,11 @@
                     <target>${target.level}</target>
                 </configuration>
             </plugin>
+            <plugin>
+                <groupId>org.springframework.boot</groupId>
+                <artifactId>spring-boot-maven-plugin</artifactId>
+                <version>2.6.8</version>
+            </plugin>
         </plugins>
     </build>
 
diff --git 
a/4-governance/dubbo-samples-metrics-spring-boot/src/main/java/org/apache/dubbo/samples/metrics/springboot/EmbeddedZooKeeper.java
 
b/4-governance/dubbo-samples-metrics-spring-boot/src/main/java/org/apache/dubbo/samples/metrics/springboot/EmbeddedZooKeeper.java
new file mode 100644
index 00000000..794311fe
--- /dev/null
+++ 
b/4-governance/dubbo-samples-metrics-spring-boot/src/main/java/org/apache/dubbo/samples/metrics/springboot/EmbeddedZooKeeper.java
@@ -0,0 +1,256 @@
+/*
+ * Copyright 2014 the original author or authors.
+ *
+ * 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 org.apache.dubbo.samples.metrics.springboot;
+
+import org.apache.zookeeper.server.ServerConfig;
+import org.apache.zookeeper.server.ZooKeeperServerMain;
+import org.apache.zookeeper.server.quorum.QuorumPeerConfig;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+import org.springframework.context.SmartLifecycle;
+import org.springframework.util.ErrorHandler;
+import org.springframework.util.SocketUtils;
+
+import java.io.File;
+import java.lang.reflect.Method;
+import java.util.Properties;
+import java.util.UUID;
+
+/**
+ * from: 
https://github.com/spring-projects/spring-xd/blob/v1.3.1.RELEASE/spring-xd-dirt/src/main/java/org/springframework/xd/dirt/zookeeper/ZooKeeperUtils.java
+ *
+ * Helper class to start an embedded instance of standalone (non clustered) 
ZooKeeper.
+ *
+ * NOTE: at least an external standalone server (if not an ensemble) are 
recommended, even for
+ * {@link org.springframework.xd.dirt.server.singlenode.SingleNodeApplication}
+ *
+ * @author Patrick Peralta
+ * @author Mark Fisher
+ * @author David Turanski
+ */
+public class EmbeddedZooKeeper implements SmartLifecycle {
+
+    /**
+     * Logger.
+     */
+    private static final Logger logger = 
LoggerFactory.getLogger(EmbeddedZooKeeper.class);
+
+    /**
+     * ZooKeeper client port. This will be determined dynamically upon startup.
+     */
+    private final int clientPort;
+
+    /**
+     * Whether to auto-start. Default is true.
+     */
+    private boolean autoStartup = true;
+
+    /**
+     * Lifecycle phase. Default is 0.
+     */
+    private int phase = 0;
+
+    /**
+     * Thread for running the ZooKeeper server.
+     */
+    private volatile Thread zkServerThread;
+
+    /**
+     * ZooKeeper server.
+     */
+    private volatile ZooKeeperServerMain zkServer;
+
+    /**
+     * {@link ErrorHandler} to be invoked if an Exception is thrown from the 
ZooKeeper server thread.
+     */
+    private ErrorHandler errorHandler;
+
+    private boolean daemon = true;
+
+    /**
+     * Construct an EmbeddedZooKeeper with a random port.
+     */
+    public EmbeddedZooKeeper() {
+        clientPort = SocketUtils.findAvailableTcpPort();
+    }
+
+    /**
+     * Construct an EmbeddedZooKeeper with the provided port.
+     *
+     * @param clientPort  port for ZooKeeper server to bind to
+     */
+    public EmbeddedZooKeeper(int clientPort, boolean daemon) {
+        this.clientPort = clientPort;
+        this.daemon = daemon;
+    }
+
+    /**
+     * Returns the port that clients should use to connect to this embedded 
server.
+     *
+     * @return dynamically determined client port
+     */
+    public int getClientPort() {
+        return this.clientPort;
+    }
+
+    /**
+     * Specify whether to start automatically. Default is true.
+     *
+     * @param autoStartup whether to start automatically
+     */
+    public void setAutoStartup(boolean autoStartup) {
+        this.autoStartup = autoStartup;
+    }
+
+    /**
+     * {@inheritDoc}
+     */
+    @Override
+    public boolean isAutoStartup() {
+        return this.autoStartup;
+    }
+
+    /**
+     * Specify the lifecycle phase for the embedded server.
+     *
+     * @param phase the lifecycle phase
+     */
+    public void setPhase(int phase) {
+        this.phase = phase;
+    }
+
+    /**
+     * {@inheritDoc}
+     */
+    @Override
+    public int getPhase() {
+        return this.phase;
+    }
+
+    /**
+     * {@inheritDoc}
+     */
+    @Override
+    public boolean isRunning() {
+        return (zkServerThread != null);
+    }
+
+    /**
+     * Start the ZooKeeper server in a background thread.
+     * <p>
+     * Register an error handler via {@link #setErrorHandler} in order to 
handle
+     * any exceptions thrown during startup or execution.
+     */
+    @Override
+    public synchronized void start() {
+        if (zkServerThread == null) {
+            zkServerThread = new Thread(new ServerRunnable(), "ZooKeeper 
Server Starter");
+            zkServerThread.setDaemon(daemon);
+            zkServerThread.start();
+        }
+    }
+
+    /**
+     * Shutdown the ZooKeeper server.
+     */
+    @Override
+    public synchronized void stop() {
+        if (zkServerThread != null) {
+            // The shutdown method is protected...thus this hack to invoke it.
+            // This will log an exception on shutdown; see
+            // https://issues.apache.org/jira/browse/ZOOKEEPER-1873 for 
details.
+            try {
+                Method shutdown = 
ZooKeeperServerMain.class.getDeclaredMethod("shutdown");
+                shutdown.setAccessible(true);
+                shutdown.invoke(zkServer);
+            }
+
+            catch (Exception e) {
+                throw new RuntimeException(e);
+            }
+
+            // It is expected that the thread will exit after
+            // the server is shutdown; this will block until
+            // the shutdown is complete.
+            try {
+                zkServerThread.join(5000);
+                zkServerThread = null;
+            }
+            catch (InterruptedException e) {
+                Thread.currentThread().interrupt();
+                logger.warn("Interrupted while waiting for embedded ZooKeeper 
to exit");
+                // abandoning zk thread
+                zkServerThread = null;
+            }
+        }
+    }
+
+    /**
+     * Stop the server if running and invoke the callback when complete.
+     */
+    @Override
+    public void stop(Runnable callback) {
+        stop();
+        callback.run();
+    }
+
+    /**
+     * Provide an {@link ErrorHandler} to be invoked if an Exception is thrown 
from the ZooKeeper server thread. If none
+     * is provided, only error-level logging will occur.
+     *
+     * @param errorHandler the {@link ErrorHandler} to be invoked
+     */
+    public void setErrorHandler(ErrorHandler errorHandler) {
+        this.errorHandler = errorHandler;
+    }
+
+    /**
+     * Runnable implementation that starts the ZooKeeper server.
+     */
+    private class ServerRunnable implements Runnable {
+
+        @Override
+        public void run() {
+            try {
+                Properties properties = new Properties();
+                File file = new File(System.getProperty("java.io.tmpdir")
+                    + File.separator + UUID.randomUUID());
+                file.deleteOnExit();
+                properties.setProperty("dataDir", file.getAbsolutePath());
+                properties.setProperty("clientPort", 
String.valueOf(clientPort));
+
+                QuorumPeerConfig quorumPeerConfig = new QuorumPeerConfig();
+                quorumPeerConfig.parseProperties(properties);
+
+                zkServer = new ZooKeeperServerMain();
+                ServerConfig configuration = new ServerConfig();
+                configuration.readFrom(quorumPeerConfig);
+
+                zkServer.runFromConfig(configuration);
+            }
+            catch (Exception e) {
+                if (errorHandler != null) {
+                    errorHandler.handleError(e);
+                }
+                else {
+                    logger.error("Exception running embedded ZooKeeper", e);
+                }
+            }
+        }
+    }
+
+}
+
diff --git 
a/4-governance/dubbo-samples-metrics-spring-boot/src/main/java/org/apache/dubbo/samples/metrics/springboot/MetricsApplication.java
 
b/4-governance/dubbo-samples-metrics-spring-boot/src/main/java/org/apache/dubbo/samples/metrics/springboot/MetricsApplication.java
new file mode 100644
index 00000000..d99a5245
--- /dev/null
+++ 
b/4-governance/dubbo-samples-metrics-spring-boot/src/main/java/org/apache/dubbo/samples/metrics/springboot/MetricsApplication.java
@@ -0,0 +1,71 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You 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.
+ */
+
+/*
+ * Copyright 2014 the original author or authors.
+ *
+ * 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 org.apache.dubbo.samples.metrics.springboot;
+
+import org.apache.dubbo.config.annotation.DubboReference;
+import org.apache.dubbo.config.spring.context.annotation.EnableDubbo;
+import org.apache.dubbo.samples.metrics.springboot.api.DemoService;
+import org.apache.dubbo.samples.metrics.springboot.model.Result;
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.boot.CommandLineRunner;
+import org.springframework.boot.SpringApplication;
+import org.springframework.boot.autoconfigure.SpringBootApplication;
+import org.springframework.context.annotation.ImportResource;
+
+
+@SpringBootApplication(scanBasePackages = { "org.apache.dubbo"})
+@EnableDubbo(scanBasePackages = "org.apache.dubbo")
+@ImportResource("classpath*:spring/dubbo-demo-*.xml")
+public class MetricsApplication implements CommandLineRunner {
+
+    @DubboReference
+    private DemoService demoService;
+
+    public static void main(String[] args) throws Exception {
+        new EmbeddedZooKeeper(2181, false).start();
+        SpringApplication.run(MetricsApplication.class, args);
+    }
+    @Override
+    public void run(String... args) throws Exception {
+        while (true) {
+            try {
+                Thread.sleep(3000);
+                Result hello = demoService.sayHello("world");
+                System.out.println(hello.getMsg());
+            } catch (InterruptedException e) {
+                e.printStackTrace();
+            }
+        }
+    }
+}
diff --git 
a/4-governance/dubbo-samples-metrics-spring-boot/src/main/java/org/apache/dubbo/samples/metrics/springboot/api/DemoService.java
 
b/4-governance/dubbo-samples-metrics-spring-boot/src/main/java/org/apache/dubbo/samples/metrics/springboot/api/DemoService.java
new file mode 100644
index 00000000..45ea059e
--- /dev/null
+++ 
b/4-governance/dubbo-samples-metrics-spring-boot/src/main/java/org/apache/dubbo/samples/metrics/springboot/api/DemoService.java
@@ -0,0 +1,51 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You 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.
+ */
+
+/*
+ * Copyright 2014 the original author or authors.
+ *
+ * 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 org.apache.dubbo.samples.metrics.springboot.api;
+
+import org.apache.dubbo.samples.metrics.springboot.model.Result;
+import org.apache.dubbo.samples.metrics.springboot.model.User;
+
+import java.util.concurrent.CompletableFuture;
+
+public interface DemoService {
+    CompletableFuture<Integer> sayHello();
+
+    Result sayHello(String name);
+
+    Result sayHello(Long id, String name);
+
+    Result sayHello(User user);
+
+    String stringArray(String[] bytes);
+}
diff --git 
a/4-governance/dubbo-samples-metrics-spring-boot/src/main/java/org/apache/dubbo/samples/metrics/springboot/impl/DemoServiceImpl.java
 
b/4-governance/dubbo-samples-metrics-spring-boot/src/main/java/org/apache/dubbo/samples/metrics/springboot/impl/DemoServiceImpl.java
new file mode 100644
index 00000000..dff63abe
--- /dev/null
+++ 
b/4-governance/dubbo-samples-metrics-spring-boot/src/main/java/org/apache/dubbo/samples/metrics/springboot/impl/DemoServiceImpl.java
@@ -0,0 +1,88 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You 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.
+ */
+
+/*
+ * Copyright 2014 the original author or authors.
+ *
+ * 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 org.apache.dubbo.samples.metrics.springboot.impl;
+
+import org.apache.dubbo.common.logger.Logger;
+import org.apache.dubbo.common.logger.LoggerFactory;
+
+import com.alibaba.dubbo.rpc.RpcContext;
+import org.apache.dubbo.samples.metrics.springboot.api.DemoService;
+import org.apache.dubbo.samples.metrics.springboot.model.Result;
+import org.apache.dubbo.samples.metrics.springboot.model.User;
+
+import java.text.SimpleDateFormat;
+import java.util.Date;
+import java.util.concurrent.CompletableFuture;
+
+public class DemoServiceImpl implements DemoService {
+
+    public static final Logger logger = 
LoggerFactory.getLogger(DemoServiceImpl.class);
+
+    private String name = "Han MeiMei";
+
+    @Override
+    public CompletableFuture<Integer> sayHello() {
+        return CompletableFuture.completedFuture(2122);
+    }
+
+
+
+    @Override
+    public Result sayHello(String localName) {
+        logger.info("[" + new SimpleDateFormat("HH:mm:ss").format(new Date()) 
+ "] Hello " + name + ", request from consumer: " + RpcContext
+                .getContext().getRemoteAddress());
+        return new Result(name, "Hello " + localName + ", response from 
provider: " + RpcContext.getContext().getLocalAddress());
+    }
+
+    @Override
+    public Result sayHello(final Long id, final String name) {
+        return sayHello(new User(id, name));
+    }
+
+    @Override
+    public Result sayHello(final User user) {
+        String localName = user.getUsername();
+        Long id = user.getId();
+        logger.info("[" + new SimpleDateFormat("HH:mm:ss").format(new Date()) 
+ "] Hello " + name + ", request from consumer: " + RpcContext
+                .getContext().getRemoteAddress());
+        return new Result(name, "Hello " + id + " " + localName +
+//                " " + "bytes: " + user.getBytes().toString() +
+                ", response from provider: " + 
RpcContext.getContext().getLocalAddress());
+    }
+
+    @Override
+    public String stringArray(String[] bytes) {
+        return bytes.toString();
+    }
+}
diff --git 
a/4-governance/dubbo-samples-metrics-spring-boot/src/main/java/org/apache/dubbo/samples/metrics/springboot/model/Result.java
 
b/4-governance/dubbo-samples-metrics-spring-boot/src/main/java/org/apache/dubbo/samples/metrics/springboot/model/Result.java
new file mode 100644
index 00000000..749f9984
--- /dev/null
+++ 
b/4-governance/dubbo-samples-metrics-spring-boot/src/main/java/org/apache/dubbo/samples/metrics/springboot/model/Result.java
@@ -0,0 +1,69 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You 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.
+ */
+
+/*
+ * Copyright 2014 the original author or authors.
+ *
+ * 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 org.apache.dubbo.samples.metrics.springboot.model;
+
+import java.io.Serializable;
+
+/**
+ * @author zmx ON 2019-07-03
+ */
+public class Result implements Serializable {
+    public Result(){
+
+    }
+
+    public Result(String userName, String msg){
+        this.msg = msg;
+        this.userName = userName;
+    }
+
+    private String userName;
+    private String msg;
+
+    public String getUserName() {
+        return userName;
+    }
+
+    public void setUserName(String userName) {
+        this.userName = userName;
+    }
+
+    public String getMsg() {
+        return msg;
+    }
+
+    public void setMsg(String msg) {
+        this.msg = msg;
+    }
+}
diff --git 
a/4-governance/dubbo-samples-metrics-spring-boot/src/main/java/org/apache/dubbo/samples/metrics/springboot/model/User.java
 
b/4-governance/dubbo-samples-metrics-spring-boot/src/main/java/org/apache/dubbo/samples/metrics/springboot/model/User.java
new file mode 100644
index 00000000..0b7de6bd
--- /dev/null
+++ 
b/4-governance/dubbo-samples-metrics-spring-boot/src/main/java/org/apache/dubbo/samples/metrics/springboot/model/User.java
@@ -0,0 +1,66 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You 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.
+ */
+
+/*
+ * Copyright 2014 the original author or authors.
+ *
+ * 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 org.apache.dubbo.samples.metrics.springboot.model;
+
+import java.io.Serializable;
+
+public class User implements Serializable {
+    private Long id;
+    private String username;
+
+    public User() {
+    }
+
+    public User(final Long id, final String username) {
+        this.id = id;
+        this.username = username;
+    }
+
+    public Long getId() {
+        return id;
+    }
+
+    public void setId(final Long id) {
+        this.id = id;
+    }
+
+    public String getUsername() {
+        return username;
+    }
+
+    public void setUsername(final String username) {
+        this.username = username;
+    }
+
+}
diff --git 
a/4-governance/dubbo-samples-metrics-spring-boot/src/main/resources/application.properties
 
b/4-governance/dubbo-samples-metrics-spring-boot/src/main/resources/application.properties
new file mode 100644
index 00000000..2341861b
--- /dev/null
+++ 
b/4-governance/dubbo-samples-metrics-spring-boot/src/main/resources/application.properties
@@ -0,0 +1,9 @@
+management.metrics.tags.application=dubbo-samples-metrics-spring-boot
+management.server.port=8081
+management.endpoints.web.base-path=/management
+management.endpoints.web.exposure.include=info,health,env,prometheus
+spring.main.allow-circular-references=true
+management.endpoint.metrics.enabled=true
+management.endpoint.prometheus.enabled=true
+management.metrics.export.prometheus.enabled=true
+server.port=8080
\ No newline at end of file
diff --git 
a/4-governance/dubbo-samples-metrics-spring-boot/src/main/resources/log4j.properties
 
b/4-governance/dubbo-samples-metrics-spring-boot/src/main/resources/log4j.properties
new file mode 100644
index 00000000..d6ecd5ce
--- /dev/null
+++ 
b/4-governance/dubbo-samples-metrics-spring-boot/src/main/resources/log4j.properties
@@ -0,0 +1,26 @@
+#
+#
+#   Licensed to the Apache Software Foundation (ASF) under one or more
+#   contributor license agreements.  See the NOTICE file distributed with
+#   this work for additional information regarding copyright ownership.
+#   The ASF licenses this file to You 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.
+#
+#
+
+###set log levels###
+log4j.rootLogger=info, stdout
+###output to the console###
+log4j.appender.stdout=org.apache.log4j.ConsoleAppender
+log4j.appender.stdout.Target=System.out
+log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
+log4j.appender.stdout.layout.ConversionPattern=[%d{dd/MM/yy hh:mm:ss:sss z}] 
%t %5p %c{2}: %m%n
\ No newline at end of file
diff --git 
a/4-governance/dubbo-samples-metrics-spring-boot/src/main/resources/spring/dubbo-demo-context.xml
 
b/4-governance/dubbo-samples-metrics-spring-boot/src/main/resources/spring/dubbo-demo-context.xml
new file mode 100644
index 00000000..89427129
--- /dev/null
+++ 
b/4-governance/dubbo-samples-metrics-spring-boot/src/main/resources/spring/dubbo-demo-context.xml
@@ -0,0 +1,44 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<!--
+  Licensed to the Apache Software Foundation (ASF) under one or more
+  contributor license agreements.  See the NOTICE file distributed with
+  this work for additional information regarding copyright ownership.
+  The ASF licenses this file to You 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.
+  -->
+
+<beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance";
+       xmlns:dubbo="http://dubbo.apache.org/schema/dubbo";
+       xmlns="http://www.springframework.org/schema/beans"; 
xmlns:context="http://www.springframework.org/schema/context";
+       xsi:schemaLocation="http://www.springframework.org/schema/beans 
http://www.springframework.org/schema/beans/spring-beans.xsd
+       http://dubbo.apache.org/schema/dubbo 
http://dubbo.apache.org/schema/dubbo/dubbo.xsd 
http://www.springframework.org/schema/context 
http://www.springframework.org/schema/context/spring-context.xsd";>
+    <context:property-placeholder/>
+
+    <dubbo:application name="metrics-provider"/>
+
+    <dubbo:registry address="zookeeper://${zookeeper.address:127.0.0.1}:2181"/>
+    <dubbo:config-center 
address="zookeeper://${zookeeper.address:127.0.0.1}:2181" />
+    <dubbo:metadata-report 
address="zookeeper://${zookeeper.address:127.0.0.1}:2181" />
+
+
+    <dubbo:metrics protocol="prometheus" enable-jvm-metrics="true">
+        <dubbo:aggregation enabled="true"/>
+        <dubbo:prometheus-exporter enabled="false"  metrics-port="20888"/>
+    </dubbo:metrics>
+
+    <bean id="demoService" 
class="org.apache.dubbo.samples.metrics.springboot.impl.DemoServiceImpl"/>
+
+    <dubbo:service 
interface="org.apache.dubbo.samples.metrics.springboot.api.DemoService" 
ref="demoService"/>
+
+<!--    <dubbo:reference id="demoService" check="false" 
interface="org.apache.dubbo.samples.metrics.springboot.api.DemoService"/>-->
+
+</beans>
diff --git 
a/4-governance/dubbo-samples-metrics-spring-boot/src/test/java/org/apache/dubbo/samples/metrics/prometheus/ProviderMetricsIT.java
 
b/4-governance/dubbo-samples-metrics-spring-boot/src/test/java/org/apache/dubbo/samples/metrics/prometheus/ProviderMetricsIT.java
new file mode 100644
index 00000000..1ae3e390
--- /dev/null
+++ 
b/4-governance/dubbo-samples-metrics-spring-boot/src/test/java/org/apache/dubbo/samples/metrics/prometheus/ProviderMetricsIT.java
@@ -0,0 +1,55 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You 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 org.apache.dubbo.samples.metrics.prometheus;
+
+import org.apache.http.client.methods.CloseableHttpResponse;
+import org.apache.http.client.methods.HttpGet;
+import org.apache.http.impl.client.CloseableHttpClient;
+import org.apache.http.impl.client.HttpClients;
+import org.junit.Assert;
+import org.junit.Test;
+import org.junit.runner.RunWith;
+import org.springframework.boot.test.context.SpringBootTest;
+import org.springframework.test.context.ContextConfiguration;
+import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
+import org.springframework.test.context.junit4.SpringRunner;
+
+import java.io.BufferedReader;
+import java.io.InputStream;
+import java.io.InputStreamReader;
+import java.nio.charset.StandardCharsets;
+import java.util.stream.Collectors;
+@RunWith(SpringRunner.class)
+@SpringBootTest(classes = 
{org.apache.dubbo.samples.metrics.springboot.MetricsApplication.class})
+public class ProviderMetricsIT {
+
+    private final String port = "8081";
+
+    @Test
+    public void test() throws Exception {
+        try (CloseableHttpClient client = HttpClients.createDefault()) {
+            HttpGet request = new HttpGet("http://localhost:"; + port + 
"/management/prometheus");
+            CloseableHttpResponse response = client.execute(request);
+            InputStream inputStream = response.getEntity().getContent();
+            String text = new BufferedReader(new 
InputStreamReader(inputStream, 
StandardCharsets.UTF_8)).lines().collect(Collectors.joining("\n"));
+            
Assert.assertTrue(text.contains("jvm_gc_memory_promoted_bytes_total"));
+        } catch (Exception e) {
+            Assert.fail(e.getMessage());
+        }
+    }
+}
diff --git a/4-governance/pom.xml b/4-governance/pom.xml
index bec01239..22f53f3b 100644
--- a/4-governance/pom.xml
+++ b/4-governance/pom.xml
@@ -32,6 +32,7 @@
         <module>dubbo-samples-meshrule-router</module>
         <module>dubbo-samples-metrics</module>
         <module>dubbo-samples-metrics-prometheus</module>
+        <module>dubbo-samples-metrics-spring-boot</module>
         <module>dubbo-samples-monitor</module>
         <module>dubbo-samples-sentinel</module>
         <module>dubbo-samples-servicelevel-override</module>


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]


Reply via email to