Springboot集成ES啓動報錯

報錯內容

  None of the configured nodes are availablejava

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#ensureNodesAreAvailablenode

繼續找到 org.elasticsearch.client.transport.TransportClientNodesService#executespring

this.nodes變量的添加邏輯是在 org.elasticsearch.client.transport.TransportClientNodesService$SimpleNodeSampler#doSamplecors

this.nodes變量保存了可用的ES鏈接節點信息,從上圖能夠看出,ReceiveTimeoutTransportException。很明顯,鏈接超時了。elasticsearch

直接訪問es ip+端口能夠得到以下信息。tcp

按理配置是沒有問題的。後來忽然意識到 「transport」 這個關鍵字,而後發覺端口配置錯誤了。ui

 

總結一下es鏈接異常緣由

    一、es服務端沒有啓動
    二、cluster.name 不匹配
    三、端口寫錯,java客戶端要配置 transport.tcp.port: 39300。
     以上3條逐個排查,多半問題就解決了。
相關文章
相關標籤/搜索