diff --git a/spring-cloud-cloudfoundry-discovery/src/main/java/org/springframework/cloud/cloudfoundry/discovery/CloudFoundryDiscoveryClient.java b/spring-cloud-cloudfoundry-discovery/src/main/java/org/springframework/cloud/cloudfoundry/discovery/CloudFoundryDiscoveryClient.java index c620cf5..4e02426 100644 --- a/spring-cloud-cloudfoundry-discovery/src/main/java/org/springframework/cloud/cloudfoundry/discovery/CloudFoundryDiscoveryClient.java +++ b/spring-cloud-cloudfoundry-discovery/src/main/java/org/springframework/cloud/cloudfoundry/discovery/CloudFoundryDiscoveryClient.java @@ -114,9 +114,16 @@ public class CloudFoundryDiscoveryClient implements DiscoveryClient { @Override public List getInstances(String s) { - CloudApplication applications = this.cloudFoundryClient.getApplication(s); - return this.createServiceInstancesFromCloudApplications( - Collections.singletonList(applications)); + try { + CloudApplication applications = this.cloudFoundryClient.getApplication(s); + return this.createServiceInstancesFromCloudApplications( + Collections.singletonList(applications)); + } + catch (Exception e) { + log.warn("Could not get service instances: " + e.getClass() + " (" + + e.getMessage() + ")"); + return Collections.emptyList(); + } } private boolean isRunning(CloudApplication ca) { @@ -136,7 +143,15 @@ public class CloudFoundryDiscoveryClient implements DiscoveryClient { @Override public List getServices() { List services = new ArrayList<>(); - List applications = this.cloudFoundryClient.getApplications(); + List applications; + try { + applications = this.cloudFoundryClient.getApplications(); + } + catch (Exception e) { + log.warn("Could not get applications: " + e.getClass() + " (" + + e.getMessage() + ")"); + applications = Collections.emptyList(); + } Set serviceIds = new HashSet<>(); for (CloudApplication ca : applications) { if (isRunning(ca)) { diff --git a/spring-cloud-cloudfoundry-discovery/src/test/java/org/springframework/cloud/cloudfoundry/discovery/CloudFoundryDiscoveryClientTest.java b/spring-cloud-cloudfoundry-discovery/src/test/java/org/springframework/cloud/cloudfoundry/discovery/CloudFoundryDiscoveryClientTest.java index 62dfdbb..2b1804c 100644 --- a/spring-cloud-cloudfoundry-discovery/src/test/java/org/springframework/cloud/cloudfoundry/discovery/CloudFoundryDiscoveryClientTest.java +++ b/spring-cloud-cloudfoundry-discovery/src/test/java/org/springframework/cloud/cloudfoundry/discovery/CloudFoundryDiscoveryClientTest.java @@ -131,6 +131,15 @@ public class CloudFoundryDiscoveryClientTest { assertEquals(instances.size(), 1); } + @Test + public void testInstancesNotAvailable() { + given(this.cloudFoundryClient.getApplications()).willThrow(new RuntimeException("Planned")); + + List instances = this.cloudFoundryDiscoveryClient + .getInstances(this.hiServiceServiceId); + assertEquals(instances.size(), 0); + } + @Test public void testLocalServiceInstanceRunning() { @@ -170,7 +179,7 @@ public class CloudFoundryDiscoveryClientTest { } @Test - public void testLocalServiceInstanceNotFoundg() { + public void testLocalServiceInstanceNotFound() { given(this.cloudFoundryClient.getApplicationInstances(this.cloudApplication)) .willThrow(new CloudFoundryException(HttpStatus.NOT_FOUND));