交心报错预警企微消息
择要
实时发明法式报错预警
枢纽词汇
企微收收帮忙消息交心、自界说诠释、aop、解耦
成就的提出
1- 立即反应用户端非常情况
2-增加 净数据的进库
处置思路和实践
关于 可以招致数据没有不合情况之处 代码中没有要catch或许 catch住要截至处置或许 扔出到controller层 正在分歧非常处置以前拦阻住非常 挪用企微收收消息交心背设置的职工收收企微消息
设置文献中设置使用称呼、情况称呼
正在预警消息中收收非常发作的交心、恳求参数、毛病疑息、另有时间戳。能够按照时间戳定位到毛病发作职位(便利盘问日记)
使用
自界说一个starter starter中尽可以少依靠其余jar包
自界说了一个诠释关于 正在交心上增加此诠释的交心截至毛病预警
设置文献中可设置使用、情况、企业、对于应企业的职工、可否激活此诠释
使用时只要供增加starter依靠、设置您自己的设置文献 正在交心报错的时候便会正在企微中收给您预警消息 对于应的开辟职员能够立即发明立即处置
诠释- /**
- * @Author ningYu
- * @create 2022/6/10 11:17
- * @Target 诠释用去指定一个诠释的使用范畴
- * @Retention 用于描绘诠释的性命周期,也即是该诠释被保存的时间是非
- * @Documented 诠释润饰的诠释类会被 JavaDoc 东西提炼成文档
- * @Inherited 是一个标识表记标帜诠释,用去指定该诠释能够被承袭
- */
- @Target(value = {ElementType.METHOD})
- @Retention(RetentionPolicy.RUNTIME)
- @Documented
- @Inherited
- @Order(Ordered.HIGHEST_PRECEDENCE)
- public @interface WarnNotice {
- //办法名
- String value() ;
- //操纵范例
- int operateType();
- //功用范例
- String functionType() ;
- }
复造代码 处置诠释代码`- @Pointcut("@annotation(com.huibo.warnNotice.annotation.WarnNotice)")
- public void access() {
- }
- /**
- *假设 交心扔堕落误 企微提醒开辟职员
- *
- * @param joinPoint
- * @throws Exception
- */
- @AfterThrowing(value = "access()", throwing = "error")
- public void addErrorLogRecord(JoinPoint joinPoint, Exception error) throws Exception {
- String errMsg = "";
- String timeStamp = System.currentTimeMillis() + "";
- log.error(timeStamp + ":" + "交心呈现非常 背设置的开辟职员倡议报告");
- try {
- Signature sig = joinPoint.getSignature();
- MethodSignature msig = (MethodSignature) sig;
- //按照aop参数获得到目标工具 获得到切里依据 反射战切里获得到办法署名战办法参数获得目标工具的办法 获得到办法的诠释 拿到诠释的属性
- Object target = joinPoint.getTarget();
- Object[] args = joinPoint.getArgs();
- Method method = target.getClass().getMethod(msig.getName(), msig.getParameterTypes());
- LocalVariableTableParameterNameDiscoverer u = new LocalVariableTableParameterNameDiscoverer();
- WarnNotice annotation = method.getAnnotation(WarnNotice.class);
- String methodName = annotation.value();
- String[] parameterNames = u.getParameterNames(method);
- Map<String, Object> paramMap = new HashMap<>();
- if (parameterNames.length > 0) {
- int i = 0;
- for (String paramName : parameterNames) {
- paramMap.put(paramName, args[i]);
- }
- }
- errMsg = qwUserConfig.getEnvironment() + "情况:" + qwUserConfig.getModuleName() + "bug消息预警--\n timeStamp:" + timeStamp + "\n 办法:" + methodName + "\n param:" + JSON.toJSONString(paramMap) + "\n error:" + error.getMessage();
- sendQwMsg(errMsg);
- } catch (Throwable e) {
- log.error(timeStamp + ":收消息代码毛病 " + e.getMessage(), e);
- } finally { //将非常扔进来 接给全部非常处置器处置
- if (error instanceof BizException) {
- BizException e = (BizException) error;
- log.error(timeStamp + e.getMessage(), e);
- throw new BizException(e.getCode(), e.getMessage());
- } else {
- log.error(timeStamp + error.getMessage(), error);
- throw new BizException(BizExceptionEnum.SYSTEM_ERROR);
- }
- }
- }`
复造代码 收收企微消息代码- //那里实在即是挪用了一个http交心
- public void sendQwMsg(String errMsg) throws Exception {
- log.info("errMsg:[{}]", errMsg);
- LambdaQueryWrapper<QyAgentInfo> qw = new LambdaQueryWrapper<>();
- qw.eq(QyAgentInfo::getIsDelete, 0)
- .eq(QyAgentInfo::getStatus, 1)
- .eq(QyAgentInfo::getCorpId, qwUserConfig.getCorpid())
- .last(" limit 1 ");
- QyAgentInfo qyAgentInfo = qyAgentInfoMapper.selectOne(qw);
- RestTemplate restTemplate = new RestTemplate();
- log.info("qyAgentInfo:[{}]", JSON.toJSONString(qyAgentInfo));
- URI uri = new URI(qwUserConfig.getSendurl() + qyAgentInfo.getAccessToken());
- Map<String, Object> paramMap = new HashMap<>();
- paramMap.put("touser", qwUserConfig.getTouser());
- paramMap.put("msgtype", qwUserConfig.getMsgtype());
- paramMap.put("agentid", qwUserConfig.getAgentid());
- paramMap.put("safe", qwUserConfig.getSafe());
- Map<String, Object> map = new HashMap<>();
- map.put("content", errMsg);
- paramMap.put("text", map);
- RequestEntity<Map<String, Object>> requestEntity = RequestEntity.post(uri).
- contentType(MediaType.APPLICATION_JSON).body(paramMap);
- ResponseEntity<QiWeiRes> exchange = restTemplate.exchange(requestEntity, QiWeiRes.class);
- return;
- }
复造代码 |