Don't throw exception in getLocalServiceInstance

fixes gh-112
This commit is contained in:
Spencer Gibb
2015-11-18 14:06:40 -07:00
parent d447374722
commit 518958e408
2 changed files with 25 additions and 5 deletions

View File

@@ -22,6 +22,7 @@ import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import org.springframework.boot.autoconfigure.web.ServerProperties;
import org.springframework.cloud.client.DefaultServiceInstance;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.client.discovery.DiscoveryClient;
@@ -35,9 +36,12 @@ import com.ecwid.consul.v1.agent.model.Self;
import com.ecwid.consul.v1.agent.model.Service;
import com.ecwid.consul.v1.catalog.model.CatalogService;
import lombok.extern.apachecommons.CommonsLog;
/**
* @author Spencer Gibb
*/
@CommonsLog
public class ConsulDiscoveryClient implements DiscoveryClient {
private ConsulLifecycle lifecycle;
@@ -45,12 +49,15 @@ public class ConsulDiscoveryClient implements DiscoveryClient {
private ConsulClient client;
private ConsulDiscoveryProperties properties;
private ServerProperties serverProperties;
public ConsulDiscoveryClient(ConsulClient client, ConsulLifecycle lifecycle,
ConsulDiscoveryProperties properties) {
ConsulDiscoveryProperties properties,
ServerProperties serverProperties) {
this.client = client;
this.lifecycle = lifecycle;
this.properties = properties;
this.serverProperties = serverProperties;
}
@Override
@@ -62,9 +69,21 @@ public class ConsulDiscoveryClient implements DiscoveryClient {
public ServiceInstance getLocalServiceInstance() {
Response<Map<String, Service>> agentServices = client.getAgentServices();
Service service = agentServices.getValue().get(lifecycle.getServiceId());
String serviceId;
Integer port;
if (service == null) {
throw new IllegalStateException("Unable to locate service in consul agent: "
//possibly called before registration
log.warn("Unable to locate service in consul agent: "
+ lifecycle.getServiceId());
serviceId = lifecycle.getServiceId();
port = lifecycle.getConfiguredPort();
if (port == 0 && serverProperties.getPort() != null) {
port = serverProperties.getPort();
}
} else {
serviceId = service.getId();
port = service.getPort();
}
String host = "localhost";
Response<Self> agentSelf = client.getAgentSelf();
@@ -76,7 +95,7 @@ public class ConsulDiscoveryClient implements DiscoveryClient {
host = member.getName();
}
}
return new DefaultServiceInstance(service.getId(), host, service.getPort(), false);
return new DefaultServiceInstance(serviceId, host, port, false);
}
@Override

View File

@@ -20,6 +20,7 @@ import com.ecwid.consul.v1.ConsulClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.boot.autoconfigure.web.ServerProperties;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.cloud.consul.ConditionalOnConsulEnabled;
import org.springframework.context.annotation.Bean;
@@ -64,8 +65,8 @@ public class ConsulDiscoveryClientConfiguration {
}
@Bean
public ConsulDiscoveryClient consulDiscoveryClient() {
return new ConsulDiscoveryClient(consulClient, consulLifecycle(), consulDiscoveryProperties());
public ConsulDiscoveryClient consulDiscoveryClient(ServerProperties serverProperties) {
return new ConsulDiscoveryClient(consulClient, consulLifecycle(), consulDiscoveryProperties(), serverProperties);
}
@Bean