При запросе и управлении данными в Elasticsearch иногда High Level REST Client может оказаться недостаточным. Например, из‑за встроенных ограничений High Level REST Client может не выполнять сложные пользовательские запросы. В этом случае можно рассмотреть использование Low Level REST Client, который инкапсулирует Elasticsearch API. Достаточно сконструировать необходимые структуры запросов для доступа к кластеру Elasticsearch. Это упрощает работу с кластерами Elasticsearch. Low Level REST Client позволяет настраивать структуру запроса, что более гибко и поддерживает все форматы запросов Elasticsearch, такие как GET, POST, DELETE и HEAD.
Вы можете использовать Low Level REST Client для доступа к кластеру Elasticsearch одним из следующих способов:
Как определить, какой метод использовать? Если необходимо выполнять сильно кастомизированные запросы, создайте Low Level Rest Client напрямую. Если вы уже используете High Level Rest Client, можете вызвать метод getLowLevelClient() для получения Low Level Rest Client. Это упрощает ваш код.
Укажите необходимые Java‑зависимости на сервере, где вы запускаете Java‑код. Объявите версию Apache в режиме Maven.
Замените 7.10.2 на фактическую версию Java‑клиента.
<dependency><groupId>org.elasticsearch.client</groupId><artifactId>elasticsearch-rest-high-level-client</artifactId><version>7.10.2</version></dependency><dependency><groupId>org.elasticsearch</groupId><artifactId>elasticsearch</artifactId><version>7.10.2</version></dependency>
Пример кода зависит от настроек режима безопасности целевого кластера Elasticsearch. Выберите соответствующий справочный документ в зависимости от вашего сценария обслуживания.
Как создаётся Low Level REST Client | Настройки режима безопасности кластера Elasticsearch | Нужно ли загружать сертификат безопасности | Details |
|---|---|---|---|
Создать Low Level REST Client напрямую | Режим без безопасности | - | Подключение к кластеру в режиме без безопасности с использованием Low Level REST Client |
Security mode + HTTP Security mode + HTTPS | No | ||
Security mode + HTTPS | Yes | Подключение к кластеру Security-Mode с использованием Low Level REST Client (с сертификатом) | |
Сначала создайте High Level REST Client, а затем вызовите getLowLevelClient() для получения Low Level REST Client | Режим без безопасности | - | Подключение к кластеру Non-Security Mode с использованием High Level REST Client |
Security mode + HTTP Security mode + HTTPS | No | Подключение к кластеру Security-Mode с использованием High Level REST Client (без сертификата) | |
Security mode + HTTPS | Yes | Подключение к кластеру Security-Mode с использованием High Level REST Client (с сертификатом) |
Используйте Low Level REST Client для подключения к кластеру Elasticsearch, у которого отключён security mode, и выполните запрос, существует ли индекс test. Пример кода приведён ниже:
1234567891011121314151617181920212223242526272829303132333435import org.apache.http.HttpHost;import org.elasticsearch.client.Request;import org.elasticsearch.client.Response;import org.elasticsearch.client.RestClient;import org.elasticsearch.client.RestClientBuilder;import java.io.IOException;import java.util.Arrays;import java.util.List;public class Main {public static void main(String[] args) throws IOException {List<String> host = Arrays.asList("{Cluster access address}");RestClientBuilder builder = RestClient.builder(constructHttpHosts(host, 9200, "http"));/***Create the Low Level Rest Client.*/RestClient lowLevelClient = builder.build();/*** Check whether the test index exists. If the index exists, 200 is returned. If the index does not exist, 404 is returned.*/Request request = new Request("HEAD", "/test");Response response = lowLevelClient.performRequest(request);System.out.println(response.getStatusLine().getStatusCode());lowLevelClient.close();}/*** Use the constructHttpHosts function to convert the node IP address list of the host cluster.*/public static HttpHost[] constructHttpHosts(List<String> host, int port, String protocol) {return host.stream().map(p -> new HttpHost(p, port, protocol)).toArray(HttpHost[]::new);}}
Этот фрагмент кода проверяет, существует ли в кластере индекс test. Если возвращён статус 200 (индекс существует) или 404 (индекс не существует), это указывает на то, что соединение с кластером установлено.
Используйте Low Level REST Client для подключения к кластеру security-mode Elasticsearch (HTTP или HTTPS) без загрузки сертификата безопасности и выполните запрос, существует ли индекс test. Пример кода приведён ниже:
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108import org.apache.http.HttpHost;import org.apache.http.auth.AuthScope;import org.apache.http.auth.UsernamePasswordCredentials;import org.apache.http.client.CredentialsProvider;import org.apache.http.conn.ssl.NoopHostnameVerifier;import org.apache.http.impl.client.BasicCredentialsProvider;import org.apache.http.nio.conn.ssl.SSLIOSessionStrategy;import org.apache.logging.log4j.LogManager;import org.apache.logging.log4j.Logger;import org.elasticsearch.client.Request;import org.elasticsearch.client.Response;import org.elasticsearch.client.RestClient;import org.elasticsearch.client.RestClientBuilder;import java.io.IOException;import java.security.KeyManagementException;import java.security.NoSuchAlgorithmException;import java.security.SecureRandom;import java.security.cert.CertificateException;import java.security.cert.X509Certificate;import java.util.Arrays;import java.util.List;import java.util.concurrent.TimeUnit;import javax.net.ssl.SSLContext;import javax.net.ssl.TrustManager;import javax.net.ssl.X509TrustManager;public class Main {private static final Logger logger = LogManager.getLogger(Main.class);/*** Create a class for the client. Define the create function.*/public static RestClient create(List<String> host, int port, String protocol, int connectTimeout,int connectionRequestTimeout, int socketTimeout, String username, String password) throws IOException {RestClientBuilder builder = RestClient.builder(constructHttpHosts(host, port, protocol)).setRequestConfigCallback(requestConfig -> requestConfig.setConnectTimeout(connectTimeout).setConnectionRequestTimeout(connectionRequestTimeout).setSocketTimeout(socketTimeout)).setHttpClientConfigCallback(httpClientBuilder -> {// enable user authenticationif (username != null && password != null) {final CredentialsProvider credentialsProvider = new BasicCredentialsProvider();credentialsProvider.setCredentials(AuthScope.ANY,new UsernamePasswordCredentials(username, password));httpClientBuilder.setDefaultCredentialsProvider(credentialsProvider);}// set keepalivehttpClientBuilder.setKeepAliveStrategy(((httpResponse, httpContext) -> TimeUnit.MINUTES.toMinutes(10)));// enable SSL / TLSSSLContext sc = null;try {sc = SSLContext.getInstance("SSL");sc.init(null, trustAllCerts, new SecureRandom());} catch (KeyManagementException | NoSuchAlgorithmException e) {e.printStackTrace();}SSLIOSessionStrategy sslStrategy = new SSLIOSessionStrategy(sc, new NoopHostnameVerifier());httpClientBuilder.setSSLStrategy(sslStrategy);return httpClientBuilder;});final RestClient client = builder.build();logger.info("es rest client build success {} ", client);return client;}/*** Use the constructHttpHosts function to convert the node IP address list of the host cluster.*/public static HttpHost[] constructHttpHosts(List<String> host, int port, String protocol) {return host.stream().map(p -> new HttpHost(p, port, protocol)).toArray(HttpHost[]::new);}/*** Configure trustAllCerts to ignore the certificate configuration.*/public static TrustManager[] trustAllCerts = new TrustManager[] {new X509TrustManager() {@Overridepublic void checkClientTrusted(X509Certificate[] chain, String authType) throws CertificateException {}@Overridepublic void checkServerTrusted(X509Certificate[] chain, String authType) throws CertificateException {}@Overridepublic X509Certificate[] getAcceptedIssuers() {return null;}}};/*** The following is an example of the main function. Call the create function to create the Low Level REST Client and check whether the test index exists.*/public static void main(String[] args) throws IOException {RestClient lowLevelClient = create(Arrays.asList("{Cluster access address}"), 9200, "http", 1000, 1000, 1000, "username","password");Request request = new Request("HEAD", "/test");Response response = lowLevelClient.performRequest(request);System.out.println(response.getStatusLine().getStatusCode());lowLevelClient.close();}}
Parameter | Description |
|---|---|
host | IP-адрес для доступа к кластеру. Если указано несколько IP-адресов, разделите их запятой (,). |
port | Порт доступа к кластеру. Значение по умолчанию — 9200. |
protocol | Протокол соединения, который может быть http или https. |
connectTimeout | Тайм‑аут сокет‑соединения (в мс). |
connectionRequestTimeout | Тайм‑аут запроса сокет‑соединения (в мс). |
socketTimeout | Таймаут запроса сокета (в мс). |
username | Имя пользователя для доступа к кластеру. |
password | Пароль пользователя. |
Этот фрагмент кода проверяет, существует ли индекс test в кластере. Если возвращается 200 (индекс существует) или 404 (индекс не существует), это указывает на то, что кластер подключён.
Используйте Low Level REST Client для подключения к кластеру Elasticsearch в режиме security-mode, использующему HTTPS с загруженным сертификатом безопасности, и проверьте, существует ли индекс test. Пример кода приведён ниже:
Чтобы узнать, как получить и загрузить сертификат безопасности, см. Obtaining and Uploading a Security Certificate.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133import org.apache.http.HttpHost;import org.apache.http.auth.AuthScope;import org.apache.http.auth.UsernamePasswordCredentials;import org.apache.http.client.CredentialsProvider;import org.apache.http.conn.ssl.NoopHostnameVerifier;import org.apache.http.impl.client.BasicCredentialsProvider;import org.apache.http.nio.conn.ssl.SSLIOSessionStrategy;import org.apache.logging.log4j.LogManager;import org.apache.logging.log4j.Logger;import org.elasticsearch.client.Request;import org.elasticsearch.client.Response;import org.elasticsearch.client.RestClient;import org.elasticsearch.client.RestClientBuilder;import java.io.File;import java.io.FileInputStream;import java.io.IOException;import java.io.InputStream;import java.security.KeyStore;import java.security.SecureRandom;import java.security.cert.CertificateException;import java.security.cert.X509Certificate;import java.util.Arrays;import java.util.List;import java.util.concurrent.TimeUnit;import javax.net.ssl.SSLContext;import javax.net.ssl.TrustManager;import javax.net.ssl.TrustManagerFactory;import javax.net.ssl.X509TrustManager;public class Main {private static final Logger logger = LogManager.getLogger(Main.class);/*** Create a class for the client. Define the create function.*/public static RestClient create(List<String> host, int port, String protocol, int connectTimeout,int connectionRequestTimeout, int socketTimeout, String username, String password, String certFilePath,String certPassword) throws IOException {RestClientBuilder builder = RestClient.builder(constructHttpHosts(host, port, protocol)).setRequestConfigCallback(requestConfig -> requestConfig.setConnectTimeout(connectTimeout).setConnectionRequestTimeout(connectionRequestTimeout).setSocketTimeout(socketTimeout)).setHttpClientConfigCallback(httpClientBuilder -> {// enable user authenticationif (username != null && password != null) {final CredentialsProvider credentialsProvider = new BasicCredentialsProvider();credentialsProvider.setCredentials(AuthScope.ANY,new UsernamePasswordCredentials(username, password));httpClientBuilder.setDefaultCredentialsProvider(credentialsProvider);}// set keepalivehttpClientBuilder.setKeepAliveStrategy(((httpResponse, httpContext) -> TimeUnit.MINUTES.toMinutes(10)));// enable SSL / TLSSSLContext sc = null;try {TrustManager[] tm = {new MyX509TrustManager(certFilePath, certPassword)};sc = SSLContext.getInstance("SSL", "SunJSSE");//You can also use SSLContext sslContext = SSLContext.getInstance("TLSv1.2");sc.init(null, tm, new SecureRandom());} catch (Exception e) {e.printStackTrace();}SSLIOSessionStrategy sslStrategy = new SSLIOSessionStrategy(sc, new NoopHostnameVerifier());httpClientBuilder.setSSLStrategy(sslStrategy);return httpClientBuilder;});final RestClient client = builder.build();logger.info("es rest client build success {} ", client);return client;}/*** Use the constructHttpHosts function to convert the node IP address list of the host cluster.*/public static HttpHost[] constructHttpHosts(List<String> host, int port, String protocol) {return host.stream().map(p -> new HttpHost(p, port, protocol)).toArray(HttpHost[]::new);}public static class MyX509TrustManager implements X509TrustManager {X509TrustManager sunJSSEX509TrustManager;MyX509TrustManager(String certFilePath, String certPassword) throws Exception {File file = new File(certFilePath);if (!file.isFile()) {throw new Exception("Wrong Certification Path");}System.out.println("Loading KeyStore " + file + "...");InputStream in = new FileInputStream(file);KeyStore ks = KeyStore.getInstance("JKS");ks.load(in, certPassword.toCharArray());TrustManagerFactory tmf = TrustManagerFactory.getInstance("SunX509", "SunJSSE");tmf.init(ks);TrustManager[] tms = tmf.getTrustManagers();for (TrustManager tm : tms) {if (tm instanceof X509TrustManager) {sunJSSEX509TrustManager = (X509TrustManager) tm;return;}}throw new Exception("Couldn't initialize");}@Overridepublic void checkClientTrusted(X509Certificate[] chain, String authType) throws CertificateException {}@Overridepublic void checkServerTrusted(X509Certificate[] chain, String authType) throws CertificateException {}@Overridepublic X509Certificate[] getAcceptedIssuers() {return new X509Certificate[0];}}/*** The following is an example of the main function. Call the create function to create the Low Level REST Client and check whether the test index exists.*/public static void main(String[] args) throws IOException {RestClient lowLevelClient = create(Arrays.asList("{Cluster access address}"), 9200, "https", 1000, 1000, 1000, "username","password", "certFilePath", "certPassword");Request request = new Request("HEAD", "test");Response response = lowLevelClient.performRequest(request);System.out.println(response.getStatusLine().getStatusCode());lowLevelClient.close();}}
Параметр | Описание |
|---|---|
host | IP-адрес для доступа к кластеру. Если указано несколько IP-адресов, разделите их запятой (,). |
port | Порт доступа к кластеру. Значение по умолчанию 9200. |
protocol | Протокол соединения. Установите этот параметр в https. |
connectTimeout | Тайм‑аут сокет‑соединения (в мс). |
connectionRequestTimeout | Тайм‑аут запроса сокет‑соединения (в мс). |
socketTimeout | Тайм‑аут сокет‑запроса (в мс). |
username | Имя пользователя для доступа к кластеру. |
password | Пароль пользователя. |
certFilePath | Путь для хранения сертификата безопасности. |
certPassword | Пароль сертификата безопасности. |
Этот фрагмент кода проверяет, существует ли индекс test в кластере. Если возвращён 200 (индекс существует) или 404 (индекс не существует), это указывает на то, что кластер подключён.
Используйте High Level REST Client для получения Low Level REST Client, вызвав getLowLevelClient(), используйте low-level client для подключения к кластеру Elasticsearch, у которого отключён режим безопасности, и запросите, существует ли индекс test. Пример кода приведён ниже:
12345678910111213141516171819202122232425262728293031323334353637import org.apache.http.HttpHost;import org.elasticsearch.client.Request;import org.elasticsearch.client.Response;import org.elasticsearch.client.RestClient;import org.elasticsearch.client.RestClientBuilder;import org.elasticsearch.client.RestHighLevelClient;import java.io.IOException;import java.util.Arrays;import java.util.List;public class Main {public static void main(String[] args) throws IOException {List<String> host = Arrays.asList("{Cluster access address}");RestClientBuilder builder = RestClient.builder(constructHttpHosts(host, 9200, "http"));final RestHighLevelClient restHighLevelClient = new RestHighLevelClient(builder);/*** Create a High Level Rest Client and then call getLowLevelClient() to obtain the Low Level Rest Client. The code differs from the client creation code only in the following line:*/final RestClient lowLevelClient = restHighLevelClient.getLowLevelClient();/*** Check whether the test index exists. If the index exists, 200 is returned. If the index does not exist, 404 is returned.*/Request request = new Request("HEAD", "/test");Response response = lowLevelClient.performRequest(request);System.out.println(response.getStatusLine().getStatusCode());lowLevelClient.close();}/*** Use the constructHttpHosts function to convert the node IP address list of the host cluster.*/public static HttpHost[] constructHttpHosts(List<String> host, int port, String protocol) {return host.stream().map(p -> new HttpHost(p, port, protocol)).toArray(HttpHost[]::new);}}
Этот фрагмент кода проверяет, существует ли индекс test в кластере. Если возвращён 200 (индекс существует) или 404 (индекс не существует), это указывает на то, что кластер подключён.
Используйте High Level REST Client для получения Low Level REST Client, вызвав getLowLevelClient(), используйте low-level client для подключения к кластеру Elasticsearch в режиме безопасности, который использует HTTP или HTTPS без загрузки сертификата безопасности, и запросите, существует ли индекс test. Пример кода приведён ниже:
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196import org.apache.http.HttpHost;import org.apache.http.HttpResponse;import org.apache.http.auth.AuthScope;import org.apache.http.auth.UsernamePasswordCredentials;import org.apache.http.client.CredentialsProvider;import org.apache.http.impl.client.BasicCredentialsProvider;import org.apache.http.impl.client.DefaultConnectionKeepAliveStrategy;import org.apache.http.impl.nio.client.HttpAsyncClientBuilder;import org.apache.http.nio.conn.ssl.SSLIOSessionStrategy;import org.apache.http.protocol.HttpContext;import org.apache.logging.log4j.LogManager;import org.apache.logging.log4j.Logger;import org.elasticsearch.client.Request;import org.elasticsearch.client.Response;import org.elasticsearch.client.RestClient;import org.elasticsearch.client.RestClientBuilder;import org.elasticsearch.common.Nullable;import java.io.IOException;import java.security.KeyManagementException;import java.security.NoSuchAlgorithmException;import java.security.SecureRandom;import java.security.cert.CertificateException;import java.security.cert.X509Certificate;import java.util.Arrays;import java.util.List;import java.util.Objects;import java.util.concurrent.TimeUnit;import javax.net.ssl.HostnameVerifier;import javax.net.ssl.SSLContext;import javax.net.ssl.SSLSession;import javax.net.ssl.TrustManager;import javax.net.ssl.X509TrustManager;import org.elasticsearch.client.RestHighLevelClient;public class Main13 {/*** Create a class for the client. Define the create function.*/public static RestHighLevelClient create(List<String> host, int port, String protocol, int connectTimeout, int connectionRequestTimeout, int socketTimeout, String username, String password) throws IOException {final CredentialsProvider credentialsProvider = new BasicCredentialsProvider();credentialsProvider.setCredentials(AuthScope.ANY, new UsernamePasswordCredentials(username, password));SSLContext sc = null;try {sc = SSLContext.getInstance("SSL");sc.init(null, trustAllCerts, new SecureRandom());} catch (KeyManagementException | NoSuchAlgorithmException e) {e.printStackTrace();}SSLIOSessionStrategy sessionStrategy = new SSLIOSessionStrategy(sc, new NullHostNameVerifier());SecuredHttpClientConfigCallback httpClientConfigCallback = new SecuredHttpClientConfigCallback(sessionStrategy,credentialsProvider);RestClientBuilder builder = RestClient.builder(constructHttpHosts(host, port, protocol)).setRequestConfigCallback(requestConfig -> requestConfig.setConnectTimeout(connectTimeout).setConnectionRequestTimeout(connectionRequestTimeout).setSocketTimeout(socketTimeout)).setHttpClientConfigCallback(httpClientConfigCallback);final RestHighLevelClient client = new RestHighLevelClient(builder);logger.info("es rest client build success {} ", client);return client;}/*** Use the constructHttpHosts function to convert the node IP address list of the host cluster.*/public static HttpHost[] constructHttpHosts(List<String> host, int port, String protocol) {return host.stream().map(p -> new HttpHost(p, port, protocol)).toArray(HttpHost[]::new);}/*** Configure trustAllCerts to ignore the certificate configuration.*/public static TrustManager[] trustAllCerts = new TrustManager[] {new X509TrustManager() {@Overridepublic void checkClientTrusted(X509Certificate[] chain, String authType) throws CertificateException {}@Overridepublic void checkServerTrusted(X509Certificate[] chain, String authType) throws CertificateException {}@Overridepublic X509Certificate[] getAcceptedIssuers() {return null;}}};/*** The CustomConnectionKeepAliveStrategy function is used to set the connection keepalive when there are a large number of short connections or when there are not many data requests.*/public static class CustomConnectionKeepAliveStrategy extends DefaultConnectionKeepAliveStrategy {public static final CustomConnectionKeepAliveStrategy INSTANCE = new CustomConnectionKeepAliveStrategy();private CustomConnectionKeepAliveStrategy() {super();}/*** Maximum keepalive time (in minutes)* The default value is 10 minutes. You can set it based on the number of TCP connections in TIME_WAIT state. If there are too many TCP connections, you can increase this value.*/private final long MAX_KEEP_ALIVE_MINUTES = 10;@Overridepublic long getKeepAliveDuration(HttpResponse response, HttpContext context) {long keepAliveDuration = super.getKeepAliveDuration(response, context);// <0 indicates an unlimited keepalive period.// Change the period from unlimited to a default period.if (keepAliveDuration < 0) {return TimeUnit.MINUTES.toMillis(MAX_KEEP_ALIVE_MINUTES);}return keepAliveDuration;}}private static final Logger logger = LogManager.getLogger(Main.class);static class SecuredHttpClientConfigCallback implements RestClientBuilder.HttpClientConfigCallback {@Nullableprivate final CredentialsProvider credentialsProvider;/*** The {@link SSLIOSessionStrategy} for all requests to enable SSL / TLS encryption.*/private final SSLIOSessionStrategy sslStrategy;/*** Create a new {@link SecuredHttpClientConfigCallback}.** @param credentialsProvider The credential provider, if a username/password have been supplied* @param sslStrategy The SSL strategy, if SSL / TLS have been supplied* @throws NullPointerException if {@code sslStrategy} is {@code null}*/SecuredHttpClientConfigCallback(final SSLIOSessionStrategy sslStrategy,@Nullable final CredentialsProvider credentialsProvider) {this.sslStrategy = Objects.requireNonNull(sslStrategy);this.credentialsProvider = credentialsProvider;}/*** Get the {@link CredentialsProvider} that will be added to the HTTP client.** @return Can be {@code null}.*/@NullableCredentialsProvider getCredentialsProvider() {return credentialsProvider;}/*** Get the {@link SSLIOSessionStrategy} that will be added to the HTTP client.** @return Never {@code null}.*/SSLIOSessionStrategy getSSLStrategy() {return sslStrategy;}/*** Sets the {@linkplain HttpAsyncClientBuilder#setDefaultCredentialsProvider(CredentialsProvider) credential provider},** @param httpClientBuilder The client to configure.* @return Always {@code httpClientBuilder}.*/@Overridepublic HttpAsyncClientBuilder customizeHttpClient(final HttpAsyncClientBuilder httpClientBuilder) {// enable SSL / TLShttpClientBuilder.setSSLStrategy(sslStrategy);// enable user authenticationif (credentialsProvider != null) {httpClientBuilder.setDefaultCredentialsProvider(credentialsProvider);}return httpClientBuilder;}}public static class NullHostNameVerifier implements HostnameVerifier {@Overridepublic boolean verify(String arg0, SSLSession arg1) {return true;}}/*** The following is an example of the main function. Call the create function to create a high-level client, call the getLowLevelClient() function to obtain a low-level client, and check whether the test index exists.*/public static void main(String[] args) throws IOException {RestHighLevelClient client = create(Arrays.asList("{Cluster access address}") 9200, "http", 1000, 1000, 1000, "username", "password");RestClient lowLevelClient = client.getLowLevelClient();Request request = new Request("HEAD", "test");Response response = lowLevelClient.performRequest(request);System.out.println(response.getStatusLine().getStatusCode());lowLevelClient.close();}}
Параметр | Описание |
|---|---|
host | IP-адрес для доступа к кластеру. Если указано несколько IP-адресов, разделите их запятой (,). |
port | Порт доступа к кластеру. Значение по умолчанию — 9200. |
protocol | Протокол соединения, который может быть http или https. |
connectTimeout | Тайм‑аут соединения сокета (в мс). |
connectionRequestTimeout | Тайм‑аут запроса соединения сокета (в мс). |
socketTimeout | Тайм‑аут запроса сокета (в мс). |
username | Имя пользователя для доступа к кластеру. |
password | Пароль пользователя. |
Этот фрагмент кода проверяет, существует ли индекс test в кластере. Если возвращён 200 (индекс существует) или 404 (индекс не существует), это указывает на то, что кластер подключён.
Используйте High Level REST Client для получения Low Level REST Client, вызвав getLowLevelClient(), используйте low-level client для подключения к security-mode Elasticsearch cluster, использующему HTTPS с загруженным сертификатом безопасности, и запросите, существует ли индекс test. Пример кода приведён ниже:
Чтобы узнать, как получить и загрузить сертификат безопасности, см. Obtaining and Uploading a Security Certificate.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162import org.apache.http.HttpHost;import org.apache.http.auth.AuthScope;import org.apache.http.auth.UsernamePasswordCredentials;import org.apache.http.client.CredentialsProvider;import org.apache.http.conn.ssl.NoopHostnameVerifier;import org.apache.http.impl.client.BasicCredentialsProvider;import org.apache.http.impl.nio.client.HttpAsyncClientBuilder;import org.apache.http.nio.conn.ssl.SSLIOSessionStrategy;import org.apache.logging.log4j.LogManager;import org.apache.logging.log4j.Logger;import org.elasticsearch.action.admin.cluster.health.ClusterHealthRequest;import org.elasticsearch.action.admin.cluster.health.ClusterHealthResponse;import org.elasticsearch.client.Request;import org.elasticsearch.client.RequestOptions;import org.elasticsearch.client.Response;import org.elasticsearch.client.RestClient;import org.elasticsearch.client.RestClientBuilder;import org.elasticsearch.client.RestHighLevelClient;import org.elasticsearch.common.Nullable;import java.io.File;import java.io.FileInputStream;import java.io.IOException;import java.io.InputStream;import java.security.KeyStore;import java.security.SecureRandom;import java.security.cert.CertificateException;import java.security.cert.X509Certificate;import java.util.Arrays;import java.util.List;import java.util.Objects;import javax.net.ssl.SSLContext;import javax.net.ssl.TrustManager;import javax.net.ssl.TrustManagerFactory;import javax.net.ssl.X509TrustManager;public class Main {private static final Logger logger = LogManager.getLogger(Main.class);/*** Create a class for the client. Define the create function.*/public static RestHighLevelClient create(List<String> host, int port, String protocol, int connectTimeout, int connectionRequestTimeout, int socketTimeout, String username, String password, String certFilePath, String certPassword) throws IOException {final CredentialsProvider credentialsProvider = new BasicCredentialsProvider();credentialsProvider.setCredentials(AuthScope.ANY, new UsernamePasswordCredentials(username, password));SSLContext sc = null;try {TrustManager[] tm = {new MyX509TrustManager(certFilePath, certPassword)};sc = SSLContext.getInstance("SSL", "SunJSSE");//You can also use SSLContext sslContext = SSLContext.getInstance("TLSv1.2");sc.init(null, tm, new SecureRandom());} catch (Exception e) {e.printStackTrace();}SSLIOSessionStrategy sessionStrategy = new SSLIOSessionStrategy(sc, new NoopHostnameVerifier());SecuredHttpClientConfigCallback httpClientConfigCallback = new SecuredHttpClientConfigCallback(sessionStrategy,credentialsProvider);RestClientBuilder builder = RestClient.builder(constructHttpHosts(host, port, protocol)).setRequestConfigCallback(requestConfig -> requestConfig.setConnectTimeout(connectTimeout).setConnectionRequestTimeout(connectionRequestTimeout).setSocketTimeout(socketTimeout)).setHttpClientConfigCallback(httpClientConfigCallback);final RestHighLevelClient client = new RestHighLevelClient(builder);logger.info("es rest client build success {} ", client);ClusterHealthRequest request = new ClusterHealthRequest();ClusterHealthResponse response = client.cluster().health(request, RequestOptions.DEFAULT);logger.info("es rest client health response {} ", response);return client;}/*** Use the constructHttpHosts function to convert the node IP address list of the host cluster.*/public static HttpHost[] constructHttpHosts(List<String> host, int port, String protocol) {return host.stream().map(p -> new HttpHost(p, port, protocol)).toArray(HttpHost[]::new);}static class SecuredHttpClientConfigCallback implements RestClientBuilder.HttpClientConfigCallback {@Nullableprivate final CredentialsProvider credentialsProvider;private final SSLIOSessionStrategy sslStrategy;SecuredHttpClientConfigCallback(final SSLIOSessionStrategy sslStrategy,@Nullable final CredentialsProvider credentialsProvider) {this.sslStrategy = Objects.requireNonNull(sslStrategy);this.credentialsProvider = credentialsProvider;}@NullableCredentialsProvider getCredentialsProvider() {return credentialsProvider;}SSLIOSessionStrategy getSSLStrategy() {return sslStrategy;}@Overridepublic HttpAsyncClientBuilder customizeHttpClient(final HttpAsyncClientBuilder httpClientBuilder) {httpClientBuilder.setSSLStrategy(sslStrategy);if (credentialsProvider != null) {httpClientBuilder.setDefaultCredentialsProvider(credentialsProvider);}return httpClientBuilder;}}public static class MyX509TrustManager implements X509TrustManager {X509TrustManager sunJSSEX509TrustManager;MyX509TrustManager(String certFilePath, String certPassword) throws Exception {File file = new File(certFilePath);if (!file.isFile()) {throw new Exception("Wrong Certification Path");}System.out.println("Loading KeyStore " + file + "...");InputStream in = new FileInputStream(file);KeyStore ks = KeyStore.getInstance("JKS");ks.load(in, certPassword.toCharArray());TrustManagerFactory tmf = TrustManagerFactory.getInstance("SunX509", "SunJSSE");tmf.init(ks);TrustManager[] tms = tmf.getTrustManagers();for (TrustManager tm : tms) {if (tm instanceof X509TrustManager) {sunJSSEX509TrustManager = (X509TrustManager) tm;return;}}throw new Exception("Couldn't initialize");}@Overridepublic void checkClientTrusted(X509Certificate[] chain, String authType) throws CertificateException {}@Overridepublic void checkServerTrusted(X509Certificate[] chain, String authType) throws CertificateException {}@Overridepublic X509Certificate[] getAcceptedIssuers() {return new X509Certificate[0];}}/*** The following is an example of the main function. Call the create function to create a high-level client, call the getLowLevelClient() function to obtain a low-level client, and check whether the test index exists.*/public static void main(String[] args) throws IOException {RestHighLevelClient client = create(Arrays.asList("{Cluster access address}", 9200, "https", 1000, 1000, 1000, "username", "password", "certFilePath", "certPassword");RestClient lowLevelClient = client.getLowLevelClient();Request request = new Request("HEAD", "test");Response response = lowLevelClient.performRequest(request);System.out.println(response.getStatusLine().getStatusCode());lowLevelClient.close();}}
Параметр | Описание |
|---|---|
host | IP-адрес для доступа к кластеру. Если указано несколько IP-адресов, разделите их запятой (,). |
port | Порт доступа к кластеру. Значение по умолчанию — 9200. |
protocol | Протокол соединения. Установите этот параметр в https. |
connectTimeout | Тайм‑аут сокет‑соединения (в мс). |
connectionRequestTimeout | Тайм‑аут запроса сокет‑соединения (в мс). |
socketTimeout | Тайм-аут запроса сокета (в мс). |
username | Имя пользователя для доступа к кластеру. |
password | Пароль пользователя. |
certFilePath | Путь для хранения сертификата безопасности. |
certPassword | Пароль сертификата безопасности. |
Этот фрагмент кода проверяет, существует ли индекс test в кластере. Если возвращается 200 (индекс существует) или 404 (индекс не существует), это указывает на то, что кластер подключён.
Для доступа к кластеру Elasticsearch в режиме безопасности, использующему HTTPS, выполните следующие шаги, чтобы при необходимости получить сертификат безопасности и загрузить его в клиент.
keytool -import -alias newname -keystore ./truststore.jks -file ./CloudSearchService.cer
keytool -import -alias newname -keystore .\truststore.jks -file .\CloudSearchService.cer
В приведённой выше команде newname указывает пользовательское имя сертификата.
После выполнения этой команды вам будет предложено задать пароль сертификата и подтвердить его. Надёжно сохраните пароль. Он будет использоваться для доступа к кластеру.