[monitor-collector-scheduler] 监控探测接口,一次性临时任务调度编码

This commit is contained in:
tomsun28
2021-11-16 12:58:46 +08:00
parent 4d27e0ed50
commit 649f68a90b
9 changed files with 122 additions and 19 deletions

View File

@@ -43,5 +43,10 @@
<artifactId>gson</artifactId> <artifactId>gson</artifactId>
<version>2.8.8</version> <version>2.8.8</version>
</dependency> </dependency>
<dependency>
<groupId>com.google.protobuf</groupId>
<artifactId>protobuf-java-util</artifactId>
<version>3.19.1</version>
</dependency>
</dependencies> </dependencies>
</project> </project>

View File

@@ -1,6 +1,8 @@
package com.usthe.common.entity.job; package com.usthe.common.entity.job;
import com.fasterxml.jackson.annotation.JsonIgnore;
import com.usthe.common.entity.message.CollectRep;
import lombok.AllArgsConstructor; import lombok.AllArgsConstructor;
import lombok.Builder; import lombok.Builder;
import lombok.Data; import lombok.Data;
@@ -67,6 +69,7 @@ public class Job {
/** /**
* collector使用 - 任务版本,此字段不存储于etcd * collector使用 - 任务版本,此字段不存储于etcd
*/ */
@JsonIgnore
private transient long version; private transient long version;
/** /**
* collector使用 - 指标组任务执行优先级视图 * collector使用 - 指标组任务执行优先级视图
@@ -78,10 +81,17 @@ public class Job {
* 126 - otherMetrics * 126 - otherMetrics
* 127 - lastPriorMetrics * 127 - lastPriorMetrics
*/ */
@JsonIgnore
private transient List<Set<Metrics>> priorMetrics; private transient List<Set<Metrics>> priorMetrics;
/** /**
* collector使用 - 构造初始化指标组 * collector使用 - 临时存储一次性任务指标组响应数据
*/
@JsonIgnore
private transient List<CollectRep.MetricsData> metricsDataTemps;
/**
* collector使用 - 构造初始化指标组执行视图
*/ */
public synchronized void constructPriorMetrics() { public synchronized void constructPriorMetrics() {
Map<Byte, List<Metrics>> map = metrics.stream() Map<Byte, List<Metrics>> map = metrics.stream()
@@ -155,4 +165,11 @@ public class Job {
return Collections.emptySet(); return Collections.emptySet();
} }
} }
public void addCollectMetricsData(CollectRep.MetricsData metricsData) {
if (metricsDataTemps == null) {
metricsDataTemps = new LinkedList<>();
}
metricsDataTemps.add(metricsData);
}
} }

View File

@@ -0,0 +1,45 @@
package com.usthe.common.util;
import com.google.protobuf.Message;
import com.google.protobuf.MessageOrBuilder;
import com.google.protobuf.util.JsonFormat;
import lombok.extern.slf4j.Slf4j;
/**
* protobuf json相互转换工具类
* @author tomsun28
* @date 2021/11/16 12:16
*/
@Slf4j
public class ProtoJsonUtil {
/**
* protobuf 转 json
* @param proto protobuf
* @return json
*/
public static String toJsonStr(Message proto) {
try {
return JsonFormat.printer().print(proto);
} catch (Exception e) {
log.error(e.getMessage(), e);
return null;
}
}
/**
* json转protobuf
* @param json json str
* @param builder proto instance builder
* @return protobuf
*/
public static Message toProtobuf(String json, Message.Builder builder) {
try {
JsonFormat.parser().merge(json, builder);
return builder.build();
} catch (Exception e) {
log.error(e.getMessage(), e);
return null;
}
}
}

View File

@@ -39,6 +39,12 @@
<artifactId>spring-boot-configuration-processor</artifactId> <artifactId>spring-boot-configuration-processor</artifactId>
<optional>true</optional> <optional>true</optional>
</dependency> </dependency>
<!-- feign -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-openfeign</artifactId>
<version>3.0.5</version>
</dependency>
<!-- data jdbc --> <!-- data jdbc -->
<dependency> <dependency>
<groupId>org.springframework.boot</groupId> <groupId>org.springframework.boot</groupId>

View File

