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 1e2f5fac..90e02e65 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 @@ -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 serverListQueryTags = new HashMap<>(); + @SuppressWarnings("unused") private ConsulDiscoveryProperties() {} public ConsulDiscoveryProperties(InetUtils inetUtils) { 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 8ebc9f61..97d18821 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 @@ -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()); diff --git a/spring-cloud-consul-discovery/src/test/java/org/springframework/cloud/consul/discovery/ConsulLifecycleCustomizedAgentAddressTests.java b/spring-cloud-consul-discovery/src/test/java/org/springframework/cloud/consul/discovery/ConsulLifecycleCustomizedAgentAddressTests.java new file mode 100644 index 00000000..85158426 --- /dev/null +++ b/spring-cloud-consul-discovery/src/test/java/org/springframework/cloud/consul/discovery/ConsulLifecycleCustomizedAgentAddressTests.java @@ -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> response = consul.getAgentServices(); + Map 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())); + } +} \ No newline at end of file