无法使用java InetAddress找到主机但能够使用curl查找

时间:2017-03-11 05:46:40

标签: java curl elasticsearch network-programming

我在私有网络中运行了一个弹性搜索实例。当我尝试使用InetAddress访问它时,它会出错:java.net.UnknownHostException:

TransportClient client = new PreBuiltTransportClient(settings)
                .addTransportAddress(new InetSocketTransportAddress(InetAddress
                        .getByName("http://elasticsearch.myorg.net"), 9200));

但是,当我尝试使用curl命令访问它时,我能够得到响应。

curl http://elasticsearch.myorg.net:9200

可能导致此问题的原因是什么?

对我来说,解决方法是使用resttemplate https://docs.spring.io/spring/docs/current/javadoc-api/org/springframework/web/client/RestTemplate.html

这是一个非常简洁的解决方案。

1 个答案:

答案 0 :(得分:2)

使用端口9300而不是9200. 9200用于REST api,9300用于群集间通信和传输客户端。

https://www.elastic.co/guide/en/elasticsearch/client/java-api/current/transport-client.html