1. 程式人生 > >elasticsearch中client.transport.sniff的使用方法和注意事項

elasticsearch中client.transport.sniff的使用方法和注意事項

(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)
......