无法连接到elasticsearch中的外部客户端

时间:2014-07-22 14:49:06

标签: elasticsearch spring-data

当我尝试在春天连接到elasticsearch的外部传输客户端时,我无法连接。它启动嵌入式服务器。

这是Java配置代码:

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

1 个答案:

答案 0 :(得分: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