@Override public ConsulClient catalogServicesWithOptions(BlockingQueryOptions options, Handler<AsyncResult<ServiceList>> resultHandler) { requestObject(HttpMethod.GET, "/v1/catalog/services", Query.of(options), null, resultHandler, (json, headers) -> { List<Service> list = json.stream().map(ServiceParser::parseCatalogInfo).collect(Collectors.toList()); return new ServiceList().setList(list).setIndex(Long.parseLong(headers.get(INDEX_HEADER))); }); return this; }
@Override public int hashCode() { int result = (int) (index ^ (index >>> 32)); result = 31 * result + (list != null ? sorted().hashCode() : 0); return result; }
public static void fromJson(Iterable<java.util.Map.Entry<String, Object>> json, ServiceList obj) { for (java.util.Map.Entry<String, Object> member : json) { switch (member.getKey()) { case "index": if (member.getValue() instanceof Number) { obj.setIndex(((Number)member.getValue()).longValue()); } break; case "list": if (member.getValue() instanceof JsonArray) { java.util.ArrayList<io.vertx.ext.consul.Service> list = new java.util.ArrayList<>(); ((Iterable<Object>)member.getValue()).forEach( item -> { if (item instanceof JsonObject) list.add(new io.vertx.ext.consul.Service((JsonObject)item)); }); obj.setList(list); } break; } } }
@Test public void testServiceListCopy() { List<Service> services = new ArrayList<>(); services.add(randomService()); services.add(randomService()); ServiceList list = new ServiceList() .setIndex(randomPositiveInt()) .setList(services); checkServiceList(list, new ServiceList(list)); checkServiceList(list, new ServiceList(list.toJson())); List<Service> shuffledServices = new ArrayList<>(); shuffledServices.add(services.get(1)); shuffledServices.add(services.get(0)); ServiceList shuffled = new ServiceList() .setIndex(list.getIndex()) .setList(shuffledServices); checkServiceList(list, shuffled); }
private void retrieveIndividualServices(ServiceList list, Future<List<ImportedConsulService>> completed) { List<Future> futures = new ArrayList<>(); list.getList().forEach(service -> {
private void checkServiceList(ServiceList expected, ServiceList actual) { assertEquals(expected, actual); assertEquals(expected.hashCode(), actual.hashCode()); assertEquals(expected.getIndex(), actual.getIndex()); List<Service> expectedList = expected.getList(); List<Service> actualList = actual.getList(); assertTrue(expectedList.containsAll(actualList)); assertTrue(actualList.containsAll(expectedList)); }
@Override void wait(long index, Handler<AsyncResult<State<ServiceList>>> handler) { BlockingQueryOptions options = new BlockingQueryOptions().setWait(BLOCKING_WAIT).setIndex(index); consulClient.catalogServicesWithOptions(options, h -> handler.handle(h.map(services -> new State<ServiceList>(services, services.getIndex())))); } }
private void retrieveIndividualServices(ServiceList list, Future<List<ImportedConsulService>> completed) { List<Future> futures = new ArrayList<>(); list.getList().forEach(service -> {
@Override public ConsulClient catalogNodeServicesWithOptions(String node, BlockingQueryOptions options, Handler<AsyncResult<ServiceList>> resultHandler) { requestObject(HttpMethod.GET, "/v1/catalog/node/" + urlEncode(node), Query.of(options), null, resultHandler, (json, headers) -> { JsonObject nodeInfo = json.getJsonObject("Node"); String nodeName = nodeInfo.getString("Node"); String nodeAddress = nodeInfo.getString("Address"); List<Service> list = json.getJsonObject("Services").stream() .map(obj -> ServiceParser.parseNodeInfo(nodeName, nodeAddress, (JsonObject) obj.getValue())) .collect(Collectors.toList()); return new ServiceList().setList(list).setIndex(Long.parseLong(headers.get(INDEX_HEADER))); }); return this; }
@Override public void getRecords(Handler<AsyncResult<List<Record>>> resultHandler) { Future<ServiceList> nameList = Future.future(); client.catalogServices(nameList); nameList.map(ServiceList::getList) .map(l -> { List<Future> recordFutureList = new ArrayList<>(); l.forEach(s -> { if (!"consul".equals(s.getName())) { ServiceQueryOptions opt = new ServiceQueryOptions(); if (!s.getTags().isEmpty()) { opt.setTag(s.getTags().get(0)); } Future<ServiceList> serviceList = Future.future(); client.catalogServiceNodesWithOptions(s.getName(), opt, serviceList); recordFutureList.add(serviceList); } }); return recordFutureList; }) .compose(CompositeFuture::all) .map(c -> c.<ServiceList>list().stream().flatMap(l -> l.getList().stream()).map(this::serviceToRecord).collect(Collectors.toList())) .compose(CompositeFuture::all) .map(c -> c.list().stream().map(o -> (Record) o).collect(Collectors.toList())) .setHandler(resultHandler); }
private void testServicesBlocking(Consumer<Handler<AsyncResult<ServiceList>>> runner, BiConsumer<BlockingQueryOptions, Handler<AsyncResult<ServiceList>>> request) throws InterruptedException { runAsync(h -> ctx.writeClient().registerService(new ServiceOptions().setName("service1").setId("id1"), h)); ServiceList list1 = getAsync(runner); list1.getList().forEach(s -> System.out.println("--- " + s.toJson().encode())); CountDownLatch latch = new CountDownLatch(1); waitBlockingQuery(latch, 10, list1.getIndex(), (idx, fut) -> { request.accept(new BlockingQueryOptions().setIndex(idx), h -> { h.result().getList().forEach(s -> System.out.println("-+- " + s.toJson().encode())); List<String> names = h.result().getList().stream().map(Service::getName).collect(Collectors.toList()); waitComplete(vertx, fut, h.result().getIndex(), names.contains("service2")); }); }); sleep(vertx, 4000); assertEquals(latch.getCount(), 1); runAsync(h -> ctx.writeClient().registerService(new ServiceOptions().setName("service2").setId("id2"), h)); awaitLatch(latch); runAsync(h -> ctx.writeClient().deregisterService("id1", h)); runAsync(h -> ctx.writeClient().deregisterService("id2", h)); }
@Override public ConsulClient catalogServiceNodesWithOptions(String service, ServiceQueryOptions options, Handler<AsyncResult<ServiceList>> resultHandler) { Query query = options == null ? null : Query.of("tag", options.getTag()).put("near", options.getNear()).put(options.getBlockingOptions()); requestArray(HttpMethod.GET, "/v1/catalog/service/" + urlEncode(service), query, null, resultHandler, (arr, headers) -> { List<Service> list = arr.stream().map(obj -> new Service((JsonObject) obj)).collect(Collectors.toList()); return new ServiceList().setList(list).setIndex(Long.parseLong(headers.get(INDEX_HEADER))); }); return this; }
@Test public void findConsul() { ServiceList localConsulList = getAsync(h -> ctx.writeClient().catalogServiceNodes("consul", h)); assertEquals(localConsulList.getList().size(), 1); List<Service> catalogConsulList = Utils.<ServiceList>getAsync(h -> ctx.writeClient().catalogServices(h)) .getList().stream().filter(s -> s.getName().equals("consul")).collect(Collectors.toList()); assertEquals(1, catalogConsulList.size()); assertEquals(0, catalogConsulList.get(0).getTags().size()); }
tc.assertEquals(c.getNotes(), opts.getCheckOptions().getNotes()); ctx.writeClient().catalogNodeServices(ctx.nodeName(), tc.asyncAssertSuccess(nodeServices -> { tc.assertEquals(2, nodeServices.getList().size()); checkService(tc, nodeServices.getList(), serviceName, opts); Async async = tc.async(2); ServiceQueryOptions knownOpts = new ServiceQueryOptions().setTag(opts.getTags().get(0)); ctx.writeClient().catalogServiceNodesWithOptions(serviceName, knownOpts, tc.asyncAssertSuccess(nodeServicesWithKnownTag -> { tc.assertEquals(1, nodeServicesWithKnownTag.getList().size()); async.countDown(); })); ServiceQueryOptions unknownOpts = new ServiceQueryOptions().setTag("unknownTag"); ctx.writeClient().catalogServiceNodesWithOptions(serviceName, unknownOpts, tc.asyncAssertSuccess(nodeServicesWithUnknownTag -> { tc.assertEquals(0, nodeServicesWithUnknownTag.getList().size()); async.countDown(); }));
@Test public void watchServices() throws InterruptedException { StateConsumer<String> consumer = new StateConsumer<>(); ServiceOptions service = new ServiceOptions() .setId(randomAlphaString(10)) .setName(randomAlphaString(10)); Watch<ServiceList> watch = Watch.services(vertx, ctx.readClientOptions()) .setHandler(list -> { if (list.succeeded()) { consumer.consume(list.nextResult().getList() .stream().map(Service::getName).filter(s -> s.equals(service.getName())) .findFirst().orElse("")); } }) .start(); consumer.await(""); runAsync(h -> ctx.writeClient().registerService(service, h)); consumer.await(service.getName()); consumer.check(); watch.stop(); runAsync(h -> ctx.writeClient().deregisterService(service.getId(), h)); }