問題
服務器上部署了一個應用 需要連接ES 配置的是內網地址
<elasticsearch:transport-client id="client" cluster-nodes="10.28.70.38:9300" cluster-name="foo"/>
telnet 10.28.70.38 9300
OK 但是實際調用接口的時候 還是報了下面的錯
NoNodeAvailableException[None of the configured nodes are available: [{#transport#-1}{10.28.70.38}{10.28.70.38:9300}]]
at org.elasticsearch.client.transport.TransportClientNodesService.ensureNodesAreAvailable(TransportClientNodesService.java:290)
感覺有點奇怪? 內網地址是通的啊 怎麼還報了這樣的錯呢?
執行 lsof -p pid
發現存在下面這樣的連接 變成了連ES的外網地址(106.14.XXX.XXX
)了呢?但該服務器連ES外網地址不通
java 15847 root 74u IPv4 1131084443 0t0 TCP 139.196.XXX.XXX:36706->106.14.XXX.XXX:vrace (SYN_SENT)
最後定位到的原因是 elasticsearchTemplate的配置項中有"client.transport.sniff" -> "true"
而client.transport.sniff的含義是
The Transport client comes with a cluster sniffing feature which allows it to dynamically add new hosts and remove old ones. When sniffing is enabled, the transport client will connect to the nodes in its internal node list, which is built via calls to addTransportAddress. After this, the client will call the internal cluster state API on those nodes to discover available data nodes. The internal node list of the client will be replaced with those data nodes only. This list is refreshed every five seconds by default. Note that the IP addresses the sniffer connects to are the ones declared as the publish address in those node’s Elasticsearch config.
參考文檔: https://www.elastic.co/guide/...
解決
顯式將client-transport-sniff置爲false
<elasticsearch:transport-client id="client" cluster-nodes="10.28.70.38:9300" cluster-name="foo" client-transport-sniff="false"/>
此時執行lsof -p pid
java 32650 root 271u IPv4 1131368694 0t0 TCP iZ11jteew8eZ:57671->10.28.70.38:vrace (ESTABLISHED)
或者也可以修改網絡配置 使得訪問ES外網也是通的