String url = String.format("%s://%s:%d", protocol, host, port); Consul.Builder builder = Consul.builder().withUrl(url); if (sslEnabled) { if (!Boolean.valueOf(commandLine.getOptionValue(CONSUL_CONFIG_SSL_VERIFY_HOSTNAME, "true"))) {
Consul.Builder consulBuilder = Consul.builder() .withUrl(consulConfig.getString("url")); AgentClient agentClient = consul.agentClient(); env.onStarted(() -> agentClient.register(registration)); env.onStop(() -> agentClient.deregister(registration.getId()));
private String getString(String path) { return consul.keyValueClient().getValueAsString(path).orNull(); }
/** * @throws SourceCommunicationException when unable to connect to Consul client */ @Override public void init() { try { LOG.info("Connecting to Consul client at " + host + ":" + port); Consul consul = Consul.builder().withHostAndPort(HostAndPort.fromParts(host, port)).build(); kvClient = consul.keyValueClient(); } catch (Exception e) { throw new SourceCommunicationException("Can't connect to host " + host + ":" + port, e); } initialized = true; }
this.serviceId = serviceId; consul = Consul.builder().build(); // todo configure with credentials / access token final List<String> tags = singletonList("couchbase-elasticsearch-connector");// emptyList(); final Map<String, String> meta = singletonMap("uuid", serviceUuid); consul.agentClient().register(0, 30L, this.serviceName, this.serviceId, tags, meta); consul.agentClient().fail(this.serviceId, "(" + this.serviceId + ") Connector process terminated."); } catch (Exception e) { System.err.println("Failed to report termination to Consul agent."); this.sessionId = consul.sessionClient().createSession(ImmutableSession.builder() .name("couchbase:cbes:" + this.serviceId) .behavior("delete") boolean acquired = consul.keyValueClient().acquireLock("couchbase:cbes:" + serviceName + ":vbucket:" + i, sessionId); System.out.println(acquired);
/** * Getter accessor for attribute 'agentClient'. * * @return * current value of 'agentClient' */ public AgentClient getAgentClient() { if (agentClient == null) { this.agentClient = consul.agentClient(); } return agentClient; }
Consul consul = Consul.builder().build(); System.out.println(consul.agentClient().getMembers()); ServiceHealthCache svHealth = ServiceHealthCache.newCache(consul.healthClient(), serviceName);
Consul.Builder builder = Consul.builder(); if (consulUrl != null) { builder.withUrl(consulUrl); sessionClient = consul.sessionClient(); keyValueClient = consul.keyValueClient();
@Override public List<ServiceDefinition> getServices(String name) { List<CatalogService> services = client.get().catalogClient() .getService(name, queryOptions) .getResponse(); List<ServiceHealth> healths = client.get().healthClient() .getAllServiceInstances(name, queryOptions) .getResponse(); return services.stream() .map(service -> newService(name, service, healths)) .collect(Collectors.toList()); }
}; final Consul consul = Consul.newClient(); Thread shutdownHook = null; final Member member = consul.agentClient().getAgent().getMember(); final String endpointId = endpointId(member, serviceId); new RpcServerTask(dispatcher, consul.keyValueClient(), serviceName, session.sessionId(), endpointId, errorConsumer).start(); new LeaderElectionTask(consul.keyValueClient(), serviceName, session.sessionId(), errorConsumer, leaderController).start()) { consul.destroy(); election.awaitTermination(); Consul.newClient().agentClient().fail(serviceId, "(" + serviceId + ") Connector process terminated."); consul.destroy();
/** * Getter accessor for attribute 'healthClient'. * * @return * current value of 'healthClient' */ public HealthClient getHealthClient() { if (healthClient == null) { this.healthClient = consul.healthClient(); } return healthClient; }
CONSUL_HOST+":"+CONSUL_PORT+"? IF NOT THIS TEST WILL FAIL! ####################"); CatalogClient consulCatalogClient = Consul.builder().withHostAndPort( HostAndPort.fromParts("localhost", 8500)) .build().catalogClient();
public void remove(String key) { // create session to avoid conflicts (not sure if that is safe enough) SessionClient sessionClient = consul.sessionClient(); String sessionName = "session_" + UUID.randomUUID().toString(); SessionCreatedResponse response = sessionClient .createSession(ImmutableSession.builder().name(sessionName).build()); String sessionId = response.getId(); kvClient = consul.keyValueClient(); String lockKey = "lock_" + key; kvClient.acquireLock(lockKey, sessionName, sessionId); Object object = lookupByName(key); if (object == null) { String msg = "Bean with key '" + key + "' did not exist in Consul Registry."; throw new NoSuchBeanException(msg); } kvClient.deleteKey(key); kvClient.deleteKey(object.getClass().getName() + "/" + key); kvClient.releaseLock(lockKey, sessionId); }
public SessionTask(Consul consul, String serviceName, String serviceId, Consumer<Throwable> fatalErrorConsumer) { serviceId = defaultIfNull(serviceId, serviceName); this.serviceId = serviceId; this.serviceName = requireNonNull(serviceName); this.consul = requireNonNull(consul); this.fatalErrorConsumer = requireNonNull(fatalErrorConsumer); try { final List<String> tags = singletonList("couchbase-elasticsearch-connector");// emptyList(); final Map<String, String> meta = singletonMap("uuid", serviceUuid); // todo catch exception, retry with backoff (wait for consul agent to start) final int sessionTtlSeconds = HEALTH_CHECK_INTERVAL_SECONDS * 2; consul.agentClient().register(0, sessionTtlSeconds, this.serviceName, this.serviceId, tags, meta); passHealthCheck(); this.sessionId = consul.sessionClient().createSession(ImmutableSession.builder() .name("couchbase:cbes:" + this.serviceId) .behavior("delete") .lockDelay("15s") .addChecks("service:" + this.serviceId) // consul client library names the health check "service:<serviceId>" .build() ).getId(); } catch (Throwable t) { fatalErrorConsumer.accept(t); // todo need to send to fatalErrorConsumer? throw t; } }
final ConsulResponse<Value> value = ConsulHelper.awaitChange(consul.keyValueClient(), key, index); if (value == null) { LOGGER.debug("Document {} does not exist.", key); consul.destroy(); LOGGER.debug("Thread exiting: {}", Thread.currentThread()); done.set(true); // prevent looping consul.destroy(); // terminate an in-flight request });
public static void main(String[] args) { final Consul consul = Consul.newClient(); for (RpcEndpoint endpoint : listRpcEndpoints(consul.keyValueClient(), Sandbox.serviceName, defaultTimeout)) { System.out.println(endpoint);
@Override public void start(StartContext startContext) throws StartException { this.client = this.consulInjector.getValue().catalogClient(); }
private void destroySession() { consul.sessionClient().destroySession(sessionId); sessionId = null; }
public ConsulSpConfig(String serviceName) { super(serviceName); //TDOO use consul adress from an environment variable Map<String, String> env = System.getenv(); Consul consul; if (env.containsKey(CONSUL_ENV_LOCATION)) { URL url = null; try { url = new URL("http", env.get(CONSUL_ENV_LOCATION), 8500, ""); } catch (MalformedURLException e) { e.printStackTrace(); } consul = Consul.builder().withUrl(url).build(); // connect to Consul on localhost } else { consul = Consul.builder().build(); } // Consul consul = Consul.builder().build(); // connect to Consul on localhost kvClient = consul.keyValueClient(); this.serviceName = serviceName; }
public void deregister(String name) { log.info("Will de register " + name); AgentClient agentClient = client.agentClient(); agentClient.deregister(name); } }