|
@@ -1,19 +1,13 @@
|
|
-/*
|
|
|
|
|
|
+
|
|
package com.dgtis.data.config;
|
|
package com.dgtis.data.config;
|
|
|
|
|
|
|
|
|
|
-import org.elasticsearch.client.transport.TransportClient;
|
|
|
|
-import org.elasticsearch.common.settings.Settings;
|
|
|
|
-import org.elasticsearch.common.transport.TransportAddress;
|
|
|
|
-import org.elasticsearch.transport.client.PreBuiltTransportClient;
|
|
|
|
|
|
+import org.apache.http.HttpHost;
|
|
|
|
+import org.elasticsearch.client.RestClient;
|
|
import org.springframework.beans.factory.annotation.Value;
|
|
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.net.InetAddress;
|
|
|
|
-import java.net.UnknownHostException;
|
|
|
|
-
|
|
|
|
-*/
|
|
|
|
/**
|
|
/**
|
|
* @author koucx
|
|
* @author koucx
|
|
* @version 1.0
|
|
* @version 1.0
|
|
@@ -22,13 +16,13 @@ import java.net.UnknownHostException;
|
|
* @copyright (c) 2019 LuoYang DGT Co'Ltd Inc. All rights reserved.
|
|
* @copyright (c) 2019 LuoYang DGT Co'Ltd Inc. All rights reserved.
|
|
* @date 2021-01-21
|
|
* @date 2021-01-21
|
|
* @since JDK1.8
|
|
* @since JDK1.8
|
|
- *//*
|
|
|
|
|
|
+ */
|
|
|
|
|
|
@Configuration
|
|
@Configuration
|
|
public class ElasticSearchConfig {
|
|
public class ElasticSearchConfig {
|
|
|
|
|
|
- @Value("${elasticsearch.host}")
|
|
|
|
- private String esHost;
|
|
|
|
|
|
+ @Value("${elasticsearch.hostlist}")
|
|
|
|
+ private String hostlist;
|
|
|
|
|
|
@Value("${elasticsearch.port}")
|
|
@Value("${elasticsearch.port}")
|
|
private int esPort;
|
|
private int esPort;
|
|
@@ -37,27 +31,26 @@ public class ElasticSearchConfig {
|
|
private String esName;
|
|
private String esName;
|
|
|
|
|
|
@Bean
|
|
@Bean
|
|
- public TransportClient esClient() throws UnknownHostException {
|
|
|
|
- TransportClient client = null;
|
|
|
|
|
|
+ public RestClient esClient() {
|
|
|
|
+ RestClient restClient = null;
|
|
try {
|
|
try {
|
|
- Settings settings = Settings.builder()
|
|
|
|
- .put("client.transport.sniff", true)
|
|
|
|
- .put("cluster.name", "cluster")
|
|
|
|
- .build();
|
|
|
|
-
|
|
|
|
- TransportAddress master = new TransportAddress(InetAddress.getByName("10.32.2.230"), 9300);
|
|
|
|
- TransportAddress master2 = new TransportAddress(InetAddress.getByName("10.32.2.231"), 9300);
|
|
|
|
- TransportAddress master3 = new TransportAddress(InetAddress.getByName("10.32.2.232"), 9300);
|
|
|
|
- TransportAddress master4 = new TransportAddress(InetAddress.getByName("10.32.2.233"), 9300);
|
|
|
|
- TransportAddress master5 = new TransportAddress(InetAddress.getByName("10.32.2.234"), 9300);
|
|
|
|
-
|
|
|
|
- client = new PreBuiltTransportClient(settings).
|
|
|
|
- addTransportAddress(master).addTransportAddress(master2).
|
|
|
|
- addTransportAddress(master3).addTransportAddress(master4).addTransportAddress(master5);
|
|
|
|
|
|
+ restClient = RestClient.builder(httpHost()).build();
|
|
} catch (Exception e) {
|
|
} catch (Exception e) {
|
|
e.printStackTrace();
|
|
e.printStackTrace();
|
|
}
|
|
}
|
|
- return client;
|
|
|
|
|
|
+ return restClient;
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ public HttpHost[] httpHost(){
|
|
|
|
+ //解析hostlist配置信息
|
|
|
|
+ String[] split = hostlist.split(",");
|
|
|
|
+ //创建HttpHost数组,其中存放es主机和端口的配置信息
|
|
|
|
+ HttpHost[] httpHostArray = new HttpHost[split.length];
|
|
|
|
+ for(int i=0;i<split.length;i++){
|
|
|
|
+ String item = split[i];
|
|
|
|
+ httpHostArray[i] = new HttpHost(item.split(":")[0], Integer.parseInt(item.split(":")[1]), "http");
|
|
|
|
+ }
|
|
|
|
+ return httpHostArray;
|
|
}
|
|
}
|
|
}
|
|
}
|
|
-*/
|
|
|
|
|
|
+
|