private void destroySession() { consul.sessionClient().destroySession(sessionId); sessionId = null; }
private String getString(String path) { return consul.keyValueClient().getValueAsString(path).orNull(); }
private String createSession() { final ImmutableSession session = ImmutableSession.builder() .name(name) .ttl(Optional.fromNullable(ttl)) .build(); return consul.sessionClient().createSession(session).getId(); }
Consul.Builder consulBuilder = Consul.builder() .withUrl(consulConfig.getString("url")); Consul consul = consulBuilder.build(); ImmutableRegistration.Builder registrationBuilder = ImmutableRegistration.builder() .name(registerConfig.getString("name")) .address(registerConfig.getString("host")) .port(registerConfig.getInt("port")) .tags(registerConfig.getStringList("tags")) .id(UUID.randomUUID().toString()); checkConfig.getString("path")); Registration.RegCheck check = Registration.RegCheck.http(http, checkConfig.getDuration("interval", TimeUnit.SECONDS), checkConfig.getDuration("timeout", TimeUnit.SECONDS)); registrationBuilder.check(check); Registration registration = registrationBuilder.build(); AgentClient agentClient = consul.agentClient(); env.onStarted(() -> agentClient.register(registration)); env.onStop(() -> agentClient.deregister(registration.getId()));
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"))) { builder.withHostnameVerifier(new NullHostnameVerifier()); sslContext.init(null, tmf.getTrustManagers(), null); builder.withSslContext(sslContext); builder.withAclToken(commandLine.getOptionValue(CONSUL_CONFIG_ACL_TOKEN)); Consul consul = builder.build(); return new ConsulConfigProvider(consul, prefix, defaultProperties);
public boolean acquireLock(long maxWait, TimeUnit unit) { KeyValueClient kv = consul.keyValueClient(); sessionId = createSession(); Optional<Value> value = kv.getValue(path); if (kv.acquireLock(path, sessionId)) { return true; } BigInteger index = BigInteger.valueOf(value.get().getModifyIndex()); kv.getValue(path, QueryOptions.blockMinutes((int) unit.toMinutes(maxWait), index).build()); if (!kv.acquireLock(path, sessionId)) { destroySession(); return false; } else { return true; } }
public void releaseLock() { try { KeyValueClient kv = consul.keyValueClient(); Optional<Value> value = kv.getValue(path); if (value.isPresent()) { Optional<String> session = value.get().getSession(); if (session.isPresent()) { kv.releaseLock(path, session.get()); } } } finally { destroySession(); } } }
@Override public LoadedInstanceConfig storeConfig(ConfigCollection config, long compareVersion) throws Exception { Long currentVersion = loadProperties().getVersion(); if (currentVersion != compareVersion) { return null; } KeyValueClient kv = consul.keyValueClient(); PropertyBasedInstanceConfig instanceConfig = new PropertyBasedInstanceConfig(config); StringWriter writer = new StringWriter(); instanceConfig.getProperties().store(writer, "Auto-generated by Exhibitor"); lock.acquireLock(lockTimeoutMs, TimeUnit.MILLISECONDS); try { kv.putValue(propertiesPath, writer.toString()); kv.putValue(versionPath, String.valueOf(currentVersion + 1)); } finally { lock.releaseLock(); } return new LoadedInstanceConfig(instanceConfig, currentVersion + 1); }
/** * Getter accessor for attribute 'agentClient'. * * @return * current value of 'agentClient' */ public AgentClient getAgentClient() { if (agentClient == null) { this.agentClient = consul.agentClient(); } return agentClient; }
/** * Getter accessor for attribute 'keyValueClient'. * * @return * current value of 'keyValueClient' */ public KeyValueClient getKeyValueClient() { if (keyValueClient == null) { this.keyValueClient = consul.keyValueClient(); } return keyValueClient; }
/** * Getter accessor for attribute 'healthClient'. * * @return * current value of 'healthClient' */ public HealthClient getHealthClient() { if (healthClient == null) { this.healthClient = consul.healthClient(); } return healthClient; }
/** * Retrieves the single service. * <p/> * GET /v1/catalog/service/{service} * * @return A {@link com.orbitz.consul.model.ConsulResponse} containing * {@link com.orbitz.consul.model.catalog.CatalogService} objects. */ public ConsulResponse<List<CatalogService>> getService(String service) { return getService(service, QueryOptions.BLANK); }
/** * Retrieves all nodes. * <p/> * GET /v1/catalog/nodes * * @return A {@link com.orbitz.consul.model.ConsulResponse} containing a list of * {@link com.orbitz.consul.model.health.Node} objects. */ public ConsulResponse<List<Node>> getNodes() { return getNodes(QueryOptions.BLANK); }
/** * Sets a TTL service check to "passing" state */ public void pass(String serviceId) throws NotRegisteredException { checkTtl(serviceId, State.PASS, null); }
/** * Sets a TTL check to "warning" state with a note. */ public void warnCheck(String checkId, String note) throws NotRegisteredException { check(checkId, State.WARN, note); }
private Long getLong(String path) { return Long.valueOf(consul.keyValueClient().getValueAsString(path).or("0")); }
/** * Sets a TTL service check to "critical" state with a note. */ public void fail(String serviceId, String note) throws NotRegisteredException { checkTtl(serviceId, State.FAIL, note); }