-
Notifications
You must be signed in to change notification settings - Fork 2.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
104 additions
and
130 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
93 changes: 0 additions & 93 deletions
93
...r-examples/src/main/java/io/vertx/example/service/discovery/ServiceDiscoveryVerticle.java
This file was deleted.
Oops, something went wrong.
86 changes: 86 additions & 0 deletions
86
...s/src/main/java/io/vertx/example/serviceresolver/consul/HttpClientWithConsulVerticle.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |