Table of Contents(中文说明)
An etcd v3 client written by Java.
jdk 1.8+
maven-3.3.1+
<dependency>
<groupId>cn.nextop</groupId>
<artifactId>gadget-etcd</artifactId>
<version>1.0.0</version>
</dependency>
$mvn clean install -Dmaven.test.skip=true
final String[] endpoints = new String[]{ "http://192.168.1.100:2379",
"http://192.168.1.101:2379", "http://192.168.1.102:2379" };
final ClientImpl.Builder builder = ClientImpl.builder();
final NameResolverFactory factory = new NameResolverFactory(endpoints);
builder.setNameResolverFactory(factory);
Client client = builder.build("my client");
final String[] endpoints = new String[]{ "http://192.168.1.100:2379",
"http://192.168.1.101:2379", "http://192.168.1.102:2379" };
final ClientImpl.Builder builder = ClientImpl.builder();
final NameResolverFactory factory = new NameResolverFactory(endpoints);
builder.setNameResolverFactory(factory);
Client client = builder.build("my client");
KV kv = client.getKV();
PutRequest.Builder request = PutRequest.newBuilder();
request.setKey(ByteString.copyFrom("key".getBytes()));
request.setValue(ByteString.copyFrom("value".getBytes()));
CompletableFuture<PutResponse> future = kv.put(request.build());
// your code goes here.
final String[] endpoints = new String[]{ "dns+srv://etcd.company.cn" };
final ClientImpl.Builder builder = ClientImpl.builder();
final NameResolverFactory factory = new NameResolverFactory(endpoints);
builder.setNameResolverFactory(factory);
Client client = builder.build("my client");
// your code goes here.
final String[] endpoints = new String[]{ "dns+srv://etcd.company.cn" };
final ClientImpl.Builder builder = ClientImpl.builder();
final NameResolverFactory factory = new NameResolverFactory(endpoints);
builder.setNameResolverFactory(factory);
Client client = builder.build("my client");
Watch watch = client.getWatch();
// request
WatchRequest.Builder request = WatchRequest.newBuilder();
WatchCreateRequest.Builder create = WatchCreateRequest.newBuilder();
create.setKey(ByteString.copyFrom("key".getBytes())).build();
request.setCreateRequest(create.build());
// watch
Watcher<List<Event>> watcher = watch.watch(request.build(), new WatchListener<List<Event>>() {
@Override
public void onWatched(CompletableFuture<List<Event>> future) {
try {
List<Event> events = future.get();
// your code goes here.
} catch (Throwable e) {
}
}
}, r -> r.getEventsList());
// your other business
watcher.close();