基于Springboot2.0构建ES的多客户端
程序员文章站
2024-02-24 10:20:16
有时候我们操作es的时候会有一些特殊的需求,例如需要操作的index使用了不同的es服务器、用户名、密码、参数等,这个时候我们需要使用不同的es的客户端进行操作,但是我们又...
有时候我们操作es的时候会有一些特殊的需求,例如需要操作的index使用了不同的es服务器、用户名、密码、参数等,这个时候我们需要使用不同的es的客户端进行操作,但是我们又不希望拆分成多个项目进行使用,这个时候我们就需要在我们的配置中自己构建一套es的多客户端了。
文章目录
pom.xml
elasticsearchconfig.java
elasticsearchrestclient.java
最终
pom.xml
首先是我们的pom.xml:
<dependencies> <dependency> <groupid>org.springframework.boot</groupid> <artifactid>spring-boot-starter</artifactid> <version>2.0.5.release</version> </dependency> <!--es for transport--> <dependency> <groupid>org.elasticsearch</groupid> <artifactid>elasticsearch</artifactid> <version>5.6.11</version> </dependency> <dependency> <groupid>org.elasticsearch.client</groupid> <artifactid>elasticsearch-rest-client</artifactid> <version>5.6.11</version> </dependency> <!--es sniffer--> <dependency> <groupid>org.elasticsearch.client</groupid> <artifactid>elasticsearch-rest-client-sniffer</artifactid> <version>5.6.3</version> <scope>compile</scope> </dependency> <!--es for rest-high-level-client--> <dependency> <groupid>org.elasticsearch.client</groupid> <artifactid>elasticsearch-rest-high-level-client</artifactid> <version>5.6.11</version> </dependency> <dependency> <groupid>org.apache.commons</groupid> <artifactid>commons-lang3</artifactid> <version>3.4</version> </dependency> <dependency> <groupid>com.alibaba</groupid> <artifactid>fastjson</artifactid> <version>1.2.39</version> </dependency> <dependency> <groupid>org.apache.logging.log4j</groupid> <artifactid>log4j-core</artifactid> <version>2.9.1</version> </dependency> <!-- springboot 热启动 --> <dependency> <groupid>org.springframework.boot</groupid> <artifactid>spring-boot-devtools</artifactid> </dependency> <dependency> <groupid>org.apache.commons</groupid> <artifactid>commons-pool2</artifactid> <version>2.4.2</version> </dependency> <dependency> <groupid>org.elasticsearch.client</groupid> <artifactid>elasticsearch-rest-client-sniffer</artifactid> <version>5.6.0</version> </dependency> <dependency> <groupid>commons-io</groupid> <artifactid>commons-io</artifactid> <version>2.6</version> </dependency> </dependencies>
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;
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。