|
@ -14,21 +14,21 @@ import org.springframework.util.StringUtils;
|
|
|
import javax.annotation.PostConstruct;
|
|
|
import java.net.InetAddress;
|
|
|
import java.net.UnknownHostException;
|
|
|
import java.util.Arrays;
|
|
|
import java.util.concurrent.TimeUnit;
|
|
|
|
|
|
/**
|
|
|
* Created by chenweida on 2017/6/5.
|
|
|
*/
|
|
|
|
|
|
@Component
|
|
|
public class ElasticFactory {
|
|
|
private static JestClientFactory factory = null;
|
|
|
|
|
|
@Value("${es.host}")
|
|
|
private String esHost;
|
|
|
@Value("${es.port}")
|
|
|
private String port;
|
|
|
@Value("${es.tPort}")
|
|
|
private String tPort;
|
|
|
private String esHost;//http://59.61.92.90:9065,http://59.61.92.90:9067
|
|
|
@Value("${es.tHost}")
|
|
|
private String tHost;// 59.61.92.90:9066,59.61.92.90:9068
|
|
|
@Value("${es.clusterName}")
|
|
|
private String clusterName;
|
|
|
//-----------------------------------jestClient----------------------------------------
|
|
@ -37,37 +37,38 @@ public class ElasticFactory {
|
|
|
* @param "http://localhost:9200"
|
|
|
* @return
|
|
|
*/
|
|
|
//@PostConstruct
|
|
|
public JestClient getJestClient() {
|
|
|
synchronized (ElasticFactory.class) {
|
|
|
if (factory == null) {
|
|
|
//初始化链接
|
|
|
init();
|
|
|
}
|
|
|
if (factory == null) {
|
|
|
//初始化链接
|
|
|
init();
|
|
|
}
|
|
|
return factory.getObject();
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
* 初始化链接
|
|
|
* 9200
|
|
|
*/
|
|
|
public synchronized void init() {
|
|
|
|
|
|
String[] hostArray = esHost.split(",");
|
|
|
// Construct a new Jest client according to configuration via factory
|
|
|
factory = new JestClientFactory();
|
|
|
factory.setHttpClientConfig(new HttpClientConfig
|
|
|
.Builder("http://" + esHost + ":" + port)
|
|
|
HttpClientConfig httpClientConfig = new HttpClientConfig
|
|
|
.Builder(Arrays.asList(hostArray))
|
|
|
.multiThreaded(true)
|
|
|
.maxTotalConnection(50)// 最大链接
|
|
|
.maxConnectionIdleTime(120, TimeUnit.SECONDS)//链接等待时间
|
|
|
.connTimeout(30000)
|
|
|
.discoveryEnabled(true)
|
|
|
.readTimeout(30000)//30秒
|
|
|
.build());//得到链接
|
|
|
.connTimeout(60 * 1000)
|
|
|
// .discoveryEnabled(true)
|
|
|
.readTimeout(60 * 1000)//60秒
|
|
|
.build();
|
|
|
|
|
|
|
|
|
factory.setHttpClientConfig(httpClientConfig);//得到链接
|
|
|
}
|
|
|
|
|
|
//-----------------------------------TransportClient----------------------------------------
|
|
|
private Client transportClient;
|
|
|
private TransportClient transportClient;
|
|
|
|
|
|
public Client getTransportClient() {
|
|
|
try {
|
|
@ -79,15 +80,26 @@ public class ElasticFactory {
|
|
|
return null;
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
* 9300
|
|
|
*
|
|
|
* @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("client.transport.sniff", true)//开启嗅探功能
|
|
|
.put("cluster.name", StringUtils.isEmpty(clusterName) ? "jkzl" : clusterName)//默认集群名字是jkzl
|
|
|
.build();
|
|
|
|
|
|
transportClient = TransportClient.builder().settings(settings).build()
|
|
|
.addTransportAddress(new InetSocketTransportAddress(InetAddress.getByName(esHost), Integer.valueOf(tPort)));
|
|
|
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])));
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
|