要不要再翻翻文档呢?

TransportClient无法连接node问题

Elasticsearch | 作者 Russell | 发布于2017年02月10日 | 阅读数:51731

在eclipese中建立了一个Maven工程,调用ES的JAVA API创建client连接本地的ES,继续创建索引程序一直报错:无法连接到node.查了下网上类似的帖子,貌似都解决不了。
(1)自动嗅探功能已取消,依然无法解决
-将client.transport.sniff置为false
Settings settings = Settings.builder().put("cluster.name", "my-cluster").put("client.transport.sniff", false).build();

 
报错结果为:
no modules loaded
loaded plugin [org.elasticsearch.index.reindex.ReindexPlugin]
loaded plugin [org.elasticsearch.percolator.PercolatorPlugin]
loaded plugin [org.elasticsearch.script.mustache.MustachePlugin]
loaded plugin [org.elasticsearch.transport.Netty3Plugin]
loaded plugin [org.elasticsearch.transport.Netty4Plugin]
failed to connect to node [{#transport#-1}{QIYpJyiqTdyO5OU0ornXJg}{Qiyxxhb-Daiw}{169.254.87.51:9300}], removed from nodes list
org.elasticsearch.transport.ConnectTransportException: [][169.254.87.51:9300] connect_timeout[30s]
at org.elasticsearch.transport.netty4.Netty4Transport.connectToChannels(Netty4Transport.java:370) ~[transport-netty4-client-5.2.0.jar:5.2.0]
at org.elasticsearch.transport.TcpTransport.openConnection(TcpTransport.java:495) ~[elasticsearch-5.2.0.jar:5.2.0]
at org.elasticsearch.transport.TcpTransport.connectToNode(TcpTransport.java:460) ~[elasticsearch-5.2.0.jar:5.2.0]
at org.elasticsearch.transport.TransportService.connectToNode(TransportService.java:318) ~[elasticsearch-5.2.0.jar:5.2.0]
at org.elasticsearch.client.transport.TransportClientNodesService$SimpleNodeSampler.doSample(TransportClientNodesService.java:408) [elasticsearch-5.2.0.jar:5.2.0]
at org.elasticsearch.client.transport.TransportClientNodesService$NodeSampler.sample(TransportClientNodesService.java:354) [elasticsearch-5.2.0.jar:5.2.0]
at org.elasticsearch.client.transport.TransportClientNodesService.addTransportAddresses(TransportClientNodesService.java:195) [elasticsearch-5.2.0.jar:5.2.0]
at org.elasticsearch.client.transport.TransportClient.addTransportAddress(TransportClient.java:312) [elasticsearch-5.2.0.jar:5.2.0]
at ElasticSearch.Exercise.ESUtilties.getClient(ESUtilties.java:32) [classes/:?]
at ElasticSearch.Exercise.App.main(App.java:18) [classes/:?]
Caused by: io.netty.channel.AbstractChannel$AnnotatedConnectException: Connection refused: no further information: Qiyxxhb-Daiw/169.254.87.51:9300
at sun.nio.ch.SocketChannelImpl.checkConnect(Native Method) ~[?:1.8.0_45]
at sun.nio.ch.SocketChannelImpl.finishConnect(SocketChannelImpl.java:717) ~[?:1.8.0_45]
at io.netty.channel.socket.nio.NioSocketChannel.doFinishConnect(NioSocketChannel.java:346) ~[netty-transport-4.1.7.Final.jar:4.1.7.Final]
at io.netty.channel.nio.AbstractNioChannel$AbstractNioUnsafe.finishConnect(AbstractNioChannel.java:340) ~[netty-transport-4.1.7.Final.jar:4.1.7.Final]
at io.netty.channel.nio.NioEventLoop.processSelectedKey(NioEventLoop.java:630) ~[netty-transport-4.1.7.Final.jar:4.1.7.Final]
at io.netty.channel.nio.NioEventLoop.processSelectedKeysPlain(NioEventLoop.java:527) ~[netty-transport-4.1.7.Final.jar:4.1.7.Final]
at io.netty.channel.nio.NioEventLoop.processSelectedKeys(NioEventLoop.java:481) ~[netty-transport-4.1.7.Final.jar:4.1.7.Final]
at io.netty.channel.nio.NioEventLoop.run(NioEventLoop.java:441) ~[netty-transport-4.1.7.Final.jar:4.1.7.Final]
at io.netty.util.concurrent.SingleThreadEventExecutor$5.run(SingleThreadEventExecutor.java:858) ~[netty-common-4.1.7.Final.jar:4.1.7.Final]
at java.lang.Thread.run(Thread.java:745) ~[?:1.8.0_45]

Exception in thread "main" NoNodeAvailableException[None of the configured nodes are available: [{#transport#-1}{QIYpJyiqTdyO5OU0ornXJg}{Qiyxxhb-Daiw}{169.254.87.51:9300}]]
at org.elasticsearch.client.transport.TransportClientNodesService.ensureNodesAreAvailable(TransportClientNodesService.java:344)
at org.elasticsearch.client.transport.TransportClientNodesService.execute(TransportClientNodesService.java:242)
at org.elasticsearch.client.transport.TransportProxyClient.execute(TransportProxyClient.java:59)
at org.elasticsearch.client.transport.TransportClient.doExecute(TransportClient.java:356)
at org.elasticsearch.client.support.AbstractClient.execute(AbstractClient.java:403)
at org.elasticsearch.action.ActionRequestBuilder.execute(ActionRequestBuilder.java:80)
at org.elasticsearch.action.ActionRequestBuilder.execute(ActionRequestBuilder.java:54)
at org.elasticsearch.action.ActionRequestBuilder.get(ActionRequestBuilder.java:62)
at ElasticSearch.Exercise.App.main(App.java:49)

 
-若将client.transport.sniff置为true,报错略有区别
no modules loaded
loaded plugin [org.elasticsearch.index.reindex.ReindexPlugin]
loaded plugin [org.elasticsearch.percolator.PercolatorPlugin]
loaded plugin [org.elasticsearch.script.mustache.MustachePlugin]
loaded plugin [org.elasticsearch.transport.Netty3Plugin]
loaded plugin [org.elasticsearch.transport.Netty4Plugin]


Exception in thread "main" NoNodeAvailableException[None of the configured nodes are available: [{#transport#-1}{Omz9g4szRyKqhnrzZaNopQ}{Qiyxxhb-Daiw}{169.254.87.51:9300}]]
at org.elasticsearch.client.transport.TransportClientNodesService.ensureNodesAreAvailable(TransportClientNodesService.java:344)
at org.elasticsearch.client.transport.TransportClientNodesService.execute(TransportClientNodesService.java:242)
at org.elasticsearch.client.transport.TransportProxyClient.execute(TransportProxyClient.java:59)
at org.elasticsearch.client.transport.TransportClient.doExecute(TransportClient.java:356)
at org.elasticsearch.client.support.AbstractClient.execute(AbstractClient.java:403)
at org.elasticsearch.action.ActionRequestBuilder.execute(ActionRequestBuilder.java:80)
at org.elasticsearch.action.ActionRequestBuilder.execute(ActionRequestBuilder.java:54)
at org.elasticsearch.action.ActionRequestBuilder.get(ActionRequestBuilder.java:62)
at ElasticSearch.Exercise.App.main(App.java:49)

(2)配置文件确认没有问题,依然无法解决
# ======================== Elasticsearch Configuration =========================
#
# NOTE: Elasticsearch comes with reasonable defaults for most settings.
# Before you set out to tweak and tune the configuration, make sure you
# understand what are you trying to accomplish and the consequences.
#
# The primary way of configuring a node is via this file. This template lists
# the most important settings you may want to configure for a production cluster.
#
# Please consult the documentation for further information on configuration options:
# https://www.elastic.co/guide/e ... .html
#
# ---------------------------------- Cluster -----------------------------------
#
# Use a descriptive name for your cluster:
#
cluster.name: my-cluster
#
# ------------------------------------ Node ------------------------------------
#
# Use a descriptive name for the node:
#
node.name: node-1
#
# Add custom attributes to the node:
#
#node.attr.rack: r1
#
# ----------------------------------- Paths ------------------------------------
#
# Path to directory where to store the data (separate multiple locations by comma):
#
path.data: E:\WEB\elasticsearch\elasticsearch-5.2.0\path\data
#
# Path to log files:
#
#path.logs: /path/to/logs
path.logs: E:\WEB\elasticsearch\elasticsearch-5.2.0\path\data
#
# ----------------------------------- Memory -----------------------------------
#
# Lock the memory on startup:
#
bootstrap.memory_lock: true
#
# Make sure that the heap size is set to about half the memory available
# on the system and that the owner of the process is allowed to use this
# limit.
#
# Elasticsearch performs poorly when the system is swapping the memory.
#
# ---------------------------------- Network -----------------------------------
#
# Set the bind address to a specific IP (IPv4 or IPv6):
#
#network.host: 192.168.1.100
#
# Set a custom port for HTTP:
#
#http.port: 9200
#
# For more information, consult the network module documentation.
#
# --------------------------------- Discovery ----------------------------------
#
# Pass an initial list of hosts to perform discovery when new node is started:
# The default list of hosts is ["127.0.0.1", "[::1]"]
#
#discovery.zen.ping.unicast.hosts: ["host1", "host2"]
discovery.zen.ping.unicast.hosts: ["192.168.1.100"]
client.transport.ping_timeout: 60s
#
# Prevent the "split brain" by configuring the majority of nodes (total number of master-eligible nodes / 2 + 1):
#
#discovery.zen.minimum_master_nodes: 3
#
# For more information, consult the zen discovery module documentation.
#
# ---------------------------------- Gateway -----------------------------------
#
# Block initial recovery after a full cluster restart until N nodes are started:
#
#gateway.recover_after_nodes: 3
#
# For more information, consult the gateway module documentation.
#
# ---------------------------------- Various -----------------------------------
#
# Require explicit names when deleting indices:
#
#action.destructive_requires_name: true
# -----------------------------------HTTP----------------------------------------
http.cors.enabled: true
http.cors.allow-origin: "*"
http.cors.allow-headers: Authorization
#http.cors.allow-headers:"X-Requested-With","Content-Type","Content-Length"

 
(3)以下是源码,请高手帮忙看下有什么错误
在Maven工程中建了2个类(pom.xml中相关依赖已经补充)
-启动/关闭client类
public class ESUtilties {
public static Client getClient() {

Settings settings = Settings.builder().put("cluster.name", "my-cluster").put("client.transport.sniff",false).build();

TransportClient client =null;

try {
client = new PreBuiltTransportClient(settings).addTransportAddress(new InetSocketTransportAddress(InetAddress.getByName("QIYXXHB-DAIW"), 9300));
} catch (UnknownHostException e) {
e.printStackTrace();
}
if (client!=null) System.out.println("Client创建成功!");
return client;
}

public static void closeClient(Client client) {
if(client != null) {
client.close();
System.out.println("Client已关闭!");
}
}
}

-主函数类
public class App 
{
public static void main( String[] args )
{
Client client = ESUtilties.getClient();

IndexResponse response=null;
try {
response = client.prepareIndex("twitter", "tweet", "1")
.setSource(XContentFactory.jsonBuilder()
.startObject()
.field("prodID", "1")
.field("prodName", "5")
.field("prodDesc", "比你想的更强大")
.field("cardID","1")
.endObject()
)
.get();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}

String _index=response.getIndex();
String _type=response.getType();

System.out.println("Index:"+_index);
System.out.println("Type:"+_type);

ESUtilties.closeClient(client);
}
}
已邀请:

kepmoving - 90后

赞同来自:

# 修改一下ES的监听地址,这样别的机器也可以访问
network.host: 0.0.0.0

DimonHo

赞同来自:

你是用外网连的es吧,我用外网连接ES导入数据,总是在导入几条数据之后就报no node avaliable。查看es服务器的日志,提示连接超时:
2017-04-21_212858.png

 
配置文件配置的超时时间是60S,但实际经常运行十几秒之后就提示超时了。

tangyx

赞同来自:

请问解决了吗?在命令行上进行操作都是正常的,用java连接查询数据也是报无法连接node

wangning

赞同来自:

请问解决了么 同样的问题 搞了一周了
 

pqy

赞同来自:

1、版本、版本、版本
2、client.transport.sniff 一点要是 false

海饼干

赞同来自:

还有一种可能,就安装es的服务器的防火墙没有开9300端口
 
验证方法:
1. 把防火墙关闭
2. 命令行:service iptables stop
3. 运行代码,验证是否是防火墙的原因
 
设置方法:
1. elasticsearch.yml要改写成network.host:0.0.0.0;
2. 通过修改防火墙:
(1)编辑防火墙文件:vim /etc/sysconfig/iptables 
(2)写入:-A INPUT -p tcp -m state --state NEW -m tcp --dport 9300 -j ACCEPT,保存
(3)重启防火墙:service iptables restart 
(4)启动es
 
匿名用户

匿名用户

赞同来自:

应该是防火墙问题,关掉防火墙,就可以了

xklc

赞同来自:

请确认你们是否安装了XPACK, 如果安装了。 需要自己修改修改
ElasticSearchConnection的实现。 

wanglei1988

赞同来自:

有时能访问,有事就是client.transport.NoNodeAvailableException

要回复问题请先登录注册