简体   繁体   中英

How to spawn verticles that will be undeployed after they've done their work

I'm currently working on my first vert.x service and I'm not really confident with the way how my deployment is implemented.

I have several verticles to accept requests from other services and delegate a bunch of work to other verticles. How can I either spin up a verticle of each task and kill it after the work is done or programmatically scale the verticles up the number of tasks.

I'm currently spawning ten verticles at the startup and spread those tasks over the verticles using the event bus.

Main Verticle

class MainVerticle : CoroutineVerticle() {

  private val databaseConfig by lazy { config.getJsonObject("migration") }

  override suspend fun start() {
    vertx.deployVerticle(AdministrationVerticle::class.java, DeploymentOptions().setConfig(config))
    vertx.deployVerticle(UpdateVerticle::class.java, DeploymentOptions().setConfig(config))
  }
}

Administration Verticle - HTTP Handler

private suspend fun handleRolloutRequest(ctx: RoutingContext) {
  val version = ctx.request().getParam("version")?.toInt() ?: findLatestVersion()
  val systems = findOutdatedSystems(version)

  for (system in systems) {
    vertx.eventBus().send("rollout", system.id, options)
  }

  ctx.response().end()
}

Update Verticle

override suspend fun start() {
  client.connectAwait(mqttServerConfig.getInteger("port"), mqttServerConfig.getString("hostname"))

  vertx.eventBus().consumer<String>("rollout") {
    launch(vertx.dispatcher()) {
      // actual rollout
    }
  }
}

deployVerticle takes a callback as it's last argument. Result of this callback is deploymentId :

vertx.deployVerticle("your.class", res -> {
    if (res.succeeded()) {
        String deploymentID = res.result();
        ...
    }
}

Theoretically, you could use this to undeploy your verticles after they completed their tasks.

Practically, I would keep a constant number of them, and scale on the container level instead.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM