注册中心Eureka监听服务

Eureka监听服务

Eureka的server端会发出5个事件通知,分别是:

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

Eureka服务端的配置

spring:
  application:
    name: eureka-server

server:
  port: 7001
eureka:
  instance:
    instance-id: ${spring.cloud.client.ip-address}:${server.port}
    hostname: peer1
#    prefer-ip-address: true   #以IP地址注册到服务中心,相互注册使用IP地址
  #    instance-id: ${spring.cloud.client.ipAddress}:${server.port}
  #    ip-address: 47.99.200.202 #强制指定IP地址,默认会获取本机的IP地址
  client:
    register-with-eureka: true         # 表示是否注册自身到eureka服务器
    fetch-registry: true         # 是否从eureka上获取注册信息
    serviceUrl:
      defaultZone: http://peer2:7002/eureka/   
  server:
    eviction-interval-timer-in-ms: 3000     # 续期时间,即扫描失效服务的间隔时间(单位毫秒,默认是60*1000)
    enableSelfPreservation: false    # 设为false,关闭自我保护
#    renewalPercentThreshold: 0.49

服务监听类

package com.hz.eureka1;

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.annotation.ComponentScan;
import org.springframework.context.event.EventListener;
import org.springframework.stereotype.Component;

@Component
@ComponentScan
public class EurekaStateListener {
    private final static Logger logger = LoggerFactory.getLogger(EurekaStateListener.class);

    @EventListener(condition = "#event.replication==false")
    public void listen(EurekaInstanceCanceledEvent event) {
        String msg="服务"+event.getAppName()+"\n"+event.getServerId()+"已下线";
        logger.info(msg);
    }

    @EventListener(condition = "#event.replication==false")
    public void listen(EurekaInstanceRegisteredEvent event) {
        InstanceInfo instanceInfo = event.getInstanceInfo();
        String msg="服务"+instanceInfo.getAppName()+"\n"+  instanceInfo.getHostName()+":"+ instanceInfo.getPort()+ " \nip: " +instanceInfo.getIPAddr() +"进行注册";
        logger.info(msg);

    }

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

}

Eureka客户端的配置(也是config server)

server:
  port: 8769

spring:
  application:
    name: config-server

  cloud:
    config:
      server:
        git:
          uri: http://[email protected]:10010/r/v.git
          username: v
          password: hz
          default-label: master

eureka:
  client:
    serviceUrl:
      defaultZone: http://localhost:7001/eureka/,http://localhost:7002/eureka/

    healthcheck:
      enabled: true                           # 开启健康检查(依赖spring-boot-starter-actuator)
  instance:
    instance-id: ${spring.cloud.client.ip-address}:${server.port}
    prefer-ip-address: true #以IP地址注册到服务中心
    lease-renewal-interval-in-seconds: 5      # 心跳时间,即服务续约间隔时间(缺省为30s)
    lease-expiration-duration-in-seconds: 15  # 发呆时间,即服务续约到期时间(缺省为90s)

启动服务端,再启动客户端,就会显示注册信息。

INFO [http-nio-7001-exec-2] 2019-03-11 20:31:13 (EurekaStateListener.java:26) 服务EUREKA-SERVER
peer2:7002 
ip: 192.168.1.127进行注册
INFO [Thread-41] 2019-03-11 20:31:42 (EurekaServerInitializerConfiguration.java:72) Started Eureka Server
 INFO [Thread-41] 2019-03-11 20:31:42 (EurekaStateListener.java:37) 注册中心启动,1552307502817
 INFO [Thread-41] 2019-03-11 20:31:42 (EurekaStateListener.java:42) 注册中心服务端启动,1552307502817
 INFO [Eureka-EvictionTimer] 2019-03-11 20:31:45 (AbstractInstanceRegistry.java:1247) Running the evict task with compensationTime 0ms
 INFO [http-nio-7001-exec-7] 2019-03-11 20:31:46 (EurekaStateListener.java:26) 服务CONFIG-SERVER
192.168.1.127:8769 
ip: 192.168.1.127进行注册

你可能感兴趣的:(注册中心Eureka监听服务)