2 changed files with 90 additions and 7 deletions
@ -0,0 +1,84 @@ |
|||||
|
/** |
||||
|
* Copyright (c) 2018 人人开源 All rights reserved. |
||||
|
* <p> |
||||
|
* https://www.renren.io
|
||||
|
* <p> |
||||
|
* 版权所有,侵权必究! |
||||
|
*/ |
||||
|
|
||||
|
package com.epmet.commons.tools.aspect; |
||||
|
|
||||
|
import com.alibaba.fastjson.JSON; |
||||
|
import com.dingtalk.api.DefaultDingTalkClient; |
||||
|
import com.dingtalk.api.DingTalkClient; |
||||
|
import com.dingtalk.api.request.OapiRobotSendRequest; |
||||
|
import com.dingtalk.api.response.OapiRobotSendResponse; |
||||
|
import com.epmet.commons.tools.enums.EnvEnum; |
||||
|
import com.epmet.commons.tools.utils.SpringContextUtils; |
||||
|
import com.taobao.api.ApiException; |
||||
|
import org.apache.logging.log4j.LogManager; |
||||
|
import org.apache.logging.log4j.Logger; |
||||
|
import org.springframework.beans.factory.annotation.Value; |
||||
|
import org.springframework.boot.context.event.ApplicationFailedEvent; |
||||
|
import org.springframework.cloud.commons.util.InetUtils; |
||||
|
import org.springframework.context.ApplicationListener; |
||||
|
import org.springframework.stereotype.Component; |
||||
|
|
||||
|
/** |
||||
|
* 应用 启动健康检查 通知类 |
||||
|
* CustomerApplicationRunner |
||||
|
* |
||||
|
* @author Mark sunlightcs@gmail.com |
||||
|
* @since 1.0.0 |
||||
|
*/ |
||||
|
@Component |
||||
|
public class ApplicationFailedEventListener implements ApplicationListener<ApplicationFailedEvent> { |
||||
|
private static Logger logger = LogManager.getLogger(ApplicationFailedEventListener.class); |
||||
|
@Value("${spring.application.name}") |
||||
|
private String appName; |
||||
|
@Value("${server.version}") |
||||
|
private String version; |
||||
|
|
||||
|
@Override |
||||
|
public void onApplicationEvent(ApplicationFailedEvent applicationFailedEvent) { |
||||
|
Throwable exception = applicationFailedEvent.getException(); |
||||
|
EnvEnum currentEnv = EnvEnum.getCurrentEnv(); |
||||
|
logger.info(currentEnv); |
||||
|
if (!EnvEnum.DEV.getCode().equals(currentEnv.getCode()) && !EnvEnum.LOCAL.getCode().equals(currentEnv.getCode())) { |
||||
|
sendDingMarkDownMsg(exception); |
||||
|
} |
||||
|
} |
||||
|
|
||||
|
|
||||
|
private String getServerIp() { |
||||
|
InetUtils inetUtils = SpringContextUtils.getBean(InetUtils.class); |
||||
|
return inetUtils.findFirstNonLoopbackHostInfo().getIpAddress(); |
||||
|
} |
||||
|
|
||||
|
private void sendDingMarkDownMsg(Throwable exception) { |
||||
|
DingTalkClient client = new DefaultDingTalkClient("https://oapi.dingtalk.com/robot/send?access_token=ffd7c972b0525e249283df1a16b65a8b9d0012601f3a458dfc588c2eac497bb5"); |
||||
|
OapiRobotSendRequest request = new OapiRobotSendRequest(); |
||||
|
request.setMsgtype("markdown"); |
||||
|
OapiRobotSendRequest.Markdown markdown = new OapiRobotSendRequest.Markdown(); |
||||
|
markdown.setTitle("部署失败通知"); |
||||
|
|
||||
|
markdown.setText("部署失败通知 \n" + |
||||
|
"> 服务:" + appName + "\n\n" + |
||||
|
"> 版本:" + version + "\n\n" + |
||||
|
"> 环境:" + EnvEnum.getCurrentEnv().getName() + "\n\n" + |
||||
|
"> IP: " + getServerIp() + "\n\n" + |
||||
|
"> 异常:" + exception.getMessage() + "\n\n" |
||||
|
); |
||||
|
request.setMarkdown(markdown); |
||||
|
OapiRobotSendRequest.At at = new OapiRobotSendRequest.At(); |
||||
|
at.setIsAtAll(true); |
||||
|
request.setAt(at); |
||||
|
try { |
||||
|
OapiRobotSendResponse execute = client.execute(request); |
||||
|
logger.info("=====通知结果===>" + JSON.toJSONString(execute)); |
||||
|
} catch (ApiException e) { |
||||
|
logger.error("sendDingMarkDownMsg exception", e); |
||||
|
} |
||||
|
} |
||||
|
|
||||
|
} |
Loading…
Reference in new issue