欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

关于Spring Cloud Eureka对服务的监控(上线,下线,续约等)

程序员文章站 2024-01-16 21:37:22
...

第一次写博客,有很多不足之处,请多多指教

项目上要求要监听各个服务宕机后的信息,发现以前没有做个,于是想了一下,猜想eureka注册中心肯定会提供这类的方法,上网一查,果然发现eureka提供了5的监听器

EurekaInstanceCanceledEvent 服务下线事件
EurekaInstanceRegisteredEvent 服务注册事件
EurekaInstanceRenewedEvent 服务续约事件
EurekaRegistryAvailableEvent Eureka注册中心启动事件
EurekaServerStartedEvent Eureka Server启动事件

 

那接下来就好说了

 

第一步 : 引入maven依赖

    略: 就是基本的依赖

第二步 : 监听服务

   在网上查询了部分资料之后,发现部分的博客缺少一些东西,会对新手有很多的误导,在这里给出一个详细的步骤

    Eureka服务端的配置

server:
  port: 8081
eureka:
  instance:
    hostname: localhost
  client:
    registerWithEureka: false
    fetchRegistry: false
    serviceUrl:
      defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka/
  server:
      #关闭自我保护
      enable-self-preservation: false
      #清理间隔
      eviction-interval-timer-in-ms: 5000

  服务配置这里的 关闭自我保护和清理间隔一定要打开 (虽然我也不知道为什么)  

启动类

      

package com.lh;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer;

/**
 * Created by 龙浩 on 2018/7/9.
 */
@SpringBootApplication
@EnableEurekaServer
public class app {
    public static void main(String[] args){
        SpringApplication.run(app.class,args);
    }
}

  服务监听类

package com.lh.event;

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;


/**
 * Created by 龙浩 on 2018/7/23.
 */
@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());
    }

    @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());
    }

}

  这里只是简单的记录,实际项目还是要做大量的通知

第三步 : 客户端

   客户端配置

spring:
  application:
    name: member
eureka:
  instance:
    # 每间隔1s,向服务端发送一次心跳,证明自己依然”存活“
    lease-renewal-interval-in-seconds: 1
    # 告诉服务端,如果我2s之内没有给你发心跳,就代表我“死”了,将我踢出掉。
    lease-expiration-duration-in-seconds: 2
  client:
    serviceUrl:
      defaultZone: http://localhost:8081/eureka/
server:
  port: 8082

  这里的instance配置一定要写,很多新手忘记写这个导致服务下线无法被监听,因为服务下线后eureka没有将该服务踢出

  启动类

package com.lh;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;

/**
 * Created by 龙浩 on 2018/7/9.
 */
@SpringBootApplication
@EnableEurekaClient
public class MemberApp {

    public static void main(String[] args){
        SpringApplication.run(MemberApp.class, args);
    }
}

 第四步 : 测试

    启动服务端,打印如下信息

   

2018-07-24 09:39:49.590  INFO 16524 --- [      Thread-13] com.lh.event.EurekaStateListener         : 注册中心启动,1532396389590
2018-07-24 09:39:49.591  INFO 16524 --- [      Thread-13] com.lh.event.EurekaStateListener         : 注册中心服务端启动,1532396389591
2018-07-24 09:39:49.631  INFO 16524 --- [           main] s.b.c.e.t.TomcatEmbeddedServletContainer : Tomcat started on port(s): 8081 (http)
2018-07-24 09:39:49.632  INFO 16524 --- [           main] .s.c.n.e.s.EurekaAutoServiceRegistration : Updating port to 8081
2018-07-24 09:39:49.635  INFO 16524 --- [           main] com.lh.app                               : Started app in 6.512 seconds (JVM running for 7.16)

 启动客户端  服务端控制台继续打印如下信息

2018-07-24 09:41:04.656  INFO 16524 --- [nio-8081-exec-2] com.lh.event.EurekaStateListener         : 服务MEMBERDESKTOP-SHDQ5I0  192.168.0.103  8082进行注册
2018-07-24 09:41:04.670  INFO 16524 --- [nio-8081-exec-2] c.n.e.registry.AbstractInstanceRegistry  : Registered instance MEMBER/DESKTOP-SHDQ5I0:member:8082 with status UP (replication=false)
2018-07-24 09:41:05.541  INFO 16524 --- [nio-8081-exec-3] com.lh.event.EurekaStateListener         : 服务DESKTOP-SHDQ5I0:member:8082  MEMBER进行续约
2018-07-24 09:41:06.551  INFO 16524 --- [nio-8081-exec-4] com.lh.event.EurekaStateListener         : 服务DESKTOP-SHDQ5I0:member:8082  MEMBER进行续约
2018-07-24 09:41:07.562  INFO 16524 --- [nio-8081-exec-5] com.lh.event.EurekaStateListener         : 服务DESKTOP-SHDQ5I0:member:8082  MEMBER进行续约
2018-07-24 09:41:08.571  INFO 16524 --- [nio-8081-exec-6] com.lh.event.EurekaStateListener         : 服务DESKTOP-SHDQ5I0:member:8082  MEMBER进行续约

  关闭客户端 服务端控制台打印如下信息

2018-07-24 09:41:59.599  INFO 16524 --- [a-EvictionTimer] com.lh.event.EurekaStateListener         : 服务MEMBER已下线
2018-07-24 09:41:59.600  INFO 16524 --- [a-EvictionTimer] com.lh.event.EurekaStateListener         : server地址信息DESKTOP-SHDQ5I0:member:8082
2018-07-24 09:41:59.601  INFO 16524 --- [a-EvictionTimer] c.n.e.registry.AbstractInstanceRegistry  : Cancelled instance MEMBER/DESKTOP-SHDQ5I0:member:8082 (replication=false)

恭喜你,又get到新技能!!!!!