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);
CONSUL_HOST+":"+CONSUL_PORT+"? IF NOT THIS TEST WILL FAIL! ####################"); CatalogClient consulCatalogClient = Consul.builder().withHostAndPort( HostAndPort.fromParts("localhost", 8500)) .build().catalogClient();
Consul consul = Consul.builder().withHostAndPort(hostAndPort).build(); KeyValueClient kvClient = consul.keyValueClient(); Optional<Value> valueOpt = kvClient.getValue(reqKey);
.withHostAndPort(HostAndPort.fromParts(settings.getConsulHost(), settings.getConsulPort())) .build());
private static <T> Flux<T> watchWhileExists(Consul.Builder consulBuilder, String key, Function<ConsulResponse<Value>, T> resultExtractor) { return Flux.create(emitter -> { final Consul consul = consulBuilder.build();
consulBuilder.withAclToken(consulAclToken); consulBuilder.withUrl("https://"+consulHost+":"+consulPort); consulBuilder.withSslContext(getSSLContext(consulSslServerCertFilePath, consulSslServerCertBase64)); consulBuilder.withHostnameVerifier(new HostnameVerifier() { public boolean verify(String s, SSLSession sslSession) { return true; consulBuilder.withHostAndPort( HostAndPort.fromParts(consulHost, consulPort)); Consul consul = consulBuilder.build();
consulBuilder.withAclToken(consulAclToken); consulBuilder.withUrl("https://"+consulHost+":"+consulPort); consulBuilder.withSslContext(getSSLContext(consulSslServerCertFilePath, consulSslServerCertBase64)); consulBuilder.withHostnameVerifier(new HostnameVerifier() { public boolean verify(String s, SSLSession sslSession) { return true; consulBuilder.withHostAndPort( HostAndPort.fromParts(consulHost, consulPort)); Consul consul = consulBuilder.build();
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);
.withHostAndPort(HostAndPort.fromParts(getConsulAgentHost(), getConsulAgentPort())); .withAclToken(token) .withHeaders(ImmutableMap.of(CONSUL_AUTH_HEADER_KEY, token)); }); new SubstitutingSourceProvider( bootstrap.getConfigurationSourceProvider(), new ConsulSubstitutor(builder.build(), strict, substitutionInVariables)));
Consul consul = Consul.builder().build();
@Override protected void commitInternal(ConfigChangeContext context) { for(HostAndPort hostAndPort: ConsulBackends.getConsulBackends()){ try{ Consul consul = Consul.builder().withHostAndPort(hostAndPort).build(); KeyValueClient kvClient = consul.keyValueClient(); for(String k: context.getRemovedProperties()){ try{ kvClient.deleteKey(k); } catch(Exception e){ LOG.info("Failed to remove key from consul: " + k); } } for(Map.Entry<String,String> en:context.getAddedProperties().entrySet()){ String key = en.getKey(); try{ kvClient.putValue(key,en.getValue()); }catch(Exception e) { LOG.info("Failed to add key to consul: " + en.getKey() + "=" + en.getValue()); } } // success: stop here break; } catch(Exception e){ LOG.log(Level.FINE, "consul access failed on " + hostAndPort + ", trying next...", e); } } } }
public Consul createConsulClient(CamelContext camelContext) throws Exception { Consul.Builder builder = Consul.builder(); builder.withPing(pingInstance); if (ObjectHelper.isNotEmpty(url)) { builder.withUrl(url); } if (ObjectHelper.isNotEmpty(camelContext) && ObjectHelper.isNotEmpty(sslContextParameters)) { builder.withSslContext(sslContextParameters.createSSLContext(camelContext)); } if (ObjectHelper.isNotEmpty(aclToken)) { builder.withAclToken(aclToken); } if (requiresBasicAuthentication()) { builder.withBasicAuth(userName, password); } if (ObjectHelper.isNotEmpty(connectTimeoutMillis)) { builder.withConnectTimeoutMillis(connectTimeoutMillis); } if (ObjectHelper.isNotEmpty(readTimeoutMillis)) { builder.withReadTimeoutMillis(readTimeoutMillis); } if (ObjectHelper.isNotEmpty(writeTimeoutMillis)) { builder.withWriteTimeoutMillis(writeTimeoutMillis); } return builder.build(); }
@Override public void run() { Consul consulThread = Consul.builder().build(); // connect to Consul on localhost KeyValueClient kvClientThread = consulThread.keyValueClient(); while (true) { configProps.keySet().forEach((s) -> { Optional<Value> te = kvClientThread.getValue(addSn(s)); if (!te.get().getValueAsString().get().equals(configProps.get(s))) { callback.onChange(); configProps.put(s, te.get().getValueAsString().get()); } }); try { Thread.sleep(10000); } catch (InterruptedException e) { e.printStackTrace(); } } }
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; }
/** * Sets the list of hosts to contact if the current request target is * unavailable. When the call to a particular URL fails for any reason, the next {@link HostAndPort} specified * is used to retry the request. This will continue until all urls are exhuasted. * * @param hostAndPort A collection of {@link HostAndPort} that define the list of Consul agent addresses to use. * @param blacklistTimeInMillis The timeout (in milliseconds) to blacklist a particular {@link HostAndPort} before trying to use it again. * @return The builder. */ public Builder withMultipleHostAndPort(Collection<HostAndPort> hostAndPort, long blacklistTimeInMillis) { Preconditions.checkArgument(blacklistTimeInMillis >= 0, "Negative Value"); Preconditions.checkArgument(hostAndPort.size() >= 2, "Minimum of 2 addresses are required"); consulFailoverInterceptor = new ConsulFailoverInterceptor(hostAndPort, blacklistTimeInMillis); withHostAndPort(hostAndPort.stream().findFirst().get()); return this; }
@JsonIgnore public Consul build() { final Consul.Builder builder = Consul.builder().withHostAndPort(endpoint).withPing(servicePing); aclToken.ifPresent( token -> { // setting both acl token here and with header, supplying an auth // header. This should cover both use cases: endpoint supports // legacy ?token query param and other case in which endpoint // requires an X-Consul-Token header. // @see https://www.consul.io/api/index.html#acls builder.withAclToken(token).withHeaders(ImmutableMap.of(CONSUL_AUTH_HEADER_KEY, token)); }); return builder.build(); }