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 new file mode 100644 index 00000000..e69de29b diff --git a/spring-cloud-consul-discovery/src/main/java/org/springframework/cloud/consul/discovery/TestConsulLifecycleConfiguration.java b/spring-cloud-consul-discovery/src/main/java/org/springframework/cloud/consul/discovery/TestConsulLifecycleConfiguration.java new file mode 100644 index 00000000..e69de29b diff --git a/spring-cloud-consul-discovery/src/main/java/org/springframework/cloud/consul/serviceregistry/ConsulAutoRegistration.java b/spring-cloud-consul-discovery/src/main/java/org/springframework/cloud/consul/serviceregistry/ConsulAutoRegistration.java index 22176742..4ecf4a6e 100644 --- a/spring-cloud-consul-discovery/src/main/java/org/springframework/cloud/consul/serviceregistry/ConsulAutoRegistration.java +++ b/spring-cloud-consul-discovery/src/main/java/org/springframework/cloud/consul/serviceregistry/ConsulAutoRegistration.java @@ -19,8 +19,6 @@ package org.springframework.cloud.consul.serviceregistry; import java.util.LinkedList; import java.util.List; -import javax.servlet.ServletContext; - import org.springframework.cloud.client.discovery.ManagementServerPortUtils; import org.springframework.cloud.client.serviceregistry.ServiceRegistry; import org.springframework.cloud.consul.discovery.ConsulDiscoveryProperties; @@ -64,7 +62,8 @@ public class ConsulAutoRegistration extends ConsulRegistration { } public static ConsulAutoRegistration registration(ConsulDiscoveryProperties properties, ApplicationContext context, - ServletContext servletContext, HeartbeatProperties heartbeatProperties) { + List registrationCustomizers, + HeartbeatProperties heartbeatProperties) { NewService service = new NewService(); String appName = getAppName(properties, context.getEnvironment()); @@ -73,7 +72,7 @@ public class ConsulAutoRegistration extends ConsulRegistration { service.setAddress(properties.getHostname()); } service.setName(normalizeForDns(appName)); - service.setTags(createTags(properties, servletContext)); + service.setTags(createTags(properties)); if (properties.getPort() != null) { service.setPort(properties.getPort()); @@ -81,12 +80,23 @@ public class ConsulAutoRegistration extends ConsulRegistration { setCheck(service, properties, context, heartbeatProperties); } - return new ConsulAutoRegistration(service, properties, context, heartbeatProperties); + ConsulAutoRegistration registration = new ConsulAutoRegistration(service, properties, context, heartbeatProperties); + customize(registrationCustomizers, registration); + return registration; + } + + public static void customize(List registrationCustomizers, ConsulAutoRegistration registration) { + if (registrationCustomizers != null) { + for (ConsulRegistrationCustomizer customizer : registrationCustomizers) { + customizer.customize(registration); + } + } } @Deprecated //TODO: do I need this here, or should I just copy what I need back into lifecycle? public static ConsulAutoRegistration lifecycleRegistration(Integer port, String instanceId, ConsulDiscoveryProperties properties, ApplicationContext context, - ServletContext servletContext, HeartbeatProperties heartbeatProperties) { + List registrationCustomizers, + HeartbeatProperties heartbeatProperties) { NewService service = new NewService(); String appName = getAppName(properties, context.getEnvironment()); service.setId(instanceId); @@ -94,7 +104,7 @@ public class ConsulAutoRegistration extends ConsulRegistration { service.setAddress(properties.getHostname()); } service.setName(normalizeForDns(appName)); - service.setTags(createTags(properties, servletContext)); + service.setTags(createTags(properties)); // If an alternate external port is specified, register using it instead if (properties.getPort() != null) { @@ -107,7 +117,9 @@ public class ConsulAutoRegistration extends ConsulRegistration { setCheck(service, properties, context, heartbeatProperties); - return new ConsulAutoRegistration(service, properties, context, heartbeatProperties); + ConsulAutoRegistration registration = new ConsulAutoRegistration(service, properties, context, heartbeatProperties); + customize(registrationCustomizers, registration); + return registration; } public static void setCheck(NewService service, ConsulDiscoveryProperties properties, ApplicationContext context, HeartbeatProperties heartbeatProperties) { @@ -169,20 +181,16 @@ public class ConsulAutoRegistration extends ConsulRegistration { return normalized.toString(); } - - public static List createTags(ConsulDiscoveryProperties properties, ServletContext servletContext) { + public static List createTags(ConsulDiscoveryProperties properties) { List tags = new LinkedList<>(properties.getTags()); - if(servletContext != null - && StringUtils.hasText(servletContext.getContextPath()) - && StringUtils.hasText(servletContext.getContextPath().replaceAll("/", ""))) { - tags.add("contextPath=" + servletContext.getContextPath()); - } + if (!StringUtils.isEmpty(properties.getInstanceZone())) { tags.add(properties.getDefaultZoneMetadataName() + "=" + properties.getInstanceZone()); } if (!StringUtils.isEmpty(properties.getInstanceGroup())) { tags.add("group=" + properties.getInstanceGroup()); } + return tags; } diff --git a/spring-cloud-consul-discovery/src/main/java/org/springframework/cloud/consul/serviceregistry/ConsulAutoServiceRegistrationAutoConfiguration.java b/spring-cloud-consul-discovery/src/main/java/org/springframework/cloud/consul/serviceregistry/ConsulAutoServiceRegistrationAutoConfiguration.java index b1eeacf6..8c4bf234 100644 --- a/spring-cloud-consul-discovery/src/main/java/org/springframework/cloud/consul/serviceregistry/ConsulAutoServiceRegistrationAutoConfiguration.java +++ b/spring-cloud-consul-discovery/src/main/java/org/springframework/cloud/consul/serviceregistry/ConsulAutoServiceRegistrationAutoConfiguration.java @@ -16,15 +16,16 @@ package org.springframework.cloud.consul.serviceregistry; +import java.util.List; + import javax.servlet.ServletContext; import org.springframework.beans.factory.ObjectProvider; -import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.autoconfigure.AutoConfigureAfter; import org.springframework.boot.autoconfigure.condition.ConditionalOnBean; +import org.springframework.boot.autoconfigure.condition.ConditionalOnClass; import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean; import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty; -import org.springframework.boot.autoconfigure.web.ServerProperties; import org.springframework.cloud.client.serviceregistry.AutoServiceRegistrationProperties; import org.springframework.cloud.consul.ConditionalOnConsulEnabled; import org.springframework.cloud.consul.discovery.ConsulDiscoveryProperties; @@ -44,9 +45,6 @@ import org.springframework.context.annotation.Configuration; @AutoConfigureAfter(ConsulServiceRegistryAutoConfiguration.class) public class ConsulAutoServiceRegistrationAutoConfiguration { - @Autowired(required = false) - private ServerProperties serverProperties; - @Bean @ConditionalOnMissingBean public ConsulAutoServiceRegistration consulAutoServiceRegistration(ConsulServiceRegistry registry, ConsulDiscoveryProperties properties, ConsulAutoRegistration consulRegistration) { @@ -56,13 +54,18 @@ public class ConsulAutoServiceRegistrationAutoConfiguration { @Bean @ConditionalOnMissingBean public ConsulAutoRegistration consulRegistration(ConsulDiscoveryProperties properties, ApplicationContext applicationContext, - ObjectProvider servletContext, HeartbeatProperties heartbeatProperties) { - ConsulAutoRegistration registration = ConsulAutoRegistration.registration(properties, applicationContext, servletContext.getIfAvailable(), heartbeatProperties); - - if (serverProperties != null && serverProperties.getPort() != null && serverProperties.getPort() > 0) { - registration.initializePort(serverProperties.getPort()); - } - return registration; + ObjectProvider> registrationCustomizers, HeartbeatProperties heartbeatProperties) { + return ConsulAutoRegistration.registration(properties, applicationContext, registrationCustomizers.getIfAvailable(), heartbeatProperties); } + @Configuration + @ConditionalOnClass(ServletContext.class) + protected static class ConsulServletConfiguration { + @Bean + public ConsulRegistrationCustomizer servletConsulCustomizer(ObjectProvider servletContext) { + return new ConsulServletRegistrationCustomizer(servletContext); + } + } + + } diff --git a/spring-cloud-consul-discovery/src/main/java/org/springframework/cloud/consul/serviceregistry/ConsulRegistrationCustomizer.java b/spring-cloud-consul-discovery/src/main/java/org/springframework/cloud/consul/serviceregistry/ConsulRegistrationCustomizer.java new file mode 100644 index 00000000..2496e313 --- /dev/null +++ b/spring-cloud-consul-discovery/src/main/java/org/springframework/cloud/consul/serviceregistry/ConsulRegistrationCustomizer.java @@ -0,0 +1,24 @@ +/* + * 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.serviceregistry; + +/** + * @author Piotr Wielgolaski + */ +public interface ConsulRegistrationCustomizer { + void customize(ConsulRegistration registration); +} diff --git a/spring-cloud-consul-discovery/src/main/java/org/springframework/cloud/consul/serviceregistry/ConsulServletRegistrationCustomizer.java b/spring-cloud-consul-discovery/src/main/java/org/springframework/cloud/consul/serviceregistry/ConsulServletRegistrationCustomizer.java new file mode 100644 index 00000000..416df70d --- /dev/null +++ b/spring-cloud-consul-discovery/src/main/java/org/springframework/cloud/consul/serviceregistry/ConsulServletRegistrationCustomizer.java @@ -0,0 +1,53 @@ +/* + * 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.serviceregistry; + +import javax.servlet.ServletContext; +import java.util.ArrayList; +import java.util.List; + +import org.springframework.beans.factory.ObjectProvider; +import org.springframework.util.StringUtils; + +/** + * @author Piotr Wielgolaski + */ +public class ConsulServletRegistrationCustomizer implements ConsulRegistrationCustomizer { + private ObjectProvider servletContext; + + public ConsulServletRegistrationCustomizer(ObjectProvider servletContext) { + this.servletContext = servletContext; + } + + @Override + public void customize(ConsulRegistration registration) { + if (servletContext == null) { + return; + } + ServletContext sc = servletContext.getIfAvailable(); + if(sc != null + && StringUtils.hasText(sc.getContextPath()) + && StringUtils.hasText(sc.getContextPath().replaceAll("/", ""))) { + List tags = registration.getService().getTags(); + if (tags == null) { + tags = new ArrayList<>(); + } + tags.add("contextPath=" + sc.getContextPath()); + registration.getService().setTags(tags); + } + } +} diff --git a/spring-cloud-consul-discovery/src/test/java/org/springframework/cloud/consul/serviceregistry/ConsulAutoServiceRegistrationCustomizedServletContextTests.java b/spring-cloud-consul-discovery/src/test/java/org/springframework/cloud/consul/serviceregistry/ConsulAutoServiceRegistrationCustomizedServletContextTests.java new file mode 100644 index 00000000..83e7970d --- /dev/null +++ b/spring-cloud-consul-discovery/src/test/java/org/springframework/cloud/consul/serviceregistry/ConsulAutoServiceRegistrationCustomizedServletContextTests.java @@ -0,0 +1,69 @@ +/* + * 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.serviceregistry; + +import java.util.Map; + +import org.junit.Test; +import org.junit.runner.RunWith; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.boot.autoconfigure.EnableAutoConfiguration; +import org.springframework.boot.test.context.SpringBootTest; +import org.springframework.cloud.client.discovery.EnableDiscoveryClient; +import org.springframework.context.annotation.Configuration; +import org.springframework.test.context.junit4.SpringRunner; + +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.assertEquals; +import static org.junit.Assert.assertNotEquals; +import static org.junit.Assert.assertNotNull; +import static org.junit.Assert.assertTrue; +import static org.springframework.boot.test.context.SpringBootTest.WebEnvironment.RANDOM_PORT; + +/** + * @author Piotr Wielgolaski + */ +@RunWith(SpringRunner.class) +@SpringBootTest(classes = ConsulAutoServiceRegistrationCustomizedServletContextTests.TestConfig.class, + properties = { "spring.application.name=myTestService-WithServletContext", + "spring.cloud.consul.discovery.instanceId=myTestService1-WithServletContext", + "server.contextPath=/customContext"}, + webEnvironment = RANDOM_PORT) +public class ConsulAutoServiceRegistrationCustomizedServletContextTests { + + @Autowired + private ConsulClient consul; + + @Test + public void contextLoads() { + Response> response = consul.getAgentServices(); + Map services = response.getValue(); + Service service = services.get("myTestService1-WithServletContext"); + assertNotNull("service was null", service); + assertNotEquals("service port is 0", 0, service.getPort().intValue()); + assertEquals("service id was wrong", "myTestService1-WithServletContext", service.getId()); + assertTrue("service context was wrong", service.getTags().contains("contextPath=/customContext")); + } + + @EnableDiscoveryClient + @Configuration + @EnableAutoConfiguration + public static class TestConfig { } +}