rest-high-level-client使用示例
pom引入
<dependency>
<groupId>org.elasticsearch.client</groupId>
<artifactId>elasticsearch-rest-high-level-client</artifactId>
<version>6.3.2</version>
</dependency>
代码
import 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.impl.client.BasicCredentialsProvider;
import org.elasticsearch.client.RestClient;
import org.elasticsearch.client.RestHighLevelClient;
public class SecurityRestClient {
private static final int HTTP_PORT = 20019;
private static final String IP = "10.10.10.13";
private static final String SecurityUser = "jiesi-6.3";
private static final String SecurityPassword = "***********";
public static RestHighLevelClient RestClient() {
RestHighLevelClient client = new RestHighLevelClient(
RestClient.builder(
new HttpHost(IP, HTTP_PORT, "http")
).setHttpClientConfigCallback(httpClientBuilder ->
httpClientBuilder.setDefaultCredentialsProvider(init()))
);
return client;
}
private static CredentialsProvider init() {
CredentialsProvider credentialsProvider = new BasicCredentialsProvider();
credentialsProvider.setCredentials(AuthScope.ANY,
new UsernamePasswordCredentials(SecurityUser, SecurityPassword));
return credentialsProvider;
}
}
注意回调方法setHttpClientConfigCallback内不要添加
httpClientBuilder.disableAuthCaching()
方法。