diff --git a/spring-cloud-consul-discovery/src/main/java/org/springframework/cloud/consul/discovery/ConsulDiscoveryProperties.java b/spring-cloud-consul-discovery/src/main/java/org/springframework/cloud/consul/discovery/ConsulDiscoveryProperties.java index cf2e621f..88f5969e 100644 --- a/spring-cloud-consul-discovery/src/main/java/org/springframework/cloud/consul/discovery/ConsulDiscoveryProperties.java +++ b/spring-cloud-consul-discovery/src/main/java/org/springframework/cloud/consul/discovery/ConsulDiscoveryProperties.java @@ -120,6 +120,11 @@ public class ConsulDiscoveryProperties { */ private boolean queryPassing = false; + /** + * Register as a service in consul. + */ + private boolean register = true; + @SuppressWarnings("unused") private ConsulDiscoveryProperties() {} diff --git a/spring-cloud-consul-discovery/src/main/java/org/springframework/cloud/consul/discovery/ConsulLifecycle.java b/spring-cloud-consul-discovery/src/main/java/org/springframework/cloud/consul/discovery/ConsulLifecycle.java index ffd14f5f..e97b186c 100644 --- a/spring-cloud-consul-discovery/src/main/java/org/springframework/cloud/consul/discovery/ConsulLifecycle.java +++ b/spring-cloud-consul-discovery/src/main/java/org/springframework/cloud/consul/discovery/ConsulLifecycle.java @@ -78,6 +78,10 @@ public class ConsulLifecycle extends AbstractDiscoveryLifecycle { @Override protected void register() { + if (!this.properties.isRegister()) { + log.debug("Registration disabled."); + return; + } Assert.notNull(service.getPort(), "service.port has not been set"); String appName = getAppName(); service.setId(getServiceId()); @@ -132,6 +136,9 @@ public class ConsulLifecycle extends AbstractDiscoveryLifecycle { @Override protected void registerManagement() { + if (!this.properties.isRegister()) { + return; + } NewService management = new NewService(); management.setId(getManagementServiceId()); management.setAddress(properties.getHostname()); @@ -177,6 +184,9 @@ public class ConsulLifecycle extends AbstractDiscoveryLifecycle { } private void deregister(String serviceId) { + if (!this.properties.isRegister()) { + return; + } if (ttlScheduler != null) { ttlScheduler.remove(serviceId); } diff --git a/spring-cloud-consul-discovery/src/test/java/org/springframework/cloud/consul/discovery/ConsulLifecycleRegistrationDisabledTests.java b/spring-cloud-consul-discovery/src/test/java/org/springframework/cloud/consul/discovery/ConsulLifecycleRegistrationDisabledTests.java new file mode 100644 index 00000000..918be834 --- /dev/null +++ b/spring-cloud-consul-discovery/src/test/java/org/springframework/cloud/consul/discovery/ConsulLifecycleRegistrationDisabledTests.java @@ -0,0 +1,55 @@ +/* + * Copyright 2013-2016 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.consul.discovery; + +import java.util.Map; + +import org.junit.FixMethodOrder; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.junit.runners.MethodSorters; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.boot.test.SpringApplicationConfiguration; +import org.springframework.boot.test.WebIntegrationTest; +import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; + +import com.ecwid.consul.v1.ConsulClient; +import com.ecwid.consul.v1.Response; +import com.ecwid.consul.v1.agent.model.Service; + +import static org.junit.Assert.assertNull; + +/** + * @author Spencer Gibb + */ +@RunWith(SpringJUnit4ClassRunner.class) +@FixMethodOrder(MethodSorters.NAME_ASCENDING) +@SpringApplicationConfiguration(classes = TestPropsConfig.class) +@WebIntegrationTest(value = { "spring.application.name=myTestNotRegisteredService", + "spring.cloud.consul.discovery.register=false"}, randomPort = true) +public class ConsulLifecycleRegistrationDisabledTests { + @Autowired + private ConsulClient consul; + + @Test + public void contextLoads() { + Response> response = consul.getAgentServices(); + Map services = response.getValue(); + Service service = services.get("myTestNotRegisteredService"); + assertNull("service was registered", service); + } +} \ No newline at end of file