java异常报警发送钉钉群

`

<dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-webflux</artifactId>
        <version>2.2.7.RELEASE</version>
</dependency>

@Documented
@Target({ElementType.METHOD, ElementType.TYPE})
@Retention(RetentionPolicy.RUNTIME)
public @interface ServiceJobExcepCatch {

}

@Component
@Aspect
@Slf4j
public class ServiceExceptionHandler {

@Resource
ExceptionWarnSend send;

//Service层切点
@Pointcut("@annotation(com.modules.app.annotation.ServiceJobExcepCatch)")
public  void serviceAspect() {
}


//全局异常注解
@Around("@annotation(com.modules.app.annotation.ServiceJobExcepCatch)  || @within(com.modules.app.annotation.ServiceJobExcepCatch)")
public Object serviceJobExceptionHandler(ProceedingJoinPoint pjp) throws Exception {
    String apiOperation="";
    Object proceed =null;
    try {
        Signature signature = pjp.getSignature();
        MethodSignature methodSignature = (MethodSignature)signature;
        Method targetMethod = methodSignature.getMethod();
        if(targetMethod!=null){
            ApiOperation annotation = targetMethod.getAnnotation(ApiOperation.class);
            if (annotation!=null){
                apiOperation  = annotation.value();
            }else{
                System.out.println("############ annotation 为null ");
            }
        }else{
            System.out.println("############ apiOperation为null");
        }
        proceed = pjp.proceed();
    } catch (Exception e) {
        e.printStackTrace();
        String stackTrace = e.getStackTrace()[0].toString();
        String msg = apiOperation + "异常##" + stackTrace + "##" + e.toString();
        log.debug("出现了异常!!!"+msg);
        send.sendWarnInfo(msg); //报警
        throw e;
    } catch (Throwable throwable) {
        throwable.printStackTrace();
    }
    return proceed;
}

}

`
import com.alibaba.fastjson.JSONObject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.http.MediaType;
import org.springframework.stereotype.Component;
import org.springframework.web.reactive.function.client.WebClient;
import reactor.core.publisher.Flux;

@Component
@Slf4j
public class ExceptionWarnSend {

@Value("${environment}")
String environment;

private static final String url="https://oapi.dingtalk.com/robot/send?access_token=xxxx";


public void sendWarnInfo(String msg){
    WebClient webClient = WebClient.create();

    JSONObject text=new JSONObject();
        text.put("content",msg+" 来自"+environment);  //异常信息

    JSONObject bodys = new JSONObject();
        bodys.put("msgtype","text");
        bodys.put("text",text);

    Flux<JSONObject> flux  =webClient.post().uri(url)
            .contentType(MediaType.valueOf(MediaType.APPLICATION_JSON_UTF8_VALUE))
            .bodyValue(bodys.toJSONString()).retrieve().bodyToFlux(JSONObject.class);

    JSONObject jsonObject = flux.blockFirst();
    String errcode = jsonObject.get("errcode").toString();
    if(errcode.equals("0")){
        log.debug("报警发送成功");
    }else{
        log.error("报警发送失败"+jsonObject.get("errmsg").toString());
    }
}

}

加在方法上面
@ApiOperation("秒杀")
@ServiceJobExcepCatch

`

posted @ 2024-05-07 23:04  水滴aym  阅读(53)  评论(0编辑  收藏  举报