记录一次elasticsearch debug过程

这两天一直捣鼓es,homebrew安装在mac上而后看文档熟悉相关概念,而后照着官方demo写下es第一行代码,然而第一行代码就卡壳了。java

问题

在mac上配置好es,并启动,curl了一下彻底ojbk,而后想经过Java api方式来链接es master节点来作一些基础不能再基础的CRUD,照着官方demo手打(Control C,Control V)了一遍,0 error 0 warning,自信满满之际console拉出了一坨…….哦,不,抛出了长长的异常,链接master节点的代码以下:node

public class ClientTest {

    public static void main(String[] args) throws UnknownHostException {
        TransportClient client = new PreBuiltTransportClient(Settings.EMPTY)
                .addTransportAddress(new TransportAddress(InetAddress.getByName("localhost"), 9300));

        Map<String, String> content = Maps.newHashMap();
        content.put("title", "es test");
        content.put("content", "This a simple content");
        IndexResponse indexResponse = client.prepareIndex("twitter", "_doc", "1")
                .setSource(content)
                .get();

        System.out.println(indexResponse);
    }
}

这代码彻底和官网demo如出一辙,拷贝的固然如出一辙,异常以下:web

2018-12-06 14:57:40,867 WARN  [elasticsearch[_client_][generic][T#4]] transport.TransportClientNodesService$SimpleNodeSampler (TransportClientNodesService.java:420) - node {#transport#-1}{_IT0wf2MTyiI3T3-fCe0eQ}{localhost}{127.0.0.1:9300} not part of the cluster Cluster [elasticsearch], ignoring...
Exception in thread "main" NoNodeAvailableException[None of the configured nodes are available: [{#transport#-1}{_IT0wf2MTyiI3T3-fCe0eQ}{localhost}{127.0.0.1:9300}]]
	at org.elasticsearch.client.transport.TransportClientNodesService.ensureNodesAreAvailable(TransportClientNodesService.java:347)
	at org.elasticsearch.client.transport.TransportClientNodesService.execute(TransportClientNodesService.java:245)
	at org.elasticsearch.client.transport.TransportProxyClient.execute(TransportProxyClient.java:60)
	at org.elasticsearch.client.transport.TransportClient.doExecute(TransportClient.java:360)
	at org.elasticsearch.client.support.AbstractClient.execute(AbstractClient.java:405)
	at org.elasticsearch.client.support.AbstractClient.execute(AbstractClient.java:394)
	at org.elasticsearch.action.ActionRequestBuilder.execute(ActionRequestBuilder.java:46)
	at org.elasticsearch.action.ActionRequestBuilder.get(ActionRequestBuilder.java:53)
	at com.fancy.client.ClientTest.main(ClientTest.java:29)

debug

抛出了异常,第一反应就是Google啊,搜索一番找不到满意的答案,而后就打个断点试试吧,api

TransportClient client = new PreBuiltTransportClient(Settings.EMPTY)
                .addTransportAddress(new TransportAddress(InetAddress.getByName("localhost"), 9300));

在这里打了一个断点,debug进去,一直debug到console抛出一个logger warning,此时的位置位于浏览器

final LivenessResponse livenessResponse = handler.txGet();
                    if (!ignoreClusterName && !clusterName.equals(livenessResponse.getClusterName())) {
                        logger.warn("node {} not part of the cluster {}, ignoring...", listedNode, clusterName);
                        newFilteredNodes.add(listedNode);
                    } else {
                        // use discovered information but do keep the original transport address,
                        // so people can control which address is exactly used.
                        DiscoveryNode nodeWithInfo = livenessResponse.getDiscoveryNode();
                        newNodes.add(new DiscoveryNode(nodeWithInfo.getName(), nodeWithInfo.getId(), nodeWithInfo.getEphemeralId(),
                            nodeWithInfo.getHostName(), nodeWithInfo.getHostAddress(), listedNode.getAddress(),
                            nodeWithInfo.getAttributes(), nodeWithInfo.getRoles(), nodeWithInfo.getVersion()));
                    }

warning是这行代码logger.warn(“node {} not part of the cluster {}, ignoring…”, listedNode, clusterName); 打印出来的,随后将listedNode加入到newFilteredNodes(一个List),先无论这些事干吗的,要是if判断为true就会执行上述那段代码,要是为false,就会执行newNodes.add()这样的操做,而后在TransportClientNidesService这个类的成员变量nodes=newNode,filteredNodes=newFilteredNodes,在这里说明一下,这里的节点就是官方文档中说的客户端节点。继续debug,curl

IndexResponse indexResponse = client.prepareIndex("twitter", "_doc", "1")
                .setSource(content)
                .get();

获取结果出现了问题elasticsearch

final List<DiscoveryNode> nodes = this.nodes;
        if (closed) {
            throw new IllegalStateException("transport client is closed");
        }
        ensureNodesAreAvailable(nodes);

ensureNodesAreAvailable(nodes),ide

private void ensureNodesAreAvailable(List<DiscoveryNode> nodes) {
        if (nodes.isEmpty()) {
            String message = String.format(Locale.ROOT, "None of the configured nodes are available: %s", this.listedNodes);
            throw new NoNodeAvailableException(message);
        }
    }

异常就是这里抛出的,注意调用这个方法传入的nodes是咱们上面分析的if判断为false才不为空,而从咱们debug中发现if判断为true,因此如今就能够好好的审视那个判断了,从新在if判断设置一个断点,判断条件为true的条件是ignoreClusterName为false 且 clusterName和活跃的集群名称相等,OK,再Google下es集群名称,发现客户端节点加入一个集群须要指定集群名称,也就是node上配置你要加入的集群名称,固然若是你不想指定集群名称也能够设置ignoreClusterName为true就行了,那么,如今有两种解决方案了svg

  • 第一种:指定集群名称,而后加入
  • 第二种:不指定集群名称,经过设置ignoreClusterName为true加入

解决

第一种解决方案:ui

public class ClientTest {

    public static void main(String[] args) throws UnknownHostException {
        Settings settings = Settings.builder()
                .put("client.transport.sniff", true)
                // 这里指定集群名称,个人集群名称能够经过浏览器输入localhost:9200来获取cluster name
                .put("cluster.name", "elasticsearch_jackie")
                .build();
        TransportClient client = new PreBuiltTransportClient(settings)
                .addTransportAddress(new TransportAddress(InetAddress.getByName("localhost"), 9300));

        Map<String, String> content = Maps.newHashMap();
        content.put("title", "es test");
        content.put("content", "This a simple content");
        IndexResponse indexResponse = client.prepareIndex("twitter", "_doc", "1")
                .setSource(content)
                .get();

        System.out.println(indexResponse);
    }
}

第二种解决方案:

public class ClientTest {

    public static void main(String[] args) throws UnknownHostException {
        Settings settings = Settings.builder()
                .put("client.transport.sniff", true)
                // 设置ignoreNodeName为true
                .put("client.transport.ignore_cluster_name", true)
                .build();
        TransportClient client = new PreBuiltTransportClient(settings)
                .addTransportAddress(new TransportAddress(InetAddress.getByName("localhost"), 9300));

        Map<String, String> content = Maps.newHashMap();
        content.put("title", "es test");
        content.put("content", "This a simple content");
        IndexResponse indexResponse = client.prepareIndex("twitter", "_doc", "1")
                .setSource(content)
                .get();

        System.out.println(indexResponse);
    }
}