這兩天一直搗鼓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)
拋出了異常,第一反應就是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
第一種解決方案: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); } }