Added support for customizing address for service via addressSource (agent of lookup)

This commit is contained in:
Daniel Hopper
2016-02-05 20:15:04 -05:00
committed by Spencer Gibb
parent 304b913e18
commit b8f4453c1f
3 changed files with 89 additions and 7 deletions

View File

@@ -22,14 +22,13 @@ import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.cloud.util.InetUtils;
import lombok.AccessLevel;
import lombok.Data;
import lombok.Getter;
import lombok.Setter;
import lombok.extern.apachecommons.CommonsLog;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.cloud.util.InetUtils;
/**
* Defines configuration for service discovery and registration.
@@ -38,7 +37,6 @@ import org.springframework.cloud.util.InetUtils;
*/
@ConfigurationProperties("spring.cloud.consul.discovery")
@Data
@CommonsLog
public class ConsulDiscoveryProperties {
protected static final String MANAGEMENT = "management";
@@ -86,7 +84,12 @@ public class ConsulDiscoveryProperties {
* Use ip address rather than hostname during registration
*/
private boolean preferIpAddress = false;
/**
* Source of how we will determine the address to use
*/
private boolean preferAgentAddress = false;
private int catalogServicesWatchDelay = 10;
private int catalogServicesWatchTimeout = 2;
@@ -109,6 +112,7 @@ public class ConsulDiscoveryProperties {
*/
private Map<String, String> serverListQueryTags = new HashMap<>();
@SuppressWarnings("unused")
private ConsulDiscoveryProperties() {}
public ConsulDiscoveryProperties(InetUtils inetUtils) {

View File

@@ -81,7 +81,9 @@ public class ConsulLifecycle extends AbstractDiscoveryLifecycle {
Assert.notNull(service.getPort(), "service.port has not been set");
String appName = getAppName();
service.setId(getServiceId());
service.setAddress(properties.getHostname());
if(!properties.isPreferAgentAddress()) {
service.setAddress(properties.getHostname());
}
service.setName(normalizeForDns(appName));
service.setTags(createTags());

View File

@@ -0,0 +1,76 @@
/*
* Copyright 2013-2015 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 static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotEquals;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertTrue;
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.context.ApplicationContext;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import org.springframework.util.StringUtils;
import com.ecwid.consul.v1.ConsulClient;
import com.ecwid.consul.v1.Response;
import com.ecwid.consul.v1.agent.model.Service;
/**
* @author Spencer Gibb
*/
@RunWith(SpringJUnit4ClassRunner.class)
@FixMethodOrder(MethodSorters.NAME_ASCENDING)
@SpringApplicationConfiguration(classes = TestPropsConfig.class)
@WebIntegrationTest(value = { "spring.application.name=myTestService",
"spring.cloud.consul.discovery.instanceId=myTestService1",
"spring.cloud.consul.discovery.serviceName=myprefix-${spring.application.name}",
"spring.cloud.consul.discovery.preferAgentAddress=true"}, randomPort = true)
public class ConsulLifecycleCustomizedAgentAddressTests {
@Autowired
ConsulLifecycle lifecycle;
@Autowired
ConsulClient consul;
@Autowired
ConsulDiscoveryProperties discoveryProperties;
@Autowired
ApplicationContext context;
@Test
public void contextLoads() {
Response<Map<String, Service>> response = consul.getAgentServices();
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 id was wrong", "myTestService1", service.getId());
assertEquals("service name was wrong", "myprefix-myTestService", service.getService());
assertTrue("service address must be empty", StringUtils.isEmpty(service.getAddress()));
}
}