2014-07-22 33 views
0

當我試圖連接彈性搜索的外部傳輸客戶端時,我無法連接。它啓動嵌入式服務器。無法連接到elasticsearch的外部客戶端

這裏是Java的配置代碼:

@Bean 
public ElasticsearchTemplate elasticsearchTemplate() { 
    Client client = new TransportClient() 
      .addTransportAddress(new InetSocketTransportAddress(
        "localhost", 9300)); 
    return new ElasticsearchTemplate(client); 
} 

回答

0

也許這個春天工廠bean,我通常使用可以幫助你:

package nl.gridshore.sample.mymusic.services; 

import org.elasticsearch.client.Client; 
import org.elasticsearch.client.transport.TransportClient; 
import org.elasticsearch.common.settings.ImmutableSettings; 
import org.elasticsearch.common.settings.Settings; 
import org.elasticsearch.common.transport.InetSocketTransportAddress; 
import org.elasticsearch.common.transport.TransportAddress; 
import org.slf4j.Logger; 
import org.slf4j.LoggerFactory; 
import org.springframework.beans.factory.DisposableBean; 
import org.springframework.beans.factory.FactoryBean; 
import org.springframework.beans.factory.InitializingBean; 
import org.springframework.beans.factory.annotation.Value; 
import org.springframework.stereotype.Component; 

import java.util.ArrayList; 
import java.util.List; 

/** 
* Factory bean creating the elasticsearch Client object 
*/ 
@Component 
public class ElasticsearchClientFactoryBean implements FactoryBean<Client>, InitializingBean, DisposableBean { 
    private static final Logger logger = LoggerFactory.getLogger(ElasticsearchClientFactoryBean.class); 
    public static final int DEFAULT_ELASITCSEARCH_PORT = 9300; 

    @Value("${elastic.node.name}") 
    private String nodeName; 

    @Value("${elastic.unicast.hosts}") 
    private String unicastHosts; 

    @Value("${elastic.cluster.name}") 
    private String clusterName; 

    private Client client; //Thread safe: its lifecycle should be similar to the application lifecycle 

    @Override 
    public void destroy() { 
     client.close(); 
    } 

    @Override 
    public Client getObject() { 
     return client; 
    } 

    @Override 
    public Class<?> getObjectType() { 
     return Client.class; 
    } 

    @Override 
    public boolean isSingleton() { 
     return true; 
    } 

    @Override 
    public void afterPropertiesSet() { 
     Settings settings = ImmutableSettings.settingsBuilder().put("cluster.name", clusterName).build(); 

     logger.debug("Settings used for connection to elasticsearch : {}", settings.toDelimitedString('#')); 

     TransportAddress[] addresses = getTransportAddresses(unicastHosts); 

     logger.debug("Hosts used for transport client : {}", (Object) addresses); 

     client = new TransportClient(settings).addTransportAddresses(addresses); 
    } 

    TransportAddress[] getTransportAddresses(String unicastHosts) { 
     List<TransportAddress> transportAddresses = new ArrayList<>(); 

     for (String unicastHost : unicastHosts.split(",")) { 
      int port = DEFAULT_ELASITCSEARCH_PORT; 
      String serverName = unicastHost; 
      if (unicastHost.contains(":")) { 
       String[] splitted = unicastHost.split(":"); 
       serverName = splitted[0]; 
       port = Integer.parseInt(splitted[1].trim()); 
      } 
      transportAddresses.add(new InetSocketTransportAddress(serverName.trim(), port)); 
     } 

     return transportAddresses.toArray(new TransportAddress[transportAddresses.size()]); 
    } 
} 

比使用下面的內容中使用屬性文件:

好的,謝謝。
elastic.node.name=client-tomcat 
elastic.unicast.hosts=localhost 
elastic.cluster.name=jc-play 
+0

好的。謝謝。我會試試這個。 –

相關問題