Skip to content
This repository has been archived by the owner on Jul 19, 2022. It is now read-only.

Latest commit

 

History

History
153 lines (130 loc) · 7.26 KB

lab06.adoc

File metadata and controls

153 lines (130 loc) · 7.26 KB

Adding Spring Cloud Config to Boot Application

In this lab we’ll utilize Spring Boot and Spring Cloud to make our UI Application more resilient. We’ll leverage Spring Cloud Circuit Breaker to configure our application behavior when our downstream dependencies are not available. Finally, we’ll use the circuit break dashboard to view metrics of the circuit breaker we implemented, which will be auto-provisioned within Cloud Foundry Pivotal Spring Cloud Services.

Define a Circuit Breaker within the UI Application

  1. These features are added by adding spring-cloud-services-starter-circuit-breaker to the classpath. Open you Maven POM found here: /cloud-native-spring-ui/pom.xml. Add the following spring cloud services dependency:

    <dependency>
      <groupId>io.pivotal.spring.cloud</groupId>
    	<artifactId>spring-cloud-services-starter-circuit-breaker</artifactId>
    </dependency>
  2. The first thing we need to add to our application is an @EnableCircuitBreaker annotation to the Spring Boot application. Add this annotation below the other ones on the CloudNativeSpringUIApplication declaration in the class io.pivotal.CloudNativeSpringUIApplication (/cloud-native-spring-ui/src/main/java/io/pivotal/CloudNativeSpringUIApplication.java):

    @SpringBootApplication
    @EnableFeignClients
    @EnableDiscoveryClient
    @EnableCircuitBreaker
    public class CloudNativeSpringUiApplication {
  3. When we introduced an @FeignClient into our application we were only required to provide an interface. We’ll provide a dummy class that implements that interface for our fallback. We’ll also reference that class as a fallback in our FeignClient annotion. First, create this inner class in the CloudNativeSpringUIApplication:

    @Component
    	public class CityClientFallback implements CityClient {
    		@Override
    		public Resources<City> getCities() {
    			//We'll just return an empty response
    			return new Resources(Collections.EMPTY_LIST);
    		}
    	}
  4. Also modify the @FeignClient annotation to reference this class as the fallback in case of failure:

    @FeignClient(name = "cloud-native-spring", fallback = CityClientFallback.class)
    public interface CityClient {
  5. Your Boot Application should now look like this CloudNativeSpringUiApplication:

    package io.pivotal;
    
    import io.pivotal.domain.City;
    import org.springframework.boot.SpringApplication;
    import org.springframework.boot.autoconfigure.SpringBootApplication;
    import org.springframework.cloud.client.circuitbreaker.EnableCircuitBreaker;
    import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
    import org.springframework.cloud.netflix.feign.EnableFeignClients;
    import org.springframework.cloud.netflix.feign.FeignClient;
    import org.springframework.hateoas.Resources;
    import org.springframework.stereotype.Component;
    import org.springframework.web.bind.annotation.RequestMapping;
    import org.springframework.web.bind.annotation.RequestMethod;
    
    import java.util.Collections;
    
    @SpringBootApplication
    @EnableFeignClients
    @EnableDiscoveryClient
    @EnableCircuitBreaker
    public class CloudNativeSpringUiApplication {
    
    	public static void main(String[] args) {
    		SpringApplication.run(CloudNativeSpringUiApplication.class, args);
    	}
    
    	@FeignClient(name = "cloud-native-spring", fallback = CityClientFallback.class)
    	public interface CityClient {
    		@RequestMapping(method= RequestMethod.GET, value="/cities", consumes="application/hal+json")
    		Resources<City> getCities();
    	}
    
    	@Component
    	public class CityClientFallback implements CityClient {
    		@Override
    		public Resources<City> getCities() {
    			//We'll just return an empty response
    			return new Resources(Collections.EMPTY_LIST);
    		}
    	}
    }

Create the Circuit Breaker Dashboard

  1. When we modified our application to use a Hystrix Circuit Breaker our application automatically begins streaming out metrics about the health of our methods wrapped with a HystrixCommand. We can stream these events through a AMQP message bus into Turbine to view on a Circuit Breaker dashboard. This can be done through cloudfoundry using the services marketplace by executing the following command:

    $ cf create-service p-circuit-breaker-dashboard standard circuit-breaker-dashboard
  2. If we view the Circuit Breaker Dashboard (accessible from the manage link in Apps Manager) you will see that a dashboard has been deployed but is empty (You may get an initializing message for a few seconds. This should eventually refresh to a dashboard):

    dash
  3. We will now bind our application to our circuit-breaker-dashboard within our Cloudfoundry deployment manifest. Add these additional reference to a the service to the bottom of /cloud-native-spring-ui/manifest.yml in the services list:

      services:
      - service-registry
      - circuit-breaker-dashboard

Deploy and test application

  1. Build the application

    $ mvn clean package -DskipTests
  2. Push application into Cloud Foundry

    $ cf push -f manifest.yml
  3. Test your application by navigating to the root URL of the application. If the dependent cities REST service is still stopped, you should simply see a blank table. Remember that last time you received a nasty exception in the browser? Now your Circuit Breaker fallback method is automatically called and the fallback behavior is executed.

    empty
  4. From a commandline start the cloud-native-spring microservice (the original city service, not the new UI)

    $ cf start cloud-native-spring
  5. Refresh the UI app and you should once again see a table listing the first page of cities.

    ui
  6. Refresh your UI application a few times to force some traffic though the circuit breaker call path. After doing this you should now see the dashboard populated with metrics about the health of your Hystrix circuit breaker:

    dash1