|
@@ -1,9 +1,14 @@
|
|
package com.jkcredit.record.storage.config;
|
|
package com.jkcredit.record.storage.config;
|
|
|
|
|
|
import org.apache.http.HttpHost;
|
|
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.action.bulk.BulkRequestBuilder;
|
|
import org.elasticsearch.action.bulk.BulkRequestBuilder;
|
|
import org.elasticsearch.client.*;
|
|
import org.elasticsearch.client.*;
|
|
import org.elasticsearch.transport.Transport;
|
|
import org.elasticsearch.transport.Transport;
|
|
|
|
+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;
|
|
|
|
|
|
@@ -13,6 +18,14 @@ import org.springframework.context.annotation.Configuration;
|
|
**/
|
|
**/
|
|
@Configuration
|
|
@Configuration
|
|
public class EsConfig {
|
|
public class EsConfig {
|
|
|
|
+ @Value("${ElasticSearch.host}")
|
|
|
|
+ private String host;
|
|
|
|
+ @Value("${ElasticSearch.port}")
|
|
|
|
+ private Integer port;
|
|
|
|
+ @Value("${ElasticSearch.username}")
|
|
|
|
+ private String username;
|
|
|
|
+ @Value("${ElasticSearch.password}")
|
|
|
|
+ private String password;
|
|
|
|
|
|
public static final org.elasticsearch.client.RequestOptions COMMON_OPTIONS;
|
|
public static final org.elasticsearch.client.RequestOptions COMMON_OPTIONS;
|
|
|
|
|
|
@@ -26,10 +39,28 @@ public class EsConfig {
|
|
RestClientBuilder builder = null;
|
|
RestClientBuilder builder = null;
|
|
// 可以指定多个es
|
|
// 可以指定多个es
|
|
builder = RestClient.builder(
|
|
builder = RestClient.builder(
|
|
- new HttpHost("192.168.50.17", 9200, "http"),
|
|
|
|
- new HttpHost("192.168.50.18", 9200, "http"),
|
|
|
|
- new HttpHost("192.168.50.19", 9200, "http"));
|
|
|
|
|
|
+ new HttpHost(host, port, "http"));
|
|
|
|
+
|
|
|
|
+ CredentialsProvider credentialsProvider = new BasicCredentialsProvider();
|
|
|
|
+ credentialsProvider.setCredentials(AuthScope.ANY,
|
|
|
|
+ //es账号密码
|
|
|
|
+
|
|
|
|
+ new UsernamePasswordCredentials(username, password));
|
|
|
|
+ builder.setHttpClientConfigCallback(f -> f.setDefaultCredentialsProvider(credentialsProvider));
|
|
RestHighLevelClient client = new RestHighLevelClient(builder);
|
|
RestHighLevelClient client = new RestHighLevelClient(builder);
|
|
return client;
|
|
return client;
|
|
}
|
|
}
|
|
|
|
+
|
|
|
|
+// @Bean
|
|
|
|
+// public RestHighLevelClient esRestClient() {
|
|
|
|
+// RestClientBuilder builder = null;
|
|
|
|
+// // 可以指定多个es
|
|
|
|
+// builder = RestClient.builder(
|
|
|
|
+// new HttpHost("192.168.50.17", 9200, "http"),
|
|
|
|
+// new HttpHost("192.168.50.18", 9200, "http"),
|
|
|
|
+// new HttpHost("192.168.50.19", 9200, "http"));
|
|
|
|
+// RestHighLevelClient client = new RestHighLevelClient(builder);
|
|
|
|
+// return client;
|
|
|
|
+// }
|
|
|
|
+
|
|
}
|
|
}
|