|  | @ -1,26 +1,28 @@
 | 
	
		
			
				|  |  | package com.yihu.jw.es.es;
 | 
	
		
			
				|  |  | 
 | 
	
		
			
				|  |  | 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.elasticsearch.common.transport.TransportAddress;
 | 
	
		
			
				|  |  | import org.elasticsearch.xpack.client.PreBuiltXPackTransportClient;
 | 
	
		
			
				|  |  | import org.springframework.beans.factory.annotation.Value;
 | 
	
		
			
				|  |  | import org.springframework.stereotype.Component;
 | 
	
		
			
				|  |  | import org.springframework.util.StringUtils;
 | 
	
		
			
				|  |  | 
 | 
	
		
			
				|  |  | import java.net.InetAddress;
 | 
	
		
			
				|  |  | import java.net.UnknownHostException;
 | 
	
		
			
				|  |  | import java.util.ArrayList;
 | 
	
		
			
				|  |  | import java.util.Arrays;
 | 
	
		
			
				|  |  | import java.util.Collections;
 | 
	
		
			
				|  |  | import java.util.concurrent.TimeUnit;
 | 
	
		
			
				|  |  | 
 | 
	
		
			
				|  |  | /**
 | 
	
		
			
				|  |  |  * Created by chenweida on 2017/6/5.
 | 
	
		
			
				|  |  |  */
 | 
	
		
			
				|  |  | 
 | 
	
		
			
				|  |  | @Component
 | 
	
		
			
				|  |  | public class ElasticFactory {
 | 
	
		
			
				|  |  |     private static JestClientFactory factory = null;
 | 
	
	
		
			
				|  | @ -31,6 +33,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----------------------------------------
 | 
	
		
			
				|  |  | 
 | 
	
		
			
				|  |  |     /**
 | 
	
	
		
			
				|  | @ -54,13 +58,13 @@ public class ElasticFactory {
 | 
	
		
			
				|  |  |         // Construct a new Jest client according to configuration via factory
 | 
	
		
			
				|  |  |         factory = new JestClientFactory();
 | 
	
		
			
				|  |  |         HttpClientConfig httpClientConfig = new HttpClientConfig
 | 
	
		
			
				|  |  |                 .Builder(Arrays.asList(hostArray))
 | 
	
		
			
				|  |  |                 .Builder(Arrays.asList(hostArray))//http://59.61.92.90:9065,http://59.61.92.90:9067
 | 
	
		
			
				|  |  |                 .multiThreaded(true)
 | 
	
		
			
				|  |  |                 .maxTotalConnection(50)// 最大链接
 | 
	
		
			
				|  |  |                 .maxConnectionIdleTime(10, TimeUnit.MINUTES)//链接等待时间
 | 
	
		
			
				|  |  |                 .connTimeout(60 * 1000*10)
 | 
	
		
			
				|  |  |                 .maxConnectionIdleTime(120, TimeUnit.SECONDS)//链接等待时间
 | 
	
		
			
				|  |  |                 .connTimeout(60 * 1000)
 | 
	
		
			
				|  |  |                 // .discoveryEnabled(true)
 | 
	
		
			
				|  |  |                 .readTimeout(60 * 1000*10)//60秒
 | 
	
		
			
				|  |  |                 .readTimeout(60 * 1000)//60秒
 | 
	
		
			
				|  |  |                 .build();
 | 
	
		
			
				|  |  | 
 | 
	
		
			
				|  |  | 
 | 
	
	
		
			
				|  | @ -88,12 +92,16 @@ public class ElasticFactory {
 | 
	
		
			
				|  |  |     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
 | 
	
		
			
				|  |  |             Settings settings = Settings.builder()
 | 
	
		
			
				|  |  |                     // es 集群的名称
 | 
	
		
			
				|  |  |                     .put("cluster.name",  StringUtils.isEmpty(clusterName) ? "jkzl" : clusterName)
 | 
	
		
			
				|  |  |                     .put("client.transport.sniff", "true")
 | 
	
		
			
				|  |  |                     //账号密码
 | 
	
		
			
				|  |  |                     .put("xpack.security.user", securityUser)
 | 
	
		
			
				|  |  |                     .build();
 | 
	
		
			
				|  |  | 
 | 
	
		
			
				|  |  |             transportClient = TransportClient.builder().settings(settings).build();
 | 
	
		
			
				|  |  | 
 | 
	
		
			
				|  |  |             transportClient = new PreBuiltXPackTransportClient(settings);
 | 
	
		
			
				|  |  | 
 | 
	
		
			
				|  |  |             for (String oneHost : hosts) {
 | 
	
		
			
				|  |  |                 String[] hostAndport = oneHost.split(":");
 | 
	
	
		
			
				|  | @ -102,4 +110,3 @@ public class ElasticFactory {
 | 
	
		
			
				|  |  |         }
 | 
	
		
			
				|  |  |     }
 | 
	
		
			
				|  |  | }
 | 
	
		
			
				|  |  | 
 |