/** * 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; }
/** * 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; }
@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())); } }); }
@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())); } }); }
@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())); } }); } }); }
/** * 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"); } } }
@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()); }); }
@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()); }); }
@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); }
@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())); }); }
@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())); }); }
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()); } }); } }
@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())); } }); }
@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(); }
@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(); }
@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); }
@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); }
@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); }
@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); }
@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(); }); }); } }