Просмотр исходного кода

[monitor] 监控增删改查接口定义

tomsun28 4 лет назад
Родитель
Сommit
ed523344e4

+ 6 - 0
CodeStandard.md

@@ -0,0 +1,6 @@
+# Code Standard  
+
+- 0x00 : 成功
+- 0x01 : 参数校验失败
+- 0x02 : 探测失败
+- 0x03 : 监控不存在

+ 6 - 0
common/pom.xml

@@ -19,6 +19,12 @@
             <artifactId>spring-boot-starter-web</artifactId>
             <scope>provided</scope>
         </dependency>
+        <!-- swagger -->
+        <dependency>
+            <groupId>io.springfox</groupId>
+            <artifactId>springfox-boot-starter</artifactId>
+            <scope>provided</scope>
+        </dependency>
         <!-- etcd -->
         <dependency>
             <groupId>io.etcd</groupId>

+ 12 - 1
common/src/main/java/com/usthe/common/entity/dto/Message.java

@@ -1,10 +1,14 @@
 package com.usthe.common.entity.dto;
 
+import io.swagger.annotations.ApiModel;
+import io.swagger.annotations.ApiModelProperty;
 import lombok.AllArgsConstructor;
 import lombok.Builder;
 import lombok.Data;
 import lombok.NoArgsConstructor;
 
+import static com.usthe.common.util.CommonConstants.SUCCESS;
+
 /**
  * Unified message structure definition for front and back ends
  *
@@ -20,21 +24,28 @@ import lombok.NoArgsConstructor;
 @Builder
 @AllArgsConstructor
 @NoArgsConstructor
+@ApiModel(description = "公共消息包装")
 public class Message<T> {
 
     /**
      * message body data
      */
+    @ApiModelProperty(value = "响应数据", position = 0)
     private T data;
 
     /**
      * exception message when error happen or success message
      */
+    @ApiModelProperty(value = "携带消息", position = 1)
     private String msg;
 
     /**
      * response code, not http code
      */
-    private Integer code;
+    @ApiModelProperty(value = "携带编码", position = 2)
+    private byte code = SUCCESS;
 
+    public Message(String msg) {
+        this.msg = msg;
+    }
 }

+ 30 - 0
common/src/main/java/com/usthe/common/util/CommonConstants.java

@@ -0,0 +1,30 @@
+package com.usthe.common.util;
+
+/**
+ * 公共常量
+ * @author tomsun28
+ * @date 2021/11/14 12:06
+ */
+public interface CommonConstants {
+
+    /**
+     * 成功
+     */
+    byte SUCCESS = 0x00;
+
+    /**
+     * 参数校验失败
+     */
+    byte PARAM_INVALID = 0x01;
+
+    /**
+     * 探测失败
+     */
+    byte DETECT_FAILED = 0x02;
+
+    /**
+     * 监控不存在
+     */
+    byte MONITOR_NOT_EXIST = 0x03;
+
+}

+ 85 - 0
manager/src/main/java/com/usthe/manager/controller/MonitorController.java

