【问题标题】:Unable to connect to external client in elasticsearch无法在elasticsearch中连接到外部客户端
【发布时间】:2014-07-22 14:49:05
【问题描述】:

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

这里是 Java 配置代码:

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

【问题讨论】:

    标签: elasticsearch spring-data


    【解决方案1】:

    也许我平时用的这个spring factory 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
    

    【讨论】:

    • 好的。谢谢。我会试试这个。
    猜你喜欢
    • 2020-07-03
    • 2015-11-24
    • 2011-03-03
    • 2020-05-26
    • 2016-01-25
    • 2020-02-03
    • 1970-01-01
    • 1970-01-01
    • 2013-06-06
    相关资源
    最近更新 更多