Estimated Time: 25 minutes
- How to protect your application (
greeting-hystrix
) from failures or latency with the circuit breaking pattern - How to publish circuit-breaking metrics from your application (
greeting-hystrix
) - How to consume metric streams with the
hystrix-dashboard
- Start the
config-server
in a terminal window. You may have terminal windows still open from previous labs. They may be reused for this lab.
$ cd $CLOUD_NATIVE_APP_LABS_HOME/config-server
$ mvn clean spring-boot:run
- Start the
service-registry
$ cd $CLOUD_NATIVE_APP_LABS_HOME/service-registry
$ mvn clean spring-boot:run
- Start the
fortune-service
$ cd $CLOUD_NATIVE_APP_LABS_HOME/fortune-service
$ mvn clean spring-boot:run
- Review the
$CLOUD_NATIVE_APP_LABS_HOME/greeting-hystrix/pom.xml
file. By addingspring-cloud-starter-hystrix
to the classpath this application is eligible to use circuit breakers via Hystrix.
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-hystrix</artifactId>
</dependency>
- Review the following file:
$CLOUD_NATIVE_APP_LABS_HOME/greeting-hystrix/src/main/java/io/pivotal/GreetingHystrixApplication.java
. Note the use of the@EnableCircuitBreaker
annotation. This allows the application to create circuit breakers.
@SpringBootApplication
@EnableDiscoveryClient
@EnableCircuitBreaker
public class GreetingHystrixApplication {
public static void main(String[] args) {
SpringApplication.run(GreetingHystrixApplication.class, args);
}
}
3). Review the following file: $CLOUD_NATIVE_APP_LABS_HOME/greeting-hystrix/src/main/java/io/pivotal/fortune/FortuneService.java
. Note the use of the @HystrixCommand
. This is our circuit breaker. If getFortune()
fails, a fallback method defaultFortune
will be invoked.
@Service
public class FortuneService {
Logger logger = LoggerFactory
.getLogger(FortuneService.class);
@Autowired
@LoadBalanced
private RestTemplate restTemplate;
@HystrixCommand(fallbackMethod = "defaultFortune")
public String getFortune() {
String fortune = restTemplate.getForObject("http://fortune-service", String.class);
return fortune;
}
public String defaultFortune(){
logger.debug("Default fortune used.");
return "This fortune is no good. Try another.";
}
}
- Open a new terminal window. Start the
greeting-hystrix
$ cd $CLOUD_NATIVE_APP_LABS_HOME/greeting-hystrix
$ mvn clean spring-boot:run
-
Refresh the
greeting-hystrix
/
endpoint. You should get fortunes from thefortune-service
. -
Stop the
fortune-service
. And refresh thegreeting-hystrix
/
endpoint again. The default fortune is given. -
Restart the
fortune-service
. And refresh thegreeting-hystrix
/
endpoint again. After some time, fortunes from thefortune-service
are back.
What Just Happened?
The circuit breaker tripped because the fortune-service
was not available. This insulates the greeting-hystrix
application so that our users have a better user experience.
Being able to monitor the state of our circuit breakers is highly valuable, but first the greeting-hystrix
application must expose the metrics.
This is accomplished by including the actuator
dependency in the greeting-hystrix
pom.xml
.
- Review the
$CLOUD_NATIVE_APP_LABS_HOME/greeting-hystrix/pom.xml
file. By addingspring-boot-starter-actuator
to the classpath this application will publish metrics at the/hystrix.stream
endpoint.
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
- Browse to http://localhost:8080/hystrix.stream to review the metric stream.
Consuming the metric stream is difficult to interpret on our own. The metric stream can be visualized with the Hystrix Dashboard.
- Review the
$CLOUD_NATIVE_APP_LABS_HOME/hystrix-dashboard/pom.xml
file. By addingspring-cloud-starter-hystrix-dashboard
to the classpath this application is exposes a Hystrix Dashboard.
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-hystrix-dashboard</artifactId>
</dependency>
- Review the following file:
$CLOUD_NATIVE_APP_LABS_HOME/hystrix-dashboard/src/main/java/io/pivotal/HystrixDashboardApplication.java
. Note the use of the@EnableHystrixDashboard
annotation. This creates a Hystrix Dashboard.
@SpringBootApplication
@EnableHystrixDashboard
public class HystrixDashboardApplication {
public static void main(String[] args) {
SpringApplication.run(HystrixDashboardApplication.class, args);
}
}
- Open a new terminal window. Start the
hystrix-dashboard
$ cd $CLOUD_NATIVE_APP_LABS_HOME/hystrix-dashboard
$ mvn clean spring-boot:run
-
Open a browser to http://localhost:8686/hystrix
-
Link the
hystrix-dashboard
to thegreeting-hystrix
app. Enterhttp://localhost:8080/hystrix.stream
as the stream to monitor. -
Experiment! Refresh the
greeting-hystrix
/
endpoint several times. Take down thefortune-service
app. What does the dashboard do? Review the dashboard doc for an explanation on metrics.