@@ -0,0 +1,85 @@
+package com.usthe.manager.controller;
+
+import com.usthe.common.entity.dto.Message;
+import com.usthe.manager.pojo.dto.MonitorDto;
+import com.usthe.manager.service.MonitorService;
+import io.swagger.annotations.Api;
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.http.ResponseEntity;
+import org.springframework.web.bind.annotation.DeleteMapping;
+import org.springframework.web.bind.annotation.GetMapping;
+import org.springframework.web.bind.annotation.PathVariable;
+import org.springframework.web.bind.annotation.PostMapping;
+import org.springframework.web.bind.annotation.PutMapping;
+import org.springframework.web.bind.annotation.RequestBody;
+import org.springframework.web.bind.annotation.RequestMapping;
+import org.springframework.web.bind.annotation.RestController;
+
+import static com.usthe.common.util.CommonConstants.MONITOR_NOT_EXIST;
+import static org.springframework.http.MediaType.APPLICATION_JSON_VALUE;
+
+/**
+ * 监控管理API
+ * @author tomsun28
+ * @date 2021/11/14 10:57
+ */
+@Api(tags = "监控管理接口")
+@RestController
+@RequestMapping(path = "/monitor", consumes = {APPLICATION_JSON_VALUE}, produces = {APPLICATION_JSON_VALUE})
+public class MonitorController {
+
+    @Autowired
+    private MonitorService monitorService;
+
+    @PostMapping
+    public ResponseEntity<Message<Void>> addNewMonitor(@RequestBody MonitorDto monitorDto) {
+        // 校验请求数据
+        monitorService.validate(monitorDto, false);
+        if (monitorDto.isDetected()) {
+            // 进行探测
+            monitorService.detectMonitor(monitorDto.getMonitor(), monitorDto.getParams());
+        }
+        monitorService.addMonitor(monitorDto.getMonitor(), monitorDto.getParams());
+        return ResponseEntity.ok().build();
+    }
+
+    @PutMapping
+    public ResponseEntity<Message<Void>> modifyMonitor(@RequestBody MonitorDto monitorDto) {
+        // 校验请求数据
+        monitorService.validate(monitorDto, true);
+        if (monitorDto.isDetected()) {
+            // 进行探测
+            monitorService.detectMonitor(monitorDto.getMonitor(), monitorDto.getParams());
+        }
+        monitorService.modifyMonitor(monitorDto.getMonitor(), monitorDto.getParams());
+        return ResponseEntity.ok(new Message<>("Modify success"));
+    }
+
+    @GetMapping(path = "/{id}")
+    public ResponseEntity<Message<MonitorDto>> getMonitor(@PathVariable("id") long id) {
+        // 获取监控信息
+        MonitorDto monitorDto = monitorService.getMonitor(id);
+        Message.MessageBuilder<MonitorDto> messageBuilder = Message.builder();
+        if (monitorDto == null) {
+            messageBuilder.code(MONITOR_NOT_EXIST).msg("Monitor not exist.");
+        } else {
+            messageBuilder.data(monitorDto);
+        }
+        return ResponseEntity.ok(messageBuilder.build());
+    }
+
+    @DeleteMapping(path = "/{id}")
+    public ResponseEntity<Message<Void>> deleteMonitor(@PathVariable("id") long id) {
+        // 删除监控,监控不存在或删除成功都返回成功
+        monitorService.deleteMonitor(id);
+        return ResponseEntity.ok(new Message<>("Delete success"));
+    }
+
+    @PostMapping(path = "/detect")
+    public ResponseEntity<Message<Void>> detectMonitor(@RequestBody MonitorDto monitorDto) {
+        monitorService.validate(monitorDto, false);
+        monitorService.detectMonitor(monitorDto.getMonitor(), monitorDto.getParams());
+        return ResponseEntity.ok(new Message<>("Detect success."));
+    }
+
+}

+ 14 - 0
manager/src/main/java/com/usthe/manager/dao/MonitorDao.java

@@ -0,0 +1,14 @@
+package com.usthe.manager.dao;
+
+import org.springframework.data.jpa.repository.JpaRepository;
+import sun.security.util.AuthResources;
+
+/**
+ * AuthResources 数据库操作
+ * @author tomsun28
+ * @date 2021/11/14 11:24
+ */
+public interface MonitorDao extends JpaRepository<AuthResources, Long> {
+
+
+}

+ 11 - 0
manager/src/main/java/com/usthe/manager/dao/ParamDao.java

@@ -0,0 +1,11 @@
+package com.usthe.manager.dao;
+
+import org.springframework.data.jpa.repository.JpaRepository;
+
+/**
+ * ParamDao 数据库操作
+ * @author tomsun28
+ * @date 2021/11/14 11:26
+ */
+public interface ParamDao extends JpaRepository<ParamDao, Long> {
+}

+ 13 - 0
manager/src/main/java/com/usthe/manager/dao/ParamDefineDao.java

@@ -0,0 +1,13 @@
+package com.usthe.manager.dao;
+
+import com.usthe.manager.pojo.entity.ParamDefine;
+import org.springframework.data.jpa.repository.JpaRepository;
+
+/**
+ * ParamDefine数据库操作
+ * @author tomsun28
+ * @date 2021/11/14 11:27
+ */
+public interface ParamDefineDao extends JpaRepository<ParamDefine, Long> {
+
+}

+ 39 - 0
manager/src/main/java/com/usthe/manager/pojo/dto/MonitorDto.java

