[monitor] 设计应用监控的采集定义结构yml,参数定义结构yml
This commit is contained in:
@@ -0,0 +1,95 @@
|
||||
package com.usthe.manager.support;
|
||||
|
||||
|
||||
import com.usthe.common.entity.dto.Message;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.dao.DataAccessException;
|
||||
import org.springframework.http.HttpStatus;
|
||||
import org.springframework.http.ResponseEntity;
|
||||
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;
|
||||
|
||||
/**
|
||||
* controller exception handler
|
||||
* @author tomsun28
|
||||
* @date 22:45 2019-08-01
|
||||
*/
|
||||
@RestControllerAdvice
|
||||
@Slf4j
|
||||
public class GlobalExceptionHandler {
|
||||
|
||||
/**
|
||||
* 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) {
|
||||
StringBuffer errorMessage = new StringBuffer();
|
||||
if (exception != null) {
|
||||
exception.getBindingResult().getAllErrors().forEach(error ->
|
||||
errorMessage.append(error.getDefaultMessage()).append("."));
|
||||
}
|
||||
if (log.isDebugEnabled()) {
|
||||
log.debug("[sample-tom]-[input argument not valid happen]-{}", errorMessage, exception);
|
||||
}
|
||||
Message message = Message.builder().msg(errorMessage.toString()).build();
|
||||
return ResponseEntity.status(HttpStatus.BAD_REQUEST).body(message);
|
||||
}
|
||||
|
||||
/**
|
||||
* handler the exception thrown for datastore error
|
||||
* @param exception datastore exception
|
||||
* @return response
|
||||
*/
|
||||
@ExceptionHandler(DataAccessException.class)
|
||||
@ResponseBody
|
||||
ResponseEntity<Message> 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();
|
||||
return ResponseEntity.status(HttpStatus.CONFLICT).body(message);
|
||||
}
|
||||
|
||||
/**
|
||||
* handle Request method not supported
|
||||
* @param exception Request method not supported
|
||||
* @return response
|
||||
*/
|
||||
@ExceptionHandler(HttpRequestMethodNotSupportedException.class)
|
||||
@ResponseBody
|
||||
ResponseEntity<Message> 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();
|
||||
return ResponseEntity.status(HttpStatus.METHOD_NOT_ALLOWED).body(message);
|
||||
}
|
||||
|
||||
/**
|
||||
* handler the exception thrown for unCatch and unKnown
|
||||
* @param exception UnknownException
|
||||
* @return response
|
||||
*/
|
||||
@ExceptionHandler(Exception.class)
|
||||
@ResponseBody
|
||||
ResponseEntity<Message> 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();
|
||||
return ResponseEntity.status(HttpStatus.CONFLICT).body(message);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,48 @@
|
||||
package com.usthe.manager.support;
|
||||
|
||||
import org.springframework.beans.BeansException;
|
||||
import org.springframework.context.ApplicationContext;
|
||||
import org.springframework.context.ApplicationContextAware;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
/**
|
||||
* Spring的ApplicationContext的持有者,可以用静态方法的方式获取spring容器中的bean
|
||||
* @author tomsun28
|
||||
* @date 21:07 2018/4/18
|
||||
*/
|
||||
@Component
|
||||
public class SpringContextHolder implements ApplicationContextAware {
|
||||
|
||||
private static ApplicationContext applicationContext;
|
||||
|
||||
@Override
|
||||
public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
|
||||
set(applicationContext);
|
||||
}
|
||||
|
||||
private static void set(ApplicationContext applicationContext) {
|
||||
SpringContextHolder.applicationContext = applicationContext;
|
||||
}
|
||||
|
||||
public static ApplicationContext getApplicationContext() {
|
||||
assertApplicationContext();
|
||||
return applicationContext;
|
||||
}
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
public static <T> T getBean(String beanName) {
|
||||
assertApplicationContext();
|
||||
return (T) applicationContext.getBean(beanName);
|
||||
}
|
||||
|
||||
public static <T> T getBean(Class<T> tClass) {
|
||||
assertApplicationContext();
|
||||
return (T) applicationContext.getBean(tClass);
|
||||
}
|
||||
|
||||
private static void assertApplicationContext() {
|
||||
if (null == SpringContextHolder.applicationContext) {
|
||||
throw new RuntimeException("applicationContext为空,请检查是否注入springContextHolder");
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user