[monitor-collector-scheduler] 监控探测接口,一次性临时任务调度编码
This commit is contained in:
@@ -2,6 +2,7 @@ package com.usthe.manager;
|
||||
|
||||
import org.springframework.boot.SpringApplication;
|
||||
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
||||
import org.springframework.cloud.openfeign.EnableFeignClients;
|
||||
|
||||
/**
|
||||
* @author tomsun28
|
||||
@@ -9,6 +10,7 @@ import org.springframework.boot.autoconfigure.SpringBootApplication;
|
||||
*/
|
||||
|
||||
@SpringBootApplication
|
||||
@EnableFeignClients(basePackages = {"com.usthe"})
|
||||
public class Manager {
|
||||
|
||||
public static void main(String[] args) {
|
||||
|
||||
@@ -7,7 +7,6 @@ import io.swagger.annotations.Api;
|
||||
import io.swagger.annotations.ApiOperation;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.http.ResponseEntity;
|
||||
import org.springframework.validation.annotation.Validated;
|
||||
import org.springframework.web.bind.annotation.DeleteMapping;
|
||||
import org.springframework.web.bind.annotation.GetMapping;
|
||||
import org.springframework.web.bind.annotation.PathVariable;
|
||||
@@ -17,6 +16,8 @@ import org.springframework.web.bind.annotation.RequestBody;
|
||||
import org.springframework.web.bind.annotation.RequestMapping;
|
||||
import org.springframework.web.bind.annotation.RestController;
|
||||
|
||||
import javax.validation.Valid;
|
||||
|
||||
import static com.usthe.common.util.CommonConstants.MONITOR_NOT_EXIST;
|
||||
import static org.springframework.http.MediaType.APPLICATION_JSON_VALUE;
|
||||
|
||||
@@ -28,7 +29,6 @@ import static org.springframework.http.MediaType.APPLICATION_JSON_VALUE;
|
||||
@Api(tags = "监控管理API")
|
||||
@RestController
|
||||
@RequestMapping(path = "/monitor", produces = {APPLICATION_JSON_VALUE})
|
||||
@Validated
|
||||
public class MonitorController {
|
||||
|
||||
@Autowired
|
||||
@@ -36,7 +36,7 @@ public class MonitorController {
|
||||
|
||||
@PostMapping
|
||||
@ApiOperation(value = "新增监控", notes = "新增一个监控应用")
|
||||
public ResponseEntity<Message<Void>> addNewMonitor(@Validated @RequestBody MonitorDto monitorDto) {
|
||||
public ResponseEntity<Message<Void>> addNewMonitor(@Valid @RequestBody MonitorDto monitorDto) {
|
||||
// 校验请求数据
|
||||
monitorService.validate(monitorDto, false);
|
||||
if (monitorDto.getDetected()) {
|
||||
@@ -49,7 +49,7 @@ public class MonitorController {
|
||||
|
||||
@PutMapping
|
||||
@ApiOperation(value = "修改监控", notes = "修改一个已存在监控应用")
|
||||
public ResponseEntity<Message<Void>> modifyMonitor(@Validated @RequestBody MonitorDto monitorDto) {
|
||||
public ResponseEntity<Message<Void>> modifyMonitor(@Valid @RequestBody MonitorDto monitorDto) {
|
||||
// 校验请求数据
|
||||
monitorService.validate(monitorDto, true);
|
||||
if (monitorDto.getDetected()) {
|
||||
@@ -84,7 +84,7 @@ public class MonitorController {
|
||||
|
||||
@PostMapping(path = "/detect")
|
||||
@ApiOperation(value = "探测监控", notes = "根据监控信息去对此监控进行可用性探测")
|
||||
public ResponseEntity<Message<Void>> detectMonitor(@Validated @RequestBody MonitorDto monitorDto) {
|
||||
public ResponseEntity<Message<Void>> detectMonitor(@Valid @RequestBody MonitorDto monitorDto) {
|
||||
monitorService.validate(monitorDto, false);
|
||||
monitorService.detectMonitor(monitorDto.getMonitor(), monitorDto.getParams());
|
||||
return ResponseEntity.ok(new Message<>("Detect success."));
|
||||
|
||||
@@ -5,7 +5,9 @@ import com.usthe.manager.pojo.entity.Param;
|
||||
import io.swagger.annotations.ApiModel;
|
||||
import io.swagger.annotations.ApiModelProperty;
|
||||
import lombok.Data;
|
||||
import org.springframework.validation.annotation.Validated;
|
||||
|
||||
import javax.validation.Valid;
|
||||
import javax.validation.constraints.NotNull;
|
||||
import java.util.List;
|
||||
|
||||
@@ -25,6 +27,7 @@ public class MonitorDto {
|
||||
*/
|
||||
@ApiModelProperty(value = "监控实体", accessMode = READ_WRITE, position = 0)
|
||||
@NotNull
|
||||
@Valid
|
||||
private Monitor monitor;
|
||||
|
||||
/**
|
||||
@@ -32,6 +35,7 @@ public class MonitorDto {
|
||||
*/
|
||||
@ApiModelProperty(value = "监控参数", accessMode = READ_WRITE, position = 1)
|
||||
@NotNull
|
||||
@Valid
|
||||
private List<Param> params;
|
||||
|
||||
/**
|
||||
|
||||
@@ -61,8 +61,11 @@ public class MonitorServiceImpl implements MonitorService {
|
||||
List<Configmap> configmaps = params.stream().map(param ->
|
||||
new Configmap(param.getField(), param.getValue(), param.getType())).collect(Collectors.toList());
|
||||
appDefine.setConfigmap(configmaps);
|
||||
CollectRep collectRep = jobScheduling.addSyncCollectJob(appDefine);
|
||||
List<CollectRep.MetricsData> collectRep = jobScheduling.addSyncCollectJob(appDefine);
|
||||
// 判断探测结果 失败则抛出探测异常
|
||||
if (collectRep == null || collectRep.isEmpty() || collectRep.get(0).getCode() != CollectRep.Code.SUCCESS) {
|
||||
throw new MonitorDetectException(collectRep.get(0).getMsg());
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
|
||||
@@ -9,12 +9,16 @@ import org.springframework.dao.DataAccessException;
|
||||
import org.springframework.http.HttpStatus;
|
||||
import org.springframework.http.ResponseEntity;
|
||||
import org.springframework.http.converter.HttpMessageNotReadableException;
|
||||
import org.springframework.validation.BindException;
|
||||
import org.springframework.validation.FieldError;
|
||||
import org.springframework.web.HttpRequestMethodNotSupportedException;
|
||||
import org.springframework.web.bind.MethodArgumentNotValidException;
|
||||
import org.springframework.web.bind.annotation.ExceptionHandler;
|
||||
import org.springframework.web.bind.annotation.ResponseBody;
|
||||
import org.springframework.web.bind.annotation.RestControllerAdvice;
|
||||
|
||||
import javax.validation.ConstraintViolationException;
|
||||
import javax.validation.ValidationException;
|
||||
import java.lang.reflect.Field;
|
||||
|
||||
import static com.usthe.common.util.CommonConstants.DETECT_FAILED;
|
||||
@@ -32,10 +36,14 @@ public class GlobalExceptionHandler {
|
||||
|
||||
private static Field detailMessage;
|
||||
|
||||
private static Field fieldErrorField;
|
||||
|
||||
static {
|
||||
try {
|
||||
detailMessage = Throwable.class.getDeclaredField("detailMessage");
|
||||
detailMessage.setAccessible(true);
|
||||
fieldErrorField = FieldError.class.getDeclaredField("field");
|
||||
fieldErrorField.setAccessible(true);
|
||||
} catch (Exception e) {}
|
||||
}
|
||||
|
||||
@@ -95,19 +103,32 @@ public class GlobalExceptionHandler {
|
||||
|
||||
/**
|
||||
* handler the exception thrown for data input verify
|
||||
* @param exception data input verify exception
|
||||
* valid注解校验框架校验异常统一处理
|
||||
* @param e data input verify exception
|
||||
* @return response
|
||||
*/
|
||||
@ExceptionHandler(MethodArgumentNotValidException.class)
|
||||
@ExceptionHandler({MethodArgumentNotValidException.class, BindException.class})
|
||||
@ResponseBody
|
||||
ResponseEntity<Message<Void>> handleInputValidException(MethodArgumentNotValidException exception) {
|
||||
ResponseEntity<Message<Void>> handleInputValidException(Exception e) {
|
||||
StringBuffer errorMessage = new StringBuffer();
|
||||
if (exception != null) {
|
||||
exception.getBindingResult().getAllErrors().forEach(error ->
|
||||
errorMessage.append(error.getDefaultMessage()).append("."));
|
||||
if (e instanceof MethodArgumentNotValidException) {
|
||||
MethodArgumentNotValidException exception = (MethodArgumentNotValidException)e;
|
||||
exception.getBindingResult().getAllErrors().forEach(error -> {
|
||||
try {
|
||||
String field = (String) fieldErrorField.get(error);
|
||||
errorMessage.append(field).append(":").append(error.getDefaultMessage()).append("||");
|
||||
} catch (Exception e1) {
|
||||
errorMessage.append(error.getDefaultMessage()).append("||");
|
||||
}
|
||||
});
|
||||
} else if (e instanceof BindException) {
|
||||
BindException exception = (BindException)e;
|
||||
exception.getAllErrors().forEach(error -> {
|
||||
errorMessage.append(error.getDefaultMessage()).append("||");
|
||||
});
|
||||
}
|
||||
if (log.isDebugEnabled()) {
|
||||
log.debug("[sample-tom]-[input argument not valid happen]-{}", errorMessage, exception);
|
||||
log.debug("[input argument not valid happen]-{}", errorMessage, e);
|
||||
}
|
||||
Message<Void> message = Message.<Void>builder().msg(errorMessage.toString()).build();
|
||||
return ResponseEntity.status(HttpStatus.BAD_REQUEST).body(message);
|
||||
@@ -125,7 +146,7 @@ public class GlobalExceptionHandler {
|
||||
if (exception != null) {
|
||||
errorMessage = exception.getMessage();
|
||||
}
|
||||
log.warn("[sample-tom]-[database error happen]-{}", errorMessage, exception);
|
||||
log.warn("[database error happen]-{}", errorMessage, exception);
|
||||
Message<Void> message = Message.<Void>builder().msg(errorMessage).build();
|
||||
return ResponseEntity.status(HttpStatus.CONFLICT).body(message);
|
||||
}
|
||||
@@ -137,13 +158,13 @@ public class GlobalExceptionHandler {
|
||||
*/
|
||||
@ExceptionHandler(HttpRequestMethodNotSupportedException.class)
|
||||
@ResponseBody
|
||||
ResponseEntity<Message> handleMethodNotSupportException(HttpRequestMethodNotSupportedException exception) {
|
||||
ResponseEntity<Message<Void>> handleMethodNotSupportException(HttpRequestMethodNotSupportedException exception) {
|
||||
String errorMessage = "Request method not supported";
|
||||
if (exception != null && exception.getMessage() != null) {
|
||||
errorMessage = exception.getMessage();
|
||||
}
|
||||
log.info("[monitor]-[Request method not supported]-{}", errorMessage);
|
||||
Message message = Message.builder().msg(errorMessage).build();
|
||||
Message<Void> message = Message.<Void>builder().msg(errorMessage).build();
|
||||
return ResponseEntity.status(HttpStatus.METHOD_NOT_ALLOWED).body(message);
|
||||
}
|
||||
|
||||
@@ -154,13 +175,13 @@ public class GlobalExceptionHandler {
|
||||
*/
|
||||
@ExceptionHandler(Exception.class)
|
||||
@ResponseBody
|
||||
ResponseEntity<Message> handleUnknownException(Exception exception) {
|
||||
ResponseEntity<Message<Void>> handleUnknownException(Exception exception) {
|
||||
String errorMessage = "unknown error happen";
|
||||
if (exception != null) {
|
||||
errorMessage = exception.getMessage();
|
||||
}
|
||||
log.error("[monitor]-[unknown 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);
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user