Bael-7439-intro-to-etcd (#15892)

* Update pom.xml

* Create JetcdExample.java
This commit is contained in:
vaibhav007jain 2024-02-20 11:34:26 +05:30 committed by GitHub
parent abb1f462b9
commit 536ffe9106
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 40 additions and 1 deletions

View File

@ -95,6 +95,12 @@
<artifactId>protobuf-java</artifactId>
<version>${google-protobuf.version}</version>
</dependency>
<!-- jetcd core api -->
<dependency>
<groupId>io.etcd</groupId>
<artifactId>jetcd-core</artifactId>
<version>${jetcd-version}</version>
</dependency>
</dependencies>
<properties>
@ -110,6 +116,7 @@
<yamlbeans.version>1.15</yamlbeans.version>
<apache-thrift.version>0.14.2</apache-thrift.version>
<google-protobuf.version>3.17.3</google-protobuf.version>
<jetcd-version>0.7.7</jetcd-version>
</properties>
</project>
</project>

View File

@ -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();
}
}
}