From e7e391a65aa30b7aec8a78b2a0aafee9544eaa6e Mon Sep 17 00:00:00 2001 From: Ryan Baxter Date: Mon, 15 Oct 2018 09:11:16 -0400 Subject: [PATCH] Adds event that will be fired before a service is registered. Fixes #302 (#424) --- .../main/asciidoc/spring-cloud-commons.adoc | 9 ++++ .../event/InstancePreRegisteredEvent.java | 47 +++++++++++++++++ .../AbstractAutoServiceRegistration.java | 2 + .../AbstractAutoServiceRegistrationTests.java | 50 ++++++++++++++++++- 4 files changed, 107 insertions(+), 1 deletion(-) create mode 100644 spring-cloud-commons/src/main/java/org/springframework/cloud/client/discovery/event/InstancePreRegisteredEvent.java diff --git a/docs/src/main/asciidoc/spring-cloud-commons.adoc b/docs/src/main/asciidoc/spring-cloud-commons.adoc index 36e576aa..440af55b 100644 --- a/docs/src/main/asciidoc/spring-cloud-commons.adoc +++ b/docs/src/main/asciidoc/spring-cloud-commons.adoc @@ -298,6 +298,15 @@ To disable that behavior, you can set: * `@EnableDiscoveryClient(autoRegister=false)` to permanently disable auto-registration. * `spring.cloud.service-registry.auto-registration.enabled=false` to disable the behavior through configuration. +===== ServiceRegistry Auto-Registration Events + +There are two events that will be fired when a service auto-registers. The first event, called +`InstancePreRegisteredEvent`, is fired before the service is registered. The second +event, called `InstanceRegisteredEvent`, is fired after the service is registered. You can register an +`ApplicationListener`(s) to listen to and react to these events. + +NOTE: These events will not be fired if `spring.cloud.service-registry.auto-registration.enabled` is set to `false`. + ==== Service Registry Actuator Endpoint Spring Cloud Commons provides a `/service-registry` actuator endpoint. diff --git a/spring-cloud-commons/src/main/java/org/springframework/cloud/client/discovery/event/InstancePreRegisteredEvent.java b/spring-cloud-commons/src/main/java/org/springframework/cloud/client/discovery/event/InstancePreRegisteredEvent.java new file mode 100644 index 00000000..6253c546 --- /dev/null +++ b/spring-cloud-commons/src/main/java/org/springframework/cloud/client/discovery/event/InstancePreRegisteredEvent.java @@ -0,0 +1,47 @@ +/* + * Copyright 2013-2018 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.springframework.cloud.client.discovery.event; + +import org.springframework.cloud.client.serviceregistry.Registration; +import org.springframework.context.ApplicationEvent; + +/** + * An event to fire before a service is registered. + * @author Ryan Baxter + */ +public class InstancePreRegisteredEvent extends ApplicationEvent { + + private Registration registration; + + /** + * Create a new pre registration event. + * + * @param source the object on which the event initially occurred (never {@code null}) + */ + public InstancePreRegisteredEvent(Object source, Registration registration) { + super(source); + this.registration = registration; + } + + /** + * Get the registration data. + * @return the registration data + */ + public Registration getRegistration() { + return registration; + } +} diff --git a/spring-cloud-commons/src/main/java/org/springframework/cloud/client/serviceregistry/AbstractAutoServiceRegistration.java b/spring-cloud-commons/src/main/java/org/springframework/cloud/client/serviceregistry/AbstractAutoServiceRegistration.java index afa67d2d..e3d6a0d5 100644 --- a/spring-cloud-commons/src/main/java/org/springframework/cloud/client/serviceregistry/AbstractAutoServiceRegistration.java +++ b/spring-cloud-commons/src/main/java/org/springframework/cloud/client/serviceregistry/AbstractAutoServiceRegistration.java @@ -6,6 +6,7 @@ import org.springframework.beans.BeansException; import org.springframework.boot.web.context.ConfigurableWebServerApplicationContext; import org.springframework.boot.web.context.WebServerInitializedEvent; import org.springframework.cloud.client.discovery.ManagementServerPortUtils; +import org.springframework.cloud.client.discovery.event.InstancePreRegisteredEvent; import org.springframework.cloud.client.discovery.event.InstanceRegisteredEvent; import org.springframework.context.ApplicationContext; import org.springframework.context.ApplicationContextAware; @@ -105,6 +106,7 @@ public abstract class AbstractAutoServiceRegistration // only initialize if nonSecurePort is greater than 0 and it isn't already running // because of containerPortInitializer below if (!this.running.get()) { + this.context.publishEvent(new InstancePreRegisteredEvent(this, getRegistration())); register(); if (shouldRegisterManagement()) { registerManagement(); diff --git a/spring-cloud-commons/src/test/java/org/springframework/cloud/client/serviceregistry/AbstractAutoServiceRegistrationTests.java b/spring-cloud-commons/src/test/java/org/springframework/cloud/client/serviceregistry/AbstractAutoServiceRegistrationTests.java index 701dbace..92d618cd 100644 --- a/spring-cloud-commons/src/test/java/org/springframework/cloud/client/serviceregistry/AbstractAutoServiceRegistrationTests.java +++ b/spring-cloud-commons/src/test/java/org/springframework/cloud/client/serviceregistry/AbstractAutoServiceRegistrationTests.java @@ -11,8 +11,12 @@ import org.springframework.boot.actuate.autoconfigure.web.server.LocalManagement import org.springframework.boot.autoconfigure.EnableAutoConfiguration; import org.springframework.boot.test.context.SpringBootTest; import org.springframework.boot.web.server.LocalServerPort; +import org.springframework.cloud.client.discovery.event.InstancePreRegisteredEvent; +import org.springframework.cloud.client.discovery.event.InstanceRegisteredEvent; +import org.springframework.context.ApplicationListener; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; +import org.springframework.stereotype.Component; import org.springframework.test.context.junit4.SpringRunner; import static org.hamcrest.Matchers.instanceOf; @@ -34,6 +38,12 @@ public class AbstractAutoServiceRegistrationTests { @Autowired private TestAutoServiceRegistration autoRegistration; + @Autowired + private PreEventListener preEventListener; + + @Autowired + public PostEventListener postEventListener; + @LocalServerPort private int port; @@ -52,6 +62,14 @@ public class AbstractAutoServiceRegistrationTests { assertEquals("Lifecycle appName is wrong", "application", autoRegistration.getAppName()); } + @Test + public void eventsFireTest() { + assertTrue(preEventListener.wasFired); + assertEquals("testRegistration2", preEventListener.registration.getServiceId()); + assertTrue(postEventListener.wasFired); + assertEquals("testRegistration2", postEventListener.config.getServiceId()); + } + @EnableAutoConfiguration @Configuration public static class Config { @@ -59,6 +77,36 @@ public class AbstractAutoServiceRegistrationTests { public TestAutoServiceRegistration testAutoServiceRegistration() { return new TestAutoServiceRegistration(); } + + @Bean + public PreEventListener preRegisterListener() { + return new PreEventListener(); + } + + @Bean + public PostEventListener postEventListener() { + return new PostEventListener(); + } + } + + public static class PreEventListener implements ApplicationListener { + public boolean wasFired = false; + public Registration registration; + @Override + public void onApplicationEvent(InstancePreRegisteredEvent event) { + this.registration = event.getRegistration(); + this.wasFired = true; + } + } + + public static class PostEventListener implements ApplicationListener { + public boolean wasFired = false; + public Registration config; + @Override + public void onApplicationEvent(InstanceRegisteredEvent event) { + this.config = (Registration)event.getConfig(); + this.wasFired = true; + } } public static class TestRegistration implements Registration { @@ -188,7 +236,7 @@ public class AbstractAutoServiceRegistrationTests { @Override protected Object getConfiguration() { - return null; + return getRegistration(); } @Override