package com.kyexpress.common.data.connect.provider.config;
import org.apache.commons.lang.StringUtils;
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.Autowire;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.stereotype.Component;
import java.io.IOException;
@Component
public class EsConfig {
private static final Logger logger = LoggerFactory.getLogger(EsConfig.class);
@Value("${elasticsearch.clients.cluster-default.clusterNodes}")
private String clusterNodes;
private String schema = “http”;
private int connectTimeOut = 3000;
private int socketTimeOut = 60000;
private int connectionRequestTimeOut = 600;
private int maxConnectNum = 200;
private int maxConnectPerRoute = 200;
private HttpHost httpHost;
private boolean uniqueConnectTimeConfig = true;
private boolean uniqueConnectNumConfig = true;
private RestClientBuilder builder;
private RestHighLevelClient client;
/**
* Bean name default 函数名字
*
* @return
*/
@Bean(autowire = Autowire.BY_NAME, name = "commonClinet")
public RestHighLevelClient client() {
logger.info("init client");
if (!StringUtils.isEmpty(clusterNodes)) {
try {
String[] ips = clusterNodes.split(",");
for (int i = 0; i < ips.length; i++) {
String ip = ips[i];
String host = ip.split(":")[0];
int port = Integer.parseInt(ip.split(":")[1]);
httpHost = new HttpHost(host, port, schema);
builder = RestClient.builder(httpHost);
if (uniqueConnectTimeConfig) {
setConnectTimeOutConfig();
}
if (uniqueConnectNumConfig) {
setMutiConnectConfig();
}
client = new RestHighLevelClient(builder);
break;
}
logger.info("end client");
} catch (Exception e) {
logger.error("elasticsearch TransportClient create error!!", e);
}
}
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;
}
});
}
/**
* 关闭连接
*/
public void close() {
if (client != null) {
try {
client.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
service层注入
@Autowired
RestHighLevelClient commonClinet;