作者:晶晶9930_195 | 来源:互联网 | 2024-10-11 18:36
***获取ES连接类**@author陈康*@description*@create20190815**@Component("Elasticsearch
/**
* 获取ES连接类
*
* @author 陈康
* @description
* @create 2019/08/15
**/
@Component("ElasticsearchRestClient")
@Configuration
public class ElasticsearchRestClient {
private static String host = "你的es地址";
private static int port = 9200;
private static String userName = "你的账号";
private static String passWord = "你的密码";
public static RestClient getRestClient() {
//初始化ES操作客户端
final CredentialsProvider credentialsProvider = new BasicCredentialsProvider();
credentialsProvider.setCredentials(AuthScope.ANY,
new UsernamePasswordCredentials(userName, passWord)); //es账号密码
esClient = new RestHighLevelClient(
RestClient.builder(
new HttpHost(host, port)
).setHttpClientConfigCallback(new RestClientBuilder.HttpClientConfigCallback() {
public HttpAsyncClientBuilder customizeHttpClient(HttpAsyncClientBuilder httpClientBuilder) {
httpClientBuilder.disableAuthCaching();
return httpClientBuilder.setDefaultCredentialsProvider(credentialsProvider);
}
})
);
return esClient.getLowLevelClient();
}
}
我使用的ES版本是6.3.2的所有使用的pom.xml是(注意:使用的版本一定要与你的ES版本相同!)
<dependency>
<groupId>org.elasticsearch.clientgroupId>
<artifactId>elasticsearch-rest-high-level-clientartifactId>
<exclusions>
<exclusion>
<groupId>org.elasticsearchgroupId>
<artifactId>elasticsearchartifactId>
exclusion>
exclusions>
<version>6.3.2version>
dependency>
<dependency>
<groupId>org.elasticsearchgroupId>
<artifactId>elasticsearchartifactId>
<version>6.3.2version>
dependency>
<dependency>
<groupId>org.elasticsearch.clientgroupId>
<artifactId>elasticsearch-rest-clientartifactId>
<version>6.3.2version>
dependency>