es 连接改成集群节点的方式。

This commit is contained in:
zhouxiunai
2019-02-26 17:18:28 +08:00
parent f393bd9d66
commit cadbf8d5a3
5 changed files with 54 additions and 17 deletions
@@ -85,18 +85,5 @@ public class HistoryFlightDataController {
SortOrder sortOrder = SortOrder.ASC;
List<Map<String, Object>> list = ElasticsearchUtil.searchListData(INDEX_NAME, ES_TYPE, boolQuery, maxSize, null, sortField,sortOrder, null);
return ResponseDto.success(list);
}
// @RequestMapping(value="/findTest/{log_type}",method = RequestMethod.GET)
// public ResponseDto<EsPage> findTest(@PathVariable String log_type){
// //获取查询条件script
// String script = GzznFileUtils.getFileContent("/es/findTest.query");
// //封装参数 分页等参数全都放入此处
// Map<String, Object> params = new HashMap<String, Object>();
// params.put("log_type", log_type);
// params.put("from", 1);
// params.put("size", 10);
// //执行查询
// return ResponseDto.success(ElasticsearchUtil.searchDataPageByScript(INDEX_NAME, params, script,1,10));
// }
}//end function
}
@@ -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));
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);
}
+1
View File
@@ -62,4 +62,5 @@ elasticsearch:
#注意cluster.name需要与config/elasticsearch.yml中的cluster.name一致
cluster:
name: docker-cluster
nodes: 130.120.3.234:9300,130.120.3.236:9300,130.120.3.238:9300
maxSize: 10000
+1
View File
@@ -62,4 +62,5 @@ 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
maxSize: 10000
@@ -0,0 +1,28 @@
package com.gzzn.omms.adminapi.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");
}
}