Skip to content

Commit

Permalink
example webapp showing how to use hystrix-request-servlet and hystrix…
Browse files Browse the repository at this point in the history
…-metrics-event-stream
  • Loading branch information
benjchristensen committed Dec 5, 2012
1 parent e5af4cc commit 67440a4
Show file tree
Hide file tree
Showing 6 changed files with 158 additions and 16 deletions.
10 changes: 10 additions & 0 deletions hystrix-examples-webapp/build.gradle
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
apply plugin: 'java'
apply plugin: 'war'
apply plugin: 'eclipse-wtp'

dependencies {
compile project(':hystrix-core')
compile project(':hystrix-examples')
compile project(':hystrix-contrib:hystrix-request-servlet')
compile project(':hystrix-contrib:hystrix-metrics-event-stream')
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
log4j.rootLogger=INFO, FILE
log4j.appender.FILE=org.apache.log4j.ConsoleAppender
log4j.appender.FILE.layout=org.apache.log4j.PatternLayout
log4j.appender.FILE.layout.ConversionPattern=%d{yyyy-MM-dd HH:mm:ss} %p %C:%L [%C{1}] [%M]: %m%n

log4j.appender.FILE.httpclient=ERROR
39 changes: 39 additions & 0 deletions hystrix-examples-webapp/src/main/webapp/WEB-INF/web.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
version="2.5">

<filter>
<display-name>HystrixRequestContextServletFilter</display-name>
<filter-name>HystrixRequestContextServletFilter</filter-name>
<filter-class>com.netflix.hystrix.contrib.requestservlet.HystrixRequestContextServletFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>HystrixRequestContextServletFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>

<filter>
<display-name>HystrixRequestLogViaResponseHeaderServletFilter</display-name>
<filter-name>HystrixRequestLogViaResponseHeaderServletFilter</filter-name>
<filter-class>com.netflix.hystrix.contrib.requestservlet.HystrixRequestLogViaResponseHeaderServletFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>HystrixRequestLogViaResponseHeaderServletFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>

<servlet>
<description></description>
<display-name>HystrixMetricsStreamServlet</display-name>
<servlet-name>HystrixMetricsStreamServlet</servlet-name>
<servlet-class>com.netflix.hystrix.contrib.metrics.eventstream.HystrixMetricsStreamServlet</servlet-class>
</servlet>

<servlet-mapping>
<servlet-name>HystrixMetricsStreamServlet</servlet-name>
<url-pattern>/hystrix.stream</url-pattern>
</servlet-mapping>

</web-app>
78 changes: 78 additions & 0 deletions hystrix-examples-webapp/src/main/webapp/index.jsp
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>HystrixCommandDemo Execution and HystrixRequestLog [hystrix-examples-webapp]</title>
</head>
<body>
<div style="width:800px;margin:0 auto;">

<center><h2>HystrixCommandDemo Execution and HystrixRequestLog<br>[hystrix-examples-webapp]</h2></center>
<center><img width="264" height="233" src="https://raw.github.com/wiki/Netflix/Hystrix/images/hystrix-logo.png"></center>
<p>
The following is the output of <i>HystrixRequestLog.getCurrentRequest().getExecutedCommandsAsString()</i> after simulating the execution of several commands.
</p>
<p>
The simulation code is in com.netflix.hystrix.examples.demo.HystrixCommandDemo.
</p>
<%@ page import="com.netflix.hystrix.examples.demo.HystrixCommandDemo, com.netflix.hystrix.HystrixRequestLog" %>
<%
new HystrixCommandDemo().executeSimulatedUserRequestForOrderConfirmationAndCreditCardPayment();
%>
<hr>
<b><%= HystrixRequestLog.getCurrentRequest().getExecutedCommandsAsString() %></b>
<hr>
<p>
This request log is also part of the HTTP response header with key name "X-HystrixLog".
</p>
<p>
You can view the realtime stream at <a href="./hystrix.stream">./hystrix.stream</a>.
</p>
<p>
To see the realtime stream change over time execute the following (with correct hostname, port etc) to keep accessing the webapp while watching the stream and you will see the metrics change:
</p>
<pre>
while true ; do curl "http://localhost:8080/hystrix-examples-webapp"; done
</pre>
<p>
The configuration of Hystrix for this functionality is done in web.xml as follows:
</p>
<pre>
&lt;filter&gt;
&lt;display-name&gt;HystrixRequestContextServletFilter&lt;/display-name&gt;
&lt;filter-name&gt;HystrixRequestContextServletFilter&lt;/filter-name&gt;
&lt;filter-class&gt;com.netflix.hystrix.contrib.requestservlet.HystrixRequestContextServletFilter&lt;/filter-class&gt;
&lt;/filter&gt;
&lt;filter-mapping&gt;
&lt;filter-name&gt;HystrixRequestContextServletFilter&lt;/filter-name&gt;
&lt;url-pattern&gt;/*&lt;/url-pattern&gt;
&lt;/filter-mapping&gt;

&lt;filter&gt;
&lt;display-name&gt;HystrixRequestLogViaResponseHeaderServletFilter&lt;/display-name&gt;
&lt;filter-name&gt;HystrixRequestLogViaResponseHeaderServletFilter&lt;/filter-name&gt;
&lt;filter-class&gt;com.netflix.hystrix.contrib.requestservlet.HystrixRequestLogViaResponseHeaderServletFilter&lt;/filter-class&gt;
&lt;/filter&gt;
&lt;filter-mapping&gt;
&lt;filter-name&gt;HystrixRequestLogViaResponseHeaderServletFilter&lt;/filter-name&gt;
&lt;url-pattern&gt;/*&lt;/url-pattern&gt;
&lt;/filter-mapping&gt;

&lt;servlet&gt;
&lt;description&gt;&lt;/description&gt;
&lt;display-name&gt;HystrixMetricsStreamServlet&lt;/display-name&gt;
&lt;servlet-name&gt;HystrixMetricsStreamServlet&lt;/servlet-name&gt;
&lt;servlet-class&gt;com.netflix.hystrix.contrib.metrics.eventstream.HystrixMetricsStreamServlet&lt;/servlet-class&gt;
&lt;/servlet&gt;

&lt;servlet-mapping&gt;
&lt;servlet-name&gt;HystrixMetricsStreamServlet&lt;/servlet-name&gt;
&lt;url-pattern&gt;/hystrix.stream&lt;/url-pattern&gt;
&lt;/servlet-mapping&gt;
</pre>

</div>
</body>
</html>
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@

import java.math.BigDecimal;
import java.net.HttpCookie;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.Future;
import java.util.concurrent.SynchronousQueue;
import java.util.concurrent.ThreadPoolExecutor;
Expand All @@ -35,6 +36,10 @@
public class HystrixCommandDemo {

public static void main(String args[]) {
new HystrixCommandDemo().startDemo();
}

public HystrixCommandDemo() {
/*
* Instead of using injected properties we'll set them via Archaius
* so the rest of the code behaves as it would in a real system
Expand All @@ -45,8 +50,6 @@ public static void main(String args[]) {
ConfigurationManager.getConfigInstance().setProperty("hystrix.command.GetUserAccountCommand.execution.isolation.thread.timeoutInMilliseconds", 50);
// set the rolling percentile more granular so we see data change every second rather than every 10 seconds as is the default
ConfigurationManager.getConfigInstance().setProperty("hystrix.command.default.metrics.rollingPercentile.numBuckets", 60);

new HystrixCommandDemo().startDemo();
}

/*
Expand All @@ -59,29 +62,18 @@ public static void main(String args[]) {
public void startDemo() {
startMetricsMonitor();
while (true) {
executeSimulatedUserRequestForOrderConfirmationAndCreditCardPayment();
runSimulatedRequestOnThread();
}
}

public void executeSimulatedUserRequestForOrderConfirmationAndCreditCardPayment() {
public void runSimulatedRequestOnThread() {
pool.execute(new Runnable() {

@Override
public void run() {
HystrixRequestContext context = HystrixRequestContext.initializeContext();
try {
/* fetch user object with http cookies */
UserAccount user = new GetUserAccountCommand(new HttpCookie("mockKey", "mockValueFromHttpRequest")).execute();

/* fetch the payment information (asynchronously) for the user so the credit card payment can proceed */
Future<PaymentInformation> paymentInformation = new GetPaymentInformationCommand(user).queue();

/* fetch the order we're processing for the user */
int orderIdFromRequestArgument = 13579;
Order previouslySavedOrder = new GetOrderCommand(orderIdFromRequestArgument).execute();

CreditCardCommand credit = new CreditCardCommand(previouslySavedOrder, paymentInformation.get(), new BigDecimal(123.45));
credit.execute();
executeSimulatedUserRequestForOrderConfirmationAndCreditCardPayment();

System.out.println("Request => " + HystrixRequestLog.getCurrentRequest().getExecutedCommandsAsString());
} catch (Exception e) {
Expand All @@ -90,9 +82,25 @@ public void run() {
context.shutdown();
}
}

});
}

