brooklyn.demo.WebClusterDatabaseExampleApp Maven / Gradle / Ivy
package brooklyn.demo;
import static brooklyn.event.basic.DependentConfiguration.attributeWhenReady;
import static brooklyn.event.basic.DependentConfiguration.formatString;
import java.util.Arrays;
import java.util.List;
import java.util.concurrent.TimeUnit;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import brooklyn.catalog.CatalogConfig;
import brooklyn.config.ConfigKey;
import brooklyn.enricher.HttpLatencyDetector;
import brooklyn.enricher.basic.SensorPropagatingEnricher;
import brooklyn.enricher.basic.SensorTransformingEnricher;
import brooklyn.entity.basic.AbstractApplication;
import brooklyn.entity.basic.Entities;
import brooklyn.entity.basic.StartableApplication;
import brooklyn.entity.database.mysql.MySqlNode;
import brooklyn.entity.group.DynamicCluster;
import brooklyn.entity.java.JavaEntityMethods;
import brooklyn.entity.proxying.EntitySpecs;
import brooklyn.entity.webapp.ControlledDynamicWebAppCluster;
import brooklyn.entity.webapp.DynamicWebAppCluster;
import brooklyn.entity.webapp.JavaWebAppService;
import brooklyn.entity.webapp.WebAppService;
import brooklyn.entity.webapp.WebAppServiceConstants;
import brooklyn.event.AttributeSensor;
import brooklyn.event.basic.BasicAttributeSensor;
import brooklyn.event.basic.BasicConfigKey;
import brooklyn.launcher.BrooklynLauncher;
import brooklyn.location.basic.PortRanges;
import brooklyn.policy.autoscaling.AutoScalerPolicy;
import brooklyn.util.CommandLineUtil;
import com.google.common.base.Functions;
import com.google.common.collect.Lists;
/**
* Launches a 3-tier app with nginx, clustered jboss, and mysql.
*
* Includes some advanced features such as KPI / derived sensors,
* and annotations for use in a catalog.
*
* This variant also increases minimum size to 2.
* Note the policy min size must have the same value,
* otherwise it fights with cluster set up trying to reduce the cluster size!
**/
public class WebClusterDatabaseExampleApp extends AbstractApplication implements StartableApplication {
public static final Logger LOG = LoggerFactory.getLogger(WebClusterDatabaseExampleApp.class);
public static final String DEFAULT_LOCATION = "localhost";
public static final String DEFAULT_WAR_PATH = "classpath://hello-world-sql-webapp.war";
@CatalogConfig(label="WAR (URL)", priority=2)
public static final ConfigKey WAR_PATH = new BasicConfigKey(String.class,
"app.war", "URL to the application archive which should be deployed",
DEFAULT_WAR_PATH);
// TODO to expose in catalog we need to let the keystore url be specified (not hard)
// and also confirm that this works for nginx (might be a bit fiddly);
// booleans in the gui are working (With checkbox)
// @CatalogConfig(label="HTTPS")
public static final ConfigKey USE_HTTPS = new BasicConfigKey(Boolean.class,
"app.https", "Whether the application should use HTTPS only or just HTTP only (default)", false);
public static final String DEFAULT_DB_SETUP_SQL_URL = "classpath://visitors-creation-script.sql";
@CatalogConfig(label="DB Setup SQL (URL)", priority=1)
public static final ConfigKey DB_SETUP_SQL_URL = new BasicConfigKey(String.class,
"app.db_sql", "URL to the SQL script to set up the database",
DEFAULT_DB_SETUP_SQL_URL);
public static final String DB_TABLE = "visitors";
public static final String DB_USERNAME = "brooklyn";
public static final String DB_PASSWORD = "br00k11n";
BasicAttributeSensor APPSERVERS_COUNT = new BasicAttributeSensor(Integer.class,
"appservers.count", "Number of app servers deployed");
public static final AttributeSensor REQUESTS_PER_SECOND_IN_WINDOW =
WebAppServiceConstants.REQUESTS_PER_SECOND_IN_WINDOW;
public static final AttributeSensor ROOT_URL = WebAppServiceConstants.ROOT_URL;
@Override
public void init() {
MySqlNode mysql = addChild(
EntitySpecs.spec(MySqlNode.class)
.configure(MySqlNode.CREATION_SCRIPT_URL, getConfig(DB_SETUP_SQL_URL)));
ControlledDynamicWebAppCluster web = addChild(
EntitySpecs.spec(ControlledDynamicWebAppCluster.class)
.configure(WebAppService.HTTP_PORT, PortRanges.fromString("8080+"))
.configure(JavaWebAppService.ROOT_WAR, getConfig(WAR_PATH))
.configure(JavaEntityMethods.javaSysProp("brooklyn.example.db.url"),
formatString("jdbc:%s%s?user=%s\\&password=%s",
attributeWhenReady(mysql, MySqlNode.MYSQL_URL), DB_TABLE, DB_USERNAME, DB_PASSWORD))
.configure(DynamicCluster.INITIAL_SIZE, 2)
.configure(WebAppService.ENABLED_PROTOCOLS, Arrays.asList(getConfig(USE_HTTPS) ? "https" : "http")) );
web.addEnricher(HttpLatencyDetector.builder().
url(ROOT_URL).
rollup(10, TimeUnit.SECONDS).
build());
web.getCluster().addPolicy(AutoScalerPolicy.builder().
metric(DynamicWebAppCluster.REQUESTS_PER_SECOND_IN_WINDOW_PER_NODE).
metricRange(10, 100).
sizeRange(2, 5).
build());
addEnricher(SensorPropagatingEnricher.newInstanceListeningTo(web,
WebAppServiceConstants.ROOT_URL,
DynamicWebAppCluster.REQUESTS_PER_SECOND_IN_WINDOW,
HttpLatencyDetector.REQUEST_LATENCY_IN_SECONDS_IN_WINDOW));
addEnricher(new SensorTransformingEnricher(web,
DynamicWebAppCluster.GROUP_SIZE, APPSERVERS_COUNT, Functions.identity()));
}
public static void main(String[] argv) {
List args = Lists.newArrayList(argv);
String port = CommandLineUtil.getCommandLineOption(args, "--port", "8081+");
String location = CommandLineUtil.getCommandLineOption(args, "--location", DEFAULT_LOCATION);
BrooklynLauncher launcher = BrooklynLauncher.newInstance()
.application(EntitySpecs.appSpec(WebClusterDatabaseExampleApp.class)
.displayName("Brooklyn WebApp Cluster with Database example"))
.webconsolePort(port)
.location(location)
.start();
Entities.dumpInfo(launcher.getApplications());
}
}