切换连接到多 es 集群。

This commit is contained in:
zhouxiunai
2019-02-26 17:57:57 +08:00
parent 86b0fc5a48
commit 79c0d1f666
4 changed files with 54 additions and 4 deletions
@@ -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);
}
+2 -1
View File
@@ -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:
+1
View File
@@ -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:
@@ -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");
}
}