@@ -0,0 +1,39 @@
+package com.usthe.manager.pojo.dto;
+
+import com.usthe.manager.pojo.entity.Monitor;
+import com.usthe.manager.pojo.entity.Param;
+import io.swagger.annotations.ApiModel;
+import io.swagger.annotations.ApiModelProperty;
+import lombok.Data;
+
+import java.util.List;
+
+import static io.swagger.annotations.ApiModelProperty.AccessMode.READ_WRITE;
+
+/**
+ * 监控信息对外交互实体
+ * @author tomsun28
+ * @date 2021/11/14 10:13
+ */
+@Data
+@ApiModel(description = "监控信息实体")
+public class MonitorDto {
+
+    /**
+     * 监控实体
+     */
+    @ApiModelProperty(value = "监控实体", accessMode = READ_WRITE, position = 0)
+    private Monitor monitor;
+
+    /**
+     * 参数
+     */
+    @ApiModelProperty(value = "监控参数", accessMode = READ_WRITE, position = 1)
+    private List<Param> params;
+
+    /**
+     * 是否探测
+     */
+    @ApiModelProperty(value = "是否进行探测", accessMode = READ_WRITE, position = 2)
+    private boolean detected;
+}

+ 105 - 0
manager/src/main/java/com/usthe/manager/pojo/entity/Monitor.java

@@ -0,0 +1,105 @@
+package com.usthe.manager.pojo.entity;
+
+import io.swagger.annotations.ApiModel;
+import io.swagger.annotations.ApiModelProperty;
+import lombok.AllArgsConstructor;
+import lombok.Builder;
+import lombok.Data;
+import lombok.NoArgsConstructor;
+
+import javax.persistence.Entity;
+import javax.persistence.GeneratedValue;
+import javax.persistence.GenerationType;
+import javax.persistence.Id;
+import javax.persistence.Table;
+import java.time.LocalDateTime;
+
+import static io.swagger.annotations.ApiModelProperty.AccessMode.READ_ONLY;
+import static io.swagger.annotations.ApiModelProperty.AccessMode.READ_WRITE;
+
+/**
+ * 监控实体
+ * @author tomsun28
+ * @date 2021/11/14 9:53
+ */
+@Entity
+@Table(name = "monitor")
+@Data
+@Builder
+@AllArgsConstructor
+@NoArgsConstructor
+@ApiModel(description = "监控实体")
+public class Monitor {
+
+    @Id
+    @GeneratedValue(strategy = GenerationType.IDENTITY)
+    @ApiModelProperty(value = "监控ID", example = "87584674384", accessMode = READ_ONLY, position = 0)
+    private Long id;
+
+    /**
+     * 监控对应下发的任务ID
+     */
+    @ApiModelProperty(value = "任务ID", example = "43243543543", accessMode = READ_ONLY, position = 1)
+    private Long jobId;
+
+    /**
+     * 监控的名称
+     */
+    @ApiModelProperty(value = "监控名称", example = "Api-bing.com", accessMode = READ_WRITE, position = 2)
+    private String name;
+
+    /**
+     * 监控的类型:linux,mysql,jvm...
+     */
+    @ApiModelProperty(value = "监控类型", example = "api", accessMode = READ_WRITE, position = 3)
+    private String app;
+
+    /**
+     * 监控的对端host:ipv4,ipv6,域名
+     */
+    @ApiModelProperty(value = "监控的对端host", example = "192.167.25.11", accessMode = READ_WRITE, position = 4)
+    private String host;
+
+    /**
+     * 监控的采集间隔时间,单位秒
+     */
+    @ApiModelProperty(value = "监控的采集间隔时间,单位秒", example = "600", accessMode = READ_WRITE, position = 5)
+    private Integer intervals;
+
+    /**
+     * 监控状态 0:未监控,1:可用,2:不可用,3:不可达,4:挂起
+     */
+    @ApiModelProperty(value = "监控状态 0:未监控,1:可用,2:不可用,3:不可达,4:挂起", example = "1", accessMode = READ_WRITE, position = 6)
+    private byte status;
+
+    /**
+     * 监控备注描述
+     */
+    @ApiModelProperty(value = "监控备注描述", example = "对搜索网站bing的可用性监控", accessMode = READ_WRITE, position = 7)
+    private String description;
+
+    /**
+     * 此条记录创建者
+     */
+    @ApiModelProperty(value = "此条记录创建者", example = "tom", accessMode = READ_ONLY, position = 8)
+    private String creator;
+
+    /**
+     * 此条记录最新修改者
+     */
+    @ApiModelProperty(value = "此条记录最新修改者", example = "tom", accessMode = READ_ONLY, position = 9)
+    private String modifier;
+
+    /**
+     * 记录创建时间
+     */
+    @ApiModelProperty(value = "记录创建时间(毫秒时间戳)", example = "1612198922000", accessMode = READ_ONLY, position = 10)
+    private LocalDateTime gmtCreate;
+
+    /**
+     * 记录最新修改时间
+     */
+    @ApiModelProperty(value = "记录最新修改时间(毫秒时间戳)", example = "1612198444000", accessMode = READ_ONLY, position = 11)
+    private LocalDateTime gmtUpdate;
+
+}

