Skip to content

Commit

Permalink
Use test containers for Consul examples
Browse files Browse the repository at this point in the history
  • Loading branch information
vietj committed Nov 6, 2024
1 parent c56875e commit 1632f89
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 13 deletions.
15 changes: 3 additions & 12 deletions consul-examples/README.adoc
Original file line number Diff line number Diff line change
@@ -1,15 +1,6 @@
= Vert.x Consul Client examples
== Vert.x Consul Client examples

== Start Consul instance
This example consists of saving and retrieving a key/value pair in Consul.

To run this example, you need a running Consul instance

[source,bash]
----
consul agent -data-dir=/tmp/kekeke -server -ui -bootstrap=1
----

== Languages

* link:src/main/java/io/vertx/examples/consul/ConsulClientVerticle.java[ConsulClientVerticle.java]
link:src/main/java/io/vertx/examples/consul/ConsulClientVerticle.java[ConsulClientVerticle.java]

5 changes: 5 additions & 0 deletions consul-examples/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,11 @@
<artifactId>vertx-launcher-application</artifactId>
<version>${project.version}</version>
</dependency>
<dependency>
<groupId>org.testcontainers</groupId>
<artifactId>consul</artifactId>
<version>${testcontainers.version}</version>
</dependency>

<dependency>
<groupId>io.vertx</groupId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,25 +3,35 @@
import io.vertx.core.Future;
import io.vertx.core.VerticleBase;
import io.vertx.ext.consul.ConsulClient;
import io.vertx.ext.consul.ConsulClientOptions;
import io.vertx.launcher.application.VertxApplication;

import org.testcontainers.consul.ConsulContainer;

/**
* @author <a href="mailto:[email protected]">Ruslan Sennov</a>
*/
public class ConsulClientVerticle extends VerticleBase {

private static final ConsulContainer CONSUL_CONTAINER = new ConsulContainer("hashicorp/consul:1.15");

/**
* Convenience method so you can run it in your IDE
*/
public static void main(String[] args) {
CONSUL_CONTAINER.start();
VertxApplication.main(new String[]{ConsulClientVerticle.class.getName()});
}

private ConsulClient consulClient;

@Override
public Future<?> start() {
consulClient = ConsulClient.create(vertx);
ConsulClientOptions options = new ConsulClientOptions();
options.setHost(CONSUL_CONTAINER.getHost());
options.setPort(CONSUL_CONTAINER.getFirstMappedPort());
consulClient = ConsulClient.create(vertx, options);

return consulClient.putValue("key11", "value11")
.compose(v -> {
System.out.println("KV pair saved");
Expand Down

0 comments on commit 1632f89

Please sign in to comment.