Skip to content

Commit

Permalink
Consul service resolver example
Browse files Browse the repository at this point in the history
  • Loading branch information
vietj committed Oct 21, 2024
1 parent 73138ae commit fe48bbb
Show file tree
Hide file tree
Showing 5 changed files with 104 additions and 130 deletions.
8 changes: 5 additions & 3 deletions README.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,11 @@ Just right-click the main method or class in your IDE and run as... application

The link:core-examples/README.adoc[Vert.x core examples] contains a wide range of examples using just Vert.x Core.

=== Service resolver examples

The link:service-resolver-examples/README.adoc[Vert.x Service Resolver] examples contain client
using a resolver to discover service endpoints.

=== Vert.x-Web examples

Vert.x-Web is a toolkit for building web applications using Vert.x
Expand Down Expand Up @@ -143,6 +148,3 @@ with link:https://micrometer.io/[Micrometer] and send them to backends such as P
=== GraphQL examples

The link:web-graphql-examples/README.adoc[Vert.x Web GraphQL] examples contain simple client/server GraphQL applications built with https://vertx.io/docs/vertx-web-graphql/java/[Vert.x Web GraphQL] and the https://www.graphql-java.com/[GraphQL-Java] library.



40 changes: 7 additions & 33 deletions service-resolver-examples/README.adoc
Original file line number Diff line number Diff line change
@@ -1,38 +1,12 @@
= Vert.x Service Discovery examples
= Vert.x Service Resolver examples

Here you will find examples demonstrating Vert.x Service Discovery
Here you will find examples demonstrating Vert.x Service Resolver

Vert.x service discovery provides an infrastructure to publish and discover various resources, such as service proxies, HTTP endpoints, data sources and etc.
The Vert.x service resolver is a plugin that lets Vert.x clients call services using logical service names instead of network addresses. The service resolver is also able to perform client side load balancing.

A service provider can:
== Consul DNS example

* publish a service record
* un-publish a published record (withdraw a record)
* update the status of a published service (down, out of service…​)
A service consumer can:

* lookup for services
* bind to a selected service (it gets a ServiceReference) and use it
* release the service once the consumer is done with it
* listen for arrival, departure and modification of services.
Providers and consumers must create their own ServiceDiscovery instance. These instances are collaborating in background to keep the set of services in sync.

== ServiceDiscoveryVerticle example

* Creates and publish a few services

* Unpublish a service

* Consumes a published service and release it.

You can run the ServiceDiscoveryVerticle.java class from your IDE or call:

vertx run src/main/java/io/vertx/example/service/discovery/ServiceDiscoveryVerticle.java
This example demonstrates how to configure the Vert.x HTTP client to resolve service address against a Consul
server using DNS.

The link:src/main/java/io/vertx/example/serviceresolver/consul/HttpClientWithConsulVerticle.java[HTTP client resolver with Consul DNS]
7 changes: 6 additions & 1 deletion service-resolver-examples/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,14 @@
<artifactId>service-resolver-examples</artifactId>

<dependencies>
<dependency>
<groupId>org.testcontainers</groupId>
<artifactId>testcontainers</artifactId>
<version>1.20.1</version>
</dependency>
<dependency>
<groupId>io.vertx</groupId>
<artifactId>vertx-service-discovery</artifactId>
<artifactId>vertx-service-resolver</artifactId>
<version>${project.version}</version>
</dependency>
<dependency>
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
package io.vertx.example.serviceresolver.consul;

import io.vertx.core.Future;
import io.vertx.core.VerticleBase;
import io.vertx.core.Vertx;
import io.vertx.core.http.*;
import io.vertx.core.json.JsonObject;
import io.vertx.core.net.SocketAddress;
import io.vertx.serviceresolver.ServiceAddress;
import io.vertx.serviceresolver.srv.SrvResolver;
import io.vertx.serviceresolver.srv.SrvResolverOptions;
import org.testcontainers.containers.FixedHostPortGenericContainer;
import org.testcontainers.containers.GenericContainer;
import org.testcontainers.containers.InternetProtocol;
import org.testcontainers.containers.wait.strategy.HostPortWaitStrategy;

import java.util.ArrayList;
import java.util.List;

public class HttpClientWithConsulVerticle extends VerticleBase {

private static final int NUM_SERVERS = 3;
private static final int NUM_QUERIES = 10;

public static void main(String[] args) throws Exception {

// Start a consul container, need to expose UDP
GenericContainer<?> container = new FixedHostPortGenericContainer<>("consul:1.9")
.withFixedExposedPort(8500, 8500)
.withFixedExposedPort(8600, 8600, InternetProtocol.UDP);
container.setWaitStrategy(new HostPortWaitStrategy().forPorts(8500));
container.start();

Vertx vertx = Vertx.vertx();

// Create a few services and populate consul
HttpClientAgent client = vertx.createHttpClient();
for (int i = 0; i < NUM_SERVERS; i++) {
String serviceId = "app" + i;
int servicePort = 8080 + i;
String serviceAddress = "localhost";
vertx.createHttpServer().requestHandler(req -> req
.response()
.end(serviceId))
.listen(servicePort, serviceAddress)
.await();
client
.request(HttpMethod.PUT, 8500, "localhost", "/v1/agent/service/register")
.compose(req -> req
.send(new JsonObject().put("ID", serviceId).put("Name", "svc").put("Address", serviceAddress).put("Port", servicePort).encode())
.expecting(HttpResponseExpectation.SC_OK)
.compose(HttpClientResponse::end))
.await();
}

vertx.deployVerticle(new HttpClientWithConsulVerticle());
}

private HttpClientAgent client;

@Override
public Future<?> start() {
client = vertx.httpClientBuilder().withAddressResolver(SrvResolver.create(new SrvResolverOptions()
.setServer(SocketAddress.inetSocketAddress(8600, "127.0.0.1"))
.setMinTTL(5)
)).build();

List<Future<?>> futs = new ArrayList<>();

for (int i = 0; i < NUM_QUERIES; i++) {
int idx = i;
ServiceAddress addr = ServiceAddress.of("svc.service.consul");
Future<?> fut = client.request(new RequestOptions().setServer(addr)).compose(req -> req
.send()
.expecting(HttpResponseExpectation.SC_OK)
.compose(HttpClientResponse::body)).andThen(ar -> {
if (ar.succeeded()) {
System.out.println(idx + " -> " + ar.result());
}
});
futs.add(fut);
}

return Future.all(futs);
}
}

0 comments on commit fe48bbb

Please sign in to comment.