Browse Source

feat: hbase health indicator

许家凯 3 years ago
parent
commit
5304df62c1

+ 7 - 0
pom.xml

@@ -33,6 +33,13 @@
         </dependency>
 
         <dependency>
+            <groupId>org.springframework.boot</groupId>
+            <artifactId>spring-boot-starter-actuator</artifactId>
+            <version>2.7.0</version>
+        </dependency>
+
+
+        <dependency>
             <groupId>org.projectlombok</groupId>
             <artifactId>lombok</artifactId>
             <optional>true</optional>

+ 0 - 29
src/main/java/com/winhc/fast/query/FastQuery4SpiderApplication.java

@@ -4,40 +4,11 @@ import org.springframework.boot.SpringApplication;
 import org.springframework.boot.autoconfigure.SpringBootApplication;
 import org.springframework.web.reactive.config.EnableWebFlux;
 
-import java.io.File;
-import java.util.ArrayList;
-import java.util.List;
-
 @SpringBootApplication
 @EnableWebFlux
 public class FastQuery4SpiderApplication {
 
     public static void main(String[] args) {
-        getList("/app/libs");
         SpringApplication.run(FastQuery4SpiderApplication.class, args);
     }
-
-    public static void getList(String path) {
-        List<String> list = new ArrayList<>();
-        File file = new File(path);
-        if (!file.exists()) {
-            System.out.println(path + "不存在.");
-            return;
-        }
-        File[] tempList = file.listFiles();
-        System.out.println("该目录下对象个数:" + tempList.length);
-        for (int i = 0; i < tempList.length; i++) {
-            if (tempList[i].isFile()) {
-                System.out.println("文件:" + tempList[i]);
-            }
-            if (tempList[i].isDirectory()) {
-                list.add(tempList[i].getPath());
-                System.out.println("文件夹:" + tempList[i].getPath());
-                //递归:
-                getList(tempList[i].getPath());
-            }
-        }
-    }
-
-
 }

+ 16 - 0
src/main/java/com/winhc/fast/query/configuration/GlobalExceptionHandler.java

@@ -1,6 +1,8 @@
 package com.winhc.fast.query.configuration;
 
+import com.alibaba.lindorm.client.exception.NotServingRegionException;
 import com.winhc.fast.query.vo.ResponseVo;
+import lombok.AllArgsConstructor;
 import lombok.extern.slf4j.Slf4j;
 import org.springframework.http.HttpStatus;
 import org.springframework.web.bind.annotation.ExceptionHandler;
@@ -13,11 +15,25 @@ import org.springframework.web.bind.annotation.RestControllerAdvice;
  */
 @Slf4j
 @RestControllerAdvice
+@AllArgsConstructor
 public class GlobalExceptionHandler {
+    private HbaseHealthIndicator hbaseHealthIndicator;
+
     @ResponseStatus(HttpStatus.OK)
     @ExceptionHandler(value = Exception.class)
     public ResponseVo handleBadRequest(Exception e) {
         log.error(e.getMessage(), e);
         return ResponseVo.failure(e.getMessage());
     }
+
+    @ResponseStatus(HttpStatus.OK)
+    @ExceptionHandler(value = NotServingRegionException.class)
+    public ResponseVo handleBadRequest(NotServingRegionException e) {
+        log.error("发现hbase异常!");
+        log.error(e.getMessage(), e);
+        hbaseHealthIndicator.putErrorInfo(e);
+        return ResponseVo.failure(e.getMessage());
+    }
+
+
 }

+ 37 - 0
src/main/java/com/winhc/fast/query/configuration/HbaseHealthIndicator.java

