2018-11-28 11:30:48 +08:00
|
|
|
package com.gzzn.omms.adminapi.elasticsearch;
|
2018-11-28 11:18:21 +08:00
|
|
|
|
|
|
|
|
import java.net.InetAddress;
|
|
|
|
|
|
|
|
|
|
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.slf4j.Logger;
|
|
|
|
|
import org.slf4j.LoggerFactory;
|
|
|
|
|
import org.springframework.beans.factory.annotation.Value;
|
|
|
|
|
import org.springframework.context.annotation.Bean;
|
|
|
|
|
import org.springframework.context.annotation.Configuration;
|
|
|
|
|
|
|
|
|
|
@Configuration
|
|
|
|
|
public class ElasticsearchConfig {
|
|
|
|
|
|
|
|
|
|
private static final Logger LOGGER = LoggerFactory.getLogger(ElasticsearchConfig.class);
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* elk集群地址
|
|
|
|
|
*/
|
|
|
|
|
@Value("${elasticsearch.ip}")
|
|
|
|
|
private String hostName;
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 端口
|
|
|
|
|
*/
|
|
|
|
|
@Value("${elasticsearch.port}")
|
|
|
|
|
private String port;
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 集群名称
|
|
|
|
|
*/
|
|
|
|
|
@Value("${elasticsearch.cluster.name}")
|
|
|
|
|
private String clusterName;
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 连接池
|
|
|
|
|
*/
|
|
|
|
|
@Value("${elasticsearch.pool}")
|
|
|
|
|
private String poolSize;
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Bean name default 函数名字
|
|
|
|
|
*
|
|
|
|
|
* @return
|
|
|
|
|
*/
|
|
|
|
|
@Bean(name = "transportClient")
|
|
|
|
|
public TransportClient transportClient() {
|
|
|
|
|
LOGGER.info("Elasticsearch初始化开始。。。。。");
|
|
|
|
|
TransportClient transportClient = null;
|
|
|
|
|
try {
|
|
|
|
|
// 配置信息
|
|
|
|
|
Settings esSetting = Settings.builder()
|
|
|
|
|
.put("cluster.name", clusterName) // 集群名字
|
2018-11-28 14:20:47 +08:00
|
|
|
// .put("client.transport.sniff", true)// 增加嗅探机制,找到ES集群 (当前api无需嗅探,而且此处开启嗅探会导致链接失败)
|
2018-11-28 11:18:21 +08:00
|
|
|
.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);
|
|
|
|
|
} catch (Exception e) {
|
|
|
|
|
LOGGER.error("elasticsearch TransportClient create error!!", e);
|
|
|
|
|
}
|
|
|
|
|
return transportClient;
|
|
|
|
|
}
|
|
|
|
|
}
|