这篇文章主要为大家详细介绍了基于Springboot2.0构建ES的多客户端,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
有时候我们操作es的时候会有一些特殊的需求,例如需要操作的index使用了不同的es服务器、用户名、密码、参数等,这个时候我们需要使用不同的es的客户端进行操作,但是我们又不希望拆分成多个项目进行使用,这个时候我们就需要在我们的配置中自己构建一套ES的多客户端了。
文章目录
pom.xml
ElasticsearchConfig.java
ElasticsearchRestClient.java
最终
pom.xml
首先是我们的pom.xml:
org.springframework.bootspring-boot-starter2.0.5.RELEASEOrg.elasticsearchelasticsearch5.6.11org.elasticsearch.clientelasticsearch-rest-client5.6.11org.elasticsearch.clientelasticsearch-rest-client-sniffer5.6.3compileorg.elasticsearch.clientelasticsearch-rest-high-level-client5.6.11org.apache.commonscommons-lang33.4com.alibabafastjson1.2.39org.apache.logging.log4jlog4j-core2.9.1org.springframework.bootspring-boot-devtoolsorg.apache.commonscommons-pool22.4.2org.elasticsearch.clientelasticsearch-rest-client-sniffer5.6.0commons-iocommons-io2.6
ElasticsearchConfig.java
然后是我们的配置文件,我这里使用的是application.properties的配置文件,因为我们使用不同的信息,所以这里我就不写了,可以根据需求自行获取。
ElasticsearchRestClient.java
import cnkj.site.config.ElasticsearchConfig; import lombok.extern.slf4j.Slf4j; 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.client.config.RequestConfig; import org.apache.http.impl.client.BasicCredentialsProvider; import org.elasticsearch.client.RestClient; import org.elasticsearch.client.RestClientBuilder; import org.elasticsearch.client.RestHighLevelClient; import org.elasticsearch.client.sniff.SniffOnFailureListener; import org.elasticsearch.client.sniff.Sniffer; import org.elasticsearch.client.sniff.SnifferBuilder; import org.springframework.beans.factory.annotation.Qualifier; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; /* * @version 1.0 created by LXW on 2018/11/22 9:43 */ @Slf4j @Configuration public class ElasticsearchClient { @Bean(name = "HighESClient") public RestClient restTomcatClient(ElasticsearchConfig elasticsearchConfig) { final CredentialsProvider credentialsProvider = new BasicCredentialsProvider(); credentialsProvider.setCredentials(AuthScope.ANY, //es账号密码 new UsernamePasswordCredentials(elasticsearchConfig.getUsername(), elasticsearchConfig.getpassword())); //自动扫描网段 //监听同网段服务 //Low Level Client init RestClientBuilder builder = RestClient.builder( new HttpHost( elasticsearchConfig.getHost(), Integer.valueOf(elasticsearchConfig.getPort()), elasticsearchConfig.getSchema() ) ).setHttpClientConfigCallback( httpClientBuilder -> httpClientBuilder.setDefaultCredentialsProvider(credentialsProvider) ).setRequestConfigCallback(new RestClientBuilder.RequestConfigCallback() { @Override public RequestConfig.Builder customizeRequestConfig(RequestConfig.Builder builder) { builder.setConnectTimeout(elasticsearchConfig.getConnectTimeout()); builder.setSocketTimeout(elasticsearchConfig.getSocketTimeout()); return builder; } }) .setMaxRetryTimeoutMillis(elasticsearchConfig.getMaxRetryTimeoutMillis()); builder.setMaxRetryTimeoutMillis(elasticsearchConfig.getMaxRetryTimeoutMillis()); SniffOnFailureListener sniffOnFailureListener = new SniffOnFailureListener(); builder.setFailureListener(sniffOnFailureListener); RestClient lowLevelRestClient = builder.build(); SnifferBuilder snifferBuilder = Sniffer.builder(lowLevelRestClient).setSniffIntervalMillis(elasticsearchConfig.getSnifferinterval()); if (elasticsearchConfig.getFailuredelay() > 0) { snifferBuilder.setSniffAfterFailureDelayMillis(elasticsearchConfig.getFailuredelay()); } sniffOnFailureListener.setSniffer(snifferBuilder.build()); return lowLevelRestClient; } @Bean(name = "HighLevelESClient") public RestHighLevelClient restHighLevelClient(@Qualifier("HighESClient") RestClient restClient) { return new RestHighLevelClient(restClient); } }
最终
在需要使用的地方直接通过注入的方式使用不同的客户端
@Resource(name = "HighLevelESClient") private RestHighLevelClient client;
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持编程之家。
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。