目录

   微服务:整合 Spring Cloud Eureka - 注册中心 Eureka Server 

   微服务:整合 Spring Cloud Eureka - 服务注册 Eureka Client  

   微服务:整合 Spring Cloud Eureka - 服务发现 DiscoveryClient 

   微服务:整合 Spring Cloud Eureka - 服务消费以及Ribbon简单使用 

   微服务:整合 Spring Cloud Eureka - 高可用集群  

   微服务:整合 Spring Cloud Eureka - .NET Core Mvc Api (C#) 

   微服务:整合 Spring Cloud Eureka - 服务治理机制  

   微服务:整合 Spring Cloud Eureka - 服务事件监听  

   微服务:整合 Spring Cloud Eureka - 高级属性Region、Zone

   微服务:整合 Spring Cloud Eureka - Rest接口文档 

   微服务:整合 Spring Cloud Eureka - Security 安全保护

一、简介

  Eureka Server提供了五种服务监听事件。因为在某些业务场景下,我们需要做一些自定义的扩展。例如:某个微服务挂掉了,我们希望能监听到,并给管理员发送邮件通知。

  • EurekaInstanceCanceledEvent: 服务下线事件
  • EurekaInstanceRegisteredEvent: 服务注册事件
  • EurekaInstanceRenewedEvent: 服务续约事件
  • EurekaRegistryAvailableEvent: eureka注册中心启动事件
  • EurekaServerStartedEvent: eureka server启动时间

微服务:整合 Spring Cloud Eureka - 服务事件监听

三、监听类:EurekaStateListener.java

1、代码结构:

微服务:整合 Spring Cloud Eureka - 服务事件监听

 

 2、服务监听类

package com.demo.register.listener;

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;

@Component
public class EurekaStateListener {

    private final static Logger logger = LoggerFactory.getLogger(EurekaStateListener.class);

    @EventListener
    public void listen(EurekaInstanceCanceledEvent event) {
        logger.info("服务{}已下线", event.getAppName());
        logger.info("server地址信息{}", event.getServerId());
        //TODO 发送邮件
    }

    @EventListener
    public void listen(EurekaInstanceRegisteredEvent event) {
        InstanceInfo instanceInfo = event.getInstanceInfo();
        logger.info("服务{}进行注册", instanceInfo.getAppName()+ instanceInfo.getHostName() +"  "+ instanceInfo.getIPAddr() +"  "+ instanceInfo.getPort());
    }

    @EventListener
    public void listen(EurekaInstanceRenewedEvent event) {
        logger.info("服务{}进行续约", event.getServerId() +"  "+ event.getAppName());
    }

    @EventListener
    public void listen(EurekaRegistryAvailableEvent event) {
        logger.info("注册中心启动,{}", System.currentTimeMillis());
    }

    @EventListener
    public void listen(EurekaServerStartedEvent event) {
        logger.info("注册中心服务端启动,{}", System.currentTimeMillis());
    }

}

3、运行测试

微服务:整合 Spring Cloud Eureka - 服务事件监听

 

相关文章:

  • 2022-12-23
  • 2022-12-23
  • 2021-07-06
  • 2021-08-28
  • 2018-12-17
  • 2022-12-23
  • 2021-06-28
  • 2022-12-23
猜你喜欢
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2021-07-02
  • 2022-01-21
  • 2022-12-23
  • 2022-12-23
相关资源
相似解决方案