@@ -2,6 +2,7 @@ package com.usthe.manager;
import org.springframework.boot.SpringApplication; import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.openfeign.EnableFeignClients;
/** /**
* @author tomsun28 * @author tomsun28
@@ -9,6 +10,7 @@ import org.springframework.boot.autoconfigure.SpringBootApplication;
*/ */
@SpringBootApplication @SpringBootApplication
@EnableFeignClients(basePackages = {"com.usthe"})
public class Manager { public class Manager {
public static void main(String[] args) { public static void main(String[] args) {

View File

@@ -7,7 +7,6 @@ import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation; import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity; import org.springframework.http.ResponseEntity;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.DeleteMapping; import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable; 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.RequestMapping;
import org.springframework.web.bind.annotation.RestController; import org.springframework.web.bind.annotation.RestController;
import javax.validation.Valid;
import static com.usthe.common.util.CommonConstants.MONITOR_NOT_EXIST; import static com.usthe.common.util.CommonConstants.MONITOR_NOT_EXIST;
import static org.springframework.http.MediaType.APPLICATION_JSON_VALUE; 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") @Api(tags = "监控管理API")
@RestController @RestController
@RequestMapping(path = "/monitor", produces = {APPLICATION_JSON_VALUE}) @RequestMapping(path = "/monitor", produces = {APPLICATION_JSON_VALUE})
@Validated
public class MonitorController { public class MonitorController {
@Autowired @Autowired
@@ -36,7 +36,7 @@ public class MonitorController {
@PostMapping @PostMapping
@ApiOperation(value = "新增监控", notes = "新增一个监控应用") @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); monitorService.validate(monitorDto, false);
if (monitorDto.getDetected()) { if (monitorDto.getDetected()) {
@@ -49,7 +49,7 @@ public class MonitorController {
@PutMapping @PutMapping
@ApiOperation(value = "修改监控", notes = "修改一个已存在监控应用") @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); monitorService.validate(monitorDto, true);
if (monitorDto.getDetected()) { if (monitorDto.getDetected()) {
@@ -84,7 +84,7 @@ public class MonitorController {
@PostMapping(path = "/detect") @PostMapping(path = "/detect")
@ApiOperation(value = "探测监控", notes = "根据监控信息去对此监控进行可用性探测") @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.validate(monitorDto, false);
monitorService.detectMonitor(monitorDto.getMonitor(), monitorDto.getParams()); monitorService.detectMonitor(monitorDto.getMonitor(), monitorDto.getParams());
return ResponseEntity.ok(new Message<>("Detect success.")); return ResponseEntity.ok(new Message<>("Detect success."));

View File

@@ -5,7 +5,9 @@ import com.usthe.manager.pojo.entity.Param;
import io.swagger.annotations.ApiModel; import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty; import io.swagger.annotations.ApiModelProperty;
import lombok.Data; import lombok.Data;
import org.springframework.validation.annotation.Validated;
import javax.validation.Valid;
import javax.validation.constraints.NotNull; import javax.validation.constraints.NotNull;
import java.util.List; import java.util.List;
@@ -25,6 +27,7 @@ public class MonitorDto {
*/ */
@ApiModelProperty(value = "监控实体", accessMode = READ_WRITE, position = 0) @ApiModelProperty(value = "监控实体", accessMode = READ_WRITE, position = 0)
@NotNull @NotNull
@Valid
private Monitor monitor; private Monitor monitor;
/** /**
@@ -32,6 +35,7 @@ public class MonitorDto {
*/ */
@ApiModelProperty(value = "监控参数", accessMode = READ_WRITE, position = 1) @ApiModelProperty(value = "监控参数", accessMode = READ_WRITE, position = 1)
@NotNull @NotNull
@Valid
private List<Param> params; private List<Param> params;
/** /**

View File

@@ -61,8 +61,11 @@ public class MonitorServiceImpl implements MonitorService {
List<Configmap> configmaps = params.stream().map(param -> List<Configmap> configmaps = params.stream().map(param ->
new Configmap(param.getField(), param.getValue(), param.getType())).collect(Collectors.toList()); new Configmap(param.getField(), param.getValue(), param.getType())).collect(Collectors.toList());
appDefine.setConfigmap(configmaps); 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 @Override

View File

@@ -9,12 +9,16 @@ import org.springframework.dao.DataAccessException;
import org.springframework.http.HttpStatus; import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity; import org.springframework.http.ResponseEntity;
import org.springframework.http.converter.HttpMessageNotReadableException; 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.HttpRequestMethodNotSupportedException;
import org.springframework.web.bind.MethodArgumentNotValidException; import org.springframework.web.bind.MethodArgumentNotValidException;
import org.springframework.web.bind.annotation.ExceptionHandler; import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody; import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestControllerAdvice; import org.springframework.web.bind.annotation.RestControllerAdvice;
import javax.validation.ConstraintViolationException;
import javax.validation.ValidationException;
import java.lang.reflect.Field; import java.lang.reflect.Field;
import static com.usthe.common.util.CommonConstants.DETECT_FAILED; import static com.usthe.common.util.CommonConstants.DETECT_FAILED;
@@ -32,10 +36,14 @@ public class GlobalExceptionHandler {
private static Field detailMessage; private static Field detailMessage;
private static Field fieldErrorField;
static { static {
try { try {
detailMessage = Throwable.class.getDeclaredField("detailMessage"); detailMessage = Throwable.class.getDeclaredField("detailMessage");
detailMessage.setAccessible(true); detailMessage.setAccessible(true);
fieldErrorField = FieldError.class.getDeclaredField("field");
fieldErrorField.setAccessible(true);
} catch (Exception e) {} } catch (Exception e) {}
} }
@@ -95,19 +103,32 @@ public class GlobalExceptionHandler {
/** /**
* handler the exception thrown for data input verify * handler the exception thrown for data input verify
* @param exception data input verify exception * valid注解校验框架校验异常统一处理
* @param e data input verify exception
* @return response * @return response
*/ */
@ExceptionHandler(MethodArgumentNotValidException.class) @ExceptionHandler({MethodArgumentNotValidException.class, BindException.class})
@ResponseBody @ResponseBody
ResponseEntity<Message<Void>> handleInputValidException(MethodArgumentNotValidException exception) { ResponseEntity<Message<Void>> handleInputValidException(Exception e) {
StringBuffer errorMessage = new StringBuffer(); StringBuffer errorMessage = new StringBuffer();
if (exception != null) { if (e instanceof MethodArgumentNotValidException) {
exception.getBindingResult().getAllErrors().forEach(error -> MethodArgumentNotValidException exception = (MethodArgumentNotValidException)e;
errorMessage.append(error.getDefaultMessage()).append(".")); 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()) { 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(); Message<Void> message = Message.<Void>builder().msg(errorMessage.toString()).build();
return ResponseEntity.status(HttpStatus.BAD_REQUEST).body(message); return ResponseEntity.status(HttpStatus.BAD_REQUEST).body(message);
@@ -125,7 +146,7 @@ public class GlobalExceptionHandler {
if (exception != null) { if (exception != null) {
errorMessage = exception.getMessage(); 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(); Message<Void> message = Message.<Void>builder().msg(errorMessage).build();
return ResponseEntity.status(HttpStatus.CONFLICT).body(message); return ResponseEntity.status(HttpStatus.CONFLICT).body(message);
} }
@@ -137,13 +158,13 @@ public class GlobalExceptionHandler {
*/ */
@ExceptionHandler(HttpRequestMethodNotSupportedException.class) @ExceptionHandler(HttpRequestMethodNotSupportedException.class)
@ResponseBody @ResponseBody
ResponseEntity<Message> handleMethodNotSupportException(HttpRequestMethodNotSupportedException exception) { ResponseEntity<Message<Void>> handleMethodNotSupportException(HttpRequestMethodNotSupportedException exception) {
String errorMessage = "Request method not supported"; String errorMessage = "Request method not supported";
if (exception != null && exception.getMessage() != null) { if (exception != null && exception.getMessage() != null) {
errorMessage = exception.getMessage(); errorMessage = exception.getMessage();
} }
log.info("[monitor]-[Request method not supported]-{}", errorMessage); 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); return ResponseEntity.status(HttpStatus.METHOD_NOT_ALLOWED).body(message);
} }
@@ -154,13 +175,13 @@ public class GlobalExceptionHandler {
*/ */
@ExceptionHandler(Exception.class) @ExceptionHandler(Exception.class)
@ResponseBody @ResponseBody
ResponseEntity<Message> handleUnknownException(Exception exception) { ResponseEntity<Message<Void>> handleUnknownException(Exception exception) {
String errorMessage = "unknown error happen"; String errorMessage = "unknown error happen";
if (exception != null) { if (exception != null) {
errorMessage = exception.getMessage(); errorMessage = exception.getMessage();
} }
log.error("[monitor]-[unknown error happen]-{}", errorMessage, exception); 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); return ResponseEntity.status(HttpStatus.CONFLICT).body(message);
} }