io.vertx.servicediscovery.Record.setRegistration()方法的使用及代码示例

x33g5p2x  于2022-01-28 转载在 其他  
字(14.9k)|赞(0)|评价(0)|浏览(66)

本文整理了Java中io.vertx.servicediscovery.Record.setRegistration方法的一些代码示例,展示了Record.setRegistration的具体用法。这些代码示例主要来源于Github/Stackoverflow/Maven等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。Record.setRegistration方法的具体详情如下:
包路径:io.vertx.servicediscovery.Record
类名称:Record
方法名:setRegistration

Record.setRegistration介绍

[英]Sets the registration id. This method is called when the service is published.
[中]设置注册id。发布服务时调用此方法。

代码示例

代码示例来源:origin: vert-x3/vertx-service-discovery

/**
 * Registers the service and completes the given future when done.
 *
 * @param publisher  the service publisher instance
 * @param completion the completion future
 * @return the current {@link ImportedConsulService}
 */
public ImportedConsulService register(ServicePublisher publisher, Future<ImportedConsulService> completion) {
 publisher.publish(record, ar -> {
  if (ar.succeeded()) {
   record.setRegistration(ar.result().getRegistration());
   completion.complete(this);
  } else {
   completion.fail(ar.cause());
  }
 });
 return this;
}

代码示例来源:origin: io.vertx/vertx-service-discovery-bridge-consul

/**
 * Registers the service and completes the given future when done.
 *
 * @param publisher  the service publisher instance
 * @param completion the completion future
 * @return the current {@link ImportedConsulService}
 */
public ImportedConsulService register(ServicePublisher publisher, Future<ImportedConsulService> completion) {
 publisher.publish(record, ar -> {
  if (ar.succeeded()) {
   record.setRegistration(ar.result().getRegistration());
   completion.complete(this);
  } else {
   completion.fail(ar.cause());
  }
 });
 return this;
}

代码示例来源:origin: vert-x3/vertx-service-discovery

@Override
public void store(Record record, Handler<AsyncResult<Record>> resultHandler) {
 if (record.getRegistration() != null) {
  resultHandler.handle(Future.failedFuture("The record has already been registered"));
  return;
 }
 String uuid = UUID.randomUUID().toString();
 record.setRegistration(uuid);
 redis.hset(key, uuid, record.toJson().encode(), ar -> {
  if (ar.succeeded()) {
   resultHandler.handle(Future.succeededFuture(record));
  } else {
   resultHandler.handle(Future.failedFuture(ar.cause()));
  }
 });
}

代码示例来源:origin: io.vertx/vertx-service-discovery-backend-redis

@Override
public void store(Record record, Handler<AsyncResult<Record>> resultHandler) {
 if (record.getRegistration() != null) {
  resultHandler.handle(Future.failedFuture("The record has already been registered"));
  return;
 }
 String uuid = UUID.randomUUID().toString();
 record.setRegistration(uuid);
 redis.hset(key, uuid, record.toJson().encode(), ar -> {
  if (ar.succeeded()) {
   resultHandler.handle(Future.succeededFuture(record));
  } else {
   resultHandler.handle(Future.failedFuture(ar.cause()));
  }
 });
}

代码示例来源:origin: io.vertx/vertx-service-discovery

@Override
public void store(Record record, Handler<AsyncResult<Record>> resultHandler) {
 String uuid = UUID.randomUUID().toString();
 if (record.getRegistration() != null) {
  throw new IllegalArgumentException("The record has already been registered");
 }
 record.setRegistration(uuid);
 retrieveRegistry(reg -> {
  if (reg.failed()) {
   resultHandler.handle(failure(reg.cause()));
  } else {
   reg.result().put(uuid, record.toJson().encode(), ar -> {
    if (ar.succeeded()) {
     resultHandler.handle(Future.succeededFuture(record));
    } else {
     resultHandler.handle(Future.failedFuture(ar.cause()));
    }
   });
  }
 });
}

代码示例来源:origin: io.vertx/vertx-service-discovery-bridge-consul

/**
 * Unregisters the service and completes the given future when done, if not {@code null}
 *
 * @param publiher  the service publisher instance
 * @param completion the completion future
 */