@@ -0,0 +1,37 @@
+package com.winhc.fast.query.configuration;
+
+import org.springframework.boot.actuate.health.Health;
+import org.springframework.boot.actuate.health.HealthIndicator;
+import org.springframework.stereotype.Component;
+
+import java.util.HashMap;
+import java.util.Map;
+
+/**
+ * @author: XuJiakai
+ * 2022/6/7 10:08
+ */
+@Component
+public class HbaseHealthIndicator implements HealthIndicator {
+
+    private Map<String,Exception> errorMap = new HashMap<>();
+
+    @Override
+    public Health health() {
+        if(errorMap.isEmpty()){
+            return  Health.up()
+                    .withDetail("message", "暂未发现异常!")
+                    .build();
+        }else{
+            return Health.down()
+                    .withDetail("error-info", errorMap)
+                    .withDetail("message", "hbase服务异常!")
+                    .build();
+        }
+    }
+
+    public void putErrorInfo(Exception e){
+        String name = e.getClass().getName();
+        errorMap.put(name, e);
+    }
+}

+ 1 - 1
src/main/java/com/winhc/fast/query/controller/HbaseQueryController.java

@@ -55,7 +55,7 @@ public class HbaseQueryController {
 
     @ApiOperation(value = "批量查询hbase,可指定字段")
     @PostMapping("bulk-get")
-    public Object fastGetHbase(@RequestParam String tableName, @RequestBody HbaseQueryParamVo hbaseQueryParamVo) {
+    public Mono<ResponseVo> fastGetHbase(@RequestParam String tableName, @RequestBody HbaseQueryParamVo hbaseQueryParamVo) {
         long start = System.currentTimeMillis();
         if (hbaseQueryParamVo.getQueryKey() == null) {
             hbaseQueryParamVo.setQueryKey(new String[]{});

+ 28 - 0
src/main/java/com/winhc/fast/query/controller/TestController.java

@@ -0,0 +1,28 @@
+package com.winhc.fast.query.controller;
+
+import com.winhc.fast.query.configuration.HbaseHealthIndicator;
+import com.winhc.fast.query.vo.ResponseVo;
+import lombok.AllArgsConstructor;
+import lombok.extern.slf4j.Slf4j;
+import org.springframework.web.bind.annotation.GetMapping;
+import org.springframework.web.bind.annotation.RequestMapping;
+import org.springframework.web.bind.annotation.RestController;
+import reactor.core.publisher.Mono;
+
+/**
+ * @author: XuJiakai
+ * 2022/6/7 10:20
+ */
+@Slf4j
+@AllArgsConstructor
+@RestController
+@RequestMapping("test")
+public class TestController {
+    HbaseHealthIndicator hbaseHealthIndicator;
+
+    @GetMapping("set-status")
+    public Mono<ResponseVo> setStatus() {
+        hbaseHealthIndicator.putErrorInfo(new RuntimeException("aaa"));
+        return Mono.just(ResponseVo.success("ok"));
+    }
+}

+ 9 - 1
src/main/java/com/winhc/fast/query/vo/ResponseVo.java

@@ -25,6 +25,14 @@ public class ResponseVo<T> {
         return responseVo;
     }
 
+    public static <T> ResponseVo success(T data) {
+        ResponseVo<T> responseVo = new ResponseVo<T>();
+        responseVo.success = true;
+        responseVo.msg = "success";
+        responseVo.data = data;
+        return responseVo;
+    }
+
     public static <T> ResponseVo failure(long startTime, String errorMsg) {
         long endTime = System.currentTimeMillis();
         ResponseVo<T> responseVo = new ResponseVo<T>();
@@ -34,7 +42,7 @@ public class ResponseVo<T> {
         return responseVo;
     }
 
-    public static <T> ResponseVo failure( String errorMsg) {
+    public static <T> ResponseVo failure(String errorMsg) {
         ResponseVo<T> responseVo = new ResponseVo<T>();
         responseVo.success = false;
         responseVo.msg = errorMsg;

+ 7 - 0
src/main/resources/application.yml

@@ -13,3 +13,10 @@ odps:
   access-key-secret: r6gWoySXC8kSK4qnfKRxEuWJ5uHIiE
   region-id: cn-shanghai
   ding-secret: SECe7b26876f443e77f872b8b10880e39b3c5dfaf44855f1aa3235372bb73698ab6
+management:
+  endpoints:
+    web:
+      base-path: /manage
+  endpoint:
+    health:
+      show-details: always