在一些场景下,我们需要监听eureka服务中心的一些状态,譬如某个微服务挂掉了,我们希望能监听到,并给管理员发送邮件通知或钉钉告警。

一、Eureka的监听事件,可以用来监控、告警
EurekaInstanceRegisteredEvent 服务注册事件
EurekaInstanceRenewedEvent 服务续约事件,续约即心跳
EurekaRegistryAvailableEvent Eureka注册中心启动事件
EurekaServerStartedEvent Eureka Server启动事件
EurekaInstanceCanceledEvent 服务下线事件

package com.mimaxueyuan.cloud.eureka.event;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.cloud.netflix.eureka.server.event.EurekaInstanceCanceledEvent;
import org.springframework.cloud.netflix.eureka.server.event.EurekaInstanceRegisteredEvent;
import org.springframework.cloud.netflix.eureka.server.event.EurekaInstanceRenewedEvent;
import org.springframework.cloud.netflix.eureka.server.event.EurekaRegistryAvailableEvent;
import org.springframework.cloud.netflix.eureka.server.event.EurekaServerStartedEvent;
import org.springframework.context.event.EventListener;
import org.springframework.stereotype.Component; import com.netflix.appinfo.InstanceInfo; @Component
public class KevinEventLinstener { private Logger logger = LoggerFactory.getLogger(KevinEventLinstener.class); @EventListener
public void listen(EurekaInstanceRegisteredEvent event) {
InstanceInfo instanceInfo = event.getInstanceInfo();
String ip = instanceInfo.getIPAddr();
String id = instanceInfo.getInstanceId();
logger.info(">>>>>>>>" + id + "已经注册到Eureka,IP=" + ip);
} @EventListener
public void listen(EurekaInstanceRenewedEvent event) {
InstanceInfo instanceInfo = event.getInstanceInfo();
String id = instanceInfo.getInstanceId();
logger.info(">>>>>>>>"+id+"续约事件触发...");
} @EventListener
public void listen(EurekaRegistryAvailableEvent event) {
logger.info(">>>>>>>>注册中心启动事件触发...");
} @EventListener
public void listen(EurekaServerStartedEvent event) {
logger.info(">>>>>>>>EurekaServer启动事件触发...");
} @EventListener
public void listen(EurekaInstanceCanceledEvent event) {
String id = event.getServerId();
logger.info(">>>>>>>>"+id+"从Eureka下线...");
} }

二、注解@DiscoveryClient与@EurekaDiscoveryClient

package com.mimaxueyuan.producer.controller;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.client.discovery.DiscoveryClient;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController; @RestController
public class EurekaController { // 以下两个客户端类,用哪个都可以,DiscoveryClient抽象程度更高,是一个接口,EurekaDiscoveryClient只是他的抽象类
@Autowired
private DiscoveryClient discoveryClient; // eureka使用展示
@GetMapping("/eureka/test")
public String eureka() {
System.out.println("eureka使用展示:/eureka/get---start"); // ----------------- 以下的代码使用DiscoveryClient-------------------
// 查询所有注册到Eureka上的服务:
System.out.println("[discoveryClient]-查询所有注册到Eureka上的服务:");
for (String string : discoveryClient.getServices()) {
System.out.println(string);
} // 查询某一个provider的所有Service实例
System.out.println("[discoveryClient]-查询某一个provider的所有Service实例");
List<ServiceInstance> instances = discoveryClient.getInstances("mima-cloud-producer");
for (ServiceInstance instance : instances) {
System.out.println("host:" + instance.getHost() + ",port:" + instance.getPort() + ",serviceId=" + instance.getServiceId() + ",uri=" + instance.getUri());
System.out.println("metadata=" + instance.getMetadata());
} // TODO 已经不推荐使用
ServiceInstance localServiceInstance = discoveryClient.getLocalServiceInstance(); return "eureka's demo, please to see console output";
} }

