Bulk RestClient Connection refused with FQDN or IP

Hi guys, hope someone will be able to help me.

I'm using RestClient in sync mode. ElasticSearch is running on local PC. ES version is 6.2.4. RestClient version is 6.3.1.
The problem is that everything just fine when I use localhost in URL. As soon as I use IP or FQDN -
Caused by: java.io.IOException: Connection refused: no further information
at org.elasticsearch.client.RestClient$SyncResponseListener.get(RestClient.java:728)
at org.elasticsearch.client.RestClient.performRequest(RestClient.java:235)
at org.elasticsearch.client.RestClient.performRequest(RestClient.java:198)
at Start.main(Start.java:70)
... 5 more

Any useful comments will be highly appreciated...
Here is the code:

    import java.io.IOException;
    import java.util.Collections;
    
    import javax.json.Json;
    import javax.json.JsonBuilderFactory;
    import javax.json.JsonObject;
    
    import org.apache.http.Header;
    import org.apache.http.HttpEntity;
    import org.apache.http.HttpHost;
    import org.apache.http.entity.ContentType;
    import org.apache.http.nio.entity.NStringEntity;
    import org.apache.http.util.EntityUtils;
    import org.elasticsearch.client.Response;
    import org.elasticsearch.client.RestClient;
    
    public class Start {

public static void main(String[] args) throws IOException {
	System.out.println("=== started  ===");

	RestClient restClient = null;
	String hostName = null;
	String schema= null;
	String api = null;
	int port = 0;

	
	for(int i = 0; i < args.length; i++) {
		switch(args[i])
		{	case "-host": hostName = args[i+1]; i++; break;
			case "-port": port = Integer.valueOf(args[i+1]); i++; break;
			case "-schema": schema = args[i+1]; i++; break;
			case "-api": api = args[i+1]; i++; break;
			default:
			System.out.println("Usage: -host <> -port <> -schema <http/https> -api <_bulk>");
		}
    }
	if (null == hostName || null == schema || null == api || 0 == port)
	{
		System.out.println("Usage: -host <> -port <> -schema <http/https> -api <>");
		return;
	}
		
	System.out.println(String.format("Creating RestClient; will send POST requests to Host: %s, Port: %d, Schema: %s, API %s\n", hostName, port, schema, api));

	restClient = RestClient.builder(new HttpHost(hostName, port, schema)).build();
	
	JsonBuilderFactory factory = Json.createBuilderFactory(null);
	JsonObject index = factory.createObjectBuilder()
	    .add("index", factory.createObjectBuilder()
	    	.add("_index", "my-test-20180723")
	        .add("_type", "log")
	        )
	    .build();
	
	JsonObject message = factory.createObjectBuilder()
        .add("message", "MyMessage")
		.build();
	
	String jsonString = index.toString() + "\n" + message.toString() + "\n";

	System.out.println(String.format("JsonString will be sent to ES:\n%s\n", jsonString));

	HttpEntity entity = new NStringEntity(jsonString, ContentType.APPLICATION_JSON);
	Response response = restClient.performRequest("POST", api, Collections.emptyMap(), entity); 		
	
	//Response response = restClient.performRequest("GET", "/");
	
	int statusCode = response.getStatusLine().getStatusCode(); 
	Header[] headers = response.getHeaders(); 
	String responseBody = EntityUtils.toString(response.getEntity());
	
	System.out.println(String.format("Response Code: %d, Response body: %s", statusCode, responseBody));
	restClient.close();

}

    }

Output:

        C:\Temp>java -jar es-direct.jar -host mcr851vladp  -port 9200 -schema http -api /_bulk
        === started v 01:13 ===
       Creating RestClient; will send POST requests to Host: mcr851vladp, Port: 9200, Schema: http, API /_bulk
    
    JsonString will be sent to ES:
    {"index":{"_index":"my-test-20180723","_type":"log"}}
    {"message":"MyMessage"}
    
    
    Exception in thread "main" java.lang.reflect.InvocationTargetException
            at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
            at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
            at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
            at java.lang.reflect.Method.invoke(Unknown Source)
            at org.eclipse.jdt.internal.jarinjarloader.JarRsrcLoader.main(JarRsrcLoader.java:58)
    Caused by: java.io.IOException
            at org.elasticsearch.client.RestClient$SyncResponseListener.get(RestClient.java:728)
            at org.elasticsearch.client.RestClient.performRequest(RestClient.java:235)
            at org.elasticsearch.client.RestClient.performRequest(RestClient.java:198)
            at Start.main(Start.java:70)
            ... 5 more
    Caused by: java.net.ConnectException
            at org.apache.http.nio.pool.RouteSpecificPool.timeout(RouteSpecificPool.java:168)
            at         org.apache.http.nio.pool.AbstractNIOConnPool.requestTimeout(AbstractNIOConnPool.java:561)
            at         org.apache.http.nio.pool.AbstractNIOConnPool$InternalSessionRequestCallback.timeout(AbstractNIOConnPool.java:822)
            at org.apache.http.impl.nio.reactor.SessionRequestImpl.timeout(SessionRequestImpl.java:183)
            at org.apache.http.impl.nio.reactor.DefaultConnectingIOReactor.processTimeouts(DefaultConnectingIOReactor.java:210)
            at org.apache.http.impl.nio.reactor.DefaultConnectingIOReactor.processEvents(DefaultConnectingIOReactor.java:155)
            at org.apache.http.impl.nio.reactor.AbstractMultiworkerIOReactor.execute(AbstractMultiworkerIOReactor.java:348)
            at org.apache.http.impl.nio.conn.PoolingNHttpClientConnectionManager.execute(PoolingNHttpClientConnectionManager.java:192)
            at org.apache.http.impl.nio.client.CloseableHttpAsyncClientBase$1.run(CloseableHttpAsyncClientBase.java:64)
            at java.lang.Thread.run(Unknown Source)

To fix it in file elasticsearch.yml parameter network.host must be 0.0.0.0.
When you start ES you will see:
when parameter network.host is default:
[2018-07-24T15:20:27,680][INFO ][o.e.h.n.Netty4HttpServerTransport] [hP3lXl_] publish_address {127.0.0.1:9200}, bound_addresses {127.0.0.1:9200}, {[::1]:9200}
or
when parameter network.host is 0.0.0.0t:
[2018-07-24T15:22:48,750][INFO ][o.e.h.n.Netty4HttpServerTransport] [hP3lXl_] publish_address {172.21.80.54:9200}, bound_addresses {[::]:9200}
[2018-07-24T15:22:48,750][INFO ][o.e.n.Node ] [hP3lXl_] started

Also you can check:
c:\temp>netstat -na | find "9200"
...
TCP [::1]:9200 [::]:0 LISTENING

or

c:\temp>netstat -na | find "9200"
...
TCP [::]:9200 [::]:0 LISTENING

This topic was automatically closed 28 days after the last reply. New replies are no longer allowed.