Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
First stab at pure-java brooklyn application syntax
package brooklyn.demo;
import static brooklyn.event.basic.DependentConfiguration.valueWhenAttributeReady;
import brooklyn.entity.basic.AbstractApplication;
import brooklyn.entity.database.mysql.MySqlNode;
import brooklyn.entity.proxy.nginx.NginxController;
import brooklyn.entity.webapp.ControlledDynamicWebAppCluster;
import brooklyn.entity.webapp.DynamicWebAppCluster;
import brooklyn.entity.webapp.jboss.JBoss7Server;
import brooklyn.policy.autoscale.AutoScalerPolicy;
/**
* Launches a 3-tier app with nginx, clustered jboss, and mysql.
*
* v2 of gist: uses "proper builders", and not factory, rather than builder-style methods on actual object...
**/
public class WebClusterDatabaseExamplePureJava extends AbstractApplication {
public static final String WAR_PATH = "classpath://hello-world-sql-webapp.war";
public static final String DB_USERNAME = "brooklyn";
public static final String DB_PASSWORD = "br00k11n";
// "jdbc:mysql://localhost/visitors?user=brooklyn&password=br00k11n"
public static final String DB_URL_FORMAT = "jdbc:%s/visitors?user="+DB_USERNAME+"&password="+DB_PASSWORD;
public static final String DB_SETUP_SQL =
"create database visitors;"+"\n"+
"use visitors; "+"\n"+
"create user '${DB_USERNAME}' identified by '${DB_PASSWORD}';"+"\n"+
"grant usage on *.* to '${DB_USERNAME}'@'%' identified by '${DB_PASSWORD}';"+"\n"+
"# ''@localhost is sometimes set up, overriding brooklyn@'%', so do a second explicit grant"+"\n"+
"grant usage on *.* to '${DB_USERNAME}'@'localhost' identified by '${DB_PASSWORD}';"+"\n"+
"grant all privileges on visitors.* to '${DB_USERNAME}'@'%';"+"\n"+
"flush privileges;"+"\n"+
"\n"+
"CREATE TABLE MESSAGES ("+"\n"+
" id INT NOT NULL AUTO_INCREMENT,"+"\n"+
" NAME VARCHAR(30) NOT NULL,"+"\n"+
" MESSAGE VARCHAR(400) NOT NULL,"+"\n"+
" PRIMARY KEY (ID)"+"\n"+
" );"+"\n"+
"\n"+
"INSERT INTO MESSAGES values (default, 'Isaac Asimov', 'I grew up in Brooklyn' );";
public void init() {
displayName("Brooklyn WebApp Cluster with Database example");
MySqlNode mysql = MySqlNode.builder()
.creationScriptContents(DB_SETUP_SQL)
.build();
addChild(mysql);
addChild(ControlledDynamicWebAppCluster.builder()
.controller(NginxController.builder()
.port(8080)
.build())
.appServerBuilder(JBoss7Server.builder()
.httpPort("8000+")
.rootWar(WAR_PATH)
.javaOption("brooklyn.example.db.url", valueWhenAttributeReady(mysql,
MySqlNode.MYSQL_URL, PostProcessors.stringFormat(DB_URL_FORMAT))))
.policy(AutoScalerPolicy.builder()
.trigger(DynamicWebAppCluster.AVERAGE_REQUESTS_PER_SECOND)
.sizeRange(1, 5)
.metricRange(10, 100)
.build())
.build());
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment