Class Consul.Builder
- Enclosing class:
- Consul
Consul
client objects.-
Method Summary
Modifier and TypeMethodDescriptionbuild()
Constructs a newConsul
client.withAclToken
(String token) Sets the ACL token to be used with ConsulwithBasicAuth
(String username, String password) Sets the username and password to be used for basic authenticationwithClientConfiguration
(ClientConfig clientConfig) Sets the configuration for the clients.withClientEventCallback
(ClientEventCallback callback) Sets the event callback for the clients.withConnectionPool
(okhttp3.ConnectionPool connectionPool) Sets the ConnectionPool to be used by OkHttp ClientwithConnectTimeoutMillis
(long timeoutMillis) Connect timeout for OkHttpClientwithConsulBookend
(ConsulBookend consulBookend) Attaches aConsulBookend
to each Consul request.withConsulFailoverInterceptor
(ConsulFailoverInterceptor failoverInterceptor) Uses the given failover interceptor.withExecutorService
(ExecutorService executorService) Sets the ExecutorService to be used by the internal task dispatcher.Deprecated, for removal: This API element is subject to removal in a future version.Constructs a failover interceptor with the givenConsulFailoverStrategy
.withHeaders
(Map<String, String> headers) Sets headers to be included with each Consul request.withHostAndPort
(com.google.common.net.HostAndPort hostAndPort) Sets the URL from aHostAndPort
object.withHostnameVerifier
(HostnameVerifier hostnameVerifier) Sets theHostnameVerifier
for the client.withHttps
(boolean withHttps) Use HTTPS connections for all requests.withMaxFailoverAttempts
(int maxFailoverAttempts) Sets the maximum number of failover attempts that theConsulFailoverInterceptor
, if one is set, should use.withMultipleHostAndPort
(Collection<com.google.common.net.HostAndPort> hostAndPort, long blacklistTimeInMillis) Sets the list of hosts to contact if the current request target is unavailable.withMultipleHostAndPort
(Collection<com.google.common.net.HostAndPort> hostAndPort, long blacklistTime, TimeUnit blacklistTimeUnit) Sets the list of hosts to contact if the current request target is unavailable.withMultipleHostAndPort
(Collection<com.google.common.net.HostAndPort> hostAndPort, Duration blacklistTime) Sets the list of hosts to contact if the current request target is unavailable.withPing
(boolean ping) Instructs the builder that the AgentClient should attempt a ping before returning the Consul instanceSets theProxy
for the client.withReadTimeoutMillis
(long timeoutMillis) Read timeout for OkHttpClientwithSslContext
(SSLContext sslContext) Sets theSSLContext
for the client.withTokenAuth
(String token) Sets the token used for authenticationwithTrustManager
(X509TrustManager trustManager) Sets theX509TrustManager
for the client.Sets the URL from a string.Sets the URL from aURL
object.withWriteTimeoutMillis
(long timeoutMillis) Write timeout for OkHttpClient
-
Method Details
-
withUrl
Sets the URL from aURL
object.- Parameters:
url
- The Consul agent URL.- Returns:
- The builder.
-
withHttps
Use HTTPS connections for all requests.- Parameters:
withHttps
- Set to true to use https for all Consul requests.- Returns:
- The builder.
-
withPing
Instructs the builder that the AgentClient should attempt a ping before returning the Consul instance- Parameters:
ping
- Whether the ping should be done or not- Returns:
- The builder.
-
withBasicAuth
Sets the username and password to be used for basic authentication- Parameters:
username
- the value of the usernamepassword
- the value of the password- Returns:
- The builder.
-
withTokenAuth
Sets the token used for authentication- Parameters:
token
- the token- Returns:
- The builder.
-
withAclToken
Sets the ACL token to be used with Consul- Parameters:
token
- the value of the token- Returns:
- The builder.
-
withHeaders
Sets headers to be included with each Consul request.- Parameters:
headers
- Map of headers.- Returns:
- The builder.
-
withConsulBookend
Attaches aConsulBookend
to each Consul request. This can be used for gathering metrics, timings or debugging.- Parameters:
consulBookend
- The bookend implementation.- Returns:
- The builder.
- See Also:
-
withHostAndPort
Sets the URL from aHostAndPort
object.- Parameters:
hostAndPort
- The Consul agent host and port.- Returns:
- The builder.
-
withConsulFailoverInterceptor
Uses the given failover interceptor.Note that only one method that sets a
ConsulFailoverInterceptor
should be used when constructing a Consul instance. Otherwise, only the last one is used.- Parameters:
failoverInterceptor
- the failover interceptor to use- Returns:
- The builder.
-
withMultipleHostAndPort
public Consul.Builder withMultipleHostAndPort(Collection<com.google.common.net.HostAndPort> hostAndPort, long blacklistTimeInMillis) 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 nextHostAndPort
specified is used to retry the request. This will continue until all urls are exhausted.Internally, this method constructs a
ConsulFailoverInterceptor
with aBlacklistingConsulFailoverStrategy
. The hosts and blacklist time are provided to the failover strategy.Note that only one method that sets a
ConsulFailoverInterceptor
should be used when constructing a Consul instance. Otherwise, only the last one is used.- Parameters:
hostAndPort
- A collection ofHostAndPort
that define the list of Consul agent addresses to use.blacklistTimeInMillis
- The timeout (in milliseconds) to blacklist a particularHostAndPort
before trying to use it again.- Returns:
- The builder.
-
withMultipleHostAndPort
public Consul.Builder withMultipleHostAndPort(Collection<com.google.common.net.HostAndPort> hostAndPort, Duration blacklistTime) 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 nextHostAndPort
specified is used to retry the request. This will continue until all urls are exhausted.This method sets a
ConsulFailoverInterceptor
using the given hosts and blacklist time. SeewithMultipleHostAndPort(Collection, long)
for more information about the internals.Note that only one method that sets a
ConsulFailoverInterceptor
should be used when constructing a Consul instance. Otherwise, only the last one is used.- Parameters:
hostAndPort
- A collection ofHostAndPort
that define the list of Consul agent addresses to use.blacklistTime
- The timeout to blacklist a particularHostAndPort
before trying to use it again.- Returns:
- The builder.
- See Also:
-
withMultipleHostAndPort
public Consul.Builder withMultipleHostAndPort(Collection<com.google.common.net.HostAndPort> hostAndPort, long blacklistTime, TimeUnit blacklistTimeUnit) 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 nextHostAndPort
specified is used to retry the request. This will continue until all urls are exhausted.This method sets a
ConsulFailoverInterceptor
using the given hosts and blacklist time. SeewithMultipleHostAndPort(Collection, long)
for more information about the internals.Note that only one method that sets a
ConsulFailoverInterceptor
should be used when constructing a Consul instance. Otherwise, only the last one is used.- Parameters:
hostAndPort
- A collection ofHostAndPort
that define the list of Consul agent addresses to use.blacklistTime
- The timeout to blacklist a particularHostAndPort
before trying to use it again.blacklistTimeUnit
- The unit ofblacklistTime
- Returns:
- The builder.
- See Also:
-
withFailoverInterceptorUsingStrategy
Constructs a failover interceptor with the givenConsulFailoverStrategy
.Note that only one method that sets a
ConsulFailoverInterceptor
should be used when constructing a Consul instance. Otherwise, only the last one is used.- Parameters:
strategy
- The strategy to use.- Returns:
- The builder.
-
withFailoverInterceptor
@Deprecated(since="1.3.0", forRemoval=true) public Consul.Builder withFailoverInterceptor(ConsulFailoverStrategy strategy) Deprecated, for removal: This API element is subject to removal in a future version.Constructs a failover interceptor with the givenConsulFailoverStrategy
.Note that only one method that sets a
ConsulFailoverInterceptor
should be used when constructing a Consul instance. Otherwise, only the last one is used.- Parameters:
strategy
- The strategy to use.- Returns:
- The builder.
-
withMaxFailoverAttempts
Sets the maximum number of failover attempts that theConsulFailoverInterceptor
, if one is set, should use.- Parameters:
maxFailoverAttempts
- the maximum number of attempts- Returns:
- The builder.
- See Also:
-
withUrl
Sets the URL from a string.- Parameters:
url
- The Consul agent URL.- Returns:
- The builder.
-
withSslContext
Sets theSSLContext
for the client.- Parameters:
sslContext
- The SSL context for HTTPS agents.- Returns:
- The builder.
-
withTrustManager
Sets theX509TrustManager
for the client.- Parameters:
trustManager
- The SSL trust manager for HTTPS agents.- Returns:
- The builder.
-
withHostnameVerifier
Sets theHostnameVerifier
for the client.- Parameters:
hostnameVerifier
- The hostname verifier to use.- Returns:
- The builder.
-
withProxy
Sets theProxy
for the client.- Parameters:
proxy
- The proxy to use.- Returns:
- The builder
-
withConnectTimeoutMillis
Connect timeout for OkHttpClient- Parameters:
timeoutMillis
- timeout values in milliseconds- Returns:
- The builder
-
withReadTimeoutMillis
Read timeout for OkHttpClient- Parameters:
timeoutMillis
- timeout value in milliseconds- Returns:
- The builder
-
withWriteTimeoutMillis
Write timeout for OkHttpClient- Parameters:
timeoutMillis
- timeout value in milliseconds- Returns:
- The builder
-
withExecutorService
Sets the ExecutorService to be used by the internal task dispatcher.By default, an ExecutorService is created internally. In this case, it will not be customizable nor manageable by the user application. It can only be shutdown by the
Consul.destroy()
method.When an application needs to be able to customize the ExecutorService parameters, and/or manage its lifecycle, it can provide an instance of ExecutorService to the Builder. In that case, this ExecutorService will be used instead of creating one internally.
- Parameters:
executorService
- The ExecutorService to be injected in the internal task dispatcher.- Returns:
- The builder
-
withConnectionPool
Sets the ConnectionPool to be used by OkHttp ClientBy default, an ConnectionPool is created internally. In this case, it will not be customizable nor manageable by the user application. It can only be shutdown by the
Consul.destroy()
method.When an application needs to be able to customize the ConnectionPool parameters, and/or manage its lifecycle, it can provide an instance of ConnectionPool to the Builder. In that case, this ConnectionPool will be used instead of creating one internally.
- Parameters:
connectionPool
- The ConnectionPool to be injected in the internal OkHttpClient- Returns:
- The builder
-
withClientConfiguration
Sets the configuration for the clients. The configuration will fall back on the library default configuration if elements are not set.- Parameters:
clientConfig
- the configuration to use.- Returns:
- The Builder
-
withClientEventCallback
Sets the event callback for the clients. The callback will be called by the consul client after each event.- Parameters:
callback
- the callback to call.- Returns:
- The Builder
-
build
Constructs a newConsul
client.- Returns:
- A new Consul client.
-
withFailoverInterceptorUsingStrategy(ConsulFailoverStrategy)