You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@servicecomb.apache.org by wu...@apache.org on 2018/01/10 07:38:26 UTC

[incubator-servicecomb-java-chassis] 02/13: SCB-166 add health check sample

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

wujimin pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/incubator-servicecomb-java-chassis.git

commit 900e87f76a9018d9baffad0a8936f0eccaeea05a
Author: zhengyangyong <ya...@huawei.com>
AuthorDate: Thu Jan 4 18:56:13 2018 +0800

    SCB-166 add health check sample
    
    Signed-off-by: zhengyangyong <ya...@huawei.com>
---
 .../health/DefaultMicroserviceHealthChecker.java   | 28 ++++++------
 .../publish/DefaultHealthCheckerPublisher.java     |  2 +-
 samples/metrics-extend-healthcheck/pom.xml         | 53 ++++++++++++++++++++++
 .../extendhealthcheck/CustomHealthChecker.java     | 34 ++++++++++++++
 .../ExtendHealthCheckApplication.java              | 31 +++++++++++++
 .../metrics/extendhealthcheck/SimpleService.java   | 48 ++++++++++++++++++++
 .../src/main/resources/microservice.yaml           | 30 ++++++++++++
 samples/pom.xml                                    |  1 +
 8 files changed, 213 insertions(+), 14 deletions(-)

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
index 1e96b11..6a0945e 100644
--- 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
@@ -30,13 +30,25 @@ import io.servicecomb.serviceregistry.api.registry.MicroserviceInstance;
 
 public class DefaultMicroserviceHealthChecker implements HealthChecker {
 
-  private final String extraData;
+  @Override
+  public String getName() {
+    return "default";
+  }
 
-  public DefaultMicroserviceHealthChecker() {
+  @Override
+  public HealthCheckResult check() {
+    return new HealthCheckResult(true, "", getExtraData());
+  }
+
+  private String getExtraData() {
     try {
+      if (RegistryUtils.getServiceRegistry() == null) {
+        RegistryUtils.init();
+      }
+
       Microservice microservice = RegistryUtils.getMicroservice();
       MicroserviceInstance instance = RegistryUtils.getMicroserviceInstance();
-      extraData = JsonUtils.writeValueAsString(new DefaultHealthCheckExtraData(
+      return JsonUtils.writeValueAsString(new DefaultHealthCheckExtraData(
           instance.getInstanceId(),
           instance.getHostName(),
           microservice.getAppId(),
@@ -47,14 +59,4 @@ public class DefaultMicroserviceHealthChecker implements HealthChecker {
       throw new ServiceCombException("unable load microservice info for healthchecker", e);
     }
   }
-
-  @Override
-  public String getName() {
-    return "default";
-  }
-
-  @Override
-  public HealthCheckResult check() {
-    return new HealthCheckResult(true, "", extraData);
-  }
 }
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
index 69eb6b1..814a9e2 100644
--- 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
@@ -30,7 +30,7 @@ import io.servicecomb.provider.rest.common.RestSchema;
 import io.swagger.annotations.ApiResponse;
 import io.swagger.annotations.ApiResponses;
 
-@RestSchema(schemaId = "metricsEndpoint")
+@RestSchema(schemaId = "healthEndpoint")
 @RequestMapping(path = "/health")
 public class DefaultHealthCheckerPublisher implements HealthCheckerPublisher {
 
diff --git a/samples/metrics-extend-healthcheck/pom.xml b/samples/metrics-extend-healthcheck/pom.xml
new file mode 100644
index 0000000..c955c89
--- /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/CustomHealthChecker.java b/samples/metrics-extend-healthcheck/src/main/java/io/servicecomb/samples/metrics/extendhealthcheck/CustomHealthChecker.java
new file mode 100644
index 0000000..1960de8
--- /dev/null
+++ b/samples/metrics-extend-healthcheck/src/main/java/io/servicecomb/samples/metrics/extendhealthcheck/CustomHealthChecker.java
@@ -0,0 +1,34 @@
+/*
+ * 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 io.servicecomb.metrics.common.HealthCheckResult;
+import io.servicecomb.metrics.common.HealthChecker;
+
+public class CustomHealthChecker implements HealthChecker {
+  @Override
+  public String getName() {
+    return "custom";
+  }
+
+  @Override
+  public HealthCheckResult check() {
+    //add your health check code here
+    return new HealthCheckResult(true, "demo health check", "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 0000000..45521b0
--- /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/SimpleService.java b/samples/metrics-extend-healthcheck/src/main/java/io/servicecomb/samples/metrics/extendhealthcheck/SimpleService.java
new file mode 100644
index 0000000..5e24cb2
--- /dev/null
+++ b/samples/metrics-extend-healthcheck/src/main/java/io/servicecomb/samples/metrics/extendhealthcheck/SimpleService.java
@@ -0,0 +1,48 @@
+/*
+ * 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.util.UUID;
+
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.web.bind.annotation.GetMapping;
+import org.springframework.web.bind.annotation.RequestMapping;
+
+import io.servicecomb.metrics.core.publish.HealthCheckerManager;
+import io.servicecomb.provider.rest.common.RestSchema;
+
+//simple service sim
+@RestSchema(schemaId = "demoServiceEndpoint")
+@RequestMapping(path = "/")
+public class SimpleService {
+
+  private final HealthCheckerManager manager;
+
+  @Autowired
+  public SimpleService(HealthCheckerManager manager) {
+    this.manager = manager;
+
+    //register your custom health check
+    this.manager.register(new CustomHealthChecker());
+  }
+
+  @GetMapping(path = "/f")
+  public String fun() {
+    return UUID.randomUUID().toString();
+  }
+}
\ No newline at end of file
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 0000000..524e785
--- /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/pom.xml b/samples/pom.xml
index 1d735dd..4a78e78 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>
 

-- 
To stop receiving notification emails like this one, please contact
"commits@servicecomb.apache.org" <co...@servicecomb.apache.org>.