Elastic Web Cluster

Before You Begin

To use the examples, you'll need curl, git, java (1.6+), and maven (v3) installed.

First, grab a copy of the Brooklyn distribution and set up BROOKLYN_HOME:

% curl -LO http://repo1.maven.org/maven2/io/brooklyn/brooklyn-dist/0.4.0/brooklyn-dist-0.4.0-dist.tar.gz
% tar xvzf brooklyn-dist-0.4.0-dist.tar.gz
% export BROOKLYN_HOME=$(pwd)/brooklyn-0.4.0/

Then, grab a copy of the brooklyn-examples source code and build with Maven:

% git clone https://github.com/brooklyncentral/brooklyn-examples.git
% cd brooklyn-examples
% git checkout 0.4.0
% mvn clean install

For more information on ways to download Brooklyn please see the download page. For more information on the Brooklyn CLI and launching apps, please visit this section of the user guide.

Now, go to this particular example's directory:

% cd simple-web-cluster

The CLI needs to know where to find your compiled examples. You can set this up by exporting the BROOKLYN_CLASSPATH environment variable in the following way:

% export BROOKLYN_CLASSPATH=$(pwd)/target/classes

The project simple-web-cluster includes several deployment descriptors for rolling out a web application, under src/main/java.

Simple Web Server

The simplest of these, SingleWebServerExample, starts JBoss on a single machine with a "Hello World" war deployed, with a single line:

public class SingleWebServerExample extends AbstractApplication {

    JBoss7Server web = new JBoss7Server(this, 
        war: "classpath://hello-world-webapp.war", 
        httpPort: 8080)
        
    // other housekeeping removed, including public static void main
    // you'll find this in the github repo code mentioned above
     
}

You can run this as follows (on *nix or Mac, assuming ssh localhost requires no password or passphrase):

% ${BROOKLYN_HOME}/bin/brooklyn launch --app brooklyn.demo.SingleWebServerExample \
  --location localhost

Then visit the webapp on port 8080, or the Brooklyn console on 8081. (Default credentials are admin/password.) Note that the installation may take some time, because the default deployment downloads the software from the official repos. You can monitor start-up activity for each entity in the Activity pane in the management console, and see more detail by tailing the log file (tail -f brooklyn.log).

With appropriate setup (as described here) this can also be deployed to your favourite cloud, let's pretend it's Amazon Ireland, as follows:

% ${BROOKLYN_HOME}/bin/brooklyn launch --app brooklyn.demo.SingleWebServerExample \
  --location aws-ec2:eu-west-1

Elastic Three-Tier

Ready for something more interesting? Try this:

% ${BROOKLYN_HOME}/bin/brooklyn launch --app brooklyn.demo.WebClusterDatabaseExample \
  --location localhost

This launches the class WebClusterDatabaseExample (also described in the walkthrough) which launches a pool of web-servers -- of size 1 initially, but manually configurable (if you stop the policy first, in the GUI, then use the resize effector) -- with an Nginx load-balancer set up in front of them, and backed by a MySQL database.

The essential code fragment looks like this:

public class WebClusterDatabaseExample extends AbstractApplication {
    
    ControlledDynamicWebAppCluster web = new ControlledDynamicWebAppCluster(this, war: WAR_PATH);
    MySqlNode mysql = new MySqlNode(this, creationScriptUrl: DB_SETUP_SQL_URL);

    {
        web.factory.configure(
            httpPort: "8080+", 
            (UsesJava.JAVA_OPTIONS):
                ["brooklyn.example.db.url": valueWhenAttributeReady(mysql, MySqlNode.MYSQL_URL,
                    { "jdbc:"+it+"visitors?user=${DB_USERNAME}\\&password=${DB_PASSWORD}" }) ]);

        web.cluster.addPolicy(new
            ResizerPolicy(DynamicWebAppCluster.AVERAGE_REQUESTS_PER_SECOND).
                setSizeRange(1, 5).
                setMetricRange(10, 100));
    }
}

You can, of course, try this with your favourite cloud, tweak the database start script, or drop in your favourite WAR.

A Few Other Things

The project includes several variants of the examples shown here. In particular note the pure-Java version in WebClusterDatabaseExampleAltJava.java some other variations in syntax (the *Alt* files), and a web-only cluster (no database) in WebClusterExample.

The webapp that is used is included under examples/hello-world-webapp.

You may wish to check out the Global Web Fabric example next.

If you encounter any difficulties, please tell us and we'll do our best to help.