基于Springboot2.0构建ES的多客户端
有时候我们操作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
importcnkj.site.config.ElasticsearchConfig; importlombok.extern.slf4j.Slf4j; importorg.apache.http.HttpHost; importorg.apache.http.auth.AuthScope; importorg.apache.http.auth.UsernamePasswordCredentials; importorg.apache.http.client.CredentialsProvider; importorg.apache.http.client.config.RequestConfig; importorg.apache.http.impl.client.BasicCredentialsProvider; importorg.elasticsearch.client.RestClient; importorg.elasticsearch.client.RestClientBuilder; importorg.elasticsearch.client.RestHighLevelClient; importorg.elasticsearch.client.sniff.SniffOnFailureListener; importorg.elasticsearch.client.sniff.Sniffer; importorg.elasticsearch.client.sniff.SnifferBuilder; importorg.springframework.beans.factory.annotation.Qualifier; importorg.springframework.context.annotation.Bean; importorg.springframework.context.annotation.Configuration; /* *@version1.0createdbyLXWon2018/11/229:43 */ @Slf4j @Configuration publicclassElasticsearchClient{ @Bean(name="HighESClient") publicRestClientrestTomcatClient(ElasticsearchConfigelasticsearchConfig){ finalCredentialsProvidercredentialsProvider=newBasicCredentialsProvider(); credentialsProvider.setCredentials(AuthScope.ANY, //es账号密码 newUsernamePasswordCredentials(elasticsearchConfig.getUsername(),elasticsearchConfig.getPassword())); //自动扫描网段 //监听同网段服务 //LowLevelClientinit RestClientBuilderbuilder=RestClient.builder( newHttpHost( elasticsearchConfig.getHost(), Integer.valueOf(elasticsearchConfig.getPort()), elasticsearchConfig.getSchema() ) ).setHttpClientConfigCallback( httpClientBuilder->httpClientBuilder.setDefaultCredentialsProvider(credentialsProvider) ).setRequestConfigCallback(newRestClientBuilder.RequestConfigCallback(){ @Override publicRequestConfig.BuildercustomizeRequestConfig(RequestConfig.Builderbuilder){ builder.setConnectTimeout(elasticsearchConfig.getConnectTimeout()); builder.setSocketTimeout(elasticsearchConfig.getSocketTimeout()); returnbuilder; } }) .setMaxRetryTimeoutMillis(elasticsearchConfig.getMaxRetryTimeoutMillis()); builder.setMaxRetryTimeoutMillis(elasticsearchConfig.getMaxRetryTimeoutMillis()); SniffOnFailureListenersniffOnFailureListener=newSniffOnFailureListener(); builder.setFailureListener(sniffOnFailureListener); RestClientlowLevelRestClient=builder.build(); SnifferBuildersnifferBuilder=Sniffer.builder(lowLevelRestClient).setSniffIntervalMillis(elasticsearchConfig.getSnifferinterval()); if(elasticsearchConfig.getFailuredelay()>0){ snifferBuilder.setSniffAfterFailureDelayMillis(elasticsearchConfig.getFailuredelay()); } sniffOnFailureListener.setSniffer(snifferBuilder.build()); returnlowLevelRestClient; } @Bean(name="HighLevelESClient") publicRestHighLevelClientrestHighLevelClient(@Qualifier("HighESClient")RestClientrestClient){ returnnewRestHighLevelClient(restClient); } }
最终
在需要使用的地方直接通过注入的方式使用不同的客户端
@Resource(name="HighLevelESClient") privateRestHighLevelClientclient;
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持毛票票。
声明:本文内容来源于网络,版权归原作者所有,内容由互联网用户自发贡献自行上传,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任。如果您发现有涉嫌版权的内容,欢迎发送邮件至:czq8825#qq.com(发邮件时,请将#更换为@)进行举报,并提供相关证据,一经查实,本站将立刻删除涉嫌侵权内容。