在一些场景下,我们需要监听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. ES6 Iterator

    不同数据集合怎么用统一的方式读取 可以用for...of循环了

  2. CentOS_mini下安装docker之 安装 golang

    取消挂载: 命令:umount /mnt/cdrom 下载 Go 语言文件 -bit Linux wget http://www.golangtc.com/static/go/go1.4.2.linu ...

  3. linux命令大全(1)

    当用户使用linux系统时,其实在和Shell在打交道,当用户发出指令,其实先将这些指令发送给Shell, 然后由Shell将用户的指令翻译后传送给内核,再由内核来控制硬件的工作. 然后内核将硬件的工 ...

  4. mysql空值排序

    SELECT * FROM lzh_topic_channel_product ORDER BY order_id is null , order_id  其中的ORDER BY order_id i ...

  5. 图解HTTP第二章

    简单的 HTTP 协议 1>HTTP 协议用于客户端和服务器端之间的通信 HTTP 协议和 TCP/IP 协议族内的其他众多的协议相同,用于客户端和服务器之间的通信.请求访问文本或图像等资源的一 ...

  6. .gitignore无效解决方案以及git rm和rm的区别

    一. gitignore 先来了解一下gitignore的常用语法 斜杠“/”表示目录, 是否已斜杠开头有很大区别,如 /build 与 build/ 的区别:其中 build/ 表示不管在哪个位置的 ...

  7. POJ - 3984 迷宫问题 dfs解法

    #include<stdio.h> #include<string.h> #include<stack> #include<algorithm> usi ...

  8. C#顺序表 & 单向链表(无头)

    C# 顺序表 非常标准的顺序表结构,等同于C#中的List<T>,但是List<T>在排错查询和数据结构替换上存在缺陷,一些情况会考虑使用自己定义的数据结构 1.优化方向 下表 ...

  9. Linux程序设计:进程通信

      日期:忘了. 关键词:Linux程序设计:System-V:进程通信:共享内存:消息队列. 一.共享内存   1.1 基本知识 (待补充)   1.2 代码 一个基于share memory实现的 ...

  10. MSRA-TD5000数据集使用详解

    中文检测的数据集,目前最火的应该是清华的CTW,https://ctwdataset.github.io/ 但是它的数据集只存储在微云和google driver,微云空间受限不能完全保存,所以下载的 ...