From f702a7bc6774a85aba21253c4d75204112b6ad5e Mon Sep 17 00:00:00 2001 From: Dave Syer Date: Thu, 2 Oct 2014 11:51:02 +0100 Subject: [PATCH] Sync docs from master to gh-pages --- spring-cloud.html | 147 ++++++++++++++++++++++++++++++++++++++++++---- 1 file changed, 136 insertions(+), 11 deletions(-) diff --git a/spring-cloud.html b/spring-cloud.html index 9c220a5..395f350 100644 --- a/spring-cloud.html +++ b/spring-cloud.html @@ -1033,6 +1033,35 @@ bootstrap context (via spring.factories). You could use this to insert additional properties from a different server, or from a database, for instance.

+
+

As an example, consider the following trivial custom locator:

+
+
+
+
@Configuration
+public class CustomPropertySourceLocator implements PropertySourceLocator {
+
+    @Override
+    public PropertySource<?> locate() {
+        return new MapPropertySource("customProperty",
+                Collections.<String, Object>singletonMap("property.from.sample.custom.source", "worked as intended"));
+    }
+}
+
+
+
+

If you create a jar with this class in it and then add a +META-INF/spring.factories containing:

+
+
+
+
org.springframework.cloud.bootstrap.BootstrapConfiguration=sample.custom.CustomPropertySourceLocator
+
+
+
+

then the "customProperty" PropertySource will show up in any +application that includes that jar on its classpath.

+
@@ -1062,14 +1091,14 @@ Intelligent Routing (Zuul) and Client Side Load Balancing (Ribbon). @RestController public class Application { - @RequestMapping("/") - public String home() { - return "Hello world"; - } + @RequestMapping("/") + public String home() { + return "Hello world"; + } - public static void main(String[] args) { - new SpringApplicationBuilder(Application.class).web(true).run(args); - } + public static void main(String[] args) { + new SpringApplicationBuilder(Application.class).web(true).run(args); + } } @@ -1105,9 +1134,9 @@ public class Application { @EnableEurekaServer public class Application { - public static void main(String[] args) { - new SpringApplicationBuilder(Application.class).web(true).run(args); - } + public static void main(String[] args) { + new SpringApplicationBuilder(Application.class).web(true).run(args); + } } @@ -1124,7 +1153,35 @@ normal Eureka functionality under /v2/*.

Circuit Breaker: Hystrix Clients

+
+

Example boot app:

+
+
+
+
@Configuration
+@EnableAutoConfiguration
+@EnableHystrix
+public class Application {
 
+    public static void main(String[] args) {
+        new SpringApplicationBuilder(Application.class).web(true).run(args);
+    }
+
+}
+
+@Component
+public class StoreIntegration {
+    @HystrixCommand(fallbackMethod = "defaultStores")
+    public Object getStores(Map<String, Object> parameters) {
+        //do stuff that might fail
+    }
+
+    public Object defaultStores(Map<String, Object> parameters) {
+        return /* something useful */;
+    }
+}
+
+
@@ -1139,13 +1196,81 @@ normal Eureka functionality under /v2/*.

Declarative REST Client: Feign

+
+

Example spring boot app

+
+
+
+
@Configuration
+@ComponentScan
+@EnableAutoConfiguration
+@EnableEurekaClient
+public class Application extends FeignConfigurer {
+    @Bean
+    public StoreClient storeClient() {
+        //loadBalance plugs Feign into ribbon.  feign() works without load balancing.
+        return loadBalance(StoreClient.class, "http://stores");
+    }
 
+    public static void main(String[] args) {
+        SpringApplication.run(Application.class, args);
+    }
+
+}
+
+
+
+

StoreClient.java

+
+
+
+
public interface StoreClient {
+    @RequestMapping(method = RequestMethod.GET, value = "/stores")
+    Stores getStores();
+
+    @RequestMapping(method = RequestMethod.POST, value = "/stores/{storeId}", consumes = "application/json")
+    Store update(@PathParameter("storeId") Long storeId, Store store);
+}
+
+

Client Side Load Balancer: Ribbon

+
+

Usage of LoadBalancerClient directly:

+
+
+
+
public class MyClass {
+    @Autowired
+    private LoadBalancerClient loadBalancer;
 
+    public void doStuff() {
+        ServiceInstance instance = loadBalancer.choose("stores");
+        URI storesUri = URI.create(String.format("http://%s:%s", instance.getHost(), instance.getPort()));
+        // ... do something with the URI
+    }
+}
+
+
+
+

Indirect usage via RestTemplate.

+
+
+
+
public class MyClass {
+    @Autowired
+    private RestTemplate restTemplate;
+
+    public String doOtherStuff() {
+        String results = restTemplate.getForObject("http://stores/stores", String.class);
+        return results;
+    }
+}
+
+
@@ -1272,7 +1397,7 @@ with Eureka will also become a Cloud Foundry service.