private static String scheme = "http";
private static String hostname = "127.0.0.1";
private static int port = 9200;
private static String username = "XXXX";
private static String password = "XXXX";
public static RestHighLevelClient esClient = null;
public void ESClientInit() {
//不需要用户名和密码的认证
//esClient = new RestHighLevelClient(RestClient.builder(new HttpHost(hostname, port, scheme)));
//需要用户名和密码的认证
final CredentialsProvider credentialsProvider = new BasicCredentialsProvider();
credentialsProvider.setCredentials(AuthScope.ANY, new UsernamePasswordCredentials(username, password));
RestClientBuilder restClientBuilder = RestClient.builder(new HttpHost(hostname, port, scheme))
.setHttpClientConfigCallback(new RestClientBuilder.HttpClientConfigCallback() {
@Override
public HttpAsyncClientBuilder customizeHttpClient(HttpAsyncClientBuilder httpAsyncClientBuilder) {
return httpAsyncClientBuilder.setDefaultCredentialsProvider(credentialsProvider);
}
});
esClient = new RestHighLevelClient(restClientBuilder);
}
Java客户端添加ES的用户名和密码认证
最新推荐文章于 2023-08-06 17:48:32 发布