Add support for registering using an alternate external port

This commit is contained in:
Daryl Robbins
2016-01-13 09:21:19 -05:00
committed by Spencer Gibb
parent b75baec673
commit ce5dd14f26
4 changed files with 96 additions and 9 deletions

View File

@@ -30,6 +30,8 @@ import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.cloud.util.InetUtils;
/**
* Defines configuration for service discovery and registration.
*
* @author Spencer Gibb
*/
@ConfigurationProperties("spring.cloud.consul.discovery")
@@ -45,24 +47,37 @@ public class ConsulDiscoveryProperties {
private String aclToken;
/** Tags to use when registering service */
private List<String> tags = new ArrayList<>();
/** Is service discovery enabled? */
private boolean enabled = true;
/** Tags to use when registering management service */
private List<String> managementTags = Arrays.asList(MANAGEMENT);
/** Alternate server path to invoke for health checking */
private String healthCheckPath = "/health";
/** Custom health check url to override default */
private String healthCheckUrl;
/** How often to perform the health check (e.g. 10s) */
private String healthCheckInterval = "10s";
/** Timeout for health check (e.g. 10s) */
private String healthCheckTimeout;
/** IP address to use when accessing service (must also set preferIpAddress
to use) */
private String ipAddress;
/** Hostname to use when accessing server */
private String hostname;
/** Port to register the service under (defaults to listening port) */
private Integer port;
private Lifecycle lifecycle = new Lifecycle();
/**
@@ -74,10 +89,13 @@ public class ConsulDiscoveryProperties {
private int catalogServicesWatchTimeout = 2;
/** Unique service instance id */
private String instanceId;
/** Whether to register an http or https service */
private String scheme = "http";
/** Suffix to use when registering management service */
private String managementSuffix = MANAGEMENT;
private ConsulDiscoveryProperties() {}

View File

@@ -50,7 +50,7 @@ public class ConsulLifecycle extends AbstractDiscoveryLifecycle {
@Autowired(required = false)
private ServletContext servletContext;
private NewService service = new NewService();
public ConsulLifecycle(ConsulClient client, ConsulDiscoveryProperties properties, HeartbeatProperties ttlConfig) {
@@ -78,13 +78,18 @@ public class ConsulLifecycle extends AbstractDiscoveryLifecycle {
service.setName(normalizeForDns(appName));
service.setTags(createTags());
Integer port;
if (shouldRegisterManagement()) {
port = getManagementPort();
} else {
port = service.getPort();
// If an alternate external port is specified, register using it instead
if (properties.getPort() != null) {
service.setPort(properties.getPort());
}
service.setCheck(createCheck(port));
Integer checkPort;
if (shouldRegisterManagement()) {
checkPort = getManagementPort();
} else {
checkPort = service.getPort();
}
service.setCheck(createCheck(checkPort));
register(service);
}

View File

@@ -47,7 +47,8 @@ import com.ecwid.consul.v1.agent.model.Service;
@FixMethodOrder(MethodSorters.NAME_ASCENDING)
@SpringApplicationConfiguration(classes = TestPropsConfig.class)
@WebIntegrationTest(value = { "spring.application.name=myTestService",
"spring.cloud.consul.discovery.instanceId=myTestService1" }, randomPort = true)
"spring.cloud.consul.discovery.instanceId=myTestService1",
"spring.cloud.consul.discovery.port=4452"}, randomPort = true)
public class ConsulLifecycleCustomizedPropsTests {
@Autowired
@@ -65,7 +66,7 @@ public class ConsulLifecycleCustomizedPropsTests {
Map<String, Service> services = response.getValue();
Service service = services.get("myTestService1");
assertNotNull("service was null", service);
assertNotEquals("service port is 0", 0, service.getPort().intValue());
assertEquals("service port is discovery port", 4452, service.getPort().intValue());
assertEquals("service id was wrong", "myTestService1", service.getId());
assertEquals("service name was wrong", "myTestService", service.getService());
}

View File

@@ -0,0 +1,63 @@
/*
* 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 com.ecwid.consul.v1.ConsulClient;
import com.ecwid.consul.v1.Response;
import com.ecwid.consul.v1.agent.model.Service;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.SpringApplicationConfiguration;
import org.springframework.boot.test.WebIntegrationTest;
import org.springframework.context.ApplicationContext;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import static org.junit.Assert.assertNotEquals;
import static org.junit.Assert.assertNotNull;
/**
* @author Spencer Gibb
*/
@RunWith(SpringJUnit4ClassRunner.class)
@SpringApplicationConfiguration(classes = TestPropsConfig.class)
@WebIntegrationTest(value = { "spring.application.name=myTestService2",
"spring.cloud.consul.discovery.instanceId=myTestService2", }, randomPort = true)
public class ConsulLifecycleDefaultPortTests {
@Autowired
ConsulLifecycle lifecycle;
@Autowired
ConsulClient consul;
@Autowired
ApplicationContext context;
@Test
public void contextLoads() {
Response<Map<String, Service>> response = consul.getAgentServices();
Map<String, Service> services = response.getValue();
Service service = services.get("myTestService2");
assertNotNull("service was null", service);
assertNotEquals("service port is 0", 0, service.getPort().intValue());
}
}