Browse Source

Update pom to support actuator

Yiming Liu 9 years ago
parent
commit
13241ea041

+ 32 - 0
apollo-adminservice/src/main/java/com/ctrip/apollo/adminservice/AdminServiceHealthIndicator.java

@@ -0,0 +1,32 @@
+package com.ctrip.apollo.adminservice;
+
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.boot.actuate.health.Health;
+import org.springframework.boot.actuate.health.HealthIndicator;
+import org.springframework.data.domain.PageRequest;
+import org.springframework.stereotype.Component;
+
+import com.ctrip.apollo.biz.service.AppService;
+
+@Component
+public class AdminServiceHealthIndicator implements HealthIndicator {
+
+  @Autowired
+  private AppService appService;
+
+  @Override
+  public Health health() {
+    int errorCode = check();
+    if (errorCode != 0) {
+      return Health.down().withDetail("Error Code", errorCode).build();
+    }
+    return Health.up().build();
+  }
+
+  private int check() {
+    PageRequest pageable = new PageRequest(0, 1);
+    appService.findAll(pageable);
+    return 0;
+  }
+
+}

+ 4 - 0
apollo-biz/pom.xml

@@ -16,6 +16,10 @@
 			<groupId>com.ctrip.apollo</groupId>
 			<artifactId>apollo-core</artifactId>
 		</dependency>
+		<dependency>
+			<groupId>org.springframework.boot</groupId>
+			<artifactId>spring-boot-starter-actuator</artifactId>
+		</dependency>
 		<dependency>
 			<groupId>org.springframework.boot</groupId>
 			<artifactId>spring-boot-starter-data-jpa</artifactId>

+ 6 - 1
apollo-biz/src/main/java/com/ctrip/apollo/biz/service/AdminService.java

@@ -1,6 +1,7 @@
 package com.ctrip.apollo.biz.service;
 
 import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.boot.actuate.metrics.CounterService;
 import org.springframework.stereotype.Service;
 
 import com.ctrip.apollo.biz.entity.App;
@@ -27,8 +28,12 @@ public class AdminService {
   @Autowired
   private ClusterRepository clusterRepository;
 
+  @Autowired
+  private CounterService counter;
+  
   public App createNewApp(String appId, String appName, String ownerName, String ownerEmail,
       String namespace) {
+    counter.increment("admin.createNewApp.start");
     App app = new App();
     app.setAppId(appId);
     app.setName(appName);
@@ -51,7 +56,7 @@ public class AdminService {
     ns.setClusterName(cluster.getName());
     ns.setNamespaceName(namespace);
     namespaceRepository.save(ns);
-
+    counter.increment("admin.createNewApp.success");
     return app;
   }
 }

+ 32 - 0
apollo-configservice/src/main/java/com/ctrip/apollo/configservice/ConfigServiceHealthIndicator.java

@@ -0,0 +1,32 @@
+package com.ctrip.apollo.configservice;
+
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.boot.actuate.health.Health;
+import org.springframework.boot.actuate.health.HealthIndicator;
+import org.springframework.data.domain.PageRequest;
+import org.springframework.stereotype.Component;
+
+import com.ctrip.apollo.biz.service.AppService;
+
+@Component
+public class ConfigServiceHealthIndicator implements HealthIndicator {
+
+  @Autowired
+  private AppService appService;
+
+  @Override
+  public Health health() {
+    int errorCode = check();
+    if (errorCode != 0) {
+      return Health.down().withDetail("Error Code", errorCode).build();
+    }
+    return Health.up().build();
+  }
+
+  private int check() {
+    PageRequest pageable = new PageRequest(0, 1);
+    appService.findAll(pageable);
+    return 0;
+  }
+
+}

+ 28 - 47
apollo-portal/pom.xml

@@ -1,50 +1,31 @@
 <?xml version="1.0"  encoding="UTF-8"?>
 <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/maven-v4_0_0.xsd">
-    <parent>
-        <groupId>com.ctrip.apollo</groupId>
-        <artifactId>apollo</artifactId>
-        <version>0.0.1-SNAPSHOT</version>
-        <relativePath>../pom.xml</relativePath>
-    </parent>
-    <modelVersion>4.0.0</modelVersion>
-    <artifactId>apollo-portal</artifactId>
-    <name>Apollo Portal</name>
-    <dependencies>
-        <dependency>
-            <groupId>com.ctrip.apollo</groupId>
-            <artifactId>apollo-core</artifactId>
-        </dependency>
-        <dependency>
-            <groupId>org.springframework.cloud</groupId>
-            <artifactId>spring-cloud-starter-eureka</artifactId>
-        </dependency>
-        <dependency>
-            <groupId>org.springframework.boot</groupId>
-            <artifactId>spring-boot-devtools</artifactId>
-            <optional>true</optional>
-        </dependency>
-        <dependency>
-            <groupId>org.springframework.boot</groupId>
-            <artifactId>spring-boot-starter-data-jpa</artifactId>
-        </dependency>
-        <dependency>
-            <groupId>com.h2database</groupId>
-            <artifactId>h2</artifactId>
-        </dependency>
-        <dependency>
-            <groupId>mysql</groupId>
-            <artifactId>mysql-connector-java</artifactId>
-            <scope>runtime</scope>
-        </dependency>
-
-    </dependencies>
-    <build>
-        <plugins>
-            <plugin>
-                <groupId>org.springframework.boot</groupId>
-                <artifactId>spring-boot-maven-plugin</artifactId>
-            </plugin>
-        </plugins>
-    </build>
+	xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
+	<parent>
+		<groupId>com.ctrip.apollo</groupId>
+		<artifactId>apollo</artifactId>
+		<version>0.0.1-SNAPSHOT</version>
+		<relativePath>../pom.xml</relativePath>
+	</parent>
+	<modelVersion>4.0.0</modelVersion>
+	<artifactId>apollo-portal</artifactId>
+	<name>Apollo Portal</name>
+	<dependencies>
+		<dependency>
+			<groupId>com.ctrip.apollo</groupId>
+			<artifactId>apollo-core</artifactId>
+		</dependency>
+		<dependency>
+			<groupId>org.springframework.boot</groupId>
+			<artifactId>spring-boot-starter-web</artifactId>
+		</dependency>
+	</dependencies>
+	<build>
+		<plugins>
+			<plugin>
+				<groupId>org.springframework.boot</groupId>
+				<artifactId>spring-boot-maven-plugin</artifactId>
+			</plugin>
+		</plugins>
+	</build>
 </project>