spring-cloud / spring-cloud-consul

Spring Cloud Consul
http://cloud.spring.io/spring-cloud-consul/
Apache License 2.0
808 stars 540 forks source link

Event publisher after registration #758

Open mario45211 opened 2 years ago

mario45211 commented 2 years ago

Is your feature request related to a problem? Please describe. I want to run specific logic after an application register itself in a Consul service registry. I'm implementing an app-level leader election based on the Consul as described in tutorial: https://learn.hashicorp.com/tutorials/consul/application-leader-elections . It would be nice to have dedicated event publisher with a result of Consul registration,

Describe the solution you'd like

public class LeaderElector implements ApplicationListener<ConsulRegistrationCompleted> {
//...
  public void onApplicationEvent(ConsulRegistrationCompleted event){
  //do election
  }
}

Describe alternatives you've considered Currently, I trigger election using

public class LeaderElector implements CommandLineRunner {
//...
}

Alternatively, above could be implemented using ApplicationListener<ApplicationStartedEvent>.

Additional context Above solutions don't give any state of an application's registration and it must be retrieved manually before starting actual logic. Also, using standard event is fragile when Consul's integration is disabled in configuration (e.g. spring.cloud.consul.enabled=false) - implementation become complicated with non-null checks against Consul-related beans. Dedicated event could simplified this case, because it never occur when consul registration is disabled.

Let me know what you think. Cheers!