|
@ -8,6 +8,7 @@ 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.elasticsearch.shield.ShieldPlugin;
|
|
|
import org.springframework.beans.factory.annotation.Value;
|
|
|
import org.springframework.stereotype.Component;
|
|
|
import org.springframework.util.StringUtils;
|
|
@ -31,6 +32,8 @@ public class ElasticFactory {
|
|
|
private String tHost;// 59.61.92.90:9066,59.61.92.90:9068
|
|
|
@Value("${es.clusterName}")
|
|
|
private String clusterName;
|
|
|
@Value("${es.securityUser}")
|
|
|
private String securityUser;
|
|
|
//-----------------------------------jestClient----------------------------------------
|
|
|
|
|
|
/**
|
|
@ -85,21 +88,53 @@ public class ElasticFactory {
|
|
|
*
|
|
|
* @throws UnknownHostException
|
|
|
*/
|
|
|
private synchronized void initTranClient() throws UnknownHostException {
|
|
|
if (transportClient == null) {
|
|
|
String[] hosts = tHost.split(",");
|
|
|
Settings settings = Settings.settingsBuilder()
|
|
|
// .put("client.transport.sniff", true)//开启嗅探功能
|
|
|
.put("cluster.name", StringUtils.isEmpty(clusterName) ? "jkzl" : clusterName)//默认集群名字是jkzl
|
|
|
.build();
|
|
|
// private synchronized void initTranClient() throws UnknownHostException {
|
|
|
// if (transportClient == null) {
|
|
|
// String[] hosts = tHost.split(",");
|
|
|
// Settings settings = Settings.settingsBuilder()
|
|
|
// // .put("client.transport.sniff", true)//开启嗅探功能
|
|
|
// .put("cluster.name", StringUtils.isEmpty(clusterName) ? "jkzl" : clusterName)//默认集群名字是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])));
|
|
|
// }
|
|
|
// }
|
|
|
// }
|
|
|
|
|
|
transportClient = TransportClient.builder().settings(settings).build();
|
|
|
|
|
|
/**
|
|
|
* 9300
|
|
|
*
|
|
|
* @throws UnknownHostException
|
|
|
*/
|
|
|
private synchronized void initTranClient() throws Exception {
|
|
|
if (transportClient == null) {
|
|
|
String[] hosts = tHost.split(",");
|
|
|
Settings settings = getSettings();
|
|
|
transportClient = TransportClient.builder().addPlugin(ShieldPlugin.class).settings(settings).build();
|
|
|
for (String oneHost : hosts) {
|
|
|
String[] hostAndport = oneHost.split(":");
|
|
|
transportClient.addTransportAddress(new InetSocketTransportAddress(InetAddress.getByName(hostAndport[0]), Integer.valueOf(hostAndport[1])));
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
* 配置连接
|
|
|
* @return
|
|
|
* @throws Exception
|
|
|
*/
|
|
|
private Settings getSettings() throws Exception {
|
|
|
Settings.Builder settingBuilder = Settings.settingsBuilder();
|
|
|
settingBuilder.put("cluster.name", clusterName);
|
|
|
settingBuilder.put("shield.user", securityUser);
|
|
|
settingBuilder.put("client.transport.sniff", false);
|
|
|
settingBuilder.put("transport.address.list", tHost);
|
|
|
return settingBuilder.build();
|
|
|
}
|
|
|
}
|
|
|
|