eureka监听各服务状态并针对服务中断邮件提醒功能实现

  eureka监听各服务状态并针对服务中断邮件提醒功能实现

  为实现重要服务单点故障的及时修复,  加上Eureka Server 包含对服务5种状态较完善的监听实现, 特基于此实现各监听处理逻辑.

  Eureka-Server的五种事件通知  

  EurekaInstanceCanceledEvent 服务下线事件

  EurekaInstanceRegisteredEvent 服务注册事件

  EurekaInstanceRenewedEvent 服务续约事件

  EurekaRegistryAvailableEvent Eureka注册中心启动事件

  EurekaServerStartedEvent Eureka Server启动事件

  Java邮箱通知实现

  pom导包:

        <!-- JavaMail-->
        <dependency>
            <groupId>javax.mail</groupId>
            <artifactId>mail</artifactId>
            <version>1.4.7</version>
        </dependency>

  连接邮件服务器(对应的邮箱账号需开通邮箱服务)

        Properties props = new Properties();
        // 发送服务器需要身份验证
        props.setProperty("mail.smtp.auth", "true");
        // 设置邮件服务器主机名
        props.setProperty("mail.host", "smtp.163.com");
        // 发送邮件协议名称
        props.setProperty("mail.transport.protocol", "smtp");

        try {
            // 设置环境信息
            Session session = Session.getInstance(props);

            // 创建邮件对象
            Message msg = new MimeMessage(session);
            msg.setSubject("Eureka Server status check");
            // 设置邮件内容
            msg.setText("服务异常关闭提醒 appName : " + appName + " , serverId : " + serverId);
            // 设置发件人(账号)
            msg.setFrom(new InternetAddress("niuya***@163.com"));

            Transport transport = session.getTransport();
            // 连接邮件服务器(账号,授权码)
            transport.connect("niuya***@163.com", "***");
            // 发送邮件
            transport.sendMessage(msg, new Address[] {new InternetAddress("niuya@***.com")});
            // 关闭连接
            transport.close();
        } catch (Exception e) {
            log.error("send module canceled message error : ", e);
        }

  完整实现

import com.netflix.appinfo.InstanceInfo;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.cloud.netflix.eureka.server.event.*;
import org.springframework.context.event.EventListener;
import org.springframework.stereotype.Component;

import javax.mail.Address;
import javax.mail.Message;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
import java.util.Properties;

@Component
public class EurekaStateChangeListener {

    private static final Logger log = LoggerFactory.getLogger(EurekaStateChangeListener.class);

    @EventListener
    public void listen(EurekaInstanceCanceledEvent eurekaInstanceCanceledEvent) {
        String appName = eurekaInstanceCanceledEvent.getAppName();
        String serverId = eurekaInstanceCanceledEvent.getServerId();
        log.info("module canceled server_id : " + serverId);
        sendCanceledWarnMail(appName,serverId);
    }

    @EventListener
    public void listen(EurekaInstanceRegisteredEvent event) {
        InstanceInfo instanceInfo = event.getInstanceInfo();
        log.info("module registered instanceInfo : " + instanceInfo);
    }

    @EventListener
    public void listen(EurekaInstanceRenewedEvent event) {
        String serverId = event.getServerId();
        log.info("module renewed server_id : " + serverId);
    }

    @EventListener
    public void listen(EurekaRegistryAvailableEvent event) {
        String eventStr = event.toString();
        log.info("module available is " + eventStr);
    }

    @EventListener
    public void listen(EurekaServerStartedEvent event) {
        String startStr = event.toString();
        log.info("module start is " + startStr);
    }
}

 

posted @ 2019-07-03 15:35  来兮子宁  阅读(745)  评论(0编辑  收藏