Change ConsulRegistrationCustomizer to take a ConsulRegistration
This commit is contained in:
@@ -22,6 +22,7 @@ import java.util.Collections;
|
||||
import java.util.List;
|
||||
|
||||
import org.springframework.beans.BeansException;
|
||||
import org.springframework.beans.factory.ObjectProvider;
|
||||
import org.springframework.boot.bind.RelaxedPropertyResolver;
|
||||
import org.springframework.cloud.client.discovery.AbstractDiscoveryLifecycle;
|
||||
import org.springframework.cloud.consul.serviceregistry.ConsulAutoRegistration;
|
||||
@@ -59,7 +60,7 @@ public class ConsulLifecycle extends AbstractDiscoveryLifecycle {
|
||||
|
||||
private TtlScheduler ttlScheduler;
|
||||
|
||||
private ServletContext servletContext;
|
||||
private ObjectProvider<ServletContext> servletContext;
|
||||
|
||||
private NewService service = new NewService();
|
||||
|
||||
@@ -76,7 +77,7 @@ public class ConsulLifecycle extends AbstractDiscoveryLifecycle {
|
||||
this.ttlScheduler = ttlScheduler;
|
||||
}
|
||||
|
||||
public void setServletContext(ServletContext servletContext) {
|
||||
public void setServletContext(ObjectProvider<ServletContext> servletContext) {
|
||||
this.servletContext = servletContext;
|
||||
}
|
||||
|
||||
|
||||
@@ -18,6 +18,7 @@ package org.springframework.cloud.consul.discovery;
|
||||
|
||||
import javax.servlet.ServletContext;
|
||||
|
||||
import org.springframework.beans.factory.ObjectProvider;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.boot.autoconfigure.web.ServerProperties;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
@@ -37,7 +38,7 @@ public class TestConsulLifecycleConfiguration {
|
||||
private TtlScheduler ttlScheduler;
|
||||
|
||||
@Autowired(required = false)
|
||||
private ServletContext servletContext;
|
||||
private ObjectProvider<ServletContext> servletContext;
|
||||
|
||||
@Bean
|
||||
public ConsulLifecycle consulLifecycle(ConsulClient consulClient, ConsulDiscoveryProperties discoveryProperties,
|
||||
|
||||
@@ -73,7 +73,7 @@ public class ConsulAutoRegistration extends ConsulRegistration {
|
||||
service.setAddress(properties.getHostname());
|
||||
}
|
||||
service.setName(normalizeForDns(appName));
|
||||
service.setTags(createTags(properties, registrationCustomizers));
|
||||
service.setTags(createTags(properties));
|
||||
|
||||
if (properties.getPort() != null) {
|
||||
service.setPort(properties.getPort());
|
||||
@@ -81,7 +81,17 @@ 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<ConsulRegistrationCustomizer> 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?
|
||||
@@ -97,7 +107,7 @@ public class ConsulAutoRegistration extends ConsulRegistration {
|
||||
service.setAddress(properties.getHostname());
|
||||
}
|
||||
service.setName(normalizeForDns(appName));
|
||||
service.setTags(createTags(properties, registrationCustomizers));
|
||||
service.setTags(createTags(properties));
|
||||
|
||||
// If an alternate external port is specified, register using it instead
|
||||
if (properties.getPort() != null) {
|
||||
@@ -110,7 +120,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) {
|
||||
@@ -173,8 +185,7 @@ public class ConsulAutoRegistration extends ConsulRegistration {
|
||||
return normalized.toString();
|
||||
}
|
||||
|
||||
public static List<String> createTags(ConsulDiscoveryProperties properties,
|
||||
List<ConsulRegistrationCustomizer> registrationCustomizers) {
|
||||
public static List<String> createTags(ConsulDiscoveryProperties properties) {
|
||||
List<String> tags = new LinkedList<>(properties.getTags());
|
||||
|
||||
if (!StringUtils.isEmpty(properties.getInstanceZone())) {
|
||||
@@ -183,11 +194,7 @@ public class ConsulAutoRegistration extends ConsulRegistration {
|
||||
if (!StringUtils.isEmpty(properties.getInstanceGroup())) {
|
||||
tags.add("group=" + properties.getInstanceGroup());
|
||||
}
|
||||
if (registrationCustomizers != null) {
|
||||
for (ConsulRegistrationCustomizer customizer : registrationCustomizers) {
|
||||
customizer.customizeTags(tags);
|
||||
}
|
||||
}
|
||||
|
||||
return tags;
|
||||
}
|
||||
|
||||
|
||||
@@ -16,9 +16,10 @@
|
||||
|
||||
package org.springframework.cloud.consul.serviceregistry;
|
||||
|
||||
import javax.servlet.ServletContext;
|
||||
import java.util.List;
|
||||
|
||||
import javax.servlet.ServletContext;
|
||||
|
||||
import org.springframework.beans.factory.ObjectProvider;
|
||||
import org.springframework.boot.autoconfigure.AutoConfigureAfter;
|
||||
import org.springframework.boot.autoconfigure.condition.ConditionalOnBean;
|
||||
@@ -61,7 +62,7 @@ public class ConsulAutoServiceRegistrationAutoConfiguration {
|
||||
@ConditionalOnClass(ServletContext.class)
|
||||
protected static class ConsulServletConfiguration {
|
||||
@Bean
|
||||
public ConsulRegistrationCustomizer servletConsulCustomizer(final ServletContext servletContext) {
|
||||
public ConsulRegistrationCustomizer servletConsulCustomizer(ObjectProvider<ServletContext> servletContext) {
|
||||
return new ConsulServletRegistrationCustomizer(servletContext);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -16,11 +16,9 @@
|
||||
|
||||
package org.springframework.cloud.consul.serviceregistry;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* @author Piotr Wielgolaski
|
||||
*/
|
||||
public interface ConsulRegistrationCustomizer {
|
||||
void customizeTags(List<String> tags);
|
||||
void customize(ConsulRegistration registration);
|
||||
}
|
||||
|
||||
@@ -17,26 +17,37 @@
|
||||
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 ServletContext servletContext;
|
||||
private ObjectProvider<ServletContext> servletContext;
|
||||
|
||||
public ConsulServletRegistrationCustomizer(ServletContext servletContext) {
|
||||
public ConsulServletRegistrationCustomizer(ObjectProvider<ServletContext> servletContext) {
|
||||
this.servletContext = servletContext;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void customizeTags(List<String> tags) {
|
||||
if(servletContext != null
|
||||
&& StringUtils.hasText(servletContext.getContextPath())
|
||||
&& StringUtils.hasText(servletContext.getContextPath().replaceAll("/", ""))) {
|
||||
tags.add("contextPath=" + servletContext.getContextPath());
|
||||
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<String> tags = registration.getService().getTags();
|
||||
if (tags == null) {
|
||||
tags = new ArrayList<>();
|
||||
}
|
||||
tags.add("contextPath=" + sc.getContextPath());
|
||||
registration.getService().setTags(tags);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -22,10 +22,8 @@ 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.autoconfigure.ImportAutoConfiguration;
|
||||
import org.springframework.boot.test.context.SpringBootTest;
|
||||
import org.springframework.cloud.client.serviceregistry.AutoServiceRegistrationConfiguration;
|
||||
import org.springframework.cloud.consul.ConsulAutoConfiguration;
|
||||
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
import org.springframework.test.context.junit4.SpringRunner;
|
||||
|
||||
@@ -64,9 +62,8 @@ public class ConsulAutoServiceRegistrationCustomizedServletContextTests {
|
||||
assertTrue("service context was wrong", service.getTags().contains("contextPath=/customContext"));
|
||||
}
|
||||
|
||||
|
||||
@EnableDiscoveryClient
|
||||
@Configuration
|
||||
@EnableAutoConfiguration
|
||||
@ImportAutoConfiguration({ AutoServiceRegistrationConfiguration.class, ConsulAutoConfiguration.class, ConsulAutoServiceRegistrationAutoConfiguration.class })
|
||||
public static class TestConfig { }
|
||||
}
|
||||
|
||||
@@ -0,0 +1,67 @@
|
||||
/*
|
||||
* Copyright 2013-2017 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.assertNotNull;
|
||||
import static org.junit.Assert.assertNull;
|
||||
import static org.springframework.boot.test.context.SpringBootTest.WebEnvironment.NONE;
|
||||
|
||||
/**
|
||||
* @author Spencer Gibb
|
||||
*/
|
||||
@RunWith(SpringRunner.class)
|
||||
@SpringBootTest(classes = ConsulAutoServiceRegistrationNonWebTests.TestConfig.class,
|
||||
properties = { "spring.application.name=consulNonWebTest", "server.port=32111" },
|
||||
webEnvironment = NONE)
|
||||
public class ConsulAutoServiceRegistrationNonWebTests {
|
||||
|
||||
@Autowired
|
||||
private ConsulClient consul;
|
||||
|
||||
@Autowired(required = false)
|
||||
private ConsulAutoServiceRegistration autoServiceRegistration;
|
||||
|
||||
@Test
|
||||
public void contextLoads() {
|
||||
assertNotNull("ConsulAutoServiceRegistration was created", autoServiceRegistration);
|
||||
|
||||
Response<Map<String, Service>> response = consul.getAgentServices();
|
||||
Map<String, Service> services = response.getValue();
|
||||
Service service = services.get("consulNonWebTest");
|
||||
assertNull("service was registered", service); //no port to listen, hence no registration
|
||||
}
|
||||
|
||||
@EnableDiscoveryClient
|
||||
@Configuration
|
||||
@EnableAutoConfiguration
|
||||
public static class TestConfig { }
|
||||
}
|
||||
Reference in New Issue
Block a user