17

I have an Eureka server running on a local machine. Eureka clients are registered to this server. I am able to view the eureka dashboard to see the instances registered.

Can I have an event listener on the server side that will be triggered when instances are registered or cancelled?

I went through the source code and figured out that eureka dispatches the following 3 events from springs ApplicationContext

  1. EurekaInstanceRenewedEvent
  2. EurekaInstanceRegisteredEvent
  3. EurekaInstanceCancelledEvent

Of these only the EurekaInstanceRenewedEvent works and the listener is triggered when the server receives a heartbeat. I am not able to listen to the other events when a new instance is registered or canceled from eureka.

Any help will be appreciated.

raiyan
  • 821
  • 6
  • 15
  • What version are you using? – spencergibb Oct 03 '16 at 17:31
  • I am using spring cloud dependencies Camden.RC1 release. It uses spring-cloud-netflix-eureka-server v1.2.0.RC1 and eureka core v1.4.11 – raiyan Oct 04 '16 at 04:39
  • Camden.RELEASE is the latest, though I doubt that will change much. – spencergibb Oct 04 '16 at 09:36
  • @spencergibb I tried older version. I had the same issue. Debugged a little and found the problem. I created an issue on [spring-cloud git](https://github.com/spring-cloud/spring-cloud-netflix/issues/1376) – raiyan Oct 04 '16 at 16:04
  • I made some experiments, and it turned out that (in my opinion) there is a missing *register* method implementation in org.springframework.cloud.netflix.eureka.server.InstanceRegistry. InstanceRegistry class extends PeerAwareInstanceRegistryImpl which has *register(final InstanceInfo info, final boolean isReplication)* which is not overridden in InstanceRegistry, but is being called by addInstance method from com.netflix.eureka.resources.ApplicationResource. I haven't analyzed cancelling operation but I think the problem might be similar. I can help with fixing it if needed. – bart.s Oct 05 '16 at 20:48
  • Similar problem regards EurekaInstanceCancelledEvent. *evict(long additionalLeaseMs)* method from AbstractInstanceRegistry calls *internalCancel* method (not *cancel*) which is also not overriden in *InstanceRegistry*. – bart.s Oct 05 '16 at 21:18
  • Yes, I had to override the `instanceRegistry` bean with my own implementation. With the existing spring code, cancel due to heartbeat failure will not be replicated on to the peer eureka nodes too. – raiyan Oct 06 '16 at 04:32

1 Answers1

0

you can implement an event listener for Eureka server events

  1. Create an event listener class: Create a class that implements the ApplicationListener interface and specifies the event type you want to listen for. For example, if you want to listen for registration events, you can implement ApplicationListener < EurekaInstanceRegisteredEvent> .

  2. Implement the event handling logic: Override the onApplicationEvent method in your listener class to handle the event. This method will be called when the specified event occurs. You can access relevant information from the event object and perform any desired actions, such as logging, updating a database, or notifying other components.

Ranjan
  • 74
  • 6