@Override String createCheck(CheckOptions opts) { String serviceId = "serviceId"; ServiceOptions service = new ServiceOptions() .setName("serviceName") .setId(serviceId) .setTags(Arrays.asList("tag1", "tag2")) .setCheckOptions(opts) .setAddress("10.0.0.1") .setPort(8080); runAsync(h -> ctx.writeClient().registerService(service, h)); return "service:" + serviceId; }
@Override public ConsulClient registerService(ServiceOptions serviceOptions, Handler<AsyncResult<Void>> resultHandler) { JsonObject jsonOpts = new JsonObject() .put("ID", serviceOptions.getId()) .put("Name", serviceOptions.getName()) .put("Tags", serviceOptions.getTags()) .put("Address", serviceOptions.getAddress()) .put("Port", serviceOptions.getPort()); if (serviceOptions.getCheckOptions() != null) { jsonOpts.put("Check", checkOpts(serviceOptions.getCheckOptions(), false)); } if (serviceOptions.getMeta() != null && !serviceOptions.getMeta().isEmpty()) { jsonOpts.put("Meta", serviceOptions.getMeta()); } requestVoid(HttpMethod.PUT, "/v1/agent/service/register", null, jsonOpts.encode(), resultHandler); return this; }
case "address": if (member.getValue() instanceof String) { obj.setAddress((String)member.getValue()); obj.setCheckOptions(new io.vertx.ext.consul.CheckOptions((JsonObject)member.getValue())); obj.setId((String)member.getValue()); map.put(entry.getKey(), (String)entry.getValue()); }); obj.setMeta(map); obj.setName((String)member.getValue()); obj.setPort(((Number)member.getValue()).intValue()); list.add((String)item); }); obj.setTags(list);
@Test public void healthState() throws InterruptedException { String serviceName = randomAlphaString(10); ServiceOptions opts = new ServiceOptions() .setName(serviceName) .setId(serviceName) .setCheckOptions(new CheckOptions().setTtl("1m")); runAsync(h -> ctx.writeClient().registerService(opts, h)); CheckList list1 = getAsync(h -> ctx.readClient().healthState(HealthState.CRITICAL, h)); CountDownLatch latch = new CountDownLatch(1); waitBlockingQuery(latch, 10, list1.getIndex(), (idx, fut) -> { CheckQueryOptions options = new CheckQueryOptions() .setBlockingOptions(new BlockingQueryOptions().setIndex(idx)); ctx.readClient().healthStateWithOptions(HealthState.PASSING, options, h -> { List<String> names = h.result().getList().stream().map(Check::getServiceName).collect(Collectors.toList()); waitComplete(vertx, fut, h.result().getIndex(), names.contains(serviceName)); }); }); runAsync(h -> ctx.writeClient().passCheck("service:" + serviceName, h)); awaitLatch(latch); runAsync(h -> ctx.writeClient().deregisterService(serviceName, h)); }
@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)); }
@Test public void bindCheckToService() { String serviceName = "serviceName"; ServiceOptions service = new ServiceOptions() .setName(serviceName) .setAddress("10.0.0.1") .setPort(8080); runAsync(h -> ctx.writeClient().registerService(service, h)); List<Service> services = getAsync(h -> ctx.writeClient().localServices(h)); Service s = services.stream().filter(i -> "serviceName".equals(i.getName())).findFirst().get(); String serviceId = s.getId(); assertEquals(s.getAddress(), "10.0.0.1"); assertEquals(s.getPort(), 8080); CheckOptions check = new CheckOptions() .setId("checkId") .setName("checkName") .setNotes("checkNotes") .setServiceId(serviceId) .setStatus(CheckStatus.PASSING) .setTtl("10s"); runAsync(h -> ctx.writeClient().registerCheck(check, h)); List<Check> checks = getAsync(h -> ctx.writeClient().localChecks(h)); Check c = checks.stream().filter(i -> "checkId".equals(i.getId())).findFirst().get(); assertEquals(c.getServiceId(), serviceId); assertEquals(c.getStatus(), CheckStatus.PASSING); assertEquals(c.getNotes(), "checkNotes"); runAsync(h -> ctx.writeClient().deregisterService(serviceId, h)); }
@Test public void createLocalService(TestContext tc) { String serviceName = randomAlphaString(10); ServiceOptions opts = randomServiceOptions().setName(serviceName).setId(null); ctx.writeClient().registerService(opts, tc.asyncAssertSuccess(reg -> { ctx.writeClient().localServices(tc.asyncAssertSuccess(services -> { .orElseThrow(NoSuchElementException::new); tc.assertEquals(c.getId(), "service:" + serviceName); 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());
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)); }
private static String checkService(TestContext tc, List<Service> list, String name, ServiceOptions options) { Service s = list.stream() .filter(i -> name.equals(i.getName())) .findFirst() .orElseThrow(NoSuchElementException::new); String serviceId = s.getId(); tc.assertEquals(s.getTags(), options.getTags()); tc.assertEquals(s.getAddress(), options.getAddress()); tc.assertEquals(s.getPort(), options.getPort()); for (Map.Entry<String, String> entry : options.getMeta().entrySet()) { tc.assertEquals(s.getMeta().get(entry.getKey()), entry.getValue()); } return serviceId; }
@Override void createCheck(TestContext tc, CheckOptions opts, Handler<String> idHandler) { ServiceOptions options = randomServiceOptions().setCheckOptions(opts); ctx.writeClient().registerService(options, tc.asyncAssertSuccess(v -> idHandler.handle("service:" + options.getId()))); } }
@Test public void execute(TestContext tc) { PreparedQueryDefinition def = new PreparedQueryDefinition() .setService("service-${match(1)}-${match(2)}") .setTemplateType("name_prefix_match") .setTemplateRegexp("^find_(.+?)_(.+?)$"); ctx.writeClient().createPreparedQuery(def, tc.asyncAssertSuccess(qid -> { ctx.writeClient().executePreparedQuery("find_1_2", tc.asyncAssertSuccess(resp1 -> { tc.assertEquals(resp1.getNodes().size(), 0); ctx.writeClient() .registerService(new ServiceOptions().setName("service-1-2"), tc.asyncAssertSuccess(v1 -> { ctx.writeClient() .executePreparedQuery("find_1_2", tc.asyncAssertSuccess(resp2 -> { tc.assertEquals(resp2.getNodes().size(), 1); ctx.writeClient() .deregisterService("service-1-2", tc.asyncAssertSuccess(v2 -> { ctx.writeClient() .deletePreparedQuery(qid, tc.asyncAssertSuccess()); })); })); })); })); })); } }
public static io.vertx.ext.consul.ConsulService registerService(io.vertx.ext.consul.ConsulService j_receiver, java.util.Map<String, Object> serviceOptions, io.vertx.core.Handler<io.vertx.core.AsyncResult<java.lang.Void>> resultHandler) { io.vertx.core.impl.ConversionHelper.fromObject(j_receiver.registerService(serviceOptions != null ? new io.vertx.ext.consul.ServiceOptions(io.vertx.core.impl.ConversionHelper.toJsonObject(serviceOptions)) : null, resultHandler != null ? new io.vertx.core.Handler<io.vertx.core.AsyncResult<java.lang.Void>>() { public void handle(io.vertx.core.AsyncResult<java.lang.Void> ar) { resultHandler.handle(ar.map(event -> io.vertx.core.impl.ConversionHelper.fromObject(event))); } } : null)); return j_receiver; } public static io.vertx.ext.consul.ConsulService maintenanceService(io.vertx.ext.consul.ConsulService j_receiver, java.util.Map<String, Object> maintenanceOptions, io.vertx.core.Handler<io.vertx.core.AsyncResult<java.lang.Void>> resultHandler) {
@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); }
@Test public void healthServices() throws InterruptedException { runAsync(h -> ctx.writeClient().registerService(new ServiceOptions() .setName("service").setId("id1").setTags(Collections.singletonList("tag1")) .setCheckOptions(new CheckOptions().setTtl("5s").setStatus(CheckStatus.PASSING)), h)); runAsync(h -> ctx.writeClient().registerService(new ServiceOptions() .setName("service").setId("id2").setTags(Collections.singletonList("tag2")) .setCheckOptions(new CheckOptions().setTtl("5s").setStatus(CheckStatus.PASSING)), h));
@Test public void watchService() throws InterruptedException { StateConsumer<String> consumer = new StateConsumer<>(); ServiceOptions service = new ServiceOptions() .setCheckOptions(new CheckOptions() .setStatus(CheckStatus.PASSING) .setTtl("4s") .setName(randomAlphaString(10))) .setId(randomAlphaString(10)) .setName(randomAlphaString(10)); Watch<ServiceEntryList> watch = Watch.service(service.getName(), vertx, ctx.readClientOptions()) .setHandler(list -> { if (list.succeeded()) { consumer.consume(list.nextResult().getList() .stream().filter(s -> s.getService().getName().equals(service.getName())) .map(e -> e.getService().getName() + "/" + e.getChecks().stream() .filter(c -> c.getId().equals("service:" + service.getId())) .map(c -> c.getStatus().name()).findFirst().orElse("")) .findFirst().orElse("")); } }) .start(); consumer.await(""); runAsync(h -> ctx.writeClient().registerService(service, h)); consumer.await(service.getName() + "/" + CheckStatus.PASSING.name()); consumer.await(service.getName() + "/" + CheckStatus.CRITICAL.name()); consumer.check(); watch.stop(); runAsync(h -> ctx.writeClient().deregisterService(service.getId(), h)); }
public static io.vertx.ext.consul.ConsulClient registerService(io.vertx.ext.consul.ConsulClient j_receiver, java.util.Map<String, Object> serviceOptions, io.vertx.core.Handler<io.vertx.core.AsyncResult<java.lang.Void>> resultHandler) { io.vertx.core.impl.ConversionHelper.fromObject(j_receiver.registerService(serviceOptions != null ? new io.vertx.ext.consul.ServiceOptions(io.vertx.core.impl.ConversionHelper.toJsonObject(serviceOptions)) : null, resultHandler != null ? new io.vertx.core.Handler<io.vertx.core.AsyncResult<java.lang.Void>>() { public void handle(io.vertx.core.AsyncResult<java.lang.Void> ar) { resultHandler.handle(ar.map(event -> io.vertx.core.impl.ConversionHelper.fromObject(event))); } } : null)); return j_receiver; } public static io.vertx.ext.consul.ConsulClient maintenanceService(io.vertx.ext.consul.ConsulClient j_receiver, java.util.Map<String, Object> maintenanceOptions, io.vertx.core.Handler<io.vertx.core.AsyncResult<java.lang.Void>> resultHandler) {
@Test public void healthChecks() throws InterruptedException { ServiceOptions opts = new ServiceOptions() .setName("serviceName") .setId("serviceId") .setTags(Collections.singletonList("tag1")); runAsync(h -> ctx.writeClient().registerService(opts, h)); runAsync(h -> ctx.writeClient().registerCheck(new CheckOptions()
public static void toJson(ServiceOptions obj, java.util.Map<String, Object> json) { if (obj.getAddress() != null) { json.put("address", obj.getAddress()); } if (obj.getCheckOptions() != null) { json.put("checkOptions", obj.getCheckOptions().toJson()); } if (obj.getId() != null) { json.put("id", obj.getId()); } if (obj.getMeta() != null) { JsonObject map = new JsonObject(); obj.getMeta().forEach((key, value) -> map.put(key, value)); json.put("meta", map); } if (obj.getName() != null) { json.put("name", obj.getName()); } json.put("port", obj.getPort()); if (obj.getTags() != null) { JsonArray array = new JsonArray(); obj.getTags().forEach(item -> array.add(item)); json.put("tags", array); } } }
public static ServiceOptions randomServiceOptions() { return new ServiceOptions() .setId(randomAlphaString(10)) .setName(randomAlphaString(10)) .setTags(randomStringList(2)) .setCheckOptions(randomCheckOptions()) .setAddress(randomAlphaString(10)) .setMeta(randomMeta()) .setPort(randomPortInt()); }
private ServiceOptions recordToServiceOptions(Record record, String uuid) { ServiceOptions serviceOptions = new ServiceOptions(); serviceOptions.setName(record.getName()); JsonArray tags = new JsonArray(); if (record.getMetadata() != null) { serviceOptions.setCheckOptions(new CheckOptions(record.getMetadata().getJsonObject("checkoptions", new JsonObject()))); record.getMetadata().remove("checkoptions"); serviceOptions.setId(record.getRegistration()); } else { serviceOptions.setId(uuid); serviceOptions.setAddress(record.getLocation().getString("host")); serviceOptions.setPort(record.getLocation().getInteger("port")); serviceOptions.setTags(tags.stream().map(String::valueOf).collect(Collectors.toList())); return serviceOptions;