You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@servicecomb.apache.org by GitBox <gi...@apache.org> on 2018/01/10 07:38:27 UTC

[GitHub] wujimin closed pull request #487: [SCB-166] Metrics Support HealthCheck

wujimin closed pull request #487: [SCB-166] Metrics Support HealthCheck
URL: https://github.com/apache/incubator-servicecomb-java-chassis/pull/487
 
 
   

This is a PR merged from a forked repository.
As GitHub hides the original diff on merge, it is displayed below for
the sake of provenance:

As this is a foreign pull request (from a fork), the diff is supplied
below (as it won't show otherwise due to GitHub magic):

diff --git a/metrics/metrics-common/src/main/java/io/servicecomb/metrics/common/DefaultHealthCheckExtraData.java b/metrics/metrics-common/src/main/java/io/servicecomb/metrics/common/DefaultHealthCheckExtraData.java
new file mode 100644
index 000000000..aa2c9b45f
--- /dev/null
+++ b/metrics/metrics-common/src/main/java/io/servicecomb/metrics/common/DefaultHealthCheckExtraData.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.
+ */
+
+package io.servicecomb.metrics.common;
+
+public class DefaultHealthCheckExtraData {
+  private String instanceId;
+
+  private String hostName;
+
+  private String appId;
+
+  private String serviceName;
+
+  private String serviceVersion;
+
+  private String endpoints;
+
+  public String getInstanceId() {
+    return instanceId;
+  }
+
+  public String getHostName() {
+    return hostName;
+  }
+
+  public String getAppId() {
+    return appId;
+  }
+
+  public String getServiceName() {
+    return serviceName;
+  }
+
+  public String getServiceVersion() {
+    return serviceVersion;
+  }
+
+  public String getEndpoints() {
+    return endpoints;
+  }
+
+  public DefaultHealthCheckExtraData() {
+  }
+
+  public DefaultHealthCheckExtraData(String instanceId, String hostName, String appId, String serviceName,
+      String serviceVersion, String endpoints) {
+    this.instanceId = instanceId;
+    this.hostName = hostName;
+    this.appId = appId;
+    this.serviceName = serviceName;
+    this.serviceVersion = serviceVersion;
+    this.endpoints = endpoints;
+  }
+}
diff --git a/metrics/metrics-common/src/main/java/io/servicecomb/metrics/common/HealthCheckResult.java b/metrics/metrics-common/src/main/java/io/servicecomb/metrics/common/HealthCheckResult.java
new file mode 100644
index 000000000..168991b66
--- /dev/null
+++ b/metrics/metrics-common/src/main/java/io/servicecomb/metrics/common/HealthCheckResult.java
@@ -0,0 +1,56 @@
+/*
+ * 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 io.servicecomb.metrics.common;
+
+public class HealthCheckResult {
+  private boolean healthy;
+
+  private String information;
+
+  //unsupport object or generic type,so string..
+  private String extraData;
+
+  private long timestamp;
+
+  public boolean isHealthy() {
+    return healthy;
+  }
+
+  public String getInformation() {
+    return information;
+  }
+
+  public String getExtraData() {
+    return extraData;
+  }
+
+  public long getTimestamp() {
+    return timestamp;
+  }
+
+  public HealthCheckResult() {
+  }
+
+  public HealthCheckResult(boolean healthy, String information, String extraData) {
+    this();
+    this.healthy = healthy;
+    this.information = information;
+    this.extraData = extraData;
+    this.timestamp = System.currentTimeMillis();
+  }
+}
diff --git a/metrics/metrics-common/src/main/java/io/servicecomb/metrics/common/HealthChecker.java b/metrics/metrics-common/src/main/java/io/servicecomb/metrics/common/HealthChecker.java
new file mode 100644
index 000000000..69025e1d9
--- /dev/null
+++ b/metrics/metrics-common/src/main/java/io/servicecomb/metrics/common/HealthChecker.java
@@ -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.
+ */
+
+package io.servicecomb.metrics.common;
+
+public interface HealthChecker {
+  String getName();
+
+  HealthCheckResult check();
+}
diff --git a/metrics/metrics-common/src/main/java/io/servicecomb/metrics/common/HealthCheckerPublisher.java b/metrics/metrics-common/src/main/java/io/servicecomb/metrics/common/HealthCheckerPublisher.java
new file mode 100644
index 000000000..68aacca91
--- /dev/null
+++ b/metrics/metrics-common/src/main/java/io/servicecomb/metrics/common/HealthCheckerPublisher.java
@@ -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.
+ */
+
+package io.servicecomb.metrics.common;
+
+import java.util.Map;
+
+public interface HealthCheckerPublisher {
+  Map<String, HealthCheckResult> health();
+
+  HealthCheckResult healthWithName(String name);
+}
diff --git a/metrics/metrics-core/src/main/java/io/servicecomb/metrics/core/health/DefaultMicroserviceHealthChecker.java b/metrics/metrics-core/src/main/java/io/servicecomb/metrics/core/health/DefaultMicroserviceHealthChecker.java
new file mode 100644
index 000000000..ec0d1e2a0
--- /dev/null
+++ b/metrics/metrics-core/src/main/java/io/servicecomb/metrics/core/health/DefaultMicroserviceHealthChecker.java
@@ -0,0 +1,67 @@
+/*
+ * 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 io.servicecomb.metrics.core.health;
+
+import javax.ws.rs.core.Response.Status;
+
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+import org.springframework.stereotype.Component;
+
+import io.servicecomb.foundation.common.utils.JsonUtils;
+import io.servicecomb.metrics.common.DefaultHealthCheckExtraData;
+import io.servicecomb.metrics.common.HealthCheckResult;
+import io.servicecomb.metrics.common.HealthChecker;
+import io.servicecomb.serviceregistry.RegistryUtils;
+import io.servicecomb.serviceregistry.api.registry.Microservice;
+import io.servicecomb.serviceregistry.api.registry.MicroserviceInstance;
+import io.servicecomb.swagger.invocation.exception.InvocationException;
+
+@Component
+public class DefaultMicroserviceHealthChecker implements HealthChecker {
+
+  private static Logger logger = LoggerFactory.getLogger(DefaultMicroserviceHealthChecker.class);
+
+  @Override
+  public String getName() {
+    return "default";
+  }
+
+  @Override
+  public HealthCheckResult check() {
+    return new HealthCheckResult(true, "", getExtraData());
+  }
+
+  private String getExtraData() {
+    try {
+      Microservice microservice = RegistryUtils.getMicroservice();
+      MicroserviceInstance instance = RegistryUtils.getMicroserviceInstance();
+      return JsonUtils.writeValueAsString(new DefaultHealthCheckExtraData(
+          instance.getInstanceId(),
+          instance.getHostName(),
+          microservice.getAppId(),
+          microservice.getServiceName(),
+          microservice.getVersion(),
+          String.join(",", instance.getEndpoints())));
+    } catch (Exception e) {
+      String error = "unable load microservice info from RegistryUtils";
+      logger.error(error, e);
+      throw new InvocationException(Status.INTERNAL_SERVER_ERROR, error);
+    }
+  }
+}
diff --git a/metrics/metrics-core/src/main/java/io/servicecomb/metrics/core/publish/DefaultHealthCheckerManager.java b/metrics/metrics-core/src/main/java/io/servicecomb/metrics/core/publish/DefaultHealthCheckerManager.java
new file mode 100644
index 000000000..f83d209e0
--- /dev/null
+++ b/metrics/metrics-core/src/main/java/io/servicecomb/metrics/core/publish/DefaultHealthCheckerManager.java
@@ -0,0 +1,73 @@
+/*
+ * 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 io.servicecomb.metrics.core.publish;
+
+import static javax.ws.rs.core.Response.Status.BAD_REQUEST;
+
+import java.util.List;
+import java.util.Map;
+import java.util.Map.Entry;
+import java.util.concurrent.ConcurrentHashMap;
+import java.util.stream.Collectors;
+
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.stereotype.Component;
+
+import io.servicecomb.metrics.common.HealthCheckResult;
+import io.servicecomb.metrics.common.HealthChecker;
+import io.servicecomb.metrics.core.health.DefaultMicroserviceHealthChecker;
+import io.servicecomb.swagger.invocation.exception.InvocationException;
+
+@Component
+public class DefaultHealthCheckerManager implements HealthCheckerManager {
+
+  private static Logger logger = LoggerFactory.getLogger(DefaultHealthCheckerManager.class);
+
+  private final Map<String, HealthChecker> healthCheckers;
+
+  @Autowired(required = false)
+  public DefaultHealthCheckerManager(List<HealthChecker> springHealthCheckers) {
+    this.healthCheckers = new ConcurrentHashMap<>();
+    if (springHealthCheckers != null && !springHealthCheckers.isEmpty()) {
+      for (HealthChecker checker : springHealthCheckers) {
+        this.healthCheckers.put(checker.getName(), checker);
+      }
+    }
+  }
+
+  @Override
+  public void register(HealthChecker checker) {
+    healthCheckers.put(checker.getName(), checker);
+  }
+
+  @Override
+  public Map<String, HealthCheckResult> check() {
+    return healthCheckers.entrySet().stream().collect(Collectors.toMap(Entry::getKey, e -> e.getValue().check()));
+  }
+
+  @Override
+  public HealthCheckResult check(String name) {
+    HealthChecker checker = healthCheckers.get(name);
+    if (checker != null) {
+      return checker.check();
+    }
+    throw new InvocationException(BAD_REQUEST, "HealthChecker name : " + name + " unregister");
+  }
+}
diff --git a/metrics/metrics-core/src/main/java/io/servicecomb/metrics/core/publish/DefaultHealthCheckerPublisher.java b/metrics/metrics-core/src/main/java/io/servicecomb/metrics/core/publish/DefaultHealthCheckerPublisher.java
new file mode 100644
index 000000000..814a9e290
--- /dev/null
+++ b/metrics/metrics-core/src/main/java/io/servicecomb/metrics/core/publish/DefaultHealthCheckerPublisher.java
@@ -0,0 +1,59 @@
+/*
+ * 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 io.servicecomb.metrics.core.publish;
+
+import java.util.Map;
+
+import org.springframework.web.bind.annotation.CrossOrigin;
+import org.springframework.web.bind.annotation.PathVariable;
+import org.springframework.web.bind.annotation.RequestMapping;
+import org.springframework.web.bind.annotation.RequestMethod;
+
+import io.servicecomb.metrics.common.HealthCheckResult;
+import io.servicecomb.metrics.common.HealthCheckerPublisher;
+import io.servicecomb.provider.rest.common.RestSchema;
+import io.swagger.annotations.ApiResponse;
+import io.swagger.annotations.ApiResponses;
+
+@RestSchema(schemaId = "healthEndpoint")
+@RequestMapping(path = "/health")
+public class DefaultHealthCheckerPublisher implements HealthCheckerPublisher {
+
+  private final HealthCheckerManager manager;
+
+  public DefaultHealthCheckerPublisher(HealthCheckerManager manager) {
+    this.manager = manager;
+  }
+
+  @RequestMapping(path = "/", method = RequestMethod.GET)
+  @CrossOrigin
+  @Override
+  public Map<String, HealthCheckResult> health() {
+    return manager.check();
+  }
+
+  @ApiResponses({
+      @ApiResponse(code = 400, response = String.class, message = "illegal request content"),
+  })
+  @RequestMapping(path = "/{name}", method = RequestMethod.GET)
+  @CrossOrigin
+  @Override
+  public HealthCheckResult healthWithName(@PathVariable(name = "name") String name) {
+    return manager.check(name);
+  }
+}
diff --git a/metrics/metrics-core/src/main/java/io/servicecomb/metrics/core/publish/HealthCheckerManager.java b/metrics/metrics-core/src/main/java/io/servicecomb/metrics/core/publish/HealthCheckerManager.java
new file mode 100644
index 000000000..693a7fd7c
--- /dev/null
+++ b/metrics/metrics-core/src/main/java/io/servicecomb/metrics/core/publish/HealthCheckerManager.java
@@ -0,0 +1,31 @@
+/*
+ * 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 io.servicecomb.metrics.core.publish;
+
+import java.util.Map;
+
+import io.servicecomb.metrics.common.HealthCheckResult;
+import io.servicecomb.metrics.common.HealthChecker;
+
+public interface HealthCheckerManager {
+  void register(HealthChecker checker);
+
+  Map<String,HealthCheckResult> check();
+
+  HealthCheckResult check(String name);
+}
diff --git a/metrics/metrics-core/src/test/java/io/servicecomb/metrics/core/TestHealthCheckerManager.java b/metrics/metrics-core/src/test/java/io/servicecomb/metrics/core/TestHealthCheckerManager.java
new file mode 100644
index 000000000..80c16f57c
--- /dev/null
+++ b/metrics/metrics-core/src/test/java/io/servicecomb/metrics/core/TestHealthCheckerManager.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.
+ */
+
+package io.servicecomb.metrics.core;
+
+import java.io.IOException;
+import java.util.ArrayList;
+import java.util.List;
+import java.util.Map;
+
+import org.junit.Assert;
+import org.junit.Test;
+
+import com.google.common.collect.Lists;
+
+import io.servicecomb.foundation.common.utils.JsonUtils;
+import io.servicecomb.metrics.common.DefaultHealthCheckExtraData;
+import io.servicecomb.metrics.common.HealthCheckResult;
+import io.servicecomb.metrics.common.HealthChecker;
+import io.servicecomb.metrics.core.health.DefaultMicroserviceHealthChecker;
+import io.servicecomb.metrics.core.publish.DefaultHealthCheckerManager;
+import io.servicecomb.metrics.core.publish.HealthCheckerManager;
+import io.servicecomb.serviceregistry.RegistryUtils;
+import io.servicecomb.serviceregistry.api.registry.Microservice;
+import io.servicecomb.serviceregistry.api.registry.MicroserviceInstance;
+import mockit.Expectations;
+
+public class TestHealthCheckerManager {
+
+  @Test
+  public void testRegistry() throws IOException {
+
+    Microservice microservice = new Microservice();
+    microservice.setAppId("appId");
+    microservice.setServiceName("serviceName");
+    microservice.setVersion("0.0.1");
+
+    MicroserviceInstance microserviceInstance = new MicroserviceInstance();
+    microserviceInstance.setEndpoints(Lists.newArrayList("127.0.0.1", "192.168.0.100"));
+    microserviceInstance.setInstanceId("001");
+    microserviceInstance.setHostName("localhost");
+
+    new Expectations(RegistryUtils.class) {
+      {
+        RegistryUtils.getMicroservice();
+        result = microservice;
+      }
+    };
+
+    new Expectations(RegistryUtils.class) {
+      {
+        RegistryUtils.getMicroserviceInstance();
+        result = microserviceInstance;
+      }
+    };
+
+    List<HealthChecker> checkers = new ArrayList<>();
+    checkers.add(new DefaultMicroserviceHealthChecker());
+
+    HealthCheckerManager manager = new DefaultHealthCheckerManager(checkers);
+    Map<String, HealthCheckResult> results = manager.check();
+
+    Assert.assertTrue(results.get("default").isHealthy());
+
+    DefaultHealthCheckExtraData data = JsonUtils.OBJ_MAPPER
+        .readValue(results.get("default").getExtraData(), DefaultHealthCheckExtraData.class);
+    Assert.assertTrue(data.getAppId().equals("appId"));
+    Assert.assertTrue(data.getServiceName().equals("serviceName"));
+    Assert.assertTrue(data.getServiceVersion().equals("0.0.1"));
+    Assert.assertTrue(data.getInstanceId().equals("001"));
+    Assert.assertTrue(data.getHostName().equals("localhost"));
+    Assert.assertTrue(data.getEndpoints().equals("127.0.0.1,192.168.0.100"));
+  }
+}
diff --git a/metrics/metrics-core/src/test/java/io/servicecomb/metrics/core/TestHealthCheckerPublisher.java b/metrics/metrics-core/src/test/java/io/servicecomb/metrics/core/TestHealthCheckerPublisher.java
new file mode 100644
index 000000000..44978dfb6
--- /dev/null
+++ b/metrics/metrics-core/src/test/java/io/servicecomb/metrics/core/TestHealthCheckerPublisher.java
@@ -0,0 +1,59 @@
+/*
+ * 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 io.servicecomb.metrics.core;
+
+import static org.mockito.Mockito.mock;
+import static org.mockito.Mockito.when;
+
+import java.util.HashMap;
+import java.util.Map;
+
+import org.junit.Assert;
+import org.junit.Test;
+
+import com.fasterxml.jackson.core.JsonProcessingException;
+
+import io.servicecomb.foundation.common.utils.JsonUtils;
+import io.servicecomb.metrics.common.HealthCheckResult;
+import io.servicecomb.metrics.common.HealthCheckerPublisher;
+import io.servicecomb.metrics.core.publish.DefaultHealthCheckerPublisher;
+import io.servicecomb.metrics.core.publish.HealthCheckerManager;
+
+public class TestHealthCheckerPublisher {
+
+  @Test
+  public void testPublisher() throws JsonProcessingException {
+    HealthCheckerManager manager = mock(HealthCheckerManager.class);
+
+    Map<String, HealthCheckResult> results = new HashMap<>();
+    HealthCheckResult result = new HealthCheckResult(true, "ok", "extra");
+    results.put("default", result);
+
+    when(manager.check()).thenReturn(results);
+    when(manager.check("default")).thenReturn(result);
+
+    HealthCheckerPublisher publisher = new DefaultHealthCheckerPublisher(manager);
+    Map<String, HealthCheckResult> content = publisher.health();
+    Assert.assertTrue(JsonUtils.writeValueAsString(content.get("default"))
+        .equals(JsonUtils.writeValueAsString(result)));
+
+    HealthCheckResult checkResult = publisher.healthWithName("default");
+    Assert.assertTrue(JsonUtils.writeValueAsString(checkResult)
+        .equals(JsonUtils.writeValueAsString(result)));
+  }
+}
diff --git a/samples/metrics-extend-healthcheck/pom.xml b/samples/metrics-extend-healthcheck/pom.xml
new file mode 100644
index 000000000..c955c892c
--- /dev/null
+++ b/samples/metrics-extend-healthcheck/pom.xml
@@ -0,0 +1,53 @@
+<?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.
+  -->
+
+<project xmlns="http://maven.apache.org/POM/4.0.0"
+  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
+  xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
+  <parent>
+    <artifactId>samples</artifactId>
+    <groupId>io.servicecomb.samples</groupId>
+    <version>0.6.0-SNAPSHOT</version>
+  </parent>
+  <modelVersion>4.0.0</modelVersion>
+
+  <artifactId>metrics-extend-healthcheck</artifactId>
+
+  <dependencies>
+    <dependency>
+      <groupId>org.springframework.boot</groupId>
+      <artifactId>spring-boot-starter</artifactId>
+    </dependency>
+
+    <dependency>
+      <groupId>io.servicecomb</groupId>
+      <artifactId>spring-boot-starter-provider</artifactId>
+    </dependency>
+
+    <dependency>
+      <groupId>org.hibernate</groupId>
+      <artifactId>hibernate-validator</artifactId>
+    </dependency>
+
+    <dependency>
+      <groupId>io.servicecomb</groupId>
+      <artifactId>metrics-core</artifactId>
+    </dependency>
+  </dependencies>
+
+</project>
\ No newline at end of file
diff --git a/samples/metrics-extend-healthcheck/src/main/java/io/servicecomb/samples/metrics/extendhealthcheck/CustomHealthCheckerAnnotation.java b/samples/metrics-extend-healthcheck/src/main/java/io/servicecomb/samples/metrics/extendhealthcheck/CustomHealthCheckerAnnotation.java
new file mode 100644
index 000000000..ba6fb7a7f
--- /dev/null
+++ b/samples/metrics-extend-healthcheck/src/main/java/io/servicecomb/samples/metrics/extendhealthcheck/CustomHealthCheckerAnnotation.java
@@ -0,0 +1,37 @@
+/*
+ * 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 io.servicecomb.samples.metrics.extendhealthcheck;
+
+import org.springframework.stereotype.Component;
+
+import io.servicecomb.metrics.common.HealthCheckResult;
+import io.servicecomb.metrics.common.HealthChecker;
+
+//this health check will auto register because spring bean annotation
+@Component
+public class CustomHealthCheckerAnnotation implements HealthChecker {
+  @Override
+  public String getName() {
+    return "custom";
+  }
+
+  @Override
+  public HealthCheckResult check() {
+    return new HealthCheckResult(true, "custom", "no extra data");
+  }
+}
diff --git a/samples/metrics-extend-healthcheck/src/main/java/io/servicecomb/samples/metrics/extendhealthcheck/ExtendHealthCheckApplication.java b/samples/metrics-extend-healthcheck/src/main/java/io/servicecomb/samples/metrics/extendhealthcheck/ExtendHealthCheckApplication.java
new file mode 100644
index 000000000..45521b036
--- /dev/null
+++ b/samples/metrics-extend-healthcheck/src/main/java/io/servicecomb/samples/metrics/extendhealthcheck/ExtendHealthCheckApplication.java
@@ -0,0 +1,31 @@
+/*
+ * 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 io.servicecomb.samples.metrics.extendhealthcheck;
+
+import org.springframework.boot.SpringApplication;
+import org.springframework.boot.autoconfigure.SpringBootApplication;
+
+import io.servicecomb.springboot.starter.provider.EnableServiceComb;
+
+@SpringBootApplication
+@EnableServiceComb
+public class ExtendHealthCheckApplication {
+  public static void main(String[] args) {
+    SpringApplication.run(ExtendHealthCheckApplication.class, args);
+  }
+}
diff --git a/samples/metrics-extend-healthcheck/src/main/java/io/servicecomb/samples/metrics/extendhealthcheck/MySqlHealthChecker.java b/samples/metrics-extend-healthcheck/src/main/java/io/servicecomb/samples/metrics/extendhealthcheck/MySqlHealthChecker.java
new file mode 100644
index 000000000..620857dae
--- /dev/null
+++ b/samples/metrics-extend-healthcheck/src/main/java/io/servicecomb/samples/metrics/extendhealthcheck/MySqlHealthChecker.java
@@ -0,0 +1,54 @@
+/*
+ * 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 io.servicecomb.samples.metrics.extendhealthcheck;
+
+import java.sql.Connection;
+import java.sql.DriverManager;
+import java.sql.SQLException;
+
+import io.servicecomb.metrics.common.HealthCheckResult;
+import io.servicecomb.metrics.common.HealthChecker;
+
+//this is a demo health checker for mysql
+public class MySqlHealthChecker implements HealthChecker {
+  @Override
+  public String getName() {
+    return "mysql";
+  }
+
+  @Override
+  public HealthCheckResult check() {
+    //add your health check code here
+    Connection connection = null;
+    try {
+      connection = DriverManager.getConnection("jdbc:mysql://localhost:3306/test_db?useSSL=false", "root", "pwd");
+      return new HealthCheckResult(true, "local mysql health check", "");
+    } catch (SQLException e) {
+      e.printStackTrace();
+      return new HealthCheckResult(false, "local mysql health check", e.toString());
+    } finally {
+      if (connection != null) {
+        try {
+          connection.close();
+        } catch (SQLException e) {
+          e.printStackTrace();
+        }
+      }
+    }
+  }
+}
diff --git a/samples/metrics-extend-healthcheck/src/main/java/io/servicecomb/samples/metrics/extendhealthcheck/SomeServiceWithHealthCheckerManager.java b/samples/metrics-extend-healthcheck/src/main/java/io/servicecomb/samples/metrics/extendhealthcheck/SomeServiceWithHealthCheckerManager.java
new file mode 100644
index 000000000..43871b19f
--- /dev/null
+++ b/samples/metrics-extend-healthcheck/src/main/java/io/servicecomb/samples/metrics/extendhealthcheck/SomeServiceWithHealthCheckerManager.java
@@ -0,0 +1,36 @@
+/*
+ * 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 io.servicecomb.samples.metrics.extendhealthcheck;
+
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.stereotype.Service;
+
+import io.servicecomb.metrics.core.publish.HealthCheckerManager;
+
+@Service
+public class SomeServiceWithHealthCheckerManager {
+
+  private final HealthCheckerManager manager;
+
+  @Autowired
+  public SomeServiceWithHealthCheckerManager(HealthCheckerManager manager) {
+    this.manager = manager;
+
+    this.manager.register(new MySqlHealthChecker());
+  }
+}
diff --git a/samples/metrics-extend-healthcheck/src/main/resources/microservice.yaml b/samples/metrics-extend-healthcheck/src/main/resources/microservice.yaml
new file mode 100644
index 000000000..524e785d6
--- /dev/null
+++ b/samples/metrics-extend-healthcheck/src/main/resources/microservice.yaml
@@ -0,0 +1,30 @@
+#
+## ---------------------------------------------------------------------------
+## 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.
+## ---------------------------------------------------------------------------
+
+# all interconnected microservices must belong to an application wth the same ID
+APPLICATION_ID: metrics
+service_description:
+# name of the declaring microservice
+  name: metricsHealthCheckDemo
+  version: 0.0.1
+cse:
+  service:
+    registry:
+      address: http://127.0.0.1:30100
+  rest:
+    address: 0.0.0.0:7777
\ No newline at end of file
diff --git a/samples/metrics-write-file-sample/metrics-write-file-log4j2-springboot/pom.xml b/samples/metrics-write-file-sample/metrics-write-file-log4j2-springboot/pom.xml
index ee7978261..130b0d56e 100644
--- a/samples/metrics-write-file-sample/metrics-write-file-log4j2-springboot/pom.xml
+++ b/samples/metrics-write-file-sample/metrics-write-file-log4j2-springboot/pom.xml
@@ -70,14 +70,4 @@
     </dependency>
 
   </dependencies>
-
-  <build>
-    <plugins>
-      <plugin>
-        <groupId>org.springframework.boot</groupId>
-        <artifactId>spring-boot-maven-plugin</artifactId>
-      </plugin>
-    </plugins>
-  </build>
-
 </project>
\ No newline at end of file
diff --git a/samples/pom.xml b/samples/pom.xml
index 1d735dd03..4a78e7881 100644
--- a/samples/pom.xml
+++ b/samples/pom.xml
@@ -34,6 +34,7 @@
     <module>auth-sample</module>
     <module>bmi</module>
     <module>metrics-write-file-sample</module>
+    <module>metrics-extend-healthcheck</module>
     <module>config-apollo-sample</module>
   </modules>
 


 

----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on GitHub and use the
URL above to go to the specific comment.
 
For queries about this service, please contact Infrastructure at:
users@infra.apache.org


With regards,
Apache Git Services