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 92cc86a1..735144c3 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 @@ -196,7 +196,10 @@ public abstract class AbstractAutoServiceRegistration im * Register the local management service with the {@link ServiceRegistry} */ protected void registerManagement() { - this.serviceRegistry.register(getManagementRegistration()); + R registration = getManagementRegistration(); + if (registration != null) { + this.serviceRegistry.register(registration); + } } /** @@ -210,7 +213,10 @@ public abstract class AbstractAutoServiceRegistration im * De-register the local management service with the {@link ServiceRegistry} */ protected void deregisterManagement() { - this.serviceRegistry.deregister(getManagementRegistration()); + R registration = getManagementRegistration(); + if (registration != null) { + this.serviceRegistry.deregister(registration); + } } public void stop() { diff --git a/spring-cloud-commons/src/test/java/org/springframework/cloud/client/loadbalancer/RetryLoadBalancerInterceptorTest.java b/spring-cloud-commons/src/test/java/org/springframework/cloud/client/loadbalancer/RetryLoadBalancerInterceptorTest.java index af508496..351fa65a 100644 --- a/spring-cloud-commons/src/test/java/org/springframework/cloud/client/loadbalancer/RetryLoadBalancerInterceptorTest.java +++ b/spring-cloud-commons/src/test/java/org/springframework/cloud/client/loadbalancer/RetryLoadBalancerInterceptorTest.java @@ -16,7 +16,6 @@ import org.springframework.mock.http.client.MockClientHttpResponse; import org.springframework.retry.policy.NeverRetryPolicy; import org.springframework.retry.support.RetryTemplate; -import static org.bouncycastle.crypto.tls.ConnectionEnd.client; import static org.hamcrest.MatcherAssert.assertThat; import static org.hamcrest.Matchers.is; import static org.mockito.Matchers.any; 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 50f88de1..cd50473c 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 @@ -5,8 +5,9 @@ import java.util.concurrent.atomic.AtomicInteger; import org.junit.Test; import org.junit.runner.RunWith; import org.springframework.beans.factory.annotation.Autowired; -import org.springframework.beans.factory.annotation.Value; +import org.springframework.boot.actuate.autoconfigure.LocalManagementPort; import org.springframework.boot.autoconfigure.EnableAutoConfiguration; +import org.springframework.boot.context.embedded.LocalServerPort; import org.springframework.boot.test.context.SpringBootTest; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; @@ -31,10 +32,10 @@ public class AbstractAutoServiceRegistrationTests { @Autowired private TestAutoServiceRegistration autoRegistration; - @Value("${local.server.port}") + @LocalServerPort private int port; - @Value("${local.management.port}") + @LocalManagementPort private int managementPort; @Test @@ -65,18 +66,35 @@ public class AbstractAutoServiceRegistrationTests { } } + public static class TestMgmtRegistration extends TestRegistration { + @Override + public String getServiceId() { + return "testMgmtRegistration2"; + } + } + public static class TestServiceRegistry implements ServiceRegistry { private boolean registered = false; private boolean deregistered = false; @Override public void register(TestRegistration registration) { - this.registered = true; + if (registration == null) { + throw new NullPointerException(); + } + if (!(registration instanceof TestMgmtRegistration)) { + this.registered = true; + } } @Override public void deregister(TestRegistration registration) { - this.deregistered = true; + if (registration == null) { + throw new NullPointerException(); + } + if (!(registration instanceof TestMgmtRegistration)) { + this.deregistered = true; + } } @Override @@ -129,7 +147,7 @@ public class AbstractAutoServiceRegistrationTests { @Override protected TestRegistration getRegistration() { - return null; + return new TestRegistration(); } @Override