From 811e53f140231e048f0aa9d18d00bbab731eff02 Mon Sep 17 00:00:00 2001 From: Spencer Gibb Date: Fri, 2 Feb 2018 14:39:51 -0500 Subject: [PATCH] Use property to disable management registration. Fixes https://github.com/spring-cloud/spring-cloud-consul/pull/323 --- .../AbstractAutoServiceRegistration.java | 14 ++ ...oServiceRegistrationMgmtDisabledTests.java | 184 ++++++++++++++++++ .../AbstractAutoServiceRegistrationTests.java | 4 + 3 files changed, 202 insertions(+) create mode 100644 spring-cloud-commons/src/test/java/org/springframework/cloud/client/serviceregistry/AbstractAutoServiceRegistrationMgmtDisabledTests.java 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 ba39e9a2..a4e3f321 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 @@ -15,11 +15,18 @@ import org.springframework.cloud.client.discovery.AbstractDiscoveryLifecycle; public abstract class AbstractAutoServiceRegistration extends AbstractDiscoveryLifecycle implements AutoServiceRegistration { private final ServiceRegistry serviceRegistry; + private AutoServiceRegistrationProperties properties; + @Deprecated protected AbstractAutoServiceRegistration(ServiceRegistry serviceRegistry) { this.serviceRegistry = serviceRegistry; } + protected AbstractAutoServiceRegistration(ServiceRegistry serviceRegistry, AutoServiceRegistrationProperties properties) { + this.serviceRegistry = serviceRegistry; + this.properties = properties; + } + protected ServiceRegistry getServiceRegistry() { return this.serviceRegistry; } @@ -77,4 +84,11 @@ public abstract class AbstractAutoServiceRegistration ex } } + @Override + protected boolean shouldRegisterManagement() { + if (this.properties == null || this.properties.isRegisterManagement()) { + return super.shouldRegisterManagement(); + } + return false; + } } diff --git a/spring-cloud-commons/src/test/java/org/springframework/cloud/client/serviceregistry/AbstractAutoServiceRegistrationMgmtDisabledTests.java b/spring-cloud-commons/src/test/java/org/springframework/cloud/client/serviceregistry/AbstractAutoServiceRegistrationMgmtDisabledTests.java new file mode 100644 index 00000000..702131fc --- /dev/null +++ b/spring-cloud-commons/src/test/java/org/springframework/cloud/client/serviceregistry/AbstractAutoServiceRegistrationMgmtDisabledTests.java @@ -0,0 +1,184 @@ +package org.springframework.cloud.client.serviceregistry; + +import org.assertj.core.api.Assertions; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.boot.autoconfigure.EnableAutoConfiguration; +import org.springframework.boot.test.context.SpringBootTest; +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Configuration; +import org.springframework.test.context.junit4.SpringRunner; + +import java.net.URI; +import java.util.Map; +import java.util.concurrent.atomic.AtomicInteger; + +import static org.springframework.boot.test.context.SpringBootTest.WebEnvironment.RANDOM_PORT; + +/** + * @author Spencer Gibb + */ +@RunWith(SpringRunner.class) +@SpringBootTest(classes = AbstractAutoServiceRegistrationMgmtDisabledTests.Config.class, + properties = {"management.port=0", "spring.cloud.service-registry.auto-registration.register-management=false"}, + webEnvironment = RANDOM_PORT) +public class AbstractAutoServiceRegistrationMgmtDisabledTests { + + @Autowired + private TestAutoServiceRegistration autoRegistration; + + @Test + public void portsWork() { + Assertions.assertThat(autoRegistration.shouldRegisterManagement()).isFalse(); + } + + @EnableAutoConfiguration + @Configuration + public static class Config { + @Bean + public TestAutoServiceRegistration testAutoServiceRegistration(AutoServiceRegistrationProperties properties) { + return new TestAutoServiceRegistration(properties); + } + } + + public static class TestRegistration implements Registration { + @Override + public String getServiceId() { + return "testRegistration3"; + } + + @Override + public String getHost() { + return null; + } + + @Override + public int getPort() { + return 0; + } + + @Override + public boolean isSecure() { + return false; + } + + @Override + public URI getUri() { + return null; + } + + @Override + public Map getMetadata() { + return null; + } + } + + public static class TestMgmtRegistration extends TestRegistration { + @Override + public String getServiceId() { + return "testMgmtRegistration3"; + } + } + + public static class TestServiceRegistry implements ServiceRegistry { + private boolean registered = false; + private boolean deregistered = false; + + @Override + public void register(TestRegistration registration) { + if (registration == null) { + throw new NullPointerException(); + } + if (!(registration instanceof TestMgmtRegistration)) { + this.registered = true; + } + } + + @Override + public void deregister(TestRegistration registration) { + if (registration == null) { + throw new NullPointerException(); + } + if (!(registration instanceof TestMgmtRegistration)) { + this.deregistered = true; + } + } + + @Override + public void close() { } + + @Override + public void setStatus(TestRegistration registration, String status) { + //TODO: test setStatus + } + + @Override + public Object getStatus(TestRegistration registration) { + //TODO: test getStatus + return null; + } + + boolean isRegistered() { + return registered; + } + + boolean isDeregistered() { + return deregistered; + } + } + + public static class TestAutoServiceRegistration extends AbstractAutoServiceRegistration { + private int port = 0; + + public TestAutoServiceRegistration(AutoServiceRegistrationProperties properties) { + super(new TestServiceRegistry(), properties); + } + + @Override + protected AtomicInteger getPort() { + return super.getPort(); + } + + @Override + protected String getAppName() { + return super.getAppName(); + } + + protected TestAutoServiceRegistration() { + super(new TestServiceRegistry()); + } + + @Override + protected int getConfiguredPort() { + return port; + } + + @Override + protected void setConfiguredPort(int port) { + this.port = port; + } + + @Override + protected TestRegistration getRegistration() { + return new TestRegistration(); + } + + @Override + protected TestRegistration getManagementRegistration() { + return null; + } + + @Override + protected Object getConfiguration() { + return null; + } + + @Override + protected boolean isEnabled() { + return true; + } + + + } +} 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 4350f9ab..629eae86 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 @@ -150,6 +150,10 @@ public class AbstractAutoServiceRegistrationTests { public static class TestAutoServiceRegistration extends AbstractAutoServiceRegistration { private int port = 0; + public TestAutoServiceRegistration(AutoServiceRegistrationProperties properties) { + super(null, properties); + } + @Override protected AtomicInteger getPort() { return super.getPort();