From 611d2f3e65bc855ba7d25a550c324d64d3c5650c Mon Sep 17 00:00:00 2001 From: Dave Syer Date: Tue, 26 Apr 2016 09:15:14 +0100 Subject: [PATCH] Make discovery client more robust if there is no connection The default behaviour is to know nothing if the client can't connect to the cloud controller. --- .../CloudFoundryDiscoveryClient.java | 23 +++++++++++++++---- .../CloudFoundryDiscoveryClientTest.java | 11 ++++++++- 2 files changed, 29 insertions(+), 5 deletions(-) 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));