private void checkService(Service expected, Service actual) { assertEquals(expected, actual); assertEquals(expected.hashCode(), actual.hashCode()); assertEquals(expected.getNode(), actual.getNode()); assertEquals(expected.getNodeAddress(), actual.getNodeAddress()); assertEquals(expected.getId(), actual.getId()); assertEquals(expected.getName(), actual.getName()); assertEquals(expected.getAddress(), actual.getAddress()); assertEquals(expected.getPort(), actual.getPort()); List<String> expectedList = expected.getTags(); List<String> actualList = actual.getTags(); assertTrue(expectedList.containsAll(actualList)); assertTrue(actualList.containsAll(expectedList)); }
@Test public void testServiceCopy() { Service service = randomService(); checkService(service, new Service(service)); checkService(service, new Service(service.toJson())); List<String> tagsSuffled = new ArrayList<>(); tagsSuffled.add(service.getTags().get(1)); tagsSuffled.add(service.getTags().get(0)); Service shuffled = new Service(service) .setTags(tagsSuffled); checkService(service, shuffled); }
static Service parseNodeInfo(String nodeName, String nodeAddress, JsonObject serviceInfo) { JsonArray tagsArr = serviceInfo.getJsonArray(AGENT_SERVICE_TAGS); return new Service() .setNode(nodeName) .setNodeAddress(nodeAddress) .setId(serviceInfo.getString(AGENT_SERVICE_ID)) .setAddress(serviceInfo.getString(AGENT_SERVICE_ADDRESS)) .setMeta(mapStringString(serviceInfo.getJsonObject(AGENT_SERVICE_META))) .setName(serviceInfo.getString(AGENT_SERVICE_SERVICE)) .setTags(listOf(tagsArr)) .setPort(serviceInfo.getInteger(AGENT_SERVICE_PORT)); }
static Service parseAgentInfo(JsonObject jsonObject) { JsonArray tagsArr = jsonObject.getJsonArray(AGENT_SERVICE_TAGS); return new Service() .setId(jsonObject.getString(AGENT_SERVICE_ID)) .setName(jsonObject.getString(AGENT_SERVICE_SERVICE)) .setTags(listOf(tagsArr)) .setMeta(mapStringString(jsonObject.getJsonObject(AGENT_SERVICE_META))) .setAddress(jsonObject.getString(AGENT_SERVICE_ADDRESS)) .setPort(jsonObject.getInteger(AGENT_SERVICE_PORT)); }
private Record createRecord(Service service) { String address = service.getAddress(); int port = service.getPort(); JsonObject metadata = service.toJson(); if (service.getTags() != null) { service.getTags().forEach(tag -> metadata.put(tag, true)); } Record record = new Record() .setName(service.getName()) .setMetadata(metadata); // To determine the record type, check if we have a tag with a "type" name record.setType(ServiceType.UNKNOWN); ServiceTypes.all().forEachRemaining(type -> { if (metadata.getBoolean(type.name(), false)) { record.setType(type.name()); } }); JsonObject location = new JsonObject(); location.put("host", address); location.put("port", port); // Manage HTTP endpoint if (record.getType().equals("http-endpoint")) { if (metadata.getBoolean("ssl", false)) { location.put("ssl", true); } location = new HttpLocation(location).toJson(); } record.setLocation(location); return record; }
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; }
private Future serviceToRecord(Service service) { //use the checks to set the record status Future<CheckList> checkListFuture = Future.future(); client.healthChecks(service.getName(), checkListFuture); return checkListFuture.map(cl -> cl.getList().stream().map(Check::getStatus).allMatch(CheckStatus.PASSING::equals)) .map(st -> st ? new Record().setStatus(Status.UP) : new Record().setStatus(Status.DOWN)) .map(record -> { record.setMetadata(new JsonObject()); record.setLocation(new JsonObject()); record.setName(service.getName()); record.setRegistration(service.getId()); List<String> tags = service.getTags(); record.setType(ServiceType.UNKNOWN); ServiceTypes.all().forEachRemaining(type -> { if (service.getTags().contains(type.name())) { record.setType(type.name()); tags.remove(type.name()); } }); //retrieve the metadata object tags.stream().filter(t -> t.startsWith("metadata:")).map(s -> s.substring("metadata:".length())).map(JsonObject::new).forEach(json -> record.getMetadata().mergeIn(json)); //retrieve the location object tags.stream().filter(t -> t.startsWith("location:")).map(s -> s.substring("location:".length())).map(JsonObject::new).forEach(json -> record.getLocation().mergeIn(json)); record.getMetadata().put("tags", new JsonArray(tags.stream().filter(t -> !t.startsWith("metadata:") && !t.startsWith("location:")).collect(Collectors.toList()))); return record; }); } }
@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)); }
@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); }
client.healthServiceNodes(service.getName(),false, ar -> { if (ar.succeeded()) { importService(ar.result().getList(), future);
private List<ServiceEntry> sorted() { List<ServiceEntry> sorted = null; if (list != null) { sorted = new ArrayList<>(list); sorted.sort(Comparator.comparing(e -> e.getService().getId())); } return sorted; } }
public static void toJson(ServiceList obj, java.util.Map<String, Object> json) { json.put("index", obj.getIndex()); if (obj.getList() != null) { JsonArray array = new JsonArray(); obj.getList().forEach(item -> array.add(item.toJson())); json.put("list", array); } } }
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; } } }
@Override public int hashCode() { int result = node != null ? node.hashCode() : 0; result = 31 * result + (service != null ? service.hashCode() : 0); result = 31 * result + (checks != null ? sorted().hashCode() : 0); return result; }
private Record createRecord(Service service) { String address = service.getAddress(); int port = service.getPort(); JsonObject metadata = service.toJson(); if (service.getTags() != null) { service.getTags().forEach(tag -> metadata.put(tag, true)); } Record record = new Record() .setName(service.getName()) .setMetadata(metadata); // To determine the record type, check if we have a tag with a "type" name record.setType(ServiceType.UNKNOWN); ServiceTypes.all().forEachRemaining(type -> { if (metadata.getBoolean(type.name(), false)) { record.setType(type.name()); } }); JsonObject location = new JsonObject(); location.put("host", address); location.put("port", port); // Manage HTTP endpoint if (record.getType().equals("http-endpoint")) { if (metadata.getBoolean("ssl", false)) { location.put("ssl", true); } location = new HttpLocation(location).toJson(); } record.setLocation(location); return record; }
@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()); }
client.healthServiceNodes(service.getName(),false, ar -> { if (ar.succeeded()) { importService(ar.result().getList(), future);