一、添加依赖
<dependency>
<groupId>io.etcd</groupId>
<artifactId>jetcd-core</artifactId>
<version>0.5.0</version>
</dependency>
二、demo程序
package cn.edu.tju;
import com.google.protobuf.ByteString;
import io.etcd.jetcd.ByteSequence;
import io.etcd.jetcd.Client;
import io.etcd.jetcd.KV;
import io.etcd.jetcd.kv.GetResponse;
import java.io.UnsupportedEncodingException;
import java.util.concurrent.CompletableFuture;
public class EtcdTest3 {
private static String SERVER_IP = "http://xx.xx.xx.xx:2379";
public static void main(String[] args) throws Exception {
ByteString username = ByteString.copyFrom("root".getBytes());
ByteString password = ByteString.copyFrom("root".getBytes());
Client client = Client.
builder()
//设置用户名
.user(ByteSequence.from(username))
//设置密码
.password(ByteSequence.from(password))
.endpoints(SERVER_IP).build();
KV kvClient = client.getKVClient();
ByteSequence key = ByteSequence.from(new String("foo").getBytes("utf-8"));
CompletableFuture<GetResponse> getFuture = kvClient.get(key);
GetResponse response = getFuture.get();
response.getKvs().forEach(item -> {
String yourKey = new String(item.getKey().getBytes());
String yourValue = new String(item.getValue().getBytes());
System.out.println(yourKey);
System.out.println(yourValue);
});
}
}
也可以使用IBM提供的客户端
<dependency>
<groupId>com.ibm.etcd</groupId>
<artifactId>etcd-java</artifactId>
<version>0.0.24</version>
</dependency>
package cn.edu.tju;
import com.google.common.util.concurrent.ListenableFuture;
import com.google.protobuf.ByteString;
import com.ibm.etcd.api.KeyValue;
import com.ibm.etcd.api.RangeResponse;
import com.ibm.etcd.client.EtcdClient;
import com.ibm.etcd.client.KvStoreClient;
import com.ibm.etcd.client.kv.KvClient;
import org.springframework.stereotype.Service;
import org.w3c.dom.ranges.Range;
import java.util.List;
public class EtcdTest2 {
private static String CONNECTION_STRING = "xx.xx.xx.xx";
private static String USERNAME="root";
private static String PASSWORD="root";
public static void main(String[] args) {
KvClient etcdClient = getEtcdClient();
ByteString bs = ByteString.copyFrom("foo".getBytes());
ByteString bs2 = ByteString.copyFrom("world2024".getBytes());
etcdClient.put(bs, bs2).timeout(30000).async();
KvClient.FluentRangeRequest fluentRangeRequest = etcdClient.get(bs).timeout(30000);
System.out.println("I will sleep 5 seconds...");
try {
Thread.sleep(5000);
} catch (InterruptedException e) {
e.printStackTrace();
}
ListenableFuture<RangeResponse> result = etcdClient.get(bs).timeout(30000).async();
RangeResponse rangeResponse = null;
try {
rangeResponse = result.get();
} catch (Exception ex) {
ex.printStackTrace();
System.out.println(ex.getMessage());
}
List<KeyValue> lst = rangeResponse.getKvsList();
for (KeyValue kv : lst) {
ByteString me = kv.getValue();
String hello = me.toStringUtf8();
System.out.println(hello);
}
}
public static KvClient getEtcdClient() {
KvStoreClient client = EtcdClient.forEndpoint(CONNECTION_STRING, 2379)
.withPlainText()
.withCredentials(USERNAME, PASSWORD)
.build();
KvClient kvClient = client.getKvClient();
return kvClient;
}
}