@Override public void start() { ServiceDiscovery discovery = ServiceDiscovery.create(vertx, new ServiceDiscoveryOptions() .setAnnounceAddress("service-announce") discovery.publish(record1, ar -> { if (ar.succeeded()) { System.out.println("\"" + record1.getName() + "\" successfully published!"); discovery.publish(record2, ar -> { if (ar.succeeded()) { System.out.println("\"" + record2.getName() + "\" successfully published!"); discovery.unpublish(record1.getRegistration(), ar -> { if (ar.succeeded()) { System.out.println("\"" + record1.getName() + "\" successfully unpublished"); discovery.getRecord(r -> r.getName().equals(record2.getName()), ar -> { if (ar.succeeded()) { if (ar.result() != null) { ServiceReference reference = discovery.getReference(ar.result()); discovery.close();
@Test public void testHttpImport() { services.add(buildService("172.17.0.2", "web", "web", new String[]{"rails", "http-endpoint"}, 80, "passing")); discovery = ServiceDiscovery.create(vertx) .registerServiceImporter(new ConsulServiceImporter(), new JsonObject().put("host", "localhost").put("port", 5601)); await().until(() -> getAllRecordsBlocking().size() > 0); List<Record> list = getAllRecordsBlocking(); assertThat(list).hasSize(1); assertThat(list.get(0).getType()).isEqualTo(HttpEndpoint.TYPE); assertThat(list.get(0).getLocation().getString("endpoint")).isEqualTo("http://172.17.0.2:80"); }
@Test public void testPublicationAndConsumptionWithConfiguration(TestContext context) { Async async = context.async(); // Publish the service Record record = HttpEndpoint.createRecord("hello-service", "localhost", 8080, "/foo"); discovery.publish(record, rec -> { Record published = rec.result(); HttpEndpoint.getClient(discovery, new JsonObject().put("name", "hello-service"), new JsonObject().put ("keepAlive", false), found -> { context.assertTrue(found.succeeded()); context.assertTrue(found.result() != null); HttpClient client = found.result(); client.getNow("/foo", response -> { context.assertEquals(response.statusCode(), 200); context.assertEquals(response.getHeader("connection"), "close"); response.bodyHandler(body -> { context.assertEquals(body.toString(), "hello"); ServiceDiscovery.releaseServiceObject(discovery, client); discovery.unpublish(published.getRegistration(), v -> async.complete()); }); }); }); }); }
@Override public void start() { Router router = Router.router(vertx); router.get("/").handler(this::invoke); // Retrieve the service discovery ServiceDiscovery.create(vertx, discovery -> // Retrieve a web client HttpEndpoint.getWebClient(discovery, svc -> svc.getName().equals("vertx-greeting"), ar -> { if (ar.failed()) { System.out.println("D'oh the service is not available"); } else { client = ar.result(); vertx.createHttpServer().requestHandler(router).listen(8080); } })); }
/** * Publishes a record. * @param record the record * @param resultHandler handler called when the operation has completed (successfully or not). In case of success, the passed record has a registration id required to modify and un-register the service. */ public void publish(Record record, Handler<AsyncResult<Record>> resultHandler) { delegate.publish(record, resultHandler); }
/** * Convenient method that looks for a JDBC datasource source and provides the configured {@link io.vertx.ext.jdbc.JDBCClient}. The * async result is marked as failed is there are no matching services, or if the lookup fails. * * @param discovery The service discovery instance * @param filter The filter (must not be {@code null}) * @param resultHandler The result handler */ static void getJDBCClient(ServiceDiscovery discovery, Function<Record, Boolean> filter, Handler<AsyncResult<JDBCClient>> resultHandler) { discovery.getRecord(filter, ar -> { if (ar.failed() || ar.result() == null) { resultHandler.handle(Future.failedFuture("No matching record")); } else { resultHandler.handle(Future.succeededFuture(discovery.<JDBCClient>getReference(ar.result()).get())); } }); }
@Test public void testAutoCloseable(TestContext context) { Async async = context.async(); // Publish the service Record record = HttpEndpoint.createRecord("hello-service", "localhost", 8080, "/foo"); discovery.publish(record, rec -> { Record published = rec.result(); discovery.getRecord(new JsonObject().put("name", "hello-service"), found -> { context.assertTrue(found.succeeded()); context.assertTrue(found.result() != null); Record match = found.result(); try (ServiceReference reference = discovery.getReference(match)) { context.assertEquals(reference.record().getLocation().getString("endpoint"), "http://localhost:8080/foo"); context.assertFalse(reference.record().getLocation().getBoolean("ssl")); WebClient client = reference.getAs(WebClient.class); WebClient client2 = reference.cachedAs(WebClient.class); context.assertTrue(client == client2); } catch (Exception e) { context.fail(e); } discovery.unpublish(published.getRegistration(), v -> async.complete()); }); }); }
/** * Lookups for a set of records. Unlike {@link io.vertx.rxjava.servicediscovery.ServiceDiscovery#getRecord}, this method returns all matching * records. * @param filter the filter - see {@link io.vertx.rxjava.servicediscovery.ServiceDiscovery#getRecord} * @param resultHandler handler called when the lookup has been completed. When there are no matching record, the operation succeed, but the async result has an empty list as result. */ public void getRecords(JsonObject filter, Handler<AsyncResult<List<Record>>> resultHandler) { delegate.getRecords(filter, resultHandler); }
@Test public void testUsingGetMethod() { HelloService svc = new HelloServiceImpl("stuff"); ProxyHelper.registerService(HelloService.class, vertx, svc, "address"); Record record = EventBusService.createRecord("Hello", "address", HelloService.class); discovery.publish(record, (r) -> { }); await().until(() -> record.getRegistration() != null); AtomicReference<HelloService> found = new AtomicReference<>(); EventBusService.getProxy(discovery, HelloService.class, ar -> { found.set(ar.result()); }); await().until(() -> found.get() != null); Assertions.assertThat(discovery.bindings()).hasSize(1); HelloService hello = found.get(); AtomicReference<String> result = new AtomicReference<>(); hello.hello(name, ar -> result.set(ar.result())); await().untilAtomic(result, not(nullValue())); ServiceDiscovery.releaseServiceObject(discovery, found.get()); Assertions.assertThat(discovery.bindings()).hasSize(0); }
/** * Closes the service discovery */ public void close() { delegate.close(); }
@Test public void testServiceAvailability_OK() { ServiceDiscovery discovery = ServiceDiscovery.create(vertx); AtomicBoolean done = new AtomicBoolean(); discovery.publish(HttpEndpoint.createRecord("my-service", "localhost"), ar -> { done.set(ar.succeeded()); }); await().untilAtomic(done, is(true)); registerServiceProcedure(discovery); get(200); }
/** * Un-publishes a record. * @param id the registration id * @param resultHandler handler called when the operation has completed (successfully or not). */ public void unpublish(String id, Handler<AsyncResult<Void>> resultHandler) { delegate.unpublish(id, resultHandler); }
/** * {@inheritDoc} */ @Override public void getRecord(JsonObject filter, Handler<AsyncResult<Record>> resultHandler) { unmanagedDiscoveryInstance.getRecord(filter, resultHandler); }
@Override public void stop(Future<Void> future) throws Exception { // In current design, the publisher is responsible for removing the service List<Future> futures = new ArrayList<>(); registeredRecords.forEach(record -> { Future<Void> cleanupFuture = Future.future(); futures.add(cleanupFuture); discovery.unpublish(record.getRegistration(), cleanupFuture.completer()); }); if (futures.isEmpty()) { discovery.close(); future.complete(); } else { CompositeFuture.all(futures) .setHandler(ar -> { discovery.close(); if (ar.failed()) { future.fail(ar.cause()); } else { future.complete(); } }); } } }
/** * {@inheritDoc} */ @Override public ServiceReference getReference(Record record) { return unmanagedDiscoveryInstance.getReference(record); }
@Override public ServiceDiscovery registerServiceImporter(ServiceImporter importer, JsonObject configuration, Handler<AsyncResult<Void>> completionHandler) { return unmanagedDiscoveryInstance.registerServiceImporter(importer, configuration, completionHandler); }
public void releaseServiceObject(Object object) { ServiceDiscovery.releaseServiceObject(serviceDiscovery, object); }
@Test public void testImportServicesWithWarningStatusThreshold() { services.add(buildService("10.1.10.12", "redis", "redis", null, 8000, "passing")); services.add(buildService("10.1.10.12", "warning", "warning", null, 8001, "warning")); discovery = ServiceDiscovery.create(vertx) .registerServiceImporter(new ConsulServiceImporter(), new JsonObject().put("host", "localhost").put("port", 5601).put("up_threshold", "warning")); await().until(() -> getAllRecordsBlocking().size() > 0); List<Record> list = getAllRecordsBlocking(); assertThat(list).hasSize(2); }
@Test public void testPublicationAndConsumptionWithConfigurationAsWebClient(TestContext context) { Async async = context.async(); // Publish the service Record record = HttpEndpoint.createRecord("hello-service", "localhost", 8080, "/foo"); discovery.publish(record, rec -> { Record published = rec.result(); HttpEndpoint.getWebClient(discovery, new JsonObject().put("name", "hello-service"), new JsonObject().put("keepAlive", false), found -> { context.assertTrue(found.succeeded()); context.assertTrue(found.result() != null); WebClient client = found.result(); client.get("/foo").send(ar -> { if (ar.failed()) { context.fail(ar.cause()); } HttpResponse<Buffer> response = ar.result(); context.assertEquals(response.statusCode(), 200); context.assertEquals(response.getHeader("connection"), "close"); context.assertEquals(response.body().toString(), "hello"); ServiceDiscovery.releaseServiceObject(discovery, client); discovery.unpublish(published.getRegistration(), v -> async.complete()); }); }); }); }