Adds event that will be fired before a service is registered. Fixes #302 (#424)

This commit is contained in:
Ryan Baxter
2018-10-15 09:11:16 -04:00
committed by GitHub
parent edd4d0e759
commit e7e391a65a
4 changed files with 107 additions and 1 deletions

View File

@@ -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.

View File

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

View File

@@ -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<R extends Registration>
// 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();

View File

@@ -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<InstancePreRegisteredEvent> {
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<InstanceRegisteredEvent> {
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