Elastic Web Cluster

Before You Begin

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

Installing Brooklyn

(If you followed the Getting Started instructions, you can skip to Installing the Examples.)

Grab a copy of the Brooklyn distribution and set up BROOKLYN_HOME:

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

Installing the Examples

Grab a copy of the brooklyn-examples source code and build it with Maven:

% git clone https://github.com/brooklyncentral/brooklyn-examples.git
% cd brooklyn-examples
% git checkout 0.6.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.

Simple Web Server

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.

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 {
    private static final String WAR_PATH = "classpath://hello-world-webapp.war";

    @Override
    public void init() {
        addChild(EntitySpec.create(JBoss7Server.class)
                .configure("war", WAR_PATH)
                .configure("httpPort", 8080));
    }
}

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 localhost:8081. 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 {
    public static final String WAR_PATH = "classpath://hello-world-sql-webapp.war";
    
    public static final String DB_SETUP_SQL_URL = "classpath://visitors-creation-script.sql";
    
    public static final String DB_TABLE = "visitors";
    public static final String DB_USERNAME = "brooklyn";
    public static final String DB_PASSWORD = "br00k11n";

    @Override
    public void init() {
        MySqlNode mysql = addChild(EntitySpec.create(MySqlNode.class)
                .configure("creationScriptUrl", DB_SETUP_SQL_URL));
        
        ControlledDynamicWebAppCluster web = addChild(EntitySpec.create(ControlledDynamicWebAppCluster.class)
                .configure("memberSpec", EntitySpec.create(JBoss7Server.class)
                        .configure("httpPort", "8080+")
                        .configure("war", WAR_PATH)
                        .configure(javaSysProp("brooklyn.example.db.url"), 
                                formatString("jdbc:%s%s?user=%s\\&password=%s", 
                                        attributeWhenReady(mysql, MySqlNode.MYSQL_URL), DB_TABLE, DB_USERNAME, DB_PASSWORD))));
        
        web.getCluster().addPolicy(AutoScalerPolicy.builder().
                        metric(DynamicWebAppCluster.AVERAGE_REQUESTS_PER_SECOND).
                        sizeRange(1, 5).
                        metricRange(10, 100).
                        build());
    }
}

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 variants of the examples shown here, including alternative 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.