如何使用elasticsearch java客户端连接到UP和Started的远程elasticsearch服务器

时间:2016-04-13 07:13:38

标签: java elasticsearch

我正在使用此代码连接我的本地弹性服务器

        String url = "localhost:9200";
        String encodedUrl = URLEncoder.encode(url, "UTF-8");
        Node node = nodeBuilder().settings(
                Settings.settingsBuilder()
                        .put("http.enabled", false)
                        .put("path.home", encodedUrl)
        )
                .client(true)
                .data(false)
                .node();
        return node.client();

我正在使用此代码连接远程弹性服务器

       // Transport client way


        Settings settings = Settings.settingsBuilder()
                .put("cluster.name", "Test") // remote elastic cluster name which is different in my case
                .build();
        Client client = TransportClient.builder().settings(settings).build()
                .addTransportAddress(new InetSocketTransportAddress(InetAddress.getByName("**.**.***.**"), 9259)); // my remote elastic server IP and its port 
        return client;


       // And my other way of connecting (Node Client way)is


       String url = "**.**.***.**:9259";

        String encodedUrl = URLEncoder.encode(url, "UTF-8");
        Node node = nodeBuilder().settings(
                Settings.settingsBuilder()
                        .put("http.enabled", false)
                        .put("path.home", encodedUrl)
                        .put("discovery.zen.ping.unicast.hosts", "52.74.***.**")
                        .put("cluster.name", "Test")
        )
                .client(true)
                .data(false)
                .node();
        return node.client();

我可以连接我的本地弹性服务器,但我无法连接到远程弹性服务器,总会出现一些错误

NodeNotConnectedException:

SendRequestTransportException

ReceiveTimeoutTransportException

NodeNotAvailableException:

等...

任何人都可以帮我弄清楚如何使用java连接到远程弹性服务器,因为我正在使用dropwizard框架开发Web应用程序并需要从这些弹性api服务器获取数据

在与远程ES服务器建立客户端连接时,会发生这种情况

INFO  [2016-04-13 11:23:45,826] org.elasticsearch.node: [Thunderbolt] version[2.2.0], pid[4720], build[8ff36d1/2016-01-27T13:32:39Z]
INFO  [2016-04-13 11:23:45,826] org.elasticsearch.node: [Thunderbolt] initializing ...
INFO  [2016-04-13 11:23:45,826] org.elasticsearch.plugins: [Thunderbolt] modules [], plugins [], sites []
INFO  [2016-04-13 11:23:46,039] org.elasticsearch.node: [Thunderbolt] initialized
INFO  [2016-04-13 11:23:46,039] org.elasticsearch.node: [Thunderbolt] starting ...
INFO  [2016-04-13 11:23:46,732] org.elasticsearch.transport: [Thunderbolt] publish_address {127.0.0.1:9302}, bound_addresses {127.0.0.1:9302}, {[::1]:9302}
INFO  [2016-04-13 11:23:46,732] org.elasticsearch.discovery: [Thunderbolt] Test/eZo2PDQuTcWdd3zsLzVm-w
WARN  [2016-04-13 11:24:16,748] org.elasticsearch.discovery: [Thunderbolt] waited for 30s and no initial state was set by the discovery
INFO  [2016-04-13 11:24:16,748] org.elasticsearch.node: [Thunderbolt] started

2 个答案:

答案 0 :(得分:2)

从ES 2.x开始,ES服务器将only bind to localhost by default,这意味着它无法从外部访问。

因此,在远程ES服务器上,请确保您的private void FillDataGridByTypeCollection() { Persons.Add(new Person() { Id = 1, Name = "Ben" }); Persons.Add(new Person() { Id = 1, Name = "Joseph" }); Persons.Add(new Person() { Id = 1, Name = "Jon" }); Persons.Add(new Person() { Id = 1, Name = "Magnus Montin" }); Persons.Add(new Person() { Id = 1, Name = "Andy" }); Person person = Persons[1]; } private ObservableCollection<Person> persons = new ObservableCollection<Person>(); public ObservableCollection<Person> Persons { get { return persons; } set { persons = value; } } 配置文件包含以下public class Person { public int Id { get; set; } public string Name { get; set; } } 设置,并且具有可公开访问的IP地址:

elasticsearch.yml

其中network.host是远程服务器的IP地址。

确保在进行更改后重新启动ES。

答案 1 :(得分:0)

我想出的答案是

Settings settings = Settings.settingsBuilder()
            .put("cluster.name", "Test") 
            .build();
Client client = TransportClient.builder().settings(settings).build()
            .addTransportAddress(new InetSocketTransportAddress(InetAddress.getByName("**.**.***.**"), 9300)); // my remote elastic server IP and its port 
return client;