Support war deployments with discovery.

server.port must be set.

fixes gh-173
This commit is contained in:
Spencer Gibb
2016-08-25 14:07:50 -06:00
parent 7aa310c991
commit 7c93f9d0e8
2 changed files with 34 additions and 6 deletions

View File

@@ -28,6 +28,8 @@ import org.springframework.context.annotation.Configuration;
import com.ecwid.consul.v1.ConsulClient;
import javax.servlet.ServletContext;
/**
* @author Spencer Gibb
*/
@@ -43,11 +45,28 @@ public class ConsulDiscoveryClientConfiguration {
@Autowired(required = false)
private ServerProperties serverProperties;
@Autowired(required = false)
private TtlScheduler ttlScheduler;
@Autowired(required = false)
private ServletContext servletContext;
@Bean
@ConditionalOnMissingBean
public ConsulLifecycle consulLifecycle(ConsulDiscoveryProperties discoveryProperties,
HeartbeatProperties heartbeatProperties) {
return new ConsulLifecycle(consulClient, discoveryProperties, heartbeatProperties);
ConsulLifecycle lifecycle = new ConsulLifecycle(consulClient, discoveryProperties, heartbeatProperties);
if (this.ttlScheduler != null) {
lifecycle.setTtlScheduler(this.ttlScheduler);
}
if (this.servletContext != null) {
lifecycle.setServletContext(this.servletContext);
}
if (this.serverProperties != null && this.serverProperties.getPort() != null && this.serverProperties.getPort() > 0) {
// no need to wait for events for this to start since the user has explicitly set the port.
lifecycle.setPort(this.serverProperties.getPort());
}
return lifecycle;
}
@Bean

View File

@@ -21,9 +21,6 @@ import java.util.List;
import javax.servlet.ServletContext;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.client.discovery.AbstractDiscoveryLifecycle;
import org.springframework.retry.annotation.Retryable;
import org.springframework.util.Assert;
@@ -32,6 +29,8 @@ import org.springframework.util.StringUtils;
import com.ecwid.consul.v1.ConsulClient;
import com.ecwid.consul.v1.agent.model.NewService;
import lombok.extern.slf4j.Slf4j;
/**
* @author Spencer Gibb
*/
@@ -46,10 +45,8 @@ public class ConsulLifecycle extends AbstractDiscoveryLifecycle {
private HeartbeatProperties ttlConfig;
@Autowired(required = false)
private TtlScheduler ttlScheduler;
@Autowired(required = false)
private ServletContext servletContext;
private NewService service = new NewService();
@@ -60,6 +57,14 @@ public class ConsulLifecycle extends AbstractDiscoveryLifecycle {
this.ttlConfig = ttlConfig;
}
public void setTtlScheduler(TtlScheduler ttlScheduler) {
this.ttlScheduler = ttlScheduler;
}
public void setServletContext(ServletContext servletContext) {
this.servletContext = servletContext;
}
@Override
protected int getConfiguredPort() {
return service.getPort() == null? 0 : service.getPort();
@@ -70,6 +75,10 @@ public class ConsulLifecycle extends AbstractDiscoveryLifecycle {
service.setPort(port);
}
public void setPort(int port) {
getPort().set(port);
}
@Override
@Retryable(interceptor = "consulRetryInterceptor")
public void start() {