Bladeren bron

数据服务-列表-详情

qxm 4 jaren geleden
bovenliggende
commit
ea24142b4c

+ 24 - 0
dgtis-modules/dgtis-modules-data/pom.xml

@@ -102,6 +102,30 @@
             <version>5.3.8</version>
         </dependency>
 
+        <!--&lt;!&ndash; Java Low Level REST Client &ndash;&gt;
+        <dependency>
+            <groupId>org.elasticsearch.client</groupId>
+            <artifactId>elasticsearch-rest-client</artifactId>
+            <version>7.6.2</version>
+        </dependency>
+
+        &lt;!&ndash; Java High Level REST Client &ndash;&gt;
+        <dependency>
+            <groupId>org.elasticsearch.client</groupId>
+            <artifactId>elasticsearch-rest-high-level-client</artifactId>
+            <version>7.6.2</version>
+        </dependency>-->
+
+        <dependency>
+            <groupId>org.elasticsearch.client</groupId>
+            <artifactId>rest</artifactId>
+            <version>5.5.1</version>
+        </dependency>
+
+        <dependency>
+            <groupId>junit</groupId>
+            <artifactId>junit</artifactId>
+        </dependency>
 
     </dependencies>
 

+ 23 - 30
dgtis-modules/dgtis-modules-data/src/main/java/com/dgtis/data/config/ElasticSearchConfig.java

@@ -1,19 +1,13 @@
-/*
+
 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.context.annotation.Bean;
 import org.springframework.context.annotation.Configuration;
 
-import java.net.InetAddress;
-import java.net.UnknownHostException;
-
-*/
 /**
  * @author koucx
  * @version 1.0
@@ -22,13 +16,13 @@ import java.net.UnknownHostException;
  * @copyright (c) 2019 LuoYang DGT Co'Ltd Inc. All rights reserved.
  * @date 2021-01-21
  * @since JDK1.8
- *//*
+ */
 
 @Configuration
 public class ElasticSearchConfig {
 
-    @Value("${elasticsearch.host}")
-    private String esHost;
+    @Value("${elasticsearch.hostlist}")
+    private String hostlist;
 
     @Value("${elasticsearch.port}")
     private int esPort;
@@ -37,27 +31,26 @@ public class ElasticSearchConfig {
     private String esName;
 
     @Bean
-    public TransportClient esClient() throws UnknownHostException {
-        TransportClient client = null;
+    public RestClient esClient() {
+        RestClient restClient = null;
         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) {
             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;
     }
 }
-*/
+

+ 4 - 0
dgtis-modules/dgtis-modules-data/src/main/resources/bootstrap.yml

@@ -10,6 +10,10 @@ elasticsearch:
     name: cluster
   host: 10.32.2.230
   port: 9200
+  hostlist: 10.32.2.230:9200,10.32.2.231:9200,10.32.2.232:9200,10.32.2.233:9200,10.32.2.234:9200
+  client:
+    connectNum: 10
+    connectPerRoute: 50
 
 # Spring
 spring: