Springboot集成ES启动报错

报错内容

  None of the configured nodes are available

elasticsearch.yml配置

cluster.name: ftest
node.name: node-72
node.master: true
node.data: true
network.host: 112.122.245.212
http.port: 39200
transport.tcp.port: 39300
discovery.zen.ping.unicast.hosts: ["127.0.0.1"]
discovery.zen.ping.unicast.hosts.resolve_timeout: 30s
#index.codec: best_compression
http.cors.allow-origin: "/.*/"
http.cors.enabled: true
path.repo: ["/home/xxx/backups"]

 

Java客户端配置

import com.xxx.commons.log.BaseLogger;
import com.xxx.data.elasticsearch.core.ElasticsearchTemplate;
import java.net.InetAddress;
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.transport.client.PreBuiltTransportClient;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
public class ElasticsearchConfiguration extends BaseLogger {
    private static TransportClient transport = null;
    @Value("${elasticsearch.cluster.sniff:true}")
    private Boolean sniff;
    @Value("${elasticsearch.cluster.name:elasticsearch}")
    private String clusterName;
    @Value("${elasticsearch.cluster.hostname:localhost}")
    private String hostname;
    @Value("${elasticsearch.cluster.port:9300}")
    private int port;

    public ElasticsearchConfiguration() {
    }

    @Bean(
        name = {"elasticsearchTemplate"}
    )
    public ElasticsearchTemplate elasticsearchTemplate() {
        return new ElasticsearchTemplate(this.client());
    }

    @Bean
    public Client client() {
        if (transport == null) {
            Settings settings = Settings.builder().put("client.transport.sniff", this.sniff).put("cluster.name", this.clusterName).build();
            this.logger.info("connection elasticserch info : hostname:{}, port: {}", this.hostname, this.port);
            transport = new PreBuiltTransportClient(settings, new Class[0]);
            String[] hostnames = this.hostname.split(",");

            try {
                for(int i = 0; i < hostnames.length; ++i) {
                    this.logger.info("链接es=======>:{}", hostnames[i]);
                    TransportAddress transportAddress = new InetSocketTransportAddress(InetAddress.getByName(hostnames[i]), this.port);
                    transport.addTransportAddresses(new TransportAddress[]{transportAddress});
                }

                return transport;
            } catch (Exception var5) {
                this.logger.error("", var5);
                return null;
            }
        } else {
            return null;
        }
    }
}

 

ES客户端属性配置

<profile>
        <id>test-HA</id>
        <properties>

        <!--系统配置-->
        <server.bind.host>0.0.0.0</server.bind.host>
        <server.bind.port>30030</server.bind.port>

        <!--elasticsearch配置-->
        <elasticsearch.cluster.name>fans</elasticsearch.cluster.name>
        <elasticsearch.cluster.hostname>112.122.245.212</elasticsearch.cluster.hostname>
        <elasticsearch.cluster.port>39200</elasticsearch.cluster.port>
</profile>

 

问题追踪

在异常栈中定位到 org.elasticsearch.client.transport.TransportClientNodesService#ensureNodesAreAvailable

继续找到 org.elasticsearch.client.transport.TransportClientNodesService#execute

this.nodes变量的添加逻辑是在 org.elasticsearch.client.transport.TransportClientNodesService$SimpleNodeSampler#doSample

this.nodes变量保存了可用的ES连接节点信息,从上图可以看出,ReceiveTimeoutTransportException。很明显,连接超时了。

直接访问es ip+端口可以获得如下信息。

按理配置是没有问题的。后来突然意识到 “transport” 这个关键字,然后发觉端口配置错误了。

 

总结一下es连接异常原因

    1、es服务端没有启动
    2、cluster.name 不匹配
    3、端口写错,java客户端要配置 transport.tcp.port: 39300。
     以上3条逐个排查,多半问题就解决了。
posted @ 2018-11-12 18:37  hjzqyx  阅读(2274)  评论(0编辑  收藏  举报