.setLocation(new JsonObject().put("endpoint", "the-service-address")) .setName("my-service") .setMetadata(new JsonObject().put("some-label", "some-value"));
/** * Create a record representing a data producer. * * @param name the name of the service * @param address the address on which the data is sent * @param type the type of payload (fully qualified name of the class) * @param metadata additional metadata * @return the created record */ static Record createRecord(String name, String address, String type, JsonObject metadata) { Objects.requireNonNull(name); Objects.requireNonNull(address); Record record = new Record().setName(name) .setType(TYPE) .setLocation(new JsonObject().put(Record.ENDPOINT, address)); if (metadata != null) { record.setMetadata(metadata); } if (type != null) { record.setMetadata(new JsonObject().put("message.type", type)); } return record; }
/** * Create a record representing a data producer. * * @param name the name of the service * @param address the address on which the data is sent * @param type the type of payload (fully qualified name of the class) * @param metadata additional metadata * @return the created record */ static Record createRecord(String name, String address, String type, JsonObject metadata) { Objects.requireNonNull(name); Objects.requireNonNull(address); Record record = new Record().setName(name) .setType(TYPE) .setLocation(new JsonObject().put(Record.ENDPOINT, address)); if (metadata != null) { record.setMetadata(metadata); } if (type != null) { record.setMetadata(new JsonObject().put("message.type", type)); } return record; }
static Record createRecord(String name, JsonObject location, JsonObject metadata) { Objects.requireNonNull(name); Objects.requireNonNull(location); Record record = new Record().setName(name) .setType(TYPE) .setLocation(location); if (metadata != null) { record.setMetadata(metadata); } return record; }
static Record createRecord(String name, JsonObject location, JsonObject metadata) { Objects.requireNonNull(name); Objects.requireNonNull(location); Record record = new Record().setName(name) .setType(TYPE) .setLocation(location); if (metadata != null) { record.setMetadata(metadata); } return record; }
/** * Sugar method to creates a record for this type. * <p> * The java interface is added to the metadata in the `service.interface` key. * * @param name the name of the service. * @param address the event bus address on which the service available * @param itf the Java interface (name) * @param metadata the metadata * @return the created record */ static Record createRecord(String name, String address, String itf, JsonObject metadata) { Objects.requireNonNull(name); Objects.requireNonNull(itf); Objects.requireNonNull(address); JsonObject meta; if (metadata == null) { meta = new JsonObject(); } else { meta = metadata.copy(); } return new Record() .setType(TYPE) .setName(name) .setMetadata(meta.put("service.interface", itf)) .setLocation(new JsonObject().put(Record.ENDPOINT, address)); }
/** * Sugar method to creates a record for this type. * <p> * The java interface is added to the metadata in the `service.interface` key. * * @param name the name of the service. * @param address the event bus address on which the service available * @param itf the Java interface (name) * @param metadata the metadata * @return the created record */ static Record createRecord(String name, String address, String itf, JsonObject metadata) { Objects.requireNonNull(name); Objects.requireNonNull(itf); Objects.requireNonNull(address); JsonObject meta; if (metadata == null) { meta = new JsonObject(); } else { meta = metadata.copy(); } return new Record() .setType(TYPE) .setName(name) .setMetadata(meta.put("service.interface", itf)) .setLocation(new JsonObject().put(Record.ENDPOINT, address)); }
/** * Convenient method to create a record for a Redis data source. * * @param name the service name * @param location the location of the service (e.g. url, port...) * @param metadata additional metadata * @return the created record */ static Record createRecord(String name, JsonObject location, JsonObject metadata) { Objects.requireNonNull(name); Objects.requireNonNull(location); Record record = new Record().setName(name) .setType(TYPE) .setLocation(location); if (metadata != null) { record.setMetadata(metadata); } return record; }
/** * Convenient method to create a record for a Redis data source. * * @param name the service name * @param location the location of the service (e.g. url, port...) * @param metadata additional metadata * @return the created record */ static Record createRecord(String name, JsonObject location, JsonObject metadata) { Objects.requireNonNull(name); Objects.requireNonNull(location); Record record = new Record().setName(name) .setType(TYPE) .setLocation(location); if (metadata != null) { record.setMetadata(metadata); } return record; }
/** * Convenient method to create a record for a Mongo data source. * * @param name the service name * @param location the location of the service (e.g. url, port...) * @param metadata additional metadata * @return the created record */ static Record createRecord(String name, JsonObject location, JsonObject metadata) { Objects.requireNonNull(name); Objects.requireNonNull(location); Record record = new Record().setName(name) .setType(TYPE) .setLocation(location); if (metadata != null) { record.setMetadata(metadata); } return record; }
/** * Convenient method to create a record for a Mongo data source. * * @param name the service name * @param location the location of the service (e.g. url, port...) * @param metadata additional metadata * @return the created record */ static Record createRecord(String name, JsonObject location, JsonObject metadata) { Objects.requireNonNull(name); Objects.requireNonNull(location); Record record = new Record().setName(name) .setType(TYPE) .setLocation(location); if (metadata != null) { record.setMetadata(metadata); } return record; }
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; }); } }
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 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; }
/** * Same as {@link #createRecord(String, String, int, String, JsonObject)} but let you configure whether or not the * service is using {@code https}. * * @param name the service name * @param ssl whether or not the service is using HTTPS * @param host the host (IP or DNS name), it must be the _public_ IP / name * @param port the port, it must be the _public_ port * @param root the path of the service, "/" if not set * @param metadata additional metadata * @return the created record */ static Record createRecord(String name, boolean ssl, String host, int port, String root, JsonObject metadata) { Objects.requireNonNull(name); Objects.requireNonNull(host); if (root == null) { root = "/"; } Record record = new Record().setName(name) .setType(TYPE) .setLocation(new HttpLocation() .setSsl(ssl).setHost(host).setPort(port).setRoot(root).toJson()); if (metadata != null) { record.setMetadata(metadata); } return record; }
/** * Same as {@link #createRecord(String, String, int, String, JsonObject)} but let you configure whether or not the * service is using {@code https}. * * @param name the service name * @param ssl whether or not the service is using HTTPS * @param host the host (IP or DNS name), it must be the _public_ IP / name * @param port the port, it must be the _public_ port * @param root the path of the service, "/" if not set * @param metadata additional metadata * @return the created record */ static Record createRecord(String name, boolean ssl, String host, int port, String root, JsonObject metadata) { Objects.requireNonNull(name); Objects.requireNonNull(host); if (root == null) { root = "/"; } Record record = new Record().setName(name) .setType(TYPE) .setLocation(new HttpLocation() .setSsl(ssl).setHost(host).setPort(port).setRoot(root).toJson()); if (metadata != null) { record.setMetadata(metadata); } return record; }
.setMetadata(new JsonObject() .put("key", "A") .put("service.interface", HelloService.class.getName()))
@Before public void setUp() { vertx = Vertx.vertx(); options = new ServiceDiscoveryOptions().setName("theDiscovery") .setAnnounceAddress("theAnnounceAddress").setUsageAddress("theUsageAddress"); discovery = ServiceDiscovery.create(vertx, options); record = new Record() .setName("theRecord") .setType(Queryable.SERVICE_TYPE) .setMetadata(new JsonObject().put("publisherId", "thePublisherId")) .setLocation(new JsonObject().put(Record.ENDPOINT, Queryable.ADDRESS_PREFIX + ".DroidQueries")) .setStatus(Status.UP); definition = SchemaDefinition.createInstance(droidsSchema, SchemaMetadata.create(new JsonObject().put("publisherId", "thePublisherId"))); consumer = ProxyHelper.registerService(Queryable.class, vertx, definition, Queryable.ADDRESS_PREFIX + ".DroidQueries"); }
@Test public void testAnnounce() { List<Record> announces = new ArrayList<>(); vertx.eventBus().consumer(ServiceDiscoveryOptions.DEFAULT_ANNOUNCE_ADDRESS, msg -> announces.add(new Record((JsonObject) msg.body()))); HelloService svc = new HelloServiceImpl("stuff"); ProxyHelper.registerService(HelloService.class, vertx, svc, "address"); Record record = new Record() .setName("Hello") .setMetadata(new JsonObject().put("key", "A")) .setLocation(new JsonObject().put(Record.ENDPOINT, "address")); Record record2 = new Record() .setName("Hello-2") .setMetadata(new JsonObject().put("key", "B")) .setLocation(new JsonObject().put(Record.ENDPOINT, "address2")); discovery.publish(record, (r) -> { }); discovery.publish(record2, (r) -> { }); await().until(() -> record.getRegistration() != null); await().until(() -> record2.getRegistration() != null); await().until(() -> announces.size() == 2); for (Record rec : announces) { assertThat(rec.getStatus()).isEqualTo(Status.UP); } discovery.unpublish(record2.getRegistration(), v -> { }); await().until(() -> announces.size() == 3); assertThat(announces.get(2).getStatus()).isEqualTo(Status.DOWN); }
@Test public void testAnnounce() { List<Record> announces = new ArrayList<>(); vertx.eventBus().consumer(ServiceDiscoveryOptions.DEFAULT_ANNOUNCE_ADDRESS, msg -> announces.add(new Record((JsonObject) msg.body()))); HelloService svc = new HelloServiceImpl("stuff"); ProxyHelper.registerService(HelloService.class, vertx, svc, "address"); Record record = new Record() .setName("Hello") .setMetadata(new JsonObject().put("key", "A")) .setLocation(new JsonObject().put(Record.ENDPOINT, "address")); Record record2 = new Record() .setName("Hello-2") .setMetadata(new JsonObject().put("key", "B")) .setLocation(new JsonObject().put(Record.ENDPOINT, "address2")); discovery.publish(record, (r) -> { }); discovery.publish(record2, (r) -> { }); await().until(() -> record.getRegistration() != null); await().until(() -> record2.getRegistration() != null); await().until(() -> announces.size() == 2); for (Record rec : announces) { assertThat(rec.getStatus()).isEqualTo(Status.UP); } discovery.unpublish(record2.getRegistration(), v -> { }); await().until(() -> announces.size() == 3); assertThat(announces.get(2).getStatus()).isEqualTo(Status.DOWN); }