|
@@ -6,6 +6,12 @@ import co.elastic.clients.transport.ElasticsearchTransport;
|
|
|
import co.elastic.clients.transport.rest_client.RestClientTransport;
|
|
|
import lombok.Setter;
|
|
|
import lombok.extern.slf4j.Slf4j;
|
|
|
+import org.apache.commons.pool2.BasePooledObjectFactory;
|
|
|
+import org.apache.commons.pool2.PooledObject;
|
|
|
+import org.apache.commons.pool2.PooledObjectFactory;
|
|
|
+import org.apache.commons.pool2.impl.DefaultPooledObject;
|
|
|
+import org.apache.commons.pool2.impl.GenericObjectPool;
|
|
|
+import org.apache.commons.pool2.impl.GenericObjectPoolConfig;
|
|
|
import org.apache.http.Header;
|
|
|
import org.apache.http.HttpHost;
|
|
|
import org.apache.http.auth.AuthScope;
|
|
@@ -20,6 +26,8 @@ import org.apache.http.ssl.SSLContexts;
|
|
|
import org.elasticsearch.client.RestClient;
|
|
|
import org.elasticsearch.client.RestClientBuilder;
|
|
|
import org.elasticsearch.client.RestClientBuilder.HttpClientConfigCallback;
|
|
|
+import org.springframework.beans.factory.InitializingBean;
|
|
|
+import org.springframework.beans.factory.annotation.Value;
|
|
|
import org.springframework.boot.context.properties.ConfigurationProperties;
|
|
|
import org.springframework.context.annotation.Bean;
|
|
|
import org.springframework.context.annotation.Configuration;
|
|
@@ -43,7 +51,7 @@ import java.security.cert.CertificateFactory;
|
|
|
@ConfigurationProperties(prefix = "elasticsearch") //配置的前缀
|
|
|
@Configuration
|
|
|
@Slf4j
|
|
|
-public class EsClientConfig {
|
|
|
+public class EsClientConfig {
|
|
|
@Setter
|
|
|
private String hosts;
|
|
|
|
|
@@ -56,6 +64,16 @@ public class EsClientConfig {
|
|
|
@Setter
|
|
|
private String apikey;
|
|
|
|
|
|
+ @Setter
|
|
|
+ private int maxTotal;
|
|
|
+
|
|
|
+ @Setter
|
|
|
+ private int maxIdle;
|
|
|
+
|
|
|
+ @Setter
|
|
|
+ private int minIdle ;
|
|
|
+
|
|
|
+
|
|
|
/**
|
|
|
* 解析配置的字符串,转为HttpHost对象数组
|
|
|
* @return
|
|
@@ -126,7 +144,7 @@ public class EsClientConfig {
|
|
|
return new RestClientTransport(client, new JacksonJsonpMapper());
|
|
|
}
|
|
|
|
|
|
- /*private static ElasticsearchTransport getElasticsearchTransport(String apiKey, HttpHost...hosts) {
|
|
|
+ private static ElasticsearchTransport getElasticsearchTransport(String apiKey, HttpHost...hosts) {
|
|
|
// 将ApiKey放入header中
|
|
|
Header[] headers = new Header[] {new BasicHeader("Authorization", "ApiKey " + apiKey)};
|
|
|
|
|
@@ -149,5 +167,44 @@ public class EsClientConfig {
|
|
|
public ElasticsearchClient clientByApiKey() throws Exception {
|
|
|
ElasticsearchTransport transport = getElasticsearchTransport(apikey, toHttpHost());
|
|
|
return new ElasticsearchClient(transport);
|
|
|
- }*/
|
|
|
+ }
|
|
|
+
|
|
|
+ @Bean
|
|
|
+ public GenericObjectPoolConfig<ElasticsearchClient> config() {
|
|
|
+ GenericObjectPoolConfig<ElasticsearchClient> poolConfig = new GenericObjectPoolConfig<>();
|
|
|
+ poolConfig.setMinIdle(minIdle);
|
|
|
+ poolConfig.setMaxTotal(maxTotal);
|
|
|
+ poolConfig.setMaxIdle(maxIdle);
|
|
|
+ poolConfig.setJmxEnabled(false);
|
|
|
+ return poolConfig;
|
|
|
+ }
|
|
|
+
|
|
|
+ @Bean
|
|
|
+ public GenericObjectPool<ElasticsearchClient> pool(
|
|
|
+ PooledObjectFactory<ElasticsearchClient> factory,
|
|
|
+ GenericObjectPoolConfig<ElasticsearchClient> config) {
|
|
|
+ return new GenericObjectPool<>(factory, config);
|
|
|
+ }
|
|
|
+
|
|
|
+ @Bean
|
|
|
+ public PooledObjectFactory<ElasticsearchClient> factory(){
|
|
|
+ return new BasePooledObjectFactory<>() {
|
|
|
+ @Override
|
|
|
+ public ElasticsearchClient create() {
|
|
|
+ ElasticsearchTransport transport = getElasticsearchTransport(apikey, toHttpHost());
|
|
|
+ return new ElasticsearchClient(transport);
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public PooledObject<ElasticsearchClient> wrap(ElasticsearchClient obj) {
|
|
|
+ return new DefaultPooledObject<>(obj);
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public void destroyObject(PooledObject<ElasticsearchClient> pooledObject) throws Exception {
|
|
|
+ ElasticsearchClient highLevelClient = pooledObject.getObject();
|
|
|
+
|
|
|
+ }
|
|
|
+ };
|
|
|
+ }
|
|
|
}
|