package examples;
import io.vertx.core.http.HttpClient;
import io.vertx.core.json.JsonObject;
import io.vertx.servicediscovery.Record;
import io.vertx.servicediscovery.ServiceDiscovery;
import io.vertx.servicediscovery.ServiceReference;
import io.vertx.servicediscovery.types.HttpEndpoint;
import io.vertx.ext.web.client.WebClient;
public class HTTPEndpointExamples {
public void example1(ServiceDiscovery discovery) {
Record record1 = HttpEndpoint.createRecord(
"some-http-service",
"localhost",
8433,
"/api"
);
discovery.publish(record1, ar -> {
});
Record record2 = HttpEndpoint.createRecord(
"some-other-name",
true,
"localhost",
8433,
"/api",
new JsonObject().put("some-metadata", "some value")
);
}
public void example2(ServiceDiscovery discovery) {
discovery.getRecord(new JsonObject().put("name", "some-http-service"), ar -> {
if (ar.succeeded() && ar.result() != null) {
ServiceReference reference = discovery.getReference(ar.result());
HttpClient client = reference.getAs(HttpClient.class);
client.getNow("/api/persons", response -> {
reference.release();
});
}
});
}
public void example2_webclient(ServiceDiscovery discovery) {
discovery.getRecord(new JsonObject().put("name", "some-http-service"), ar -> {
if (ar.succeeded() && ar.result() != null) {
ServiceReference reference = discovery.getReference(ar.result());
WebClient client = reference.getAs(WebClient.class);
client.get("/api/persons").send(
response -> {
reference.release();
});
}
});
}
public void example3(ServiceDiscovery discovery) {
HttpEndpoint.getClient(discovery, new JsonObject().put("name", "some-http-service"), ar -> {
if (ar.succeeded()) {
HttpClient client = ar.result();
client.getNow("/api/persons", response -> {
ServiceDiscovery.releaseServiceObject(discovery, client);
});
}
});
}
public void example3_webclient(ServiceDiscovery discovery) {
HttpEndpoint.getWebClient(discovery, new JsonObject().put("name", "some-http-service"), ar -> {
if (ar.succeeded()) {
WebClient client = ar.result();
client.get("/api/persons")
.send(response -> {
ServiceDiscovery.releaseServiceObject(discovery, client);
});
}
});
}
}