Tidy up failure cases of discovery client

This commit is contained in:
Dave Syer
2015-05-21 13:51:58 +01:00
parent e1bdaa7851
commit 24aca83e96
16 changed files with 741 additions and 603 deletions

View File

@@ -16,6 +16,9 @@
package org.springframework.cloud.cloudfoundry.sample;
import java.util.List;
import java.util.Map;
import org.apache.commons.lang.builder.ReflectionToStringBuilder;
import org.apache.commons.lang.builder.ToStringStyle;
import org.apache.commons.logging.Log;
@@ -24,89 +27,105 @@ import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.client.discovery.DiscoveryClient;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.cloud.client.loadbalancer.LoadBalancerClient;
import org.springframework.cloud.cloudfoundry.discovery.CloudFoundryDiscoveryClient;
import org.springframework.cloud.cloudfoundry.discovery.EnableCloudFoundryClient;
import org.springframework.cloud.netflix.feign.EnableFeignClients;
import org.springframework.cloud.netflix.feign.FeignClient;
import org.springframework.context.annotation.Bean;
import org.springframework.core.annotation.Order;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.client.RestTemplate;
import java.util.List;
import java.util.Map;
/**
* This example assumes you've registered an application on <A href="http://cloudfoundry.org">Cloud Foundry</A>
* named {@code hi-service} that responds with a String at {@code /hi/{name}}. There is a sample file in the project
* root called {@code hi-service.groovy} which you can deploy using the {@code spring} CLI and the {@code cf} CLI that
* works appropriately for this demonstration.
* This example assumes you've registered an application on <A
* href="http://cloudfoundry.org">Cloud Foundry</A> named {@code hi-service} that responds
* with a String at {@code /hi/ name} . There is a sample file in the project root called
* {@code hi-service.groovy} which you can deploy using the {@code spring} CLI and the
* {@code cf} CLI that works appropriately for this demonstration.
*
* @author <a href="mailto:josh@joshlong.com">Josh Long</a>
* @author Spencer Gibb
* @author Dave Syer
*/
@SpringBootApplication
@EnableDiscoveryClient
@EnableCloudFoundryClient
@EnableFeignClients
public class DemoApplication {
public static void main(String[] args) {
SpringApplication.run(DemoApplication.class, args);
}
public static void main(String[] args) {
SpringApplication.run(DemoApplication.class, args);
}
private Log log = LogFactory.getLog(getClass());
private Log log = LogFactory.getLog(getClass());
@Bean
CommandLineRunner consume(final LoadBalancerClient loadBalancerClient,
final DiscoveryClient discoveryClient,
final HiServiceClient hiServiceClient,
final RestTemplate restTemplate) {
@Bean
CommandLineRunner consume(final LoadBalancerClient loadBalancerClient,
final CloudFoundryDiscoveryClient discoveryClient, final HiServiceClient hiServiceClient,
final RestTemplate restTemplate) {
return new CommandLineRunner() {
@Override
public void run(String... args) throws Exception {
return new CommandLineRunner() {
@Override
public void run(String... args) throws Exception {
// this demonstrates using the CF/Ribbon-aware RestTemplate interceptor
log.info("=====================================");
log.info("Hi: " + restTemplate.getForEntity("http://hi-service/hi/{name}", String.class, "Josh"));
try {
// this demonstrates using the CF/Ribbon-aware RestTemplate
// interceptor
log.info("=====================================");
log.info("Hi: "
+ restTemplate.getForEntity("http://hi-service/hi/{name}",
String.class, "Josh"));
}
catch (Exception e) {
log.warn("Failed to fetch hi-service", e);
}
// this demonstrates using the Spring Cloud Commons DiscoveryClient abstraction
log.info("=====================================");
for (String svc : discoveryClient.getServices()) {
log.info("service = " + svc);
List<ServiceInstance> instances = discoveryClient.getInstances(svc);
for (ServiceInstance si : instances) {
log.info("\t" + ReflectionToStringBuilder.reflectionToString(si, ToStringStyle.MULTI_LINE_STYLE));
}
}
// this demonstrates using the Spring Cloud Commons DiscoveryClient
// abstraction
log.info("=====================================");
for (String svc : discoveryClient.getServices()) {
log.info("service = " + svc);
List<ServiceInstance> instances = discoveryClient.getInstances(svc);
for (ServiceInstance si : instances) {
log.info("\t"
+ ReflectionToStringBuilder.reflectionToString(si,
ToStringStyle.MULTI_LINE_STYLE));
}
}
log.info("=====================================");
log.info("local: ");
log.info("\t" + ReflectionToStringBuilder.reflectionToString(
discoveryClient.getLocalServiceInstance(), ToStringStyle.MULTI_LINE_STYLE));
log.info("=====================================");
log.info("local: ");
log.info("\t"
+ ReflectionToStringBuilder.reflectionToString(
discoveryClient.getLocalServiceInstance(),
ToStringStyle.MULTI_LINE_STYLE));
// this demonstrates using a CF/Ribbon-aware Feign client
log.info("=====================================");
log.info("Hi:" + hiServiceClient.hi("Josh"));
try {
// this demonstrates using a CF/Ribbon-aware Feign client
log.info("=====================================");
log.info("Hi:" + hiServiceClient.hi("Josh"));
}
catch (Exception e) {
log.warn("Failed to fetch hi-service", e);
}
// this demonstrates using the Spring Cloud Commons LoadBalancerClient
log.info("=====================================");
ServiceInstance choose = loadBalancerClient.choose("hi-service");
log.info("chose: " + '(' + choose.getServiceId() + ") " + choose.getHost() + ':' + choose.getPort());
}
};
}
// this demonstrates using the Spring Cloud Commons LoadBalancerClient
log.info("=====================================");
ServiceInstance choose = loadBalancerClient.choose("hi-service");
if (choose != null) {
log.info("chose: " + '(' + choose.getServiceId() + ") "
+ choose.getHost() + ':' + choose.getPort());
}
}
};
}
}
@FeignClient("hi-service")
interface HiServiceClient {
@RequestMapping(value = "/hi/{name}", method = RequestMethod.GET)
Map<String, Object> hi(@PathVariable("name") String name);
@RequestMapping(value = "/hi/{name}", method = RequestMethod.GET)
Map<String, Object> hi(@PathVariable("name") String name);
}

View File

@@ -1,4 +1,4 @@
spring.application.name=test-app
spring.cloud.cloudfoundry.discovery.email=starbuxman@gmail.com
spring.cloud.cloudfoundry.discovery.password=xxxx
spring.cloud.cloudfoundry.discovery.email=${email:starbuxman@gmail.com}
spring.cloud.cloudfoundry.discovery.password=${password:xxxx}
VCAP_APPLICATION={"limits":{"mem":1024,"disk":1024,"fds":16384},"application_version":"36eff082-96d6-498f-8214-508fda72ba65","application_name":"hi-service","application_uris":["hi-service.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","instance_id":"e4843ca23bd947b28e6d4cb3f9b92cbb","instance_index":0,"host":"0.0.0.0","port":61590,"started_at":"2015-05-07 20:00:10 +0000","started_at_timestamp":1431028810,"start":"2015-05-07 20:00:10 +0000","state_timestamp":1431028810}