(1)通过TransportClient这个接口,我们可以不启动节点就可以和es集群进行通信,它需要指定es集群中其中一台或多台机的ip地址和端口,例子如下:
Client client = new TransportClient()
.addTransportAddress(newInetSocketTransportAddress("host1", 9300))
.addTransportAddress(newInetSocketTransportAddress("host2", 9300));
client.close();
(2)如果你需要更改集群名(默认是elasticsearch),需要如下设置:
Settings settings =ImmutableSettings.settingsBuilder()
.put("cluster.name","myClusterName").build();
Client client = newTransportClient(settings);
(3)你可以设置client.transport.sniff为true来使客户端去嗅探整个集群的状态,把集群中其它机器的ip地址加到客户端中。这样做的好处是,一般你不用手动设置集群里所有集群的ip到连接客户端,它会自动帮你添加,并且自动发现新加入集群的机器。代码实例如下:
Settings settings = ImmutableSettings.settingsBuilder()
.put("client.transport.sniff", true).build();
TransportClienclient = new TransportClient(settings);
注意:当ES服务器监听(publish_address )使用内网服务器IP,而访问(bound_addresses )使用外网IP时,不要设置client.transport.sniff为true。不设置client.transport.sniff时,默认为false(关闭客户端去嗅探整个集群的状态)。因为在自动发现时会使用内网IP进行通信,导致无法连接到ES服务器。因此此时需要直接使用addTransportAddress方法把集群中其它机器的ip地址加到客户端中。举例如下。
使用docker安装es,下面是部分es启动日志:
[2018-03-10T05:56:20,639][INFO ][o.e.n.Node ] [node-1] starting ...
[2018-03-10T05:56:20,994][INFO ][o.e.t.TransportService ] [node-1] publish_address {172.17.0.3:9300}, bound_addresses {0.0.0.0:9300}
[2018-03-10T05:56:21,036][INFO ][o.e.b.BootstrapChecks ] [node-1] bound or publishing to a non-loopback or non-link-local address, enforcing bootstrap checks
[2018-03-10T05:56:24,373][INFO ][o.e.c.s.ClusterService ] [node-1] new_master {node-1}{xBV8XSG0QDeSSSjqBidBzg}{9IE0hcJRQBWp4ZOjPkWajg}{172.17.0.3}{172.17.0.3:9300}, reason: zen-disco-elected-as-master ([0] nodes joined)
[2018-03-10T05:56:24,644][INFO ][o.e.h.n.Netty4HttpServerTransport] [node-1] publish_address {172.17.0.3:9200}, bound_addresses {0.0.0.0:9200}
[2018-03-10T05:56:24,644][INFO ][o.e.n.Node ] [node-1] started
此时172.17.0.3为内网ip。此时将client.transport.sniff设置为true是不合适的。因为采用TransportClient 连接es时,可能会报如下错误:
2018-03-10 15:06:01 [DEBUG][org.elasticsearch.client.transport.TransportClientNodesService][validateNewNodes][376]-> failed to connect to discovered node [{node-1}{xBV8XSG0QDeSSSjqBidBzg}{9IE0hcJRQBWp4ZOjPkWajg}{172.17.0.3}{172.17.0.3:9300}]
org.elasticsearch.transport.ConnectTransportException: [node-1][172.17.0.3:9300] connect_timeout[30s]
at org.elasticsearch.transport.netty4.Netty4Transport.connectToChannels(Netty4Transport.java:363)
at org.elasticsearch.transport.TcpTransport.openConnection(TcpTransport.java:570)
at org.elasticsearch.transport.TcpTransport.connectToNode(TcpTransport.java:473)
Caused by: io.netty.channel.AbstractChannel$AnnotatedConnectException: Connection timed out: no further information: 172.17.0.3/172.17.0.3:9300
at sun.nio.ch.SocketChannelImpl.checkConnect(Native Method)
at sun.nio.ch.SocketChannelImpl.finishConnect(SocketChannelImpl.java:717)
at io.netty.channel.socket.nio.NioSocketChannel.doFinishConnect(NioSocketChannel.java:325)
......
Caused by: java.net.ConnectException: Connection timed out: no further information
... 10 common frames omitted
或者报以下错误
NoNodeAvailableException[None of the configured nodes are available: [{#transport#-1}{VTss4h8SRsCpP6EW5PoLrQ}{192.168.106.128}{192.168.106.128: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:59)
......
参考和转载地址:点击打开链接