+ 64 - 0
manager/src/main/java/com/usthe/manager/service/MonitorService.java

@@ -0,0 +1,64 @@
+package com.usthe.manager.service;
+
+import com.usthe.manager.pojo.dto.MonitorDto;
+import com.usthe.manager.pojo.entity.Monitor;
+import com.usthe.manager.pojo.entity.Param;
+import com.usthe.manager.support.exception.MonitorDetectException;
+
+import java.util.List;
+
+/**
+ * 监控管理服务
+ * @author tomsun28
+ * @date 2021/11/14 11:28
+ */
+public interface MonitorService {
+
+
+    /**
+     * 监控可用性探测
+     * @param monitor 监控实体信息
+     * @param params 参数信息
+     * @throws MonitorDetectException 探测失败抛出
+     */
+    void detectMonitor(Monitor monitor, List<Param> params) throws MonitorDetectException;
+
+    /**
+     * 新增监控
+     * @param monitor 监控实体
+     * @param params 参数信息
+     * @throws RuntimeException 新增过程异常抛出
+     */
+    void addMonitor(Monitor monitor, List<Param> params) throws RuntimeException;
+
+    /**
+     * 校验请求数据参数正确性
+     * @param monitorDto monitorDto
+     * @param isModify 是否是修改监控
+     * @throws IllegalArgumentException 校验参数错误抛出
+     */
+    void validate(MonitorDto monitorDto, boolean isModify) throws IllegalArgumentException;
+
+    /**
+     * 修改更新监控
+     * @param monitor 监控实体
+     * @param params 参数信息
+     * @throws RuntimeException 修改过程中异常抛出
+     */
+    void modifyMonitor(Monitor monitor, List<Param> params) throws RuntimeException;
+
+    /**
+     * 删除监控
+     * @param id 监控ID
+     * @throws RuntimeException 删除过程中异常抛出
+     */
+    void deleteMonitor(long id) throws RuntimeException;
+
+    /**
+     * 获取监控信息
+     * @param id 监控ID
+     * @return MonitorDto
+     * @throws RuntimeException 查询过程中异常抛出
+     */
+    MonitorDto getMonitor(long id) throws RuntimeException;
+}

+ 50 - 0
manager/src/main/java/com/usthe/manager/service/impl/MonitorServiceImpl.java

@@ -0,0 +1,50 @@
+package com.usthe.manager.service.impl;
+
+import com.usthe.manager.pojo.dto.MonitorDto;
+import com.usthe.manager.pojo.entity.Monitor;
+import com.usthe.manager.pojo.entity.Param;
+import com.usthe.manager.service.MonitorService;
+import com.usthe.manager.support.exception.MonitorDetectException;
+import org.springframework.stereotype.Service;
+
+import java.util.List;
+
+/**
+ * 监控管理服务实现
+ * @author tomsun28
+ * @date 2021/11/14 13:06
+ */
+@Service
+public class MonitorServiceImpl implements MonitorService {
+
+
+    @Override
+    public void detectMonitor(Monitor monitor, List<Param> params) throws MonitorDetectException {
+
+    }
+
+    @Override
+    public void addMonitor(Monitor monitor, List<Param> params) throws RuntimeException {
+
+    }
+
+    @Override
+    public void validate(MonitorDto monitorDto, boolean isModify) throws IllegalArgumentException {
+
+    }
+
+    @Override
+    public void modifyMonitor(Monitor monitor, List<Param> params) throws RuntimeException {
+
+    }
+
+    @Override
+    public void deleteMonitor(long id) throws RuntimeException {
+
+    }
+
+    @Override
+    public MonitorDto getMonitor(long id) throws RuntimeException {
+        return null;
+    }
+}

