這篇文章主要介紹了基于Springboot2.0如何構(gòu)建ES多客戶端,具有一定借鑒價(jià)值,感興趣的朋友可以參考下,希望大家閱讀完這篇文章之后大有收獲,下面讓小編帶著大家一起了解一下。
石首ssl適用于網(wǎng)站、小程序/APP、API接口等需要進(jìn)行數(shù)據(jù)傳輸應(yīng)用場(chǎng)景,ssl證書(shū)未來(lái)市場(chǎng)廣闊!成為創(chuàng)新互聯(lián)的ssl證書(shū)銷售渠道,可以享受市場(chǎng)價(jià)格4-6折優(yōu)惠!如果有意向歡迎電話聯(lián)系或者加微信:18980820575(備注:SSL證書(shū)合作)期待與您的合作!
有時(shí)候我們操作es的時(shí)候會(huì)有一些特殊的需求,例如需要操作的index使用了不同的es服務(wù)器、用戶名、密碼、參數(shù)等,這個(gè)時(shí)候我們需要使用不同的es的客戶端進(jìn)行操作,但是我們又不希望拆分成多個(gè)項(xiàng)目進(jìn)行使用,這個(gè)時(shí)候我們就需要在我們的配置中自己構(gòu)建一套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的配置文件,因?yàn)槲覀兪褂貌煌男畔?,所以這里我就不寫(xiě)了,可以根據(jù)需求自行獲取。
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賬號(hào)密碼 new UsernamePasswordCredentials(elasticsearchConfig.getUsername(), elasticsearchConfig.getPassword())); //自動(dòng)掃描網(wǎng)段 //監(jiān)聽(tīng)同網(wǎng)段服務(wù) //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); } }
最終
在需要使用的地方直接通過(guò)注入的方式使用不同的客戶端
@Resource(name = "HighLevelESClient") private RestHighLevelClient client;
感謝你能夠認(rèn)真閱讀完這篇文章,希望小編分享的“基于Springboot2.0如何構(gòu)建ES多客戶端”這篇文章對(duì)大家有幫助,同時(shí)也希望大家多多支持創(chuàng)新互聯(lián),關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道,更多相關(guān)知識(shí)等著你來(lái)學(xué)習(xí)!