Merge pull request #45 from nmarasoiu/publishServletContextAsTag

publish servlet context as tag
This commit is contained in:
Spencer Gibb
2015-06-22 16:34:53 -04:00

View File

@@ -18,6 +18,7 @@ package org.springframework.cloud.consul.discovery;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.client.discovery.AbstractDiscoveryLifecycle;
@@ -25,6 +26,10 @@ import com.ecwid.consul.v1.ConsulClient;
import com.ecwid.consul.v1.agent.model.NewService;
import org.springframework.util.Assert;
import javax.servlet.ServletContext;
import java.util.LinkedList;
import java.util.List;
/**
* @author Spencer Gibb
*/
@@ -43,6 +48,9 @@ public class ConsulLifecycle extends AbstractDiscoveryLifecycle {
@Autowired
private HeartbeatProperties ttlConfig;
@Autowired(required = false)
private ServletContext servletContext;
private NewService service = new NewService();
@Override
@@ -67,7 +75,7 @@ public class ConsulLifecycle extends AbstractDiscoveryLifecycle {
}
service.setId(id);
service.setName(appName);
service.setTags(properties.getTags());
service.setTags(createTags());
NewService.Check check = new NewService.Check();
if (ttlConfig.isEnabled()) {
@@ -121,6 +129,16 @@ public class ConsulLifecycle extends AbstractDiscoveryLifecycle {
deregister(getManagementServiceName());
}
private List<String> createTags() {
List<String> tags = new LinkedList<>(properties.getTags());
if(servletContext != null
&& StringUtils.isNotBlank(servletContext.getContextPath())
&& StringUtils.isNotBlank(servletContext.getContextPath().replaceAll("/", ""))) {
tags.add("contextPath=" + servletContext.getContextPath());
}
return tags;
}
private void deregister(String serviceId) {
if (ttlScheduler != null) {
ttlScheduler.remove(serviceId);