|
|
|
@ -6,21 +6,26 @@ import org.apache.commons.pool2.PooledObject;
|
|
|
|
|
import org.apache.commons.pool2.PooledObjectFactory;
|
|
|
|
|
import org.apache.commons.pool2.impl.DefaultPooledObject;
|
|
|
|
|
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.impl.client.BasicCredentialsProvider;
|
|
|
|
|
import org.elasticsearch.client.RestClient;
|
|
|
|
|
import org.elasticsearch.client.RestClientBuilder;
|
|
|
|
|
import org.elasticsearch.client.RestHighLevelClient;
|
|
|
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
|
|
|
import org.springframework.beans.factory.annotation.Value;
|
|
|
|
|
import org.springframework.stereotype.Component;
|
|
|
|
|
|
|
|
|
|
import java.time.Duration;
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* EliasticSearch连接池工厂对象
|
|
|
|
|
*
|
|
|
|
|
*/
|
|
|
|
|
@Slf4j
|
|
|
|
|
@Component
|
|
|
|
|
public class ElasticsearchClientPoolFactory implements PooledObjectFactory<RestHighLevelClient> {
|
|
|
|
|
|
|
|
|
|
@Autowired
|
|
|
|
|
private ElasticsearchMonitorProperties esProperties;
|
|
|
|
|
|
|
|
|
@ -44,6 +49,8 @@ public class ElasticsearchClientPoolFactory implements PooledObjectFactory<RestH
|
|
|
|
|
@Override
|
|
|
|
|
public PooledObject<RestHighLevelClient> makeObject() {
|
|
|
|
|
RestHighLevelClient restHighLevelClient = null;
|
|
|
|
|
final CredentialsProvider credentialsProvider = new BasicCredentialsProvider();
|
|
|
|
|
credentialsProvider.setCredentials(AuthScope.ANY, new UsernamePasswordCredentials(esProperties.getUserName(), esProperties.getPassword()));
|
|
|
|
|
try {
|
|
|
|
|
String[] urlArr = esProperties.getAddress().split(",");
|
|
|
|
|
HttpHost[] httpPostArr = new HttpHost[urlArr.length];
|
|
|
|
@ -52,8 +59,10 @@ public class ElasticsearchClientPoolFactory implements PooledObjectFactory<RestH
|
|
|
|
|
Integer.parseInt(urlArr[i].split(":")[1].trim()), "http");
|
|
|
|
|
httpPostArr[i] = httpHost;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
RestClientBuilder builder = RestClient.builder(httpPostArr);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// 异步httpclient连接延时配置
|
|
|
|
|
builder.setRequestConfigCallback(requestConfigBuilder -> {
|
|
|
|
|
requestConfigBuilder.setConnectTimeout(esProperties.getConnectTimeout());
|
|
|
|
@ -68,6 +77,7 @@ public class ElasticsearchClientPoolFactory implements PooledObjectFactory<RestH
|
|
|
|
|
httpClientBuilder.setMaxConnPerRoute(esProperties.getMaxConnectPerRoute());
|
|
|
|
|
// httpclient保活策略
|
|
|
|
|
httpClientBuilder.setKeepAliveStrategy(((response, context) -> Duration.ofMinutes(5).toMillis()));
|
|
|
|
|
httpClientBuilder.setDefaultCredentialsProvider(credentialsProvider);
|
|
|
|
|
return httpClientBuilder;
|
|
|
|
|
});
|
|
|
|
|
restHighLevelClient = new RestHighLevelClient(builder);
|
|
|
|
|