public void unregister(ServicePublisher publiher, Future<Void> completion) {
 if (record.getRegistration() != null) {
  publiher.unpublish(record.getRegistration(), ar -> {
   if (ar.succeeded()) {
    record.setRegistration(null);
   }
   if (completion != null) {
    completion.complete();
   }
  });
 } else {
  if (completion != null) {
   completion.fail("Record not published");
  }
 }
}

代码示例来源:origin: io.vertx/vertx-service-discovery

@Override
public void unpublish(String id, Handler<AsyncResult<Void>> resultHandler) {
 backend.remove(id, record -> {
  if (record.failed()) {
   resultHandler.handle(Future.failedFuture(record.cause()));
   return;
  }
  for (ServiceExporter exporter : exporters) {
   exporter.onUnpublish(id);
  }
  Record announcedRecord = new Record(record.result());
  announcedRecord
   .setRegistration(null)
   .setStatus(Status.DOWN);
  vertx.eventBus().publish(announce, announcedRecord.toJson());
  resultHandler.handle(Future.succeededFuture());
 });
}

代码示例来源:origin: vert-x3/vertx-service-discovery

@Override
public void unpublish(String id, Handler<AsyncResult<Void>> resultHandler) {
 backend.remove(id, record -> {
  if (record.failed()) {
   resultHandler.handle(Future.failedFuture(record.cause()));
   return;
  }
  for (ServiceExporter exporter : exporters) {
   exporter.onUnpublish(id);
  }
  Record announcedRecord = new Record(record.result());
  announcedRecord
   .setRegistration(null)
   .setStatus(Status.DOWN);
  vertx.eventBus().publish(announce, announcedRecord.toJson());
  resultHandler.handle(Future.succeededFuture());
 });
}

代码示例来源:origin: vert-x3/vertx-service-discovery

@Override
public void store(Record record, Handler<AsyncResult<Record>> resultHandler) {
 String uuid = UUID.randomUUID().toString();
 if (record.getRegistration() != null) {
  throw new IllegalArgumentException("The record has already been registered");
 }
 ServiceOptions serviceOptions = recordToServiceOptions(record, uuid);
 record.setRegistration(serviceOptions.getId());
 Future<Void> registration = Future.future();
 client.registerService(serviceOptions, registration);
 registration.map(record).setHandler(resultHandler);
}

代码示例来源:origin: vert-x3/vertx-service-discovery

@Override
public void publish(Record record, Handler<AsyncResult<Record>> resultHandler) {
 Status status = record.getStatus() == null || record.getStatus() == Status.UNKNOWN
  ? Status.UP : record.getStatus();
 backend.store(record.setStatus(status), ar -> {
  if (ar.failed()) {
   resultHandler.handle(Future.failedFuture(ar.cause()));
   return;
  }
  for (ServiceExporter exporter : exporters) {
   exporter.onPublish(new Record(ar.result()));
  }
  Record announcedRecord = new Record(ar.result());
  announcedRecord
   .setRegistration(null)
   .setStatus(status);
  vertx.eventBus().publish(announce, announcedRecord.toJson());
  resultHandler.handle(Future.succeededFuture(ar.result()));
 });
}

代码示例来源:origin: io.vertx/vertx-service-discovery

@Override
public void publish(Record record, Handler<AsyncResult<Record>> resultHandler) {
 Status status = record.getStatus() == null || record.getStatus() == Status.UNKNOWN
  ? Status.UP : record.getStatus();
 backend.store(record.setStatus(status), ar -> {
  if (ar.failed()) {
   resultHandler.handle(Future.failedFuture(ar.cause()));
   return;
  }
  for (ServiceExporter exporter : exporters) {
   exporter.onPublish(new Record(ar.result()));
  }
  Record announcedRecord = new Record(ar.result());
  announcedRecord
   .setRegistration(null)
   .setStatus(status);
  vertx.eventBus().publish(announce, announcedRecord.toJson());
  resultHandler.handle(Future.succeededFuture(ar.result()));
 });
}

代码示例来源:origin: vert-x3/vertx-service-discovery

