一、基础信息
1、pom.xml(根据业务选择适合版本)
<dependency><groupId>org.elasticsearch</groupId><artifactId>elasticsearch</artifactId><version>${elasticsearch.version}</version></dependency><dependency><groupId>org.elasticsearch.client</groupId><artifactId>elasticsearch-rest-client</artifactId><version>${elasticsearch.version}</version></dependency><dependency><groupId>org.elasticsearch.client</groupId><artifactId>elasticsearch-rest-high-level-client</artifactId><version>${elasticsearch.version}</version></dependency>
2、nacos配置(根据业务选择适合模式)
elasticsearch:
prefix: http
uris: es-cn-**********************.aliyun.com:9200
username: esadmin
password:123456
3、创建ElasticSearchConfig(项目启动自动注入)
importorg.springframework.beans.factory.annotation.Value;importorg.springframework.context.annotation.Configuration;@ConfigurationpublicclassElasticSearchConfig{//http@Value("${elasticsearch.prefix}")privateString prefix;//es连接url(多个url用","隔开)@Value("${elasticsearch.uris}")privateString esUrl;//账户(没有可不填)@Value("${elasticsearch.username}")privateString username;//密码(没有可不填)@Value("${elasticsearch.password}")privateString password;//方法}
二、低级客户端创建
@BeanpublicRestClientrestClient(){//处理多连接模式String[] split = esUrl.split(",");HttpHost[] httpHost =newHttpHost[split.length];for(int i =0; i < split.length; i++){String[] item = split[i].split(":");
httpHost[i]=newHttpHost(item[0],Integer.parseInt(item[1]), prefix);}returnRestClient.builder(httpHost).build();}
三、高级客户端创建
@BeanpublicRestHighLevelClientrestHighLevelClient(){//处理多连接模式String[] split = esUrl.split(",");HttpHost[] httpHost =newHttpHost[split.length];for(int i =0; i < split.length; i++){String[] item = split[i].split(":");
httpHost[i]=newHttpHost(item[0],Integer.parseInt(item[1]), prefix);}returnnewRestHighLevelClient(RestClient.builder(httpHost));}
四、高级客户端创建(带用户名和密码)
@BeanpublicRestHighLevelClientrestHighLevelClient(){//处理多连接模式String[] split = esUrl.split(",");HttpHost[] httpHost =newHttpHost[split.length];for(int i =0; i < split.length; i++){String[] item = split[i].split(":");
httpHost[i]=newHttpHost(item[0],Integer.parseInt(item[1]), prefix);}//需要用户名和密码的认证finalCredentialsProvider credentialsProvider =newBasicCredentialsProvider();
credentialsProvider.setCredentials(AuthScope.ANY,newUsernamePasswordCredentials(username, password));RestClientBuilder restClientBuilder =RestClient.builder(httpHost).setHttpClientConfigCallback(httpAsyncClientBuilder -> httpAsyncClientBuilder.setDefaultCredentialsProvider(credentialsProvider));returnnewRestHighLevelClient(restClientBuilder);}
附:增加部分参数
RestClientBuilder restClientBuilder =RestClient.builder(httpHost)// 认证和线程数.setHttpClientConfigCallback(httpClientBuilder ->{
httpClientBuilder.setDefaultCredentialsProvider(credentialsProvider);int threadCount =10;
httpClientBuilder.setDefaultIOReactorConfig(IOReactorConfig.custom().setIoThreadCount(threadCount).build());return httpClientBuilder;})// 超时超时设置.setRequestConfigCallback(requestConfigCallback ->{
requestConfigCallback.setConnectTimeout(10);
requestConfigCallback.setSocketTimeout(10);return requestConfigCallback;});
本文转载自: https://blog.csdn.net/w_shimmer/article/details/127486542
版权归原作者 王疏蔬 所有, 如有侵权,请联系我们删除。
版权归原作者 王疏蔬 所有, 如有侵权,请联系我们删除。