|
@@ -8,9 +8,13 @@ import org.apache.http.impl.client.BasicCredentialsProvider;
|
|
import org.apache.http.impl.nio.client.HttpAsyncClientBuilder;
|
|
import org.apache.http.impl.nio.client.HttpAsyncClientBuilder;
|
|
import org.elasticsearch.client.RestClient;
|
|
import org.elasticsearch.client.RestClient;
|
|
import org.elasticsearch.client.RestClientBuilder;
|
|
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.Bean;
|
|
import org.springframework.context.annotation.Configuration;
|
|
import org.springframework.context.annotation.Configuration;
|
|
|
|
|
|
|
|
+import java.util.stream.Stream;
|
|
|
|
+
|
|
/**
|
|
/**
|
|
* @Author: XuJiakai
|
|
* @Author: XuJiakai
|
|
* @Date: 2020/9/15 20:01
|
|
* @Date: 2020/9/15 20:01
|
|
@@ -18,14 +22,33 @@ import org.springframework.context.annotation.Configuration;
|
|
*/
|
|
*/
|
|
@Configuration
|
|
@Configuration
|
|
public class ElasticSearchConfiguration {
|
|
public class ElasticSearchConfiguration {
|
|
|
|
+ @Value("${es.username}")
|
|
|
|
+ private String username;
|
|
|
|
+ @Value("${es.password}")
|
|
|
|
+ private String password;
|
|
|
|
+ @Value("${es.host}")
|
|
|
|
+ private String host;
|
|
|
|
+
|
|
|
|
+ @Value("${es.schema:http}")
|
|
|
|
+ String schema;
|
|
|
|
+ @Value(value = "${es.connect-timeout:1000}")
|
|
|
|
+ String connectTimeout;
|
|
|
|
+ @Value(value = "${es.socket-timeout:600000}")
|
|
|
|
+ String socketTimeout;
|
|
|
|
+ @Value(value = "${es.connection-request-timeout:500}")
|
|
|
|
+ String connectionRequestTimeout;
|
|
|
|
+ @Value(value = "${es.max-conn-total:100}")
|
|
|
|
+ String maxConnTotal;
|
|
|
|
+ @Value(value = "${es.max-conn-per-route:100}")
|
|
|
|
+ String maxConnPerRoute;
|
|
|
|
|
|
@Bean
|
|
@Bean
|
|
public RestClient bean() {
|
|
public RestClient bean() {
|
|
final CredentialsProvider credentialsProvider = new BasicCredentialsProvider();
|
|
final CredentialsProvider credentialsProvider = new BasicCredentialsProvider();
|
|
credentialsProvider.setCredentials(AuthScope.ANY,
|
|
credentialsProvider.setCredentials(AuthScope.ANY,
|
|
- new UsernamePasswordCredentials("USER NAME", "PASSWORD"));
|
|
|
|
|
|
+ new UsernamePasswordCredentials(username, password));
|
|
// 单击所创建的Elasticsearch实例ID,在基本信息页面获取公网地址,即为HOST。
|
|
// 单击所创建的Elasticsearch实例ID,在基本信息页面获取公网地址,即为HOST。
|
|
- return RestClient.builder(new HttpHost("HOST", 9200))
|
|
|
|
|
|
+ return RestClient.builder(new HttpHost(host, 9200))
|
|
.setHttpClientConfigCallback(new RestClientBuilder.HttpClientConfigCallback() {
|
|
.setHttpClientConfigCallback(new RestClientBuilder.HttpClientConfigCallback() {
|
|
@Override
|
|
@Override
|
|
public HttpAsyncClientBuilder customizeHttpClient(HttpAsyncClientBuilder httpClientBuilder) {
|
|
public HttpAsyncClientBuilder customizeHttpClient(HttpAsyncClientBuilder httpClientBuilder) {
|
|
@@ -35,7 +58,34 @@ public class ElasticSearchConfiguration {
|
|
}
|
|
}
|
|
|
|
|
|
|
|
|
|
- public void b(){
|
|
|
|
- RestClient bean = bean();
|
|
|
|
|
|
+ @Bean
|
|
|
|
+ public RestHighLevelClient getClient() {
|
|
|
|
+ HttpHost[] httpHosts = Stream.of(host.split(",")).map(host -> {
|
|
|
|
+ String[] split = host.split(":");
|
|
|
|
+ return new HttpHost(split[0], 9200, schema);
|
|
|
|
+ }).toArray(HttpHost[]::new);
|
|
|
|
+
|
|
|
|
+ // 阿里云Elasticsearch集群需要basic auth验证。
|
|
|
|
+ final CredentialsProvider credentialsProvider = new BasicCredentialsProvider();
|
|
|
|
+ //访问用户名和密码为您创建阿里云Elasticsearch实例时设置的用户名和密码,也是Kibana控制台的登录用户名和密码。
|
|
|
|
+ credentialsProvider.setCredentials(AuthScope.ANY, new UsernamePasswordCredentials(username, password));
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+ return new RestHighLevelClient(RestClient
|
|
|
|
+ .builder(httpHosts)
|
|
|
|
+ .setMaxRetryTimeoutMillis(60000 * 3)
|
|
|
|
+ .setRequestConfigCallback(builder -> {
|
|
|
|
+ builder.setConnectTimeout(!connectTimeout.isEmpty() ? Integer.valueOf(connectTimeout) : 1000);
|
|
|
|
+ builder.setSocketTimeout(!socketTimeout.isEmpty() ? Integer.valueOf(socketTimeout) : 60000);
|
|
|
|
+ builder.setConnectionRequestTimeout(!connectionRequestTimeout.isEmpty() ? Integer.valueOf(connectionRequestTimeout) : 500);
|
|
|
|
+ return builder;
|
|
|
|
+ })
|
|
|
|
+ .setHttpClientConfigCallback(httpAsyncClientBuilder -> {
|
|
|
|
+ httpAsyncClientBuilder.setDefaultCredentialsProvider(credentialsProvider);
|
|
|
|
+ httpAsyncClientBuilder.setMaxConnTotal(!maxConnTotal.isEmpty() ? Integer.valueOf(maxConnTotal) : 100);
|
|
|
|
+ httpAsyncClientBuilder.setMaxConnPerRoute(!maxConnPerRoute.isEmpty() ? Integer.valueOf(maxConnPerRoute) : 100);
|
|
|
|
+ return httpAsyncClientBuilder;
|
|
|
|
+ }).build()
|
|
|
|
+ );
|
|
}
|
|
}
|
|
}
|
|
}
|