Vert.X ServerDiscovery端点在群集中多次

时间:2019-07-16 16:19:35

标签: vert.x

我使用ServerDiscovery来确定我的EventBus端点。我做了这个代表案例,展示了我的集群问题。我以

开始
java -jar starter-1.0.0-SNAPSHOT-fat.jar --cluster

,因为我在网络中有节点,所以它将多次出现在端点上。在群集的第二个节点上,它将是两次。

io.vertx.core.impl.launcher.commands.VertxIsolatedDeployer
    INFO: Succeeded in deploying verticle
    Endpoint : Starter
    Endpoint : Starter

如果第一个节点终止并重新启动,它将是三次,下一次重新启动以此类推

INFO: [10.11.53.74]:5701 [dev] [3.10.5] [10.11.53.74]:5701 is STARTED
successfully published!
HTTP server started on port 8888
INFO: Succeeded in deploying verticle
Endpoint : Starter
Endpoint : Starter
Endpoint : Starter
Endpoint : Starter

是否可以注册未在群集上填充的服务端点? Event-Bus仅在本地节点上使用,我只想复制会话上下文。

package com.example.starter;

import java.util.List;

import io.vertx.core.AbstractVerticle;
import io.vertx.core.Future;
import io.vertx.core.json.JsonObject;
import io.vertx.servicediscovery.Record;
import io.vertx.servicediscovery.ServiceDiscovery;
import io.vertx.servicediscovery.ServiceDiscoveryOptions;
import io.vertx.servicediscovery.Status;

public class MainVerticle extends AbstractVerticle {

  @Override
  public void start(Future<Void> startFuture) throws Exception {

      ServiceDiscovery discovery = ServiceDiscovery.create(this.getVertx(),
              new ServiceDiscoveryOptions()
                .setAnnounceAddress("starter-vertx")
                .setName("MainVerticle"));  

      Record record = new Record()
              .setType("starter-vertx-eventbus")
              .setName("MainVerticle.ebBusName")
              .setStatus(Status.UP)
              .setLocation(new JsonObject().put(Record.ENDPOINT, "ebBusName"))
              .setMetadata(new JsonObject()
                                    .put("Method",    "GET")
                                    .put("EndPoint",  "Starter"));
        discovery.publish(record, ar -> {
            if (ar.succeeded()) {
                System.out.println("successfully published!");
            } else {
                System.out.println("Publish failed." + ar.cause());
            }
        });

    discovery.getRecords(r -> r.getType().equals("starter-vertx-eventbus"), resultHandler -> {
        if (resultHandler.succeeded()) {
            List<Record> records = resultHandler.result();
            for (Record discoveryRec : records) {
                String endpoint    = discoveryRec.getMetadata().getString("EndPoint");
                System.out.println("Endpoint : " + endpoint);
            }
        }
    });

    vertx.createHttpServer().requestHandler(req -> {
      req.response()
        .putHeader("content-type", "text/plain")
        .end("Hello from Vert.x!");
    }).listen(8888, http -> {
      if (http.succeeded()) {
        startFuture.complete();
        System.out.println("HTTP server started on port 8888");
      } else {
        startFuture.fail(http.cause());
      }
    });
  }
}

0 个答案:

没有答案