|
@ -7,13 +7,19 @@ import io.searchbox.client.JestClient;
|
|
|
import io.searchbox.client.JestClientFactory;
|
|
|
import io.searchbox.client.config.HttpClientConfig;
|
|
|
import org.elasticsearch.client.Client;
|
|
|
import org.elasticsearch.client.transport.TransportClient;
|
|
|
import org.elasticsearch.common.settings.Settings;
|
|
|
import org.elasticsearch.common.transport.InetSocketTransportAddress;
|
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
|
import org.springframework.beans.factory.annotation.Value;
|
|
|
import org.springframework.context.annotation.Bean;
|
|
|
import org.springframework.context.annotation.Configuration;
|
|
|
import org.springframework.data.elasticsearch.core.ElasticsearchTemplate;
|
|
|
import org.springframework.jdbc.core.JdbcTemplate;
|
|
|
import org.springframework.util.StringUtils;
|
|
|
|
|
|
import java.net.InetAddress;
|
|
|
import java.net.UnknownHostException;
|
|
|
import java.util.Arrays;
|
|
|
import java.util.Properties;
|
|
|
import java.util.concurrent.TimeUnit;
|
|
@ -24,10 +30,6 @@ import java.util.concurrent.TimeUnit;
|
|
|
@Configuration
|
|
|
public class ElasticFactory {
|
|
|
private static JestClientFactory factory = null;
|
|
|
|
|
|
@Autowired
|
|
|
private ElasticsearchTemplate elasticsearchTemplate;
|
|
|
|
|
|
@Autowired
|
|
|
private ElasticSearchPorperties elasticSearchPorperties;
|
|
|
//-----------------------------------jestClient----------------------------------------
|
|
@ -68,15 +70,40 @@ public class ElasticFactory {
|
|
|
|
|
|
//-----------------------------------TransportClient----------------------------------------
|
|
|
|
|
|
private TransportClient transportClient;
|
|
|
|
|
|
public Client getTransportClient() {
|
|
|
try {
|
|
|
return elasticsearchTemplate.getClient();
|
|
|
initTranClient();
|
|
|
return transportClient;
|
|
|
} catch (Exception e) {
|
|
|
e.printStackTrace();
|
|
|
}
|
|
|
return null;
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
* 9300
|
|
|
*
|
|
|
* @throws UnknownHostException
|
|
|
*/
|
|
|
private synchronized void initTranClient() throws UnknownHostException {
|
|
|
if (transportClient == null) {
|
|
|
String[] hosts = elasticSearchPorperties.getClusterNodes().split(",");
|
|
|
Settings settings = Settings.settingsBuilder()
|
|
|
.put("client.transport.sniff", elasticSearchPorperties.getJestDiscoveryEnabled())//开启嗅探功能
|
|
|
.put("cluster.name", StringUtils.isEmpty(elasticSearchPorperties.getClusterName()) ? "jkzl" : elasticSearchPorperties.getClusterName())//默认集群名字是jkzl
|
|
|
.build();
|
|
|
|
|
|
transportClient = TransportClient.builder().settings(settings).build();
|
|
|
|
|
|
for (String oneHost : hosts) {
|
|
|
String[] hostAndport = oneHost.split(":");
|
|
|
transportClient.addTransportAddress(new InetSocketTransportAddress(InetAddress.getByName(hostAndport[0]), Integer.valueOf(hostAndport[1])));
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
* es连接池
|
|
|
*
|