From 79c0d1f6662640cb155fd841eebdbfed4d7779d4 Mon Sep 17 00:00:00 2001 From: zhouxiunai <154707516@qq.com> Date: Tue, 26 Feb 2019 17:57:57 +0800 Subject: [PATCH] =?UTF-8?q?=E5=88=87=E6=8D=A2=E8=BF=9E=E6=8E=A5=E5=88=B0?= =?UTF-8?q?=E5=A4=9A=20es=20=E9=9B=86=E7=BE=A4=E3=80=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../elasticsearch/ElasticsearchConfig.java | 26 +++++++++++++++-- src/main/resources/application-dev.yml | 3 +- src/main/resources/application-pro.yml | 1 + .../elasticsearch/ElasticsearchUtilTest.java | 28 +++++++++++++++++++ 4 files changed, 54 insertions(+), 4 deletions(-) create mode 100644 src/test/java/com/gzzn/omms/msgexchangeapi/elasticsearch/ElasticsearchUtilTest.java diff --git a/src/main/java/com/gzzn/omms/msgexchangeapi/elasticsearch/ElasticsearchConfig.java b/src/main/java/com/gzzn/omms/msgexchangeapi/elasticsearch/ElasticsearchConfig.java index 11ab89d8..232c6983 100644 --- a/src/main/java/com/gzzn/omms/msgexchangeapi/elasticsearch/ElasticsearchConfig.java +++ b/src/main/java/com/gzzn/omms/msgexchangeapi/elasticsearch/ElasticsearchConfig.java @@ -17,6 +17,9 @@ public class ElasticsearchConfig { private static final Logger LOGGER = LoggerFactory.getLogger(ElasticsearchConfig.class); + @Value("${elasticsearch.cluster.nodes}") + private String nodes; + /** * elk集群地址 */ @@ -54,13 +57,30 @@ public class ElasticsearchConfig { // 配置信息 Settings esSetting = Settings.builder() .put("cluster.name", clusterName) // 集群名字 -// .put("client.transport.sniff", true)// 增加嗅探机制,找到ES集群 (当前api无需嗅探,而且此处开启嗅探会导致链接失败) .put("thread_pool.search.size", Integer.parseInt(poolSize))// 增加线程池个数,暂时设为5 .build(); // 配置信息Settings自定义 transportClient = new PreBuiltTransportClient(esSetting); - TransportAddress transportAddress = new TransportAddress(InetAddress.getByName(hostName), Integer.valueOf(port)); - transportClient.addTransportAddresses(transportAddress); + + + String []nodeArr = nodes.split(","); + for(String node:nodeArr) + { + String host; + String port; + if(node.contains(":")) + { + host = node.split(":")[0]; + port = node.split(":")[1]; + } + else { + host = node; + port = "9300";//默认端口 + } + TransportAddress transportAddress = new TransportAddress(InetAddress.getByName(host), + Integer.valueOf(port)); + transportClient.addTransportAddresses(transportAddress); + }//end for } catch (Exception e) { LOGGER.error("elasticsearch TransportClient create error!!", e); } diff --git a/src/main/resources/application-dev.yml b/src/main/resources/application-dev.yml index 7575d124..f1d57009 100644 --- a/src/main/resources/application-dev.yml +++ b/src/main/resources/application-dev.yml @@ -86,7 +86,8 @@ elasticsearch: pool: 5 #注意cluster.name需要与config/elasticsearch.yml中的cluster.name一致 cluster: - name: docker-cluster + name: docker-cluster + nodes: 130.120.3.234:9300,130.120.3.236:9300,130.120.3.238:9300 eureka: client: service-url: diff --git a/src/main/resources/application-pro.yml b/src/main/resources/application-pro.yml index 43eeea11..743cf142 100644 --- a/src/main/resources/application-pro.yml +++ b/src/main/resources/application-pro.yml @@ -87,6 +87,7 @@ elasticsearch: #注意cluster.name需要与config/elasticsearch.yml中的cluster.name一致 cluster: name: docker-cluster + nodes: 172.17.35.160:9300,172.17.35.161:9300,172.17.35.162:9300 eureka: client: service-url: diff --git a/src/test/java/com/gzzn/omms/msgexchangeapi/elasticsearch/ElasticsearchUtilTest.java b/src/test/java/com/gzzn/omms/msgexchangeapi/elasticsearch/ElasticsearchUtilTest.java new file mode 100644 index 00000000..bcb8c0a9 --- /dev/null +++ b/src/test/java/com/gzzn/omms/msgexchangeapi/elasticsearch/ElasticsearchUtilTest.java @@ -0,0 +1,28 @@ +package com.gzzn.omms.msgexchangeapi.elasticsearch; + +import org.junit.Test; +import org.junit.runner.RunWith; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.boot.test.context.SpringBootTest; +import org.springframework.test.context.junit4.SpringRunner; + +@RunWith(SpringRunner.class) +@SpringBootTest +public class ElasticsearchUtilTest { + + @Autowired + private ElasticsearchUtil elasticsearchUtil; + + @Test + public void testCreateIndex() + { + elasticsearchUtil.createIndex("testindex"); + } + + + @Test + public void testDeleteIndex() + { + elasticsearchUtil.deleteIndex("testindex"); + } +}