Allow s.c.c.d.register=false to disable registering.
fixes gh-155
This commit is contained in:
@@ -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() {}
|
||||
|
||||
|
||||
@@ -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);
|
||||
}
|
||||
|
||||
@@ -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<Map<String, Service>> response = consul.getAgentServices();
|
||||
Map<String, Service> services = response.getValue();
|
||||
Service service = services.get("myTestNotRegisteredService");
|
||||
assertNull("service was registered", service);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user