Java Code Examples for com.orbitz.consul.Consul#Builder
The following examples show how to use
com.orbitz.consul.Consul#Builder .
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: ConsulFactory.java From dropwizard-consul with Apache License 2.0 | 6 votes |
@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(); }
Example 2
Source File: ConsulService.java From ARCHIVE-wildfly-swarm with Apache License 2.0 | 6 votes |
@Override public void start(StartContext startContext) throws StartException { Consul.Builder builder = Consul.builder(); // pool because of multiple threads. ResteasyClientBuilder clientBuilder = new ResteasyClientBuilder(); clientBuilder = clientBuilder.connectionPoolSize(20); builder.withClientBuilder(clientBuilder); builder.withUrl(this.url); try { this.consul = builder.build(); } catch (Exception e) { throw new StartException("Failed to connect consul at "+url, e); } }
Example 3
Source File: ClusterModuleConsulProviderTest.java From skywalking with Apache License 2.0 | 6 votes |
@Test @SuppressWarnings("unchecked") public void prepare() throws Exception { ClusterModuleConsulConfig consulConfig = new ClusterModuleConsulConfig(); consulConfig.setHostPort("10.0.0.1:1000,10.0.0.2:1001"); Whitebox.setInternalState(provider, "config", consulConfig); Consul consulClient = mock(Consul.class); Consul.Builder builder = mock(Consul.Builder.class); when(builder.build()).thenReturn(consulClient); PowerMockito.mockStatic(Consul.class); when(Consul.builder()).thenReturn(builder); when(builder.withConnectTimeoutMillis(anyLong())).thenReturn(builder); when(builder.withMultipleHostAndPort(anyCollection(), anyLong())).thenReturn(builder); provider.prepare(); ArgumentCaptor<Collection> addressCaptor = ArgumentCaptor.forClass(Collection.class); ArgumentCaptor<Long> timeCaptor = ArgumentCaptor.forClass(long.class); verify(builder).withMultipleHostAndPort(addressCaptor.capture(), timeCaptor.capture()); List<HostAndPort> address = (List<HostAndPort>) addressCaptor.getValue(); assertEquals(2, address.size()); assertEquals(Lists.newArrayList(HostAndPort.fromParts("10.0.0.1", 1000), HostAndPort.fromParts("10.0.0.2", 1001)), address); }
Example 4
Source File: ClusterModuleConsulProviderTest.java From skywalking with Apache License 2.0 | 6 votes |
@Test public void prepareSingle() throws Exception { ClusterModuleConsulConfig consulConfig = new ClusterModuleConsulConfig(); consulConfig.setHostPort("10.0.0.1:1000"); Whitebox.setInternalState(provider, "config", consulConfig); Consul consulClient = mock(Consul.class); Consul.Builder builder = mock(Consul.Builder.class); when(builder.build()).thenReturn(consulClient); PowerMockito.mockStatic(Consul.class); when(Consul.builder()).thenReturn(builder); when(builder.withConnectTimeoutMillis(anyLong())).thenCallRealMethod(); when(builder.withHostAndPort(any())).thenReturn(builder); provider.prepare(); ArgumentCaptor<HostAndPort> hostAndPortArgumentCaptor = ArgumentCaptor.forClass(HostAndPort.class); verify(builder).withHostAndPort(hostAndPortArgumentCaptor.capture()); HostAndPort address = hostAndPortArgumentCaptor.getValue(); assertEquals(HostAndPort.fromParts("10.0.0.1", 1000), address); }
Example 5
Source File: ConsulClient.java From pragmatic-microservices-lab with MIT License | 5 votes |
public static Consul build() { Consul.Builder consulBuilder = Consul.builder(); ConfigProvider.getConfig() .getOptionalValue(CONFIG_CONSUL_URL, String.class) .ifPresent(url -> { consulBuilder.withUrl(url); }); return consulBuilder.build(); }
Example 6
Source File: ConsulService.java From thorntail with Apache License 2.0 | 5 votes |
@Override public void start(StartContext startContext) throws StartException { Consul.Builder builder = Consul.builder(); builder.withUrl(this.url); try { this.consul = builder.build(); } catch (Exception e) { throw new StartException("Failed to connect consul at " + url, e); } }
Example 7
Source File: ConsulConfigurationWatcherRegister.java From skywalking with Apache License 2.0 | 5 votes |
public ConsulConfigurationWatcherRegister(ConsulConfigurationCenterSettings settings) { super(settings.getPeriod()); this.configItemKeyedByName = new ConcurrentHashMap<>(); this.cachesByKey = new ConcurrentHashMap<>(); List<HostAndPort> hostAndPorts = Splitter.on(",") .splitToList(settings.getHostAndPorts()) .parallelStream() .map(hostAndPort -> HostAndPort.fromString(hostAndPort) .withDefaultPort(DEFAULT_PORT)) .collect(Collectors.toList()); Consul.Builder builder = Consul.builder().withConnectTimeoutMillis(3000); if (hostAndPorts.size() == 1) { builder.withHostAndPort(hostAndPorts.get(0)); } else { builder.withMultipleHostAndPort(hostAndPorts, 5000); } if (StringUtils.isNotEmpty(settings.getAclToken())) { builder.withAclToken(settings.getAclToken()); } consul = builder.build().keyValueClient(); }
Example 8
Source File: ClusterModuleConsulProvider.java From skywalking with Apache License 2.0 | 5 votes |
@Override public void prepare() throws ServiceNotProvidedException, ModuleStartException { try { List<Address> addressList = ConnectUtils.parse(config.getHostPort()); List<HostAndPort> hostAndPorts = new ArrayList<>(); for (Address address : addressList) { hostAndPorts.add(HostAndPort.fromParts(address.getHost(), address.getPort())); } Consul.Builder consulBuilder = Consul.builder() // we should set this value or it will be blocked forever .withConnectTimeoutMillis(3000); if (StringUtils.isNotEmpty(config.getAclToken())) { consulBuilder.withAclToken(config.getAclToken()); } if (hostAndPorts.size() > 1) { client = consulBuilder.withMultipleHostAndPort(hostAndPorts, 5000).build(); } else { client = consulBuilder.withHostAndPort(hostAndPorts.get(0)).build(); } } catch (ConnectStringParseException | ConsulException e) { throw new ModuleStartException(e.getMessage(), e); } ConsulCoordinator coordinator = new ConsulCoordinator(config, client); this.registerServiceImplementation(ClusterRegister.class, coordinator); this.registerServiceImplementation(ClusterNodesQuery.class, coordinator); }
Example 9
Source File: ConsulBundle.java From dropwizard-consul with Apache License 2.0 | 4 votes |
@Override public void initialize(Bootstrap<?> bootstrap) { // Replace variables with values from Consul KV. Please override // getConsulAgentHost() and getConsulAgentPort() if Consul is not // listening on the default localhost:8500. try { LOGGER.debug("Connecting to Consul at {}:{}", getConsulAgentHost(), getConsulAgentPort()); final Consul.Builder builder = Consul.builder() .withHostAndPort(HostAndPort.fromParts(getConsulAgentHost(), getConsulAgentPort())); getConsulAclToken() .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 LOGGER.debug("Using Consul ACL token: {}", token); builder .withAclToken(token) .withHeaders(ImmutableMap.of(CONSUL_AUTH_HEADER_KEY, token)); }); // using Consul as a configuration substitution provider bootstrap.setConfigurationSourceProvider( new SubstitutingSourceProvider( bootstrap.getConfigurationSourceProvider(), new ConsulSubstitutor(builder.build(), strict, substitutionInVariables))); } catch (ConsulException e) { LOGGER.warn( "Unable to query Consul running on {}:{}," + " disabling configuration substitution", getConsulAgentHost(), getConsulAgentPort(), e); } }
Example 10
Source File: ConsulModule.java From nano-framework with Apache License 2.0 | 4 votes |
private Consul build(final ConsulConfig cfg) { final Consul.Builder builder = Consul.builder(); final HostAndPort hostAndPort = cfg.getHostAndPort(); if (hostAndPort != null) { builder.withHostAndPort(cfg.getHostAndPort()); } final String username = cfg.getUsernaem(); final String password = cfg.getPassword(); if (username != null && password != null) { builder.withBasicAuth(cfg.getUsernaem(), cfg.getPassword()); } final URL url = cfg.getUrl(); if (url != null) { builder.withUrl(cfg.getUrl()); } final String token = cfg.getToken(); if (token != null) { builder.withAclToken(cfg.getToken()); } final Map<String, String> headers = cfg.getHeaders(); if (!CollectionUtils.isEmpty(headers)) { builder.withHeaders(cfg.getHeaders()); } final Long connectTimeout = cfg.getConnectTimeout(); if (connectTimeout != null) { builder.withConnectTimeoutMillis(cfg.getConnectTimeout()); } final Long readTimeout = cfg.getReadTimeout(); if (readTimeout != null) { builder.withReadTimeoutMillis(cfg.getReadTimeout()); } final Long writeTimeout = cfg.getWriteTimeout(); if (writeTimeout != null) { builder.withWriteTimeoutMillis(cfg.getWriteTimeout()); } return builder.build(); }