1 changed files with 77 additions and 0 deletions
@ -0,0 +1,77 @@ |
|||
/** |
|||
* Copyright (c) 2018 人人开源 All rights reserved. |
|||
* <p> |
|||
* https://www.renren.io
|
|||
* <p> |
|||
* 版权所有,侵权必究! |
|||
*/ |
|||
|
|||
package com.tduck.cloud.api.handler; |
|||
|
|||
import com.alibaba.fastjson.JSON; |
|||
import com.github.jaemon.dinger.DingerSender; |
|||
import com.github.jaemon.dinger.core.entity.DingerRequest; |
|||
import com.github.jaemon.dinger.core.entity.DingerResponse; |
|||
import com.github.jaemon.dinger.core.entity.enums.MessageSubType; |
|||
import com.tduck.cloud.common.util.EnvEnum; |
|||
import com.tduck.cloud.common.util.SpringContextUtils; |
|||
import org.apache.logging.log4j.LogManager; |
|||
import org.apache.logging.log4j.Logger; |
|||
import org.springframework.beans.factory.annotation.Autowired; |
|||
import org.springframework.beans.factory.annotation.Value; |
|||
import org.springframework.boot.context.event.ApplicationReadyEvent; |
|||
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 ApplicationReadyEventListener implements ApplicationListener<ApplicationReadyEvent> { |
|||
private static Logger logger = LogManager.getLogger(ApplicationReadyEventListener.class); |
|||
@Value("${spring.application.name}") |
|||
private String appName; |
|||
// @Value("${server.version}")
|
|||
// private String version;
|
|||
@Autowired |
|||
private DingerSender dingerSender; |
|||
|
|||
@Override |
|||
public void onApplicationEvent(ApplicationReadyEvent applicationReadyEvent) { |
|||
EnvEnum currentEnv = EnvEnum.getCurrentEnv(); |
|||
logger.info(currentEnv); |
|||
if (!EnvEnum.DEV.getCode().equals(currentEnv.getCode())) { |
|||
sendDingMarkDownMsg(); |
|||
} |
|||
} |
|||
|
|||
private String getServerIp() { |
|||
InetUtils inetUtils = SpringContextUtils.getBean(InetUtils.class); |
|||
if (inetUtils == null){ |
|||
return null; |
|||
} |
|||
return inetUtils.findFirstNonLoopbackHostInfo().getIpAddress(); |
|||
} |
|||
|
|||
private void sendDingMarkDownMsg() { |
|||
String msg = "部署成功通知 \n" + |
|||
"> 服务:" + appName + "\n\n" + |
|||
//"> 版本:" + version + "\n\n" +
|
|||
"> 环境:" + EnvEnum.getCurrentEnv().getName() + "\n\n" + |
|||
"> IP: " + getServerIp() + "\n\n"; |
|||
try { |
|||
|
|||
DingerResponse send = dingerSender.send(MessageSubType.MARKDOWN, DingerRequest.request(msg, "部署成功通知")); |
|||
logger.info("=====通知结果===>" + JSON.toJSONString(send)); |
|||
} catch (Exception e) { |
|||
logger.error("sendDingMarkDownMsg exception", e); |
|||
} |
|||
} |
|||
|
|||
|
|||
} |
Loading…
Reference in new issue