Bael-7439-intro-to-etcd (#15892)
* Update pom.xml * Create JetcdExample.java
This commit is contained in:
parent
abb1f462b9
commit
536ffe9106
|
@ -95,6 +95,12 @@
|
||||||
<artifactId>protobuf-java</artifactId>
|
<artifactId>protobuf-java</artifactId>
|
||||||
<version>${google-protobuf.version}</version>
|
<version>${google-protobuf.version}</version>
|
||||||
</dependency>
|
</dependency>
|
||||||
|
<!-- jetcd core api -->
|
||||||
|
<dependency>
|
||||||
|
<groupId>io.etcd</groupId>
|
||||||
|
<artifactId>jetcd-core</artifactId>
|
||||||
|
<version>${jetcd-version}</version>
|
||||||
|
</dependency>
|
||||||
</dependencies>
|
</dependencies>
|
||||||
|
|
||||||
<properties>
|
<properties>
|
||||||
|
@ -110,6 +116,7 @@
|
||||||
<yamlbeans.version>1.15</yamlbeans.version>
|
<yamlbeans.version>1.15</yamlbeans.version>
|
||||||
<apache-thrift.version>0.14.2</apache-thrift.version>
|
<apache-thrift.version>0.14.2</apache-thrift.version>
|
||||||
<google-protobuf.version>3.17.3</google-protobuf.version>
|
<google-protobuf.version>3.17.3</google-protobuf.version>
|
||||||
|
<jetcd-version>0.7.7</jetcd-version>
|
||||||
</properties>
|
</properties>
|
||||||
|
|
||||||
</project>
|
</project>
|
||||||
|
|
|
@ -0,0 +1,32 @@
|
||||||
|
package com.baeldung.etcd;
|
||||||
|
|
||||||
|
import java.util.concurrent.CompletableFuture;
|
||||||
|
|
||||||
|
import io.etcd.jetcd.kv.GetResponse;
|
||||||
|
import io.etcd.jetcd.ByteSequence;
|
||||||
|
import io.etcd.jetcd.KV;
|
||||||
|
import io.etcd.jetcd.Client;
|
||||||
|
|
||||||
|
public class JetcdExample {
|
||||||
|
public static void main(String[] args) {
|
||||||
|
String etcdEndpoint = "http://localhost:2379";
|
||||||
|
ByteSequence key = ByteSequence.from("/mykey".getBytes());
|
||||||
|
ByteSequence value = ByteSequence.from("Hello, etcd!".getBytes());
|
||||||
|
|
||||||
|
try (Client client = Client.builder().endpoints(etcdEndpoint).build()) {
|
||||||
|
KV kvClient = client.getKVClient();
|
||||||
|
|
||||||
|
// Put a key-value pair
|
||||||
|
kvClient.put(key, value).get();
|
||||||
|
|
||||||
|
// Retrieve the value using CompletableFuture
|
||||||
|
CompletableFuture<GetResponse> getFuture = kvClient.get(key);
|
||||||
|
GetResponse response = getFuture.get();
|
||||||
|
|
||||||
|
// Delete the key
|
||||||
|
kvClient.delete(key).get();
|
||||||
|
} catch (Exception e) {
|
||||||
|
e.printStackTrace();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in New Issue