Vert.x's Verticle(s) JSON/YAML configuration (preferable per environment)

Viewed 1174

Is there a "reasonable" way to configure deployment options, instances, worker, etc. in Vert.x?

I would like to be able to "get rid" of DeploymentOptions while deploying and have that in a JSON/YAML configuration file that somehow Vert.x understands ― preferably split by "environments", in the same way Spring Boot does.

This is what I'm currently using:

class MainVerticle : AbstractVerticle() {
  private val logger: Logger = LoggerFactory.getLogger(this.javaClass.name)

  override fun start(future: Future<Void>) {
    val config = config().getJsonObject("verticle_instances")
    deploy(AuthVerticle::class.java, DeploymentOptions().setInstances(config.getInteger("auth_instances")))
    deploy(HttpServerVerticle::class.java, DeploymentOptions().setConfig(config().getJsonObject("http_server_verticle"))
        .setInstances(config.getInteger("http_server_instances")))
    deploy(DialPadVerticle::class.java, DeploymentOptions().setConfig(config().getJsonObject("phone_verticle"))
        .setWorker(true))
    logger.info("Module(s) and/or verticle(s) deployment...DONE")
    future.complete()
  }

  override fun stop(future: Future<Void>) {
    logger.debug("Undeploying verticle(s)...DONE")
    logger.info("Application stopped successfully. Enjoy the elevator music while we're offline...")
    future.complete()
  }

  private fun deploy(clazz: Class<out AbstractVerticle>, options: DeploymentOptions) {
    vertx.deployVerticle(clazz.name, options) { handler ->
      if (handler.succeeded()) {
        logger.debug("${clazz.simpleName} started successfully (deployment identifier: ${handler.result()})")
      } else {
        logger.error("${clazz.simpleName} deployment failed due to: ${handler.cause()}")
        //stop();
      }
    }
  }
}

...and config.json:

{
  "verticle_instances": {
    "auth_instances": 3,
    "http_server_instances": 6
  },
  "http_server_verticle": {
    "hostname": "0.0.0.0",
    "port": 9080,
    "cert_path": "server-cert.pem",
    "key_path": "server-key.pem",
    "use_alpn": true,
    "use_ssl": true
  }
}
2 Answers
Related