package examples;
import io.vertx.core.Vertx;
import io.vertx.core.eventbus.MessageConsumer;
import io.vertx.core.http.HttpClient;
import io.vertx.core.json.JsonObject;
import io.vertx.servicediscovery.ServiceDiscoveryOptions;
import io.vertx.servicediscovery.ServiceDiscovery;
import io.vertx.servicediscovery.Record;
import io.vertx.servicediscovery.ServiceReference;
import io.vertx.servicediscovery.types.HttpEndpoint;
import io.vertx.ext.jdbc.JDBCClient;
import java.util.List;
public class ServiceDiscoveryExamples {
public void example1(Vertx vertx) {
ServiceDiscovery discovery = ServiceDiscovery.create(vertx);
discovery = ServiceDiscovery.create(vertx,
new ServiceDiscoveryOptions()
.setAnnounceAddress("service-announce")
.setName("my-name"));
discovery.close();
}
public void example2(ServiceDiscovery discovery) {
Record record = new Record()
.setType("eventbus-service-proxy")
.setLocation(new JsonObject().put("endpoint", "the-service-address"))
.setName("my-service")
.setMetadata(new JsonObject().put("some-label", "some-value"));
discovery.publish(record, ar -> {
if (ar.succeeded()) {
Record publishedRecord = ar.result();
} else {
}
});
record = HttpEndpoint.createRecord("some-rest-api", "localhost", 8080, "/api");
discovery.publish(record, ar -> {
if (ar.succeeded()) {
Record publishedRecord = ar.result();
} else {
}
});
}
public void example3(ServiceDiscovery discovery, Record record) {
discovery.unpublish(record.getRegistration(), ar -> {
if (ar.succeeded()) {
} else {
}
});
}
public void example4(ServiceDiscovery discovery) {
discovery.getRecord(r -> true, ar -> {
if (ar.succeeded()) {
if (ar.result() != null) {
} else {
}
} else {
}
});
discovery.getRecord((JsonObject) null, ar -> {
if (ar.succeeded()) {
if (ar.result() != null) {
} else {
}
} else {
}
});
discovery.getRecord(r -> r.getName().equals("some-name"), ar -> {
if (ar.succeeded()) {
if (ar.result() != null) {
} else {
}
} else {
}
});
discovery.getRecord(new JsonObject().put("name", "some-service"), ar -> {
if (ar.succeeded()) {
if (ar.result() != null) {
} else {
}
} else {
}
});
discovery.getRecords(r -> "some-value".equals(r.getMetadata().getString("some-label")), ar -> {
if (ar.succeeded()) {
List<Record> results = ar.result();
} else {
}
});
discovery.getRecords(new JsonObject().put("some-label", "some-value"), ar -> {
if (ar.succeeded()) {
List<Record> results = ar.result();
} else {
}
});
}
public void example5(ServiceDiscovery discovery, Record record1, Record record2) {
ServiceReference reference1 = discovery.getReference(record1);
ServiceReference reference2 = discovery.getReference(record2);
HttpClient client = reference1.getAs(HttpClient.class);
MessageConsumer consumer = reference2.getAs(MessageConsumer.class);
reference1.release();
reference2.release();
}
public void example51(ServiceDiscovery discovery, Record record, JsonObject conf) {
ServiceReference reference = discovery.getReferenceWithConfiguration(record, conf);
JDBCClient client = reference.getAs(JDBCClient.class);
reference.release();
}
}