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); } } }
@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 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; }
@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 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)); }
@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)); }