扫二维码与项目经理沟通
我们在微信上24小时期待你的声音
解答本文疑问/技术咨询/运营咨询/技术建议/互联网交流
这篇文章主要介绍了基于Springboot2.0如何构建ES多客户端,具有一定借鉴价值,感兴趣的朋友可以参考下,希望大家阅读完这篇文章之后大有收获,下面让小编带着大家一起了解一下。
石首ssl适用于网站、小程序/APP、API接口等需要进行数据传输应用场景,ssl证书未来市场广阔!成为创新互联的ssl证书销售渠道,可以享受市场价格4-6折优惠!如果有意向欢迎电话联系或者加微信:18980820575(备注:SSL证书合作)期待与您的合作!
有时候我们操作es的时候会有一些特殊的需求,例如需要操作的index使用了不同的es服务器、用户名、密码、参数等,这个时候我们需要使用不同的es的客户端进行操作,但是我们又不希望拆分成多个项目进行使用,这个时候我们就需要在我们的配置中自己构建一套ES的多客户端了。
文章目录
pom.xml
ElasticsearchConfig.java
ElasticsearchRestClient.java
最终
pom.xml
首先是我们的pom.xml:
org.springframework.boot spring-boot-starter 2.0.5.RELEASE org.elasticsearch elasticsearch 5.6.11 org.elasticsearch.client elasticsearch-rest-client 5.6.11 org.elasticsearch.client elasticsearch-rest-client-sniffer 5.6.3 compile org.elasticsearch.client elasticsearch-rest-high-level-client 5.6.11 org.apache.commons commons-lang3 3.4 com.alibaba fastjson 1.2.39 org.apache.logging.log4j log4j-core 2.9.1 org.springframework.boot spring-boot-devtools org.apache.commons commons-pool2 2.4.2 org.elasticsearch.client elasticsearch-rest-client-sniffer 5.6.0 commons-io commons-io 2.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;
感谢你能够认真阅读完这篇文章,希望小编分享的“基于Springboot2.0如何构建ES多客户端”这篇文章对大家有帮助,同时也希望大家多多支持创新互联,关注创新互联行业资讯频道,更多相关知识等着你来学习!
我们在微信上24小时期待你的声音
解答本文疑问/技术咨询/运营咨询/技术建议/互联网交流