Merge branch 'master' into 2.0.x
This commit is contained in:
@@ -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<ConsulRegistrationCustomizer> 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<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?
|
||||
public static ConsulAutoRegistration lifecycleRegistration(Integer port, String instanceId, ConsulDiscoveryProperties properties, ApplicationContext context,
|
||||
ServletContext servletContext, HeartbeatProperties heartbeatProperties) {
|
||||
List<ConsulRegistrationCustomizer> 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<String> createTags(ConsulDiscoveryProperties properties, ServletContext servletContext) {
|
||||
public static List<String> createTags(ConsulDiscoveryProperties properties) {
|
||||
List<String> 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;
|
||||
}
|
||||
|
||||
|
||||
@@ -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> 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<List<ConsulRegistrationCustomizer>> 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> servletContext) {
|
||||
return new ConsulServletRegistrationCustomizer(servletContext);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
@@ -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);
|
||||
}
|
||||
@@ -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> servletContext;
|
||||
|
||||
public ConsulServletRegistrationCustomizer(ObjectProvider<ServletContext> 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<String> tags = registration.getService().getTags();
|
||||
if (tags == null) {
|
||||
tags = new ArrayList<>();
|
||||
}
|
||||
tags.add("contextPath=" + sc.getContextPath());
|
||||
registration.getService().setTags(tags);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -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<Map<String, Service>> response = consul.getAgentServices();
|
||||
Map<String, Service> 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 { }
|
||||
}
|
||||
Reference in New Issue
Block a user