问题
服务器上部署了一个应用 需要连接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外网也是通的
**粗体** _斜体_ [链接](http://example.com) `代码` - 列表 > 引用
。你还可以使用@
来通知其他用户。