@Override public void start() throws Exception { ConsulClient consulClient = ConsulClient.create(vertx); consulClient.putValue("key11", "value11", putResult -> { if (putResult.succeeded()) { System.out.println("KV pair saved"); consulClient.getValue("key11", getResult -> { if (getResult.succeeded()) { System.out.println("KV pair retrieved"); System.out.println(getResult.result().getValue()); } else { getResult.cause().printStackTrace(); } }); } else { putResult.cause().printStackTrace(); } }); } }
/** * Creates {@code Watch} to monitoring a prefix of keys in the KV store. * The underlying Consul client will be created with default options. * This maps to the <a href="https://www.consul.io/api/kv.html">/v1/kv/</a> API internally. * * @param keyPrefix the key * @param vertx the {@code Vertx} instance * @return the {@code Watch} instance */ static Watch<KeyValueList> keyPrefix(String keyPrefix, Vertx vertx) { return keyPrefix(keyPrefix, vertx, new ConsulClientOptions()); }
/** * Creates {@code Watch} to monitoring the list of available services. * The underlying Consul client will be created with default options. * This maps to the <a href="https://www.consul.io/docs/agent/http/catalog.html#catalog_services">/v1/catalog/services</a> API internally. * * @param vertx the {@code Vertx} instance * @return the {@code Watch} instance */ static Watch<ServiceList> services(Vertx vertx) { return services(vertx, new ConsulClientOptions()); }
@Override String createCheck(CheckOptions opts) { String serviceId = "serviceId"; ServiceOptions service = new ServiceOptions() .setName("serviceName") .setId(serviceId) .setTags(Arrays.asList("tag1", "tag2")) .setCheckOptions(opts) .setAddress("10.0.0.1") .setPort(8080); runAsync(h -> ctx.writeClient().registerService(service, h)); return "service:" + serviceId; }
@Override public void init(Vertx vertx, JsonObject config) { ConsulClientOptions opt = new ConsulClientOptions(config); this.client = ConsulClient.create(vertx, opt); }
public static CheckOptions randomCheckOptions() { return new CheckOptions() .setId(randomAlphaString(10)) .setName(randomAlphaString(10)) .setNotes(randomAlphaString(10)) .setServiceId(randomAlphaString(10)) .setStatus(randomElement(CheckStatus.values())) .setTtl(randomSeconds()); }
/** * Creates {@code Watch} to monitoring the nodes providing the service. * The underlying Consul client will be created with default options. * This maps to the <a href="https://www.consul.io/docs/agent/http/health.html#health_service">/v1/health/service/<service></a> API internally. * * @param service the service name * @param vertx the {@code Vertx} instance * @return the {@code Watch} instance */ static Watch<ServiceEntryList> service(String service, Vertx vertx) { return service(service, vertx, new ConsulClientOptions()); }
/** * Creates {@code Watch} to monitoring a specific key in the KV store. * The underlying Consul client will be created with default options. * This maps to the <a href="https://www.consul.io/api/kv.html">/v1/kv/</a> API internally. * * @param key the key * @param vertx the {@code Vertx} instance * @return the {@code Watch} instance */ static Watch<KeyValue> key(String key, Vertx vertx) { return key(key, vertx, new ConsulClientOptions()); }
/** * Creates {@code Watch} to monitoring the custom user events. * The underlying Consul client will be created with default options. * This maps to the <a href="https://www.consul.io/docs/agent/http/event.html#event_list">/v1/event/list</a> API internally. * * @param event the event name * @param vertx the {@code Vertx} instance * @return the {@code Watch} instance */ static Watch<EventList> events(String event, Vertx vertx) { return events(event, vertx, new ConsulClientOptions()); }
@Override void createCheck(TestContext tc, CheckOptions opts, Handler<String> idHandler) { ServiceOptions options = randomServiceOptions().setCheckOptions(opts); ctx.writeClient().registerService(options, tc.asyncAssertSuccess(v -> idHandler.handle("service:" + options.getId()))); } }
public static ServiceEntry randomServiceEntry() { return new ServiceEntry() .setNode(randomNode()) .setService(randomService()) .setChecks(randomObjectList(2, RandomObjects::randomCheck)); }
public static SessionOptions randomSessionOptions() { return new SessionOptions() .setBehavior(randomElement(SessionBehavior.values())) .setChecks(randomStringList(2)) .setLockDelay(randomPositiveInt()) .setName(randomAlphaString(10)) .setNode(randomAlphaString(10)) .setTtl(10 + (randomPositiveInt() % 100)); }
@Override String createCheck(CheckOptions opts) { String id = opts.getId(); if (id == null) { id = "checkId"; opts.setId(id); } runAsync(h -> ctx.writeClient().registerCheck(opts, h)); return id; }
@Test public void testAclToken() { AclToken token = randomAclToken(); checkAclToken(token, new AclToken(token)); checkAclToken(token, new AclToken(token.toJson())); }
@Test public void testSessionOptions() { SessionOptions opts = randomSessionOptions(); checkSessionOptions(opts, new SessionOptions(opts)); checkSessionOptions(opts, new SessionOptions(opts.toJson())); SessionOptions nullChecks = opts.setChecks(null); checkSessionOptions(nullChecks, new SessionOptions(nullChecks)); checkSessionOptions(nullChecks, new SessionOptions(nullChecks.toJson())); }
@Test public void testPreparedQueryDefinitionCopy() { PreparedQueryDefinition pqd = randomPreparedQueryDefinition(); checkPreparedQueryDefinition(pqd, new PreparedQueryDefinition(pqd.toJson())); }
@Test public void testEventCopy() { Event event = randomEvent(); checkEvent(event, new Event(event)); checkEvent(event, new Event(event.toJson())); }
@Test public void testCheckCopy() { Check check = randomCheck(); checkCheck(check, new Check(check)); checkCheck(check, new Check(check.toJson())); }
public ConsulClient createSecureClient(boolean trustAll, PemTrustOptions trustOptions) { ConsulClientOptions options = config(ConsulCluster.writeToken(), true) .setTrustAll(trustAll) .setPemTrustOptions(trustOptions); return creator.apply(options); }