1、引入pom.xml依赖
org.elasticsearch.client elasticsearch-rest-high-level-client7.6.1 org.elasticsearch elasticsearch7.6.1
2、Elasticsearch配置类
package com.hdy.manage.configuration; import org.apache.commons.lang3.StringUtils; import org.apache.http.Header; import org.apache.http.HttpHost; import org.apache.http.client.config.RequestConfig; import org.apache.http.impl.nio.client.HttpAsyncClientBuilder; import org.apache.http.message.BasicHeader; import org.elasticsearch.client.RestClient; import org.elasticsearch.client.RestClientBuilder; import org.elasticsearch.client.RestHighLevelClient; import org.springframework.beans.factory.annotation.Value; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import java.util.ArrayList; import java.util.List; @Configuration public class ElasticsearchConfiguration { @Value("${spring.data.elasticsearch.endpoints}") private String elasticsearchEndpoints; @Value("${spring.data.elasticsearch.connection-timeout}") private Integer connectTimeout; @Value("${spring.data.elasticsearch.socket-timeout}") private Integer socketTimeout; @Value("${spring.data.elasticsearch.connection-request-timeout}") private Integer connectionRequestTimeout; @Value("${spring.data.elasticsearch.max-conn-total}") private Integer maxConnTotal; @Value("${spring.data.elasticsearch.max-conn-per-route}") private Integer maxConnPerRoute; @Bean public RestHighLevelClient restHighLevelClient() throws Exception { if (StringUtils.isBlank(elasticsearchEndpoints)) { throw new Exception("elasticsearch 节点信息获取失败,请在配置文件中配置"); } ListhostList = new ArrayList<>(); String[] endpoints = elasticsearchEndpoints.split(","); for (String endpoint : endpoints) { String[] endpointSpit = endpoint.split(":"); hostList.add(new HttpHost(endpointSpit[0], Integer.parseInt(endpointSpit[1]))); } RestClientBuilder restClientBuilder = RestClient.builder(hostList.toArray(new HttpHost[0])); // timeout=5, max=100 restClientBuilder.setDefaultHeaders(new Header[]{new BasicHeader("Keep-Alive", "timeout=1800, max=1000")}); // 异步httpclient连接延时配置 restClientBuilder.setRequestConfigCallback(new RestClientBuilder.RequestConfigCallback() { @Override public RequestConfig.Builder customizeRequestConfig(RequestConfig.Builder requestConfigBuilder) { requestConfigBuilder.setConnectTimeout(connectTimeout); requestConfigBuilder.setSocketTimeout(socketTimeout); requestConfigBuilder.setConnectionRequestTimeout(connectionRequestTimeout); return requestConfigBuilder; } }); // 异步httpclient连接数配置 restClientBuilder.setHttpClientConfigCallback(new RestClientBuilder.HttpClientConfigCallback() { @Override public HttpAsyncClientBuilder customizeHttpClient(HttpAsyncClientBuilder httpClientBuilder) { httpClientBuilder.setMaxConnTotal(maxConnTotal); httpClientBuilder.setMaxConnPerRoute(maxConnPerRoute); return httpClientBuilder; } }); RestHighLevelClient restHighLevelClient = new RestHighLevelClient(restClientBuilder); return restHighLevelClient; } }
- Elasticsearch客户端与服务器进行交互使用长连接,需要在header中设置Keep-Alive的timeout时间
- Elasticsearch服务器端长连接长时间不使用会自动断开,但是客户端无感,所以可能会造成客户端下次请求客户端连接报错,因此需要设置Keep-Alive的timeout
3、application.properties文件
# 设置elasticsearch配置 spring.data.elasticsearch.endpoints=host:port,host:port,host:port # 针对一个域名同时间正在使用的最多的连接数,默认值为 5 spring.data.elasticsearch.max-conn-per-route=20 # 同时间正在使用的最多的连接数,默认值为 2 * 5 spring.data.elasticsearch.max-conn-total=20 # 客户端和服务器建立连接超时时间 spring.data.elasticsearch.connection-timeout=10000 # 从服务器端到客户端传输数据超时时间 spring.data.elasticsearch.socket-timeout=30000 # 从连接池中获取连接超时时间 spring.data.elasticsearch.connection-request-timeout=500