private void publish(DockerService service) {
 for (Record record : service.records()) {
  publisher.publish(record, ar -> {
   if (ar.succeeded()) {
    record.setRegistration(ar.result().getRegistration());
    LOGGER.info("Service from container " + service.id() + " on location "
      + record.getLocation() + " has been published");
   } else {
    LOGGER.error("Service from container " + service.id() + " on location "
      + record.getLocation() + " could not have been published", ar.cause());
   }
  });
 }
}

代码示例来源:origin: io.vertx/vertx-service-discovery

@Override
public void store(Record record, Handler<AsyncResult<Record>> resultHandler) {
 String uuid = UUID.randomUUID().toString();
 if (record.getRegistration() != null) {
  throw new IllegalArgumentException("The record has already been registered");
 }
 record.setRegistration(uuid);
 registry.put(uuid, record.toJson().encode(), ar -> {
  // Put takes some time to complete
  try {
   Thread.sleep(2000);
  } catch (InterruptedException e) {
   e.printStackTrace();
  }
  if (ar.succeeded()) {
   resultHandler.handle(Future.succeededFuture(record));
  } else {
   resultHandler.handle(Future.failedFuture(ar.cause()));
  }
 });
}

代码示例来源:origin: vert-x3/vertx-service-discovery

@Test
public void testMatch() {
 Record record = new Record().setName("Name");
 assertThat(record.match(new JsonObject().put("name", "Name"))).isTrue();
 assertThat(record.match(new JsonObject().put("name", "Name-2"))).isFalse();
 record.setStatus(Status.UP);
 assertThat(record.match(new JsonObject().put("status", "Up"))).isTrue();
 assertThat(record.match(new JsonObject().put("status", "Down"))).isFalse();
 assertThat(record.match(new JsonObject().put("status", "Up").put("name", "Name"))).isTrue();
 assertThat(record.match(new JsonObject().put("status", "Down").put("name", "Name"))).isFalse();
 record.setRegistration("the-registration");
 assertThat(record.match(new JsonObject().put("registration", "the-registration"))).isTrue();
 assertThat(record.match(new JsonObject().put("registration", "wrong"))).isFalse();
 record.getMetadata().put("foo", "bar").put("key", 2);
 assertThat(record.match(new JsonObject().put("foo", "bar"))).isTrue();
 assertThat(record.match(new JsonObject().put("foo", "bar2"))).isFalse();
 assertThat(record.match(new JsonObject().put("foo", "bar").put("other", "nope"))).isFalse();
 assertThat(record.match(new JsonObject().put("foo", "bar").put("other", "*"))).isFalse();
 assertThat(record.match(new JsonObject().put("foo", "bar").put("key", 2))).isTrue();
 assertThat(record.match(new JsonObject().put("foo", "*").put("key", 2))).isTrue();
}

代码示例来源:origin: io.vertx/vertx-service-discovery

@Test
public void testMatch() {
 Record record = new Record().setName("Name");
 assertThat(record.match(new JsonObject().put("name", "Name"))).isTrue();
 assertThat(record.match(new JsonObject().put("name", "Name-2"))).isFalse();
 record.setStatus(Status.UP);
 assertThat(record.match(new JsonObject().put("status", "Up"))).isTrue();
 assertThat(record.match(new JsonObject().put("status", "Down"))).isFalse();
 assertThat(record.match(new JsonObject().put("status", "Up").put("name", "Name"))).isTrue();
 assertThat(record.match(new JsonObject().put("status", "Down").put("name", "Name"))).isFalse();
 record.setRegistration("the-registration");
 assertThat(record.match(new JsonObject().put("registration", "the-registration"))).isTrue();
 assertThat(record.match(new JsonObject().put("registration", "wrong"))).isFalse();
 record.getMetadata().put("foo", "bar").put("key", 2);
 assertThat(record.match(new JsonObject().put("foo", "bar"))).isTrue();
 assertThat(record.match(new JsonObject().put("foo", "bar2"))).isFalse();
 assertThat(record.match(new JsonObject().put("foo", "bar").put("other", "nope"))).isFalse();
 assertThat(record.match(new JsonObject().put("foo", "bar").put("other", "*"))).isFalse();
 assertThat(record.match(new JsonObject().put("foo", "bar").put("key", 2))).isTrue();
 assertThat(record.match(new JsonObject().put("foo", "*").put("key", 2))).isTrue();
}

