Vert.x Verticle (s) JSON / YAML config (preferred for every environment)

Whether there is a "reasonable" way to customize deployment options

, instances

, worker

etc. in Vert.x?

I would like to be able to "get rid" of DeploymentOptions

during deployment and have this in a JSON / YAML config file that Vert.x understands somehow - preferably split into "environments" in the same way Spring Boot is done.

This is what I am 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
  }
}

      

+3


source to share


2 answers


I found Vert.x Config Launcher , but still, I think this should be the "core" functionality.

In any case, it DeploymentOptions

accepts JsonObject

... and it is neat. The only caveat is to make sure that the keys in the JSON configuration file (not including the ones inside config

, of course) match the values ​​that the fromJson

( DeploymentOptionsConverter

) method "expects".

A "partial" solution would be to have config.json

both:

{
  "io.shido.core.verticle.AuthVerticle": { },
  "io.shido.core.verticle.DialPadVerticle": { },
  "io.shido.core.verticle.HttpServerVerticle": {
    "config": {
      "hostname": "0.0.0.0",
      "port": 9081,
      "certPath": "server-cert.pem",
      "keyPath": "server-key.pem",
      "useAlpn": true,
      "useSsl": true
    },
    "instances": 5
  }
}

      



... is pretty similar to a subset of what it foreksorg/vertx-config-launcher

does - since I'm only interested in the inset config.

In the end, the method (originally posted) deploy

will look like this:

private fun deploy(clazz: Class<out AbstractVerticle>) {
  vertx.deployVerticle(clazz.name, DeploymentOptions(config().getJsonObject(clazz.name) ?: JsonObject())) { 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();
    }
  }
}

      

0


source


As far as I know, no. However, you can make some adjustments to config.json

your method and yours deploy(Class, DeploymentOptions)

to achieve a similar result.

For config.json

, if you change the name of each vertice to a qualified class name and have an object deployment_options

for each vertex, you can then change deploy()

to load the parameters without specifying them in your start

. In Vert.x, you can provide default configuration options, so you can do something like:



override fun start(future: Future<Void>) {
    deploy(AuthVerticle::class.java)
    deploy(HttpServerVerticle::class.java)
    deploy(DialPadVerticle::class.java)
    ...
}

private fun deploy(clazz: Class<out AbstractVerticle>) {
    val options = getDeploymentOptionsFromConfig(clazz)
    vertx.deployVerticle(clazz.name, options) { handler ->
        ...
    }
}

private fun getDeploymentOptionsFromConfig(clazz: Class<out AbstractVerticle>): DeploymentOptions {
    val config = config()
            .getJsonObject(clazz.name)
            .getJsonObject("deployment_options")

    return DeploymentOptions()
            .setInstances(config.getInteger("instances", 1))
            .setWorker(config.getBoolean("worker", false))
}

      

+1


source







All Articles