Tinku
Tinku

Reputation: 21

How to connect to remote elasticsearch server which is UP and Started using elasticsearch java client

i am using this code to connect my local elastic server

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

I am using this code to connect remote elastic server

       // 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();

I am able to connect my local elastic server but i am unable to connect to remote elastic server, there were always some errors coming

NodeNotConnectedException:

SendRequestTransportException

ReceiveTimeoutTransportException

NodeNotAvailableException:

etc...

Can anyone help me to figure out how to connect to remote elastic server using java, as i am developing web applications in dropwizard framework and need to get data from these elastic api servers

while establishing a client connection with remote ES server this thing happens

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

Upvotes: 2

Views: 3628

Answers (2)

Tinku
Tinku

Reputation: 21

The answer i figured out is

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;

Upvotes: 0

Val
Val

Reputation: 217254

Since ES 2.x, the ES server will only bind to localhost by default, which means it is not reachable from the outside.

So on your remote ES server, make sure that your elasticsearch.yml configuration file contains the following network.host setting with a publicly reachable IP address:

network.host: a.b.c.d

where a.b.c.d is the IP address of your remote server.

Make sure to restart ES after making the change.

Upvotes: 2

Related Questions