代码示例来源:origin: io.vertx/vertx-service-discovery

@Test
public void testFailedPublication() {
 HelloService svc = new HelloServiceImpl("stuff");
 ProxyHelper.registerService(HelloService.class, vertx, svc, "address");
 Record record = new Record()
  .setName("Hello")
  .setRegistration("this-is-not-allowed")
  .setLocation(new JsonObject().put(Record.ENDPOINT, "address"));
 Restafari.Response response = given().request().body(record.toJson().toString()).post("/discovery");
 assertThat(response.getStatusCode()).isEqualTo(500);
}

代码示例来源:origin: vert-x3/vertx-service-discovery

@Test
public void testFailedPublication() {
 HelloService svc = new HelloServiceImpl("stuff");
 ProxyHelper.registerService(HelloService.class, vertx, svc, "address");
 Record record = new Record()
  .setName("Hello")
  .setRegistration("this-is-not-allowed")
  .setLocation(new JsonObject().put(Record.ENDPOINT, "address"));
 Restafari.Response response = given().request().body(record.toJson().toString()).post("/discovery");
 assertThat(response.getStatusCode()).isEqualTo(500);
}

代码示例来源:origin: io.vertx/vertx-service-discovery

@Test
public void testUpdateWithUUIDMismatch() throws UnsupportedEncodingException {
 HelloService svc = new HelloServiceImpl("stuff");
 ProxyHelper.registerService(HelloService.class, vertx, svc, "address");
 Record record = new Record()
  .setName("Hello")
  .setLocation(new JsonObject().put(Record.ENDPOINT, "address"));
 discovery.publish(record, (r) -> {
 });
 await().until(() -> record.getRegistration() != null);
 Record retrieved = retrieve(record.getRegistration());
 assertThat(retrieved.getStatus()).isEqualTo(Status.UP);
 retrieved.setStatus(Status.OUT_OF_SERVICE).setRegistration("not-the-right-one").getMetadata().put("foo", "bar");
 Restafari.Response response = given().body(retrieved.toJson().toString())
  .put("/discovery/" + record.getRegistration());
 assertThat(response.getStatusCode()).isEqualTo(400);
}

代码示例来源:origin: vert-x3/vertx-service-discovery

@Test
public void testUpdateWithUUIDMismatch() throws UnsupportedEncodingException {
 HelloService svc = new HelloServiceImpl("stuff");
 ProxyHelper.registerService(HelloService.class, vertx, svc, "address");
 Record record = new Record()
  .setName("Hello")
  .setLocation(new JsonObject().put(Record.ENDPOINT, "address"));
 discovery.publish(record, (r) -> {
 });
 await().until(() -> record.getRegistration() != null);
 Record retrieved = retrieve(record.getRegistration());
 assertThat(retrieved.getStatus()).isEqualTo(Status.UP);
 retrieved.setStatus(Status.OUT_OF_SERVICE).setRegistration("not-the-right-one").getMetadata().put("foo", "bar");
 Restafari.Response response = given().body(retrieved.toJson().toString())
  .put("/discovery/" + record.getRegistration());
 assertThat(response.getStatusCode()).isEqualTo(400);
}

代码示例来源:origin: engagingspaces/vertx-graphql-service-discovery

@Test
  public void should_Return_Failure_Un_Publishing_Unknown_Record(TestContext context) {
    Async async = context.async();
    GraphQLService.publish(vertx, discovery, starWarsSchema, options, null, rh ->
    {
      context.assertTrue(rh.succeeded());
      SchemaRegistration registration1 = rh.result();

      GraphQLService.unpublish(SchemaRegistration.create(registration1.getDiscovery(),
          registration1.getDiscoveryOptions(), new Record(registration1.getRecord()).setRegistration("foo"),
          registration1.getSchemaDefinition(), registration1.getServiceConsumer()), rh2 ->
      {
        context.assertFalse(rh2.succeeded());
        async.complete();
      });
    });
  }
}

相关文章