|
private static ElasticsearchClient getElasticsearchClient(String host, int port, String username, String password) {
RequestConfig config = RequestConfig.custom().setSocketTimeout(120000).build();
// 设置身份验证信息
CredentialsProvider credentialsProvider = new BasicCredentialsProvider();
credentialsProvider.setCredentials(AuthScope.ANY, new UsernamePasswordCredentials(username, password));
RestClientBuilder builder = RestClient.builder(new HttpHost(host, port))
.setHttpClientConfigCallback(httpClientBuilder -> httpClientBuilder
.setKeepAliveStrategy((response, context) -> Duration.ofMinutes(5).toMillis())
.setDefaultRequestConfig(config)
.setDefaultHeaders(Arrays.asList(
new BasicHeader(HttpHeaders.CONTENT_TYPE, "application/json"),
new BasicHeader("X-Elastic-Product", "Elasticsearch")
))
);
RestClient restClient = builder.build();
ElasticsearchTransport transport = new RestClientTransport(restClient, new JacksonJsonpMapper());
return new ElasticsearchClient(transport);
} |
|