Be more liberal about not having a local instance

If an app is running locally it can still have a local service
instance, so it shouldn't barf if it can't find itself in
the cloud controller.
This commit is contained in:
Dave Syer
2016-04-21 17:17:52 +01:00
parent 166142e479
commit 0c3ce308c5
2 changed files with 26 additions and 44 deletions

View File

@@ -16,7 +16,6 @@
package org.springframework.cloud.cloudfoundry.discovery;
import java.io.IOException;
import java.net.URL;
import java.util.ArrayList;
import java.util.Collection;
@@ -33,14 +32,11 @@ import org.cloudfoundry.client.lib.domain.CloudApplication;
import org.cloudfoundry.client.lib.domain.InstanceInfo;
import org.cloudfoundry.client.lib.domain.InstanceState;
import org.cloudfoundry.client.lib.domain.InstancesInfo;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.cloud.client.DefaultServiceInstance;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.client.discovery.DiscoveryClient;
import org.springframework.core.env.Environment;
import org.springframework.http.converter.json.Jackson2ObjectMapperBuilder;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
/**
* A Cloud Foundry v2 API-aware implementation of the {@link DiscoveryClient discovery
@@ -52,7 +48,7 @@ import com.fasterxml.jackson.databind.ObjectMapper;
* You need to provide an instance of the {@link CloudFoundryClient}. A workable
* configuration looks like this:
* <p/>
*
*
* <pre class="code">
* &#064;Bean
* CloudFoundryClient cloudFoundryClient(
@@ -84,32 +80,14 @@ public class CloudFoundryDiscoveryClient implements DiscoveryClient {
private static final Log log = LogFactory.getLog(CloudFoundryDiscoveryClient.class);
private final ObjectMapper objectMapper = Jackson2ObjectMapperBuilder.json().build();
private final CloudFoundryClient cloudFoundryClient;
private final String vcapApplicationName;
@Value("${vcap.application.name:${spring.application.name:application}}")
private String vcapApplicationName = "application";
public CloudFoundryDiscoveryClient(CloudFoundryClient cloudFoundryClient,
Environment environment) {
this.cloudFoundryClient = cloudFoundryClient;
String vcapApplication = environment.getProperty("VCAP_APPLICATION");
try {
JsonNode jsonNode = objectMapper.readTree(vcapApplication);
JsonNode appNameNode = jsonNode.get("application_name");
this.vcapApplicationName = appNameNode.toString().replaceAll("\"", "");
log.debug("Current ServiceInstance information...");
log.debug("\tvcapApplicationName: " + this.vcapApplicationName);
}
catch (IOException e) {
throw new RuntimeException(e);
}
}
@Override
@@ -187,7 +165,7 @@ public class CloudFoundryDiscoveryClient implements DiscoveryClient {
private final CloudApplication cloudApplication;
public CloudApplication getCloudApplication() {
return cloudApplication;
return this.cloudApplication;
}
public CloudFoundryServiceInstance(CloudApplication ca) {

View File

@@ -16,6 +16,17 @@
package org.springframework.cloud.cloudfoundry.discovery;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertTrue;
import static org.mockito.BDDMockito.given;
import static org.mockito.Mockito.mock;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.cloudfoundry.client.lib.CloudFoundryClient;
@@ -31,15 +42,6 @@ import org.springframework.cloud.client.ServiceInstance;
import org.springframework.core.env.Environment;
import org.springframework.http.HttpStatus;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import static org.junit.Assert.*;
import static org.mockito.BDDMockito.given;
import static org.mockito.BDDMockito.mock;
/**
* @author <A href="mailto:josh@Joshlong.com">Josh Long</A>
*/
@@ -71,10 +73,10 @@ public class CloudFoundryDiscoveryClientTest {
.willReturn(
"{\"limits\":{\"mem\":1024,\"disk\":1024,\"fds\":16384},\"application_version\":"
+ "\"36eff082-96d6-498f-8214-508fda72ba65\",\"application_name\":\""
+ hiServiceServiceId
+ this.hiServiceServiceId
+ "\",\"application_uris\""
+ ":[\""
+ hiServiceServiceId
+ this.hiServiceServiceId
+ ".cfapps.io\"],\"version\":\"36eff082-96d6-498f-8214-508fda72ba65\",\"name\":"
+ "\"hi-service\",\"space_name\":\"joshlong\",\"space_id\":\"e0cd969c-3461-41ae-abde-4e11bb5acbd1\","
+ "\"uris\":[\"hi-service.cfapps.io\"],\"users\":null,\"application_id\":\"af350f7c-88c4-4e35-a04e-698a1dbc7354\","
@@ -90,9 +92,9 @@ public class CloudFoundryDiscoveryClientTest {
given(this.cloudFoundryClient.getApplications()).willReturn(cloudApplications);
cloudApplication = cloudApplications.get(0);
this.cloudApplication = cloudApplications.get(0);
given(this.cloudFoundryClient.getApplication(this.hiServiceServiceId))
.willReturn(cloudApplication);
.willReturn(this.cloudApplication);
given(this.cloudFoundryClient.getApplication(this.hiServiceServiceId))
.willReturn(this.cloudApplication);
@@ -107,7 +109,7 @@ public class CloudFoundryDiscoveryClientTest {
.willReturn(instancesInfo);
this.cloudFoundryDiscoveryClient = new CloudFoundryDiscoveryClient(
cloudFoundryClient, environment);
this.cloudFoundryClient, environment);
}
@Test
@@ -132,13 +134,15 @@ public class CloudFoundryDiscoveryClientTest {
@Test
public void testLocalServiceInstanceRunning() {
given(this.cloudFoundryClient.getApplication("application"))
.willReturn(this.cloudApplication);
InstanceInfo instanceInfo = mock(InstanceInfo.class);
InstancesInfo instancesInfo = mock(InstancesInfo.class);
given(instancesInfo.getInstances()).willReturn(
Collections.singletonList(instanceInfo));
given(instanceInfo.getState()).willReturn(InstanceState.RUNNING);
given(cloudFoundryClient.getApplicationInstances(this.cloudApplication))
given(this.cloudFoundryClient.getApplicationInstances(this.cloudApplication))
.willReturn(instancesInfo);
ServiceInstance localServiceInstance = this.cloudFoundryDiscoveryClient
@@ -157,7 +161,7 @@ public class CloudFoundryDiscoveryClientTest {
Collections.singletonList(instanceInfo));
given(instanceInfo.getState()).willReturn(InstanceState.CRASHED);
given(cloudFoundryClient.getApplicationInstances(this.cloudApplication))
given(this.cloudFoundryClient.getApplicationInstances(this.cloudApplication))
.willReturn(instancesInfo);
ServiceInstance localServiceInstance = this.cloudFoundryDiscoveryClient
@@ -168,7 +172,7 @@ public class CloudFoundryDiscoveryClientTest {
@Test
public void testLocalServiceInstanceNotFoundg() {
given(cloudFoundryClient.getApplicationInstances(this.cloudApplication))
given(this.cloudFoundryClient.getApplicationInstances(this.cloudApplication))
.willThrow(new CloudFoundryException(HttpStatus.NOT_FOUND));
ServiceInstance localServiceInstance = this.cloudFoundryDiscoveryClient