2018-10-01 15:22:15 -04:00
[[java-rest-low-config]]
2017-07-04 04:58:57 -04:00
== Common configuration
2016-07-29 05:22:47 -04:00
2017-07-06 04:05:50 -04:00
As explained in <<java-rest-low-usage-initialization>>, the `RestClientBuilder`
supports providing both a `RequestConfigCallback` and an `HttpClientConfigCallback`
which allow for any customization that the Apache Async Http Client exposes.
Those callbacks make it possible to modify some specific behaviour of the client
without overriding every other default configuration that the `RestClient`
is initialized with. This section describes some common scenarios that require
additional configuration for the low-level Java REST Client.
2016-07-29 05:22:47 -04:00
2017-07-04 04:58:57 -04:00
=== Timeouts
2016-07-29 05:22:47 -04:00
Configuring requests timeouts can be done by providing an instance of
`RequestConfigCallback` while building the `RestClient` through its builder.
2017-08-29 10:26:36 -04:00
The interface has one method that receives an instance of
https://hc.apache.org/httpcomponents-client-ga/httpclient/apidocs/org/apache/http/client/config/RequestConfig.Builder.html[`org.apache.http.client.config.RequestConfig.Builder`]
2016-07-29 05:22:47 -04:00
as an argument and has the same return type. The request config builder can
be modified and then returned. In the following example we increase the
2016-12-05 10:54:51 -05:00
connect timeout (defaults to 1 second) and the socket timeout (defaults to 30
2019-02-06 02:43:47 -05:00
seconds).
2016-07-29 05:22:47 -04:00
2017-07-06 04:05:50 -04:00
["source","java",subs="attributes,callouts,macros"]
2016-07-29 05:22:47 -04:00
--------------------------------------------------
2017-07-06 04:05:50 -04:00
include-tagged::{doc-tests}/RestClientDocumentation.java[rest-client-config-timeouts]
2016-07-29 05:22:47 -04:00
--------------------------------------------------
2020-07-14 13:34:53 -04:00
timeouts also can set by RequestOptions per request.this will override RestClient customizeRequestConfig.
["source","java",subs="attributes,callouts,macros"]
--------------------------------------------------
include-tagged::{doc-tests}/RestClientDocumentation.java[rest-client-config-request-options-timeouts]
--------------------------------------------------
2017-07-04 04:58:57 -04:00
=== Number of threads
2016-07-29 05:22:47 -04:00
The Apache Http Async Client starts by default one dispatcher thread, and a
number of worker threads used by the connection manager, as many as the number
of locally detected processors (depending on what
`Runtime.getRuntime().availableProcessors()` returns). The number of threads
can be modified as follows:
2017-07-06 04:05:50 -04:00
["source","java",subs="attributes,callouts,macros"]
2016-07-29 05:22:47 -04:00
--------------------------------------------------
2017-07-06 04:05:50 -04:00
include-tagged::{doc-tests}/RestClientDocumentation.java[rest-client-config-threads]
2016-07-29 05:22:47 -04:00
--------------------------------------------------
2017-07-04 04:58:57 -04:00
=== Basic authentication
2016-07-29 05:22:47 -04:00
Configuring basic authentication can be done by providing an
`HttpClientConfigCallback` while building the `RestClient` through its builder.
2017-08-29 10:26:36 -04:00
The interface has one method that receives an instance of
https://hc.apache.org/httpcomponents-asyncclient-dev/httpasyncclient/apidocs/org/apache/http/impl/nio/client/HttpAsyncClientBuilder.html[`org.apache.http.impl.nio.client.HttpAsyncClientBuilder`]
2016-07-29 05:22:47 -04:00
as an argument and has the same return type. The http client builder can be
modified and then returned. In the following example we set a default
credentials provider that requires basic authentication.
2017-07-06 04:05:50 -04:00
["source","java",subs="attributes,callouts,macros"]
2016-07-29 05:22:47 -04:00
--------------------------------------------------
2017-07-06 04:05:50 -04:00
include-tagged::{doc-tests}/RestClientDocumentation.java[rest-client-config-basic-auth]
2016-07-29 05:22:47 -04:00
--------------------------------------------------
2017-07-06 04:05:50 -04:00
Preemptive Authentication can be disabled, which means that every request will be sent without
2018-09-17 15:35:55 -04:00
authorization headers to see if it is accepted and, upon receiving an HTTP 401 response, it will
2017-01-24 11:34:05 -05:00
resend the exact same request with the basic authentication header. If you wish to do this, then
you can do so by disabling it via the `HttpAsyncClientBuilder`:
2017-07-06 04:05:50 -04:00
["source","java",subs="attributes,callouts,macros"]
2017-01-24 11:34:05 -05:00
--------------------------------------------------
2017-07-06 04:05:50 -04:00
include-tagged::{doc-tests}/RestClientDocumentation.java[rest-client-config-disable-preemptive-auth]
2017-01-24 11:34:05 -05:00
--------------------------------------------------
2017-07-06 04:05:50 -04:00
<1> Disable preemptive authentication
2017-01-24 11:34:05 -05:00
2020-01-29 01:14:38 -05:00
=== Other authentication methods
==== Elasticsearch Token Service tokens
If you want the client to authenticate with an Elasticsearch access token, set the relevant HTTP request header.
If the client makes requests on behalf of a single user only, you can set the necessary `Authorization` header as a default header as shown
in the following example:
["source","java",subs="attributes,callouts,macros"]
--------------------------------------------------
include-tagged::{doc-tests}/RestClientDocumentation.java[rest-client-auth-bearer-token]
--------------------------------------------------
==== Elasticsearch API keys
If you want the client to authenticate with an Elasticsearch API key, set the relevant HTTP request header.
If the client makes requests on behalf of a single user only, you can set the necessary `Authorization` header as a default header as shown
in the following example:
["source","java",subs="attributes,callouts,macros"]
--------------------------------------------------
include-tagged::{doc-tests}/RestClientDocumentation.java[rest-client-auth-api-key]
--------------------------------------------------
2017-07-04 04:58:57 -04:00
=== Encrypted communication
2016-07-29 05:22:47 -04:00
2020-01-29 01:14:38 -05:00
Encrypted communication using TLS can also be configured through the
2017-08-29 10:26:36 -04:00
`HttpClientConfigCallback`. The
https://hc.apache.org/httpcomponents-asyncclient-dev/httpasyncclient/apidocs/org/apache/http/impl/nio/client/HttpAsyncClientBuilder.html[`org.apache.http.impl.nio.client.HttpAsyncClientBuilder`]
2016-07-29 05:22:47 -04:00
received as an argument exposes multiple methods to configure encrypted
communication: `setSSLContext`, `setSSLSessionStrategy` and
`setConnectionManager`, in order of precedence from the least important.
2020-01-29 01:14:38 -05:00
When accessing an Elasticsearch cluster that is setup for TLS on the HTTP layer, the client needs to trust the certificate that
Elasticsearch is using.
The following is an example of setting up the client to trust the CA that has signed the certificate that Elasticsearch is using, when
that CA certificate is available in a PKCS#12 keystore:
2016-07-29 05:22:47 -04:00
2017-07-06 04:05:50 -04:00
["source","java",subs="attributes,callouts,macros"]
2016-07-29 05:22:47 -04:00
--------------------------------------------------
2017-07-06 04:05:50 -04:00
include-tagged::{doc-tests}/RestClientDocumentation.java[rest-client-config-encrypted-communication]
2016-07-29 05:22:47 -04:00
--------------------------------------------------
2020-01-29 01:14:38 -05:00
The following is an example of setting up the client to trust the CA that has signed the certificate that Elasticsearch is using, when
that CA certificate is available as a PEM encoded file.
["source","java",subs="attributes,callouts,macros"]
--------------------------------------------------
include-tagged::{doc-tests}/RestClientDocumentation.java[rest-client-config-trust-ca-pem]
--------------------------------------------------
When Elasticsearch is configured to require client TLS authentication, for example when a PKI realm is configured, the client needs to provide
a client certificate during the TLS handshake in order to authenticate. The following is an example of setting up the client for TLS
authentication with a certificate and a private key that are stored in a PKCS#12 keystore.
["source","java",subs="attributes,callouts,macros"]
--------------------------------------------------
include-tagged::{doc-tests}/RestClientDocumentation.java[rest-client-config-mutual-tls-authentication]
--------------------------------------------------
If the client certificate and key are not available in a keystore but rather as PEM encoded files, you cannot use them
directly to build an SSLContext. You must rely on external libraries to parse the PEM key into a PrivateKey instance. Alternatively, you
can use external tools to build a keystore from your PEM files, as shown in the following example:
```
openssl pkcs12 -export -in client.crt -inkey private_key.pem \
-name "client" -out client.p12
```
2017-07-20 09:36:56 -04:00
If no explicit configuration is provided, the http://docs.oracle.com/javase/7/docs/technotes/guides/security/jsse/JSSERefGuide.html#CustomizingStores[system default configuration]
will be used.
2017-07-04 04:58:57 -04:00
=== Others
2016-07-29 05:22:47 -04:00
For any other required configuration needed, the Apache HttpAsyncClient docs
2016-12-05 10:54:51 -05:00
should be consulted: https://hc.apache.org/httpcomponents-asyncclient-4.1.x/ .
2018-03-22 21:23:52 -04:00
NOTE: If your application runs under the security manager you might be subject
to the JVM default policies of caching positive hostname resolutions
indefinitely and negative hostname resolutions for ten seconds. If the resolved
addresses of the hosts to which you are connecting the client to vary with time
then you might want to modify the default JVM behavior. These can be modified by
adding
http://docs.oracle.com/javase/8/docs/technotes/guides/net/properties.html[`networkaddress.cache.ttl=<timeout>`]
and
http://docs.oracle.com/javase/8/docs/technotes/guides/net/properties.html[`networkaddress.cache.negative.ttl=<timeout>`]
to your
http://docs.oracle.com/javase/8/docs/technotes/guides/security/PolicyFiles.html[Java
security policy].
2018-06-22 11:15:29 -04:00
=== Node selector
The client sends each request to one of the configured nodes in round-robin
fashion. Nodes can optionally be filtered through a node selector that needs
to be provided when initializing the client. This is useful when sniffing is
enabled, in case only dedicated master nodes should be hit by HTTP requests.
For each request the client will run the eventually configured node selector
to filter the node candidates, then select the next one in the list out of the
remaining ones.
["source","java",subs="attributes,callouts,macros"]
--------------------------------------------------
include-tagged::{doc-tests}/RestClientDocumentation.java[rest-client-init-allocation-aware-selector]
--------------------------------------------------
<1> Set an allocation aware node selector that allows to pick a node in the
local rack if any available, otherwise go to any other node in any rack. It
acts as a preference rather than a strict requirement, given that it goes to
another rack if none of the local nodes are available, rather than returning
no nodes in such case which would make the client forcibly revive a local node
whenever none of the nodes from the preferred rack is available.
WARNING: Node selectors that do not consistently select the same set of nodes
will make round-robin behaviour unpredictable and possibly unfair. The
preference example above is fine as it reasons about availability of nodes
which already affects the predictability of round-robin. Node selection should
not depend on other external factors or round-robin will not work properly.