【问题标题】:How to spawn verticles that will be undeployed after they've done their work如何生成完成工作后将取消部署的 Verticle
【发布时间】:2020-06-11 11:00:08
【问题描述】:

我目前正在开发我的第一个 vert.x 服务,但我对我的部署实施方式并不十分自信。

我有几个 Verticle 来接受来自其他服务的请求并将一堆工作委托给其他 Verticle。如何在工作完成后启动每个任务的 verticle 并杀死它,或者以编程方式将 verticles 扩大到任务的数量。

我目前在启动时生成 10 个 Verticle,并使用事件总线将这些任务分散到各个 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))
  }
}

管理垂直 - HTTP 处理程序

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()
}

更新垂直

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

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

【问题讨论】:

    标签: vert.x vertx-verticle actor-model


    【解决方案1】:

    deployVerticle 将回调作为最后一个参数。这个回调的结果是deploymentId:

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

    理论上,您可以在完成任务后使用它来取消部署您的 Verticle。

    实际上,我会保持它们的数量不变,然后在容器级别进行扩展。

    【讨论】:

      猜你喜欢
      • 2017-01-27
      • 1970-01-01
      • 1970-01-01
      • 2020-06-02
      • 2020-05-06
      • 2021-12-22
      • 1970-01-01
      • 1970-01-01
      • 2020-05-05
      相关资源
      最近更新 更多