这两天一直捣鼓es,homebrew安装在mac上然后看文档熟悉相关概念,然后照着官方demo写下es第一行代码,然而第一行代码就卡壳了。
问题
在mac上配置好es,并启动,curl了一下完全ojbk,然后想通过Java api方式来连接es master节点来做一些基础不能再基础的CRUD,照着官方demo手打(Control C,Control V)了一遍,0 error 0 warning,自信满满之际console拉出了一坨…….哦,不,抛出了长长的异常,连接master节点的代码如下:
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一模一样,拷贝的当然一模一样,异常如下:
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啊,搜索一番找不到满意的答案,然后就打个断点试试吧,
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,
IndexResponse indexResponse = client.prepareIndex("twitter", "_doc", "1")
.setSource(content)
.get();
获取结果出现了问题
final List<DiscoveryNode> nodes = this.nodes;
if (closed) {
throw new IllegalStateException("transport client is closed");
}
ensureNodesAreAvailable(nodes);
ensureNodesAreAvailable(nodes),
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就好了,那么,现在有两种解决方案了
- 第一种:指定集群名称,然后加入
- 第二种:不指定集群名称,通过设置ignoreClusterName为true加入
解决
第一种解决方案:
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);
}
}