Eureka编程的更多相关文章

  1. 【学亮编程手记】Spring Cloud三大组件Eureka/Feign/Histrix的原理及使用

  2. Eureka的原理

    http://blog.csdn.net/awschina/article/details/17639191 关于AWS的区域和可用区概念解释: Eureka的原理:Region与Zone. 因为在编 ...

  3. SpringCloud实战之初级入门(一)— eureka注册中心

    目录 写在前面 1.资料目录 2.环境介绍 3.eureka注册中心 3.1 创建工程 3.2 启动工程 5.eureka注册中心集群高可用 6.结语 7.一点点重要的事情 写在前面 我在软件行业浸泡 ...

  4. convention over configuration 约定优于配置 按约定编程 约定大于配置 PEP 20 -- The Zen of Python

    为什么说 Java 程序员必须掌握 Spring Boot ?_知识库_博客园 https://kb.cnblogs.com/page/606682/ 为什么说 Java 程序员必须掌握 Spring ...

  5. <Spring Cloud>入门二 Eureka Client

    1.搭建一个通用工程 1.1 pom 文件 <?xml version="1.0" encoding="UTF-8"?> <project x ...

  6. SpringCloud核心教程 | 第三篇:服务注册与发现 Eureka篇

    Spring Cloud简介 Spring Cloud是一个基于Spring Boot实现的云应用开发工具,它为基于JVM的云应用开发中涉及的配置管理.服务发现.断路器.智能路由.微代理.控制总线.全 ...

  7. Spring Cloud官方文档中文版-服务发现:Eureka客户端

    官方文档地址为:http://cloud.spring.io/spring-cloud-static/Dalston.SR2/#_spring_cloud_netflix 文中例子我做了一些测试在:h ...

  8. Spring Cloud Eureka 分布式开发之服务注册中心、负载均衡、声明式服务调用实现

    介绍 本示例主要介绍 Spring Cloud 系列中的 Eureka,使你能快速上手负载均衡.声明式服务.服务注册中心等 Eureka Server Eureka 是 Netflix 的子模块,它是 ...

  9. SpringCloud-服务注册与实现-Eureka创建服务提供者(附源码下载)

    场景 SpringCloud-服务注册与实现-Eureka创建服务注册中心(附源码下载): https://blog.csdn.net/BADAO_LIUMANG_QIZHI/article/deta ...

随机推荐

  1. 通过Solr所提供的Dataimporthandler实现数据源的导入

    如需要使用到Solr中的dataimporthandler增量导入功能,则还需要引入两个所依赖的jar包,在上一篇随笔中所提到的下载的Solr项目文件solr-4.10.3\dist目录下可以找到所依 ...

  2. python 实现 Fortran的读取10*0以及换行读问题

    思路,用read来全部读取,然后替换带*的元素来解决.代码如下 import numpy as np import re inf = open('SF.usr') title = inf.readli ...

  3. django by example 第四章 dashboard处html无法渲染问题

    描述: 实现django by example 代码时,第四章 dashboard处html无法渲染问题. 此时报错,NoReverseMatch at /account/login/, Error ...

  4. web版ssh的使用

    一.web_ssh版本安装使用 web_ssh源码:https://github.com/shellinabox/shellinabox 1)安装依赖包 yum install git openssl ...

  5. tomcat 配置 使用 HTTPS

    1.生成证书 keytool -genkeypair -alias "tomcat" -keyalg "RSA" -keystore "d:\temp ...

  6. python repr()和str()

    两者功能差不多,都是把对象转为字符串表示形式,但是也有区别,repr()之后再eval()可以转为原型,但str()只能保证大多数,不能100% 其中主要的 差别在与 字符串对象本身,比如 a = ' ...

  7. ScriptOJ-safeGet#99

    const safeGet = (data, path) => { if(!path) return undefined; const pathArr = path.split('.'); le ...

  8. PHP字符串函数之 strpos stripos strrpos strripos

    strpos – 查找字符串首次出现的位置 stripos – 查找字符串首次出现的位置(不区分大小写) strrpos – 计算指定字符串在目标字符串中最后一次出现的位置 strripos – 计算 ...

  9. 基于ALTERA SOPC设计的概述

    下图是比较传统的系统设计开发板的设备图 由于元器件比较多,成本,复杂性和功耗都比较高,所以需要一种新的方案来降低成本和复杂性. ALTERAL  就提供了一种SOPC解决方案,将系统的I/O.CPU和 ...

  10. 枚举类型enum详解——C语言

    enum enum是C语言中的一个关键字,enum叫枚举数据类型,枚举数据类型描述的是一组整型值的集合(这句话其实不太妥当),枚举型是预处理指令#define的替代,枚举和宏其实非常类似,宏在预处理阶 ...