Elasticsearch教程---Springboot2.x中使用High Level REST Client配置(五)

第七章 Springboot2.x中使用High Level REST Client

注意:以后章节的讲课都通过API操作与kibana查看操作结果的方式来演示。

Elasticsearch 会在7.0之后的版本废弃TransportClient,在8.0之后的版本移除TransportClient (文档)。因此,使用RestClient来进行相关的操作。

We plan on deprecating the TransportClient in Elasticsearch 7.0 and removing it completely in 8.0. Instead, you should be using the Java High Level REST Client, which executes HTTP requests rather than serialized Java requests. The migration guidedescribes all the steps needed to migrate.

取而代之的是High Level REST Client。

后续的JAVA API使用我们都是使用的High Level REST Client。

7.1 maven配置

新建SpringBoot2.0.3的WEB工程,在MAVEN的pom.xml文件中加入如下依赖

pom文件:

 <dependencies>
        <dependency>
            <groupId>org.springframework.bootgroupId>
            <artifactId>spring-boot-starter-webartifactId>
        dependency>
        <dependency>
            <groupId>org.elasticsearchgroupId>
            <artifactId>elasticsearchartifactId>
            <version>6.5.0version>
        dependency>
        <dependency>
            <groupId>org.elasticsearch.clientgroupId>
            <artifactId>elasticsearch-rest-high-level-clientartifactId>
            <version>6.5.0version>
        dependency>
        <dependency>
            <groupId>com.alibabagroupId>
            <artifactId>fastjsonartifactId>
            <version>1.2.56version>
        dependency>
        <dependency>
            <groupId>org.springframework.bootgroupId>
            <artifactId>spring-boot-starter-testartifactId>
            <scope>testscope>
        dependency>
    dependencies>

7.2 配置文件配置

application.properties针对ES配置示例如下:

#日志
logging.path=D:/logs/cache
#es 配置信息
spring.data.elasticsearch.host=192.168.157.140
spring.data.elasticsearch.port=9200
spring.data.elasticsearch.connectTimeOut=1000
spring.data.elasticsearch.socketTimeOut=30000
spring.data.elasticsearch.connectionRequestTimeOut=500
spring.data.elasticsearch.maxConnectNum=100
spring.data.elasticsearch.maxConnectPerRoute=100

7.3 ES配置类

package com.javablog.elasticsearch.config;

import org.apache.http.HttpHost;
import org.apache.http.client.config.RequestConfig;
import org.apache.http.impl.nio.client.HttpAsyncClientBuilder;
import org.elasticsearch.client.RestClient;
import org.elasticsearch.client.RestClientBuilder;
import org.elasticsearch.client.RestHighLevelClient;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import java.util.ArrayList;

@Configuration
public class ElasticsearchConfig {
    private final static Logger log = LoggerFactory.getLogger(ElasticsearchConfig.class);
    private static final String SCHEMA = "http"; // 使用的协议
    private  ArrayList<HttpHost> hostList;
    @Value("${spring.data.elasticsearch.host}")
    private String hosts; // 集群地址,多个用,隔开
    @Value("${spring.data.elasticsearch.port}")
    private int port; // 集群地址,多个用,隔开
    @Value("${spring.data.elasticsearch.connectTimeOut}")
    private int connectTimeOut = 1000; // 连接超时时间
    @Value("${spring.data.elasticsearch.socketTimeOut}")
    private int socketTimeOut = 30000; // 连接超时时间
    @Value("${spring.data.elasticsearch.connectionRequestTimeOut}")
    private int connectionRequestTimeOut = 500; // 获取连接的超时时间
    @Value("${spring.data.elasticsearch.maxConnectNum}")
    private int maxConnectNum = 100; // 最大连接数
    @Value("${spring.data.elasticsearch.maxConnectPerRoute}")
    private int maxConnectPerRoute = 100; // 最大路由连接数
    private RestClientBuilder builder;

    @Bean
    @ConditionalOnMissingBean(RestHighLevelClient.class)
    public RestHighLevelClient client() {
        hostList = new ArrayList<>();
        String[] hostStrs = hosts.split(",");
        for (String host : hostStrs) {
            String server = host;
            hostList.add(new HttpHost(host, port , SCHEMA));
        }
        builder = RestClient.builder(hostList.toArray(new HttpHost[0]));
        setConnectTimeOutConfig();
        setMutiConnectConfig();
        RestHighLevelClient client = new RestHighLevelClient(builder);
        return client;
    }

    // 异步httpclient的连接延时配置
    public void setConnectTimeOutConfig() {
        builder.setRequestConfigCallback(new RestClientBuilder.RequestConfigCallback() {
            @Override
            public RequestConfig.Builder customizeRequestConfig(RequestConfig.Builder requestConfigBuilder) {
                requestConfigBuilder.setConnectTimeout(connectTimeOut);
                requestConfigBuilder.setSocketTimeout(socketTimeOut);
                requestConfigBuilder.setConnectionRequestTimeout(connectionRequestTimeOut);
                return requestConfigBuilder;
            }
        });
    }

    // 异步httpclient的连接数配置
    public void setMutiConnectConfig() {
        builder.setHttpClientConfigCallback(new RestClientBuilder.HttpClientConfigCallback() {
            @Override
            public HttpAsyncClientBuilder customizeHttpClient(HttpAsyncClientBuilder httpClientBuilder) {
                httpClientBuilder.setMaxConnTotal(maxConnectNum);
                httpClientBuilder.setMaxConnPerRoute(maxConnectPerRoute);
                return httpClientBuilder;
            }
        });
    }

}

完整代码:https://github.com/chutianmen/elasticsearch-examples

你可能感兴趣的:(Elasticsearch)