public void executeSimulatedUserRequestForOrderConfirmationAndCreditCardPayment() throws InterruptedException, ExecutionException {
/* fetch user object with http cookies */
UserAccount user = new GetUserAccountCommand(new HttpCookie("mockKey", "mockValueFromHttpRequest")).execute();

/* fetch the payment information (asynchronously) for the user so the credit card payment can proceed */
Future<PaymentInformation> paymentInformation = new GetPaymentInformationCommand(user).queue();

/* fetch the order we're processing for the user */
int orderIdFromRequestArgument = 13579;
Order previouslySavedOrder = new GetOrderCommand(orderIdFromRequestArgument).execute();

CreditCardCommand credit = new CreditCardCommand(previouslySavedOrder, paymentInformation.get(), new BigDecimal(123.45));
credit.execute();
}

public void startMetricsMonitor() {
Thread t = new Thread(new Runnable() {

Expand Down
1 change: 1 addition & 0 deletions settings.gradle
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
rootProject.name='hystrix'
include 'hystrix-core', \
'hystrix-examples', \
'hystrix-examples-webapp', \
'hystrix-contrib:hystrix-request-servlet', \
'hystrix-contrib:hystrix-servo-stream', \
'hystrix-contrib:hystrix-servo-metrics-publisher', \
Expand Down

0 comments on commit 67440a4

Please sign in to comment.