+ 19 - 4
manager/src/main/java/com/usthe/manager/support/GlobalExceptionHandler.java

@@ -2,6 +2,7 @@ package com.usthe.manager.support;
 
 
 import com.usthe.common.entity.dto.Message;
+import com.usthe.manager.support.exception.MonitorDetectException;
 import lombok.extern.slf4j.Slf4j;
 import org.springframework.dao.DataAccessException;
 import org.springframework.http.HttpStatus;
@@ -12,6 +13,8 @@ import org.springframework.web.bind.annotation.ExceptionHandler;
 import org.springframework.web.bind.annotation.ResponseBody;
 import org.springframework.web.bind.annotation.RestControllerAdvice;
 
+import static com.usthe.common.util.CommonConstants.DETECT_FAILED;
+
 /**
  * controller exception handler
  * @author tomsun28
@@ -22,13 +25,25 @@ import org.springframework.web.bind.annotation.RestControllerAdvice;
 public class GlobalExceptionHandler {
 
     /**
+     * 处理探测失败
+     * @param exception 探测异常
+     * @return response
+     */
+    @ExceptionHandler(MonitorDetectException.class)
+    @ResponseBody
+    ResponseEntity<Message<Void>> handleMonitorDetectException(MonitorDetectException exception) {
+        Message<Void> message = Message.<Void>builder().msg(exception.getMessage()).code(DETECT_FAILED).build();
+        return ResponseEntity.status(HttpStatus.BAD_REQUEST).body(message);
+    }
+
+    /**
      * handler the exception thrown for data input verify
      * @param exception data input verify exception
      * @return response
      */
     @ExceptionHandler(MethodArgumentNotValidException.class)
     @ResponseBody
-    ResponseEntity<Message> handleInputValidException(MethodArgumentNotValidException exception) {
+    ResponseEntity<Message<Void>> handleInputValidException(MethodArgumentNotValidException exception) {
         StringBuffer errorMessage = new StringBuffer();
         if (exception != null) {
             exception.getBindingResult().getAllErrors().forEach(error ->
@@ -37,7 +52,7 @@ public class GlobalExceptionHandler {
         if (log.isDebugEnabled()) {
             log.debug("[sample-tom]-[input argument not valid happen]-{}", errorMessage, exception);
         }
-        Message message = Message.builder().msg(errorMessage.toString()).build();
+        Message<Void> message = Message.<Void>builder().msg(errorMessage.toString()).build();
         return ResponseEntity.status(HttpStatus.BAD_REQUEST).body(message);
     }
 
@@ -48,13 +63,13 @@ public class GlobalExceptionHandler {
      */
     @ExceptionHandler(DataAccessException.class)
     @ResponseBody
-    ResponseEntity<Message> handleDataAccessException(DataAccessException exception) {
+    ResponseEntity<Message<Void>> handleDataAccessException(DataAccessException exception) {
         String errorMessage = "database error happen";
         if (exception != null) {
             errorMessage = exception.getMessage();
         }
         log.warn("[sample-tom]-[database error happen]-{}", errorMessage, exception);
-        Message message = Message.builder().msg(errorMessage).build();
+        Message<Void> message = Message.<Void>builder().msg(errorMessage).build();
         return ResponseEntity.status(HttpStatus.CONFLICT).body(message);
     }
 

+ 13 - 0
manager/src/main/java/com/usthe/manager/support/exception/MonitorDetectException.java

@@ -0,0 +1,13 @@
+package com.usthe.manager.support.exception;
+
+/**
+ * 探测失败异常
+ * @author tomsun28
+ * @date 2021/11/14 12:19
+ */
+public class MonitorDetectException extends RuntimeException {
+
+    public MonitorDetectException(String message) {
+        super(message);
+    }
+}

+ 1 - 1
manager/src/main/resources/db/schema.sql

@@ -7,7 +7,7 @@ DROP TABLE IF EXISTS  monitor ;
 CREATE TABLE  monitor
 (
      id           bigint       not null auto_increment comment '监控ID',
-     jobId        bigint       not null comment '监控对应下发的任务ID',
+     job_id       bigint       not null comment '监控对应下发的任务ID',
      name         varchar(100) not null comment '监控的名称',
      app          varchar(100) not null comment '监控的类型:linux,mysql,jvm...',
      host         varchar(100) not null comment '监控的对端host:ipv4,ipv6,域名',