欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

基于Springboot2.0构建多客户端的

程序员文章站 2022-03-15 22:05:12
...

有时候我们操作es的时候会有一些特殊的需求,例如需要操作的index使用了不同的es服务器、用户名、密码、参数等,这个时候我们需要使用不同的es的客户端进行操作,但是我们又不希望拆分成多个项目进行使用,这个时候我们就需要在我们的配置中自己构建一套ES的多客户端了。

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

@Slf4j
@Configuration
public class ElasticsearchRestClient {

    @Bean(name = "esClient1")
    public RestClient restClient(ElasticsearchConfig elasticsearchConfig) {
        final CredentialsProvider credentialsProvider = new BasicCredentialsProvider();
        credentialsProvider.setCredentials(AuthScope.ANY,
                new UsernamePasswordCredentials("用户名","密码"));
        RestClientBuilder builder = RestClient.builder(
                new HttpHost("地址", "端口", "ConsumeSchema模式"))
                .setHttpClientConfigCallback(httpClientBuilder -> httpClientBuilder.setDefaultCredentialsProvider(credentialsProvider))
                .setRequestConfigCallback(new RestClientBuilder.RequestConfigCallback() {
                    @Override
                    public RequestConfig.Builder customizeRequestConfig(RequestConfig.Builder builder) {
                        builder.setConnectTimeout(5000);
                        builder.setSocketTimeout(40000);
                        return builder;
                    }
                }).setMaxRetryTimeoutMillis(2*60*1000);
        builder.setMaxRetryTimeoutMillis("超时时间");
        SniffOnFailureListener sniffOnFailureListener = new SniffOnFailureListener();
        builder.setFailureListener(sniffOnFailureListener);
        RestClient restClient = builder.build();

        SnifferBuilder snifferBuilder = Sniffer.builder(restClient).setSniffIntervalMillis("Snifferinterval,默认为空,6000");
        if ("Failuredelay,默认为空,3000" > 0) {
            snifferBuilder.setSniffAfterFailureDelayMillis("Failuredelay,默认为空,3000");
        }
        sniffOnFailureListener.setSniffer(snifferBuilder.build());
        return restClient;
    }
    
    @Bean(name = "HesClient1")
    public RestHighLevelClient restHighLevelClient(@Qualifier("esClient1") RestClient restClient) {
        return new RestHighLevelClient(restClient);
    }
}

最终

在需要使用的地方直接通过注入的方式使用不同的客户端

@Autowird
private HesClient1 hesClient1;