ConnectionClosedException: Connection is closed exception in secure elastic search

java-version: 1.8
elastic-version: 7.10
Ubuntu: 18.04LTS

we created separate Machine for Elastic search 7.10.0 in Ubuntu.In the same machine ,we installed java1.8 and also install Kibana .we setup the security in elastic by using xpack security and elastic is password protected and working fine.

Now we establish Secure Elastic connection using Rest Client without ssl. After Establish connection, we perform successful operation for create index, search index and delete index. Below code working fine.

final CredentialsProvider credsProvider = new BasicCredentialsProvider();
credsProvider.setCredentials(AuthScope.ANY, new UsernamePasswordCredentials(elastic, elastic));

    // 3. Changes for add multiple IP address
    String[] hosts = elasticHost.split(",");
    
    HttpHost[] httpHosts = Arrays.stream(hosts)
            .map(host -> new HttpHost(host.trim(), elasticPort, "http"))
            .collect(Collectors.toList())
            .toArray(new HttpHost[hosts.length]);
    
    // 4. Build the low-level client
    RestClientBuilder builder = RestClient.builder(httpHosts)
            .setHttpClientConfigCallback(new HttpClientConfigCallback() {
                @Override
                public HttpAsyncClientBuilder customizeHttpClient(
                        HttpAsyncClientBuilder httpClientBuilder) {

                            // set Basic credentials
                            httpClientBuilder.setDefaultCredentialsProvider(credsProvider);
                            // set SSL context
                            return httpClientBuilder;//.setSSLContext(sslContext);
                        }
            });         
    // 5. Build the high-level client
    client = new RestHighLevelClient(builder);

But when we create two different machine in Ubuntu , one for java 1.8 and other one for Elasticsearch 7.10.0. After that, when we try to run above code in java 1.8 machine . its showing below exception:

org.apache.http.ConnectionClosedException: Connection is closed
at org.elasticsearch.client.RestClient.extractAndWrapCause(RestClient.java:855)
at org.elasticsearch.client.RestClient.performRequest(RestClient.java:275)
at org.elasticsearch.client.RestClient.performRequest(RestClient.java:262)
at org.elasticsearch.client.RestHighLevelClient.internalPerformRequest(RestHighLevelClient.java:1632)
at org.elasticsearch.client.RestHighLevelClient.performRequest(RestHighLevelClient.java:1602)
at org.elasticsearch.client.RestHighLevelClient.performRequestAndParseEntity(RestHighLevelClient.java:1572)
at org.elasticsearch.client.RestHighLevelClient.search(RestHighLevelClient.java:1088)
at testsecureelastic.TestSecureElasticConnection.getElasticConnectionOther(TestSecureElasticConnection.java:175)
at testsecureelastic.TestSecureElasticConnection.main(TestSecureElasticConnection.java:318)
Caused by: org.apache.http.ConnectionClosedException: Connection is closed
at org.apache.http.nio.protocol.HttpAsyncRequestExecutor.endOfInput(HttpAsyncRequestExecutor.java:356)
at org.apache.http.impl.nio.DefaultNHttpClientConnection.consumeInput(DefaultNHttpClientConnection.java:261)
at org.apache.http.impl.nio.client.InternalIODispatch.onInputReady(InternalIODispatch.java:81)
at org.apache.http.impl.nio.client.InternalIODispatch.onInputReady(InternalIODispatch.java:39)
at org.apache.http.impl.nio.reactor.AbstractIODispatch.inputReady(AbstractIODispatch.java:114)
at org.apache.http.impl.nio.reactor.BaseIOReactor.readable(BaseIOReactor.java:162)
at org.apache.http.impl.nio.reactor.AbstractIOReactor.processEvent(AbstractIOReactor.java:337)
at org.apache.http.impl.nio.reactor.AbstractIOReactor.processEvents(AbstractIOReactor.java:315)
at org.apache.http.impl.nio.reactor.AbstractIOReactor.execute(AbstractIOReactor.java:276)
at org.apache.http.impl.nio.reactor.BaseIOReactor.execute(BaseIOReactor.java:104)
at org.apache.http.impl.nio.reactor.AbstractMultiworkerIOReactor$Worker.run(AbstractMultiworkerIOReactor.java:591)
at java.lang.Thread.run(Thread.java:748)

when we run above java code in same instance (where java 1.8 and elastic 7.10 install) its run successfully.In same instance we use 9200 port.
but when we run in different instance (where java 1.8 machine is different and elastic 7.10.0 machine is different), we use 9300 port. its showing "connection closed" exception.

Please let me know,what i am missing .Thanks in advance.

Welcome to our community! :smiley:

I am not 100% sure I follow this, but when you talk to Elasticsearch via the Java client you need to be using port 9200. Port 9300 will not work.

@warkolm : our java machine and elastics machine is different. when we use port 9200 from java machine to connect elastic machine.its showing below exception:

java.net.ConnectException: Timeout connecting to [/xx.xx.x.xx:9200]
at org.elasticsearch.client.RestClient.extractAndWrapCause(RestClient.java:865)
at org.elasticsearch.client.RestClient.performRequest(RestClient.java:275)
at org.elasticsearch.client.RestClient.performRequest(RestClient.java:262)
at org.elasticsearch.client.RestHighLevelClient.internalPerformRequest(RestHighLevelClient.java:1632)
at org.elasticsearch.client.RestHighLevelClient.performRequest(RestHighLevelClient.java:1602)
at org.elasticsearch.client.RestHighLevelClient.performRequestAndParseEntity(RestHighLevelClient.java:1572)
at org.elasticsearch.client.RestHighLevelClient.search(RestHighLevelClient.java:1088)
at testsecureelastic.TestSecureElasticConnection.getElasticConnectionOther(TestSecureElasticConnection.java:174)
at testsecureelastic.TestSecureElasticConnection.main(TestSecureElasticConnection.java:318)
Caused by: java.net.ConnectException: Timeout connecting to [/xx.xx.x.xx:9200]
at org.apache.http.nio.pool.RouteSpecificPool.timeout(RouteSpecificPool.java:169)
at org.apache.http.nio.pool.AbstractNIOConnPool.requestTimeout(AbstractNIOConnPool.java:628)
at org.apache.http.nio.pool.AbstractNIOConnPool$InternalSessionRequestCallback.timeout(AbstractNIOConnPool.java:894)
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:351)
at org.apache.http.impl.nio.conn.PoolingNHttpClientConnectionManager.execute(PoolingNHttpClientConnectionManager.java:221)
at org.apache.http.impl.nio.client.CloseableHttpAsyncClientBase$1.run(CloseableHttpAsyncClientBase.java:64)
at java.lang.Thread.run(Thread.java:748)

Can you post your Elasticsearch logs from startup onwards please?

@warkolm :

[o.e.p.PluginsService ] [la-test-elastic-2] loaded module [x-pack-ilm]
[o.e.p.PluginsService ] [la-test-elastic-2] loaded module [x-pack-logstash]
[2020-12-08T07:04:12,429][INFO ][o.e.p.PluginsService ] [la-test-elastic-2] loaded module [x-pack-ml]
[2020-12-08T07:04:12,430][INFO ][o.e.p.PluginsService ] [la-test-elastic-2] loaded module [x-pack-monitoring]
[2020-12-08T07:04:12,431][INFO ][o.e.p.PluginsService ] [la-test-elastic-2] loaded module [x-pack-ql]
[2020-12-08T07:04:12,431][INFO ][o.e.p.PluginsService ] [la-test-elastic-2] loaded module [x-pack-rollup]
[2020-12-08T07:04:12,432][INFO ][o.e.p.PluginsService ] [la-test-elastic-2] loaded module [x-pack-security]
[2020-12-08T07:04:12,432][INFO ][o.e.p.PluginsService ] [la-test-elastic-2] loaded module [x-pack-sql]
[2020-12-08T07:04:12,432][INFO ][o.e.p.PluginsService ] [la-test-elastic-2] loaded module [x-pack-stack]
[2020-12-08T07:04:12,432][INFO ][o.e.p.PluginsService ] [la-test-elastic-2] loaded module [x-pack-voting-only-node]
[2020-12-08T07:04:12,433][INFO ][o.e.p.PluginsService ] [la-test-elastic-2] loaded module [x-pack-watcher]
[2020-12-08T07:04:12,434][INFO ][o.e.p.PluginsService ] [la-test-elastic-2] no plugins loaded
[2020-12-08T07:04:12,706][INFO ][o.e.e.NodeEnvironment ] [la-test-elastic-2] using [1] data paths, mounts [[/ (/dev/sda1)]], net usable_space [7.5gb], net total_space [9.5gb], types [ext4]
[2020-12-08T07:04:12,707][INFO ][o.e.e.NodeEnvironment ] [la-test-elastic-2] heap size [1gb], compressed ordinary object pointers [true]
[2020-12-08T07:04:12,956][INFO ][o.e.n.Node ] [la-test-elastic-2] node name [la-test-elastic-2], node ID [b8wQuQ6_SYqW226_BKn7TA], cluster name [elasticsearch], roles [transform, master, remote_cluster_client, data, ml, data_content, data_hot, data_warm, data_cold, ingest]
[2020-12-08T07:04:23,477][INFO ][o.e.x.m.p.l.CppLogMessageHandler] [la-test-elastic-2] [controller/3350] [Main.cc@114] controller (64 bit): Version 7.10.0 (Build ac991e2e31f99d) Copyright (c) 2020 Elasticsearch BV
[2020-12-08T07:04:24,597][INFO ][o.e.x.s.a.s.FileRolesStore] [la-test-elastic-2] parsed [0] roles from file [/etc/elasticsearch/roles.yml]
[2020-12-08T07:04:28,131][INFO ][o.e.t.NettyAllocator ] [la-test-elastic-2] creating NettyAllocator with the following configs: [name=unpooled, suggested_max_allocation_size=256kb, factors={es.unsafe.use_unpooled_allocator=null, g1gc_enabled=true, g1gc_region_size=1mb, heap_size=1gb}]
[2020-12-08T07:04:28,537][INFO ][o.e.d.DiscoveryModule ] [la-test-elastic-2] using discovery type [zen] and seed hosts providers [settings]
[2020-12-08T07:04:29,735][WARN ][o.e.g.DanglingIndicesState] [la-test-elastic-2] gateway.auto_import_dangling_indices is disabled, dangling indices will not be automatically detected or imported and must be managed manually
[2020-12-08T07:04:30,566][INFO ][o.e.n.Node ] [la-test-elastic-2] initialized
[2020-12-08T07:04:30,566][INFO ][o.e.n.Node ] [la-test-elastic-2] starting ...
[2020-12-08T07:04:31,014][INFO ][o.e.t.TransportService ] [la-test-elastic-2] publish_address {127.0.0.1:9300}, bound_addresses {[::1]:9300}, {127.0.0.1:9300}
[2020-12-08T07:04:31,498][WARN ][o.e.b.BootstrapChecks ] [la-test-elastic-2] the default discovery settings are unsuitable for production use; at least one of [discovery.seed_hosts, discovery.seed_providers, cluster.initial_master_nodes] must be configured
[2020-12-08T07:04:31,565][INFO ][o.e.c.c.ClusterBootstrapService] [la-test-elastic-2] no discovery configuration found, will perform best-effort cluster bootstrapping after [3s] unless existing master is discovered
[2020-12-08T07:04:34,569][INFO ][o.e.c.c.Coordinator ] [la-test-elastic-2] setting initial configuration to VotingConfiguration{b8wQuQ6_SYqW226_BKn7TA}
[2020-12-08T07:04:34,765][INFO ][o.e.c.s.MasterService ] [la-test-elastic-2] elected-as-master ([1] nodes joined)[{la-test-elastic-2}{b8wQuQ6_SYqW226_BKn7TA}{kZiqXvx_Q2emr32hRhnGYg}{127.0.0.1}{127.0.0.1:9300}{cdhilmrstw}{ml.machine_memory=2087587840, xpack.installed=true, transform.node=true, ml.max_open_jobs=20} elect leader, BECOME_MASTER_TASK, FINISH_ELECTION], term: 1, version: 1, delta: master node changed {previous , current [{la-test-elastic-2}{b8wQuQ6_SYqW226_BKn7TA}{kZiqXvx_Q2emr32hRhnGYg}{127.0.0.1}{127.0.0.1:9300}{cdhilmrstw}{ml.machine_memory=2087587840, xpack.installed=true, transform.node=true, ml.max_open_jobs=20}]}
[2020-12-08T07:04:34,837][INFO ][o.e.c.c.CoordinationState] [la-test-elastic-2] cluster UUID set to [5akVMLqFR0-vxTWFEcriPw]
[2020-12-08T07:04:34,882][INFO ][o.e.c.s.ClusterApplierService] [la-test-elastic-2] master node changed {previous , current [{la-test-elastic-2}{b8wQuQ6_SYqW226_BKn7TA}{kZiqXvx_Q2emr32hRhnGYg}{127.0.0.1}{127.0.0.1:9300}{cdhilmrstw}{ml.machine_memory=2087587840, xpack.installed=true, transform.node=true, ml.max_open_jobs=20}]}, term: 1, version: 1, reason: Publication{term=1, version=1}
[2020-12-08T07:04:34,986][INFO ][o.e.h.AbstractHttpServerTransport] [la-test-elastic-2] publish_address {127.0.0.1:9200}, bound_addresses {[::1]:9200}, {127.0.0.1:9200}
[2020-12-08T07:04:34,995][INFO ][o.e.n.Node ] [la-test-elastic-2] started
[2020-12-08T07:04:35,077][INFO ][o.e.x.c.t.IndexTemplateRegistry] [la-test-elastic-2] adding legacy template [.ml-anomalies-] for [ml], because it doesn't exist
[2020-12-08T07:04:35,087][INFO ][o.e.x.c.t.IndexTemplateRegistry] [la-test-elastic-2] adding legacy template [.ml-state] for [ml], because it doesn't exist
[2020-12-08T07:04:35,089][INFO ][o.e.x.c.t.IndexTemplateRegistry] [la-test-elastic-2] adding legacy template [.ml-config] for [ml], because it doesn't exist
[2020-12-08T07:04:35,103][INFO ][o.e.x.c.t.IndexTemplateRegistry] [la-test-elastic-2] adding legacy template [.ml-inference-000003] for [ml], because it doesn't exist
[2020-12-08T07:04:35,103][INFO ][o.e.x.c.t.IndexTemplateRegistry] [la-test-elastic-2] adding legacy template [.ml-meta] for [ml], because it doesn't exist
[2020-12-08T07:04:35,104][INFO ][o.e.x.c.t.IndexTemplateRegistry] [la-test-elastic-2] adding legacy template [.ml-notifications-000001] for [ml], because it doesn't exist
[2020-12-08T07:04:35,115][INFO ][o.e.x.c.t.IndexTemplateRegistry] [la-test-elastic-2] adding legacy template [.ml-stats] for [ml], because it doesn't exist
[2020-12-08T07:04:35,355][INFO ][o.e.g.GatewayService ] [la-test-elastic-2] recovered [0] indices into cluster_state
[2020-12-08T07:04:35,964][INFO ][o.e.c.m.MetadataIndexTemplateService] [la-test-elastic-2] adding template [.ml-notifications-000001] for index patterns [.ml-notifications-000001]
[2020-12-08T07:04:36,065][INFO ][o.e.c.m.MetadataIndexTemplateService] [la-test-elastic-2] adding template [.ml-meta] for index patterns [.ml-meta]
[2020-12-08T07:04:36,130][INFO ][o.e.c.m.MetadataIndexTemplateService] [la-test-elastic-2] adding template [.ml-inference-000003] for index patterns [.ml-inference-000003]
[2020-12-08T07:04:36,213][INFO ][o.e.c.m.MetadataIndexTemplateService] [la-test-elastic-2] adding template [.ml-stats] for index patterns [.ml-stats-]
[2020-12-08T07:04:36,274][INFO ][o.e.c.m.MetadataIndexTemplateService] [la-test-elastic-2] adding template [.ml-state] for index patterns [.ml-state
]
[2020-12-08T07:04:36,373][INFO ][o.e.c.m.MetadataIndexTemplateService] [la-test-elastic-2] adding template [.ml-config] for index patterns [.ml-config]
[2020-12-08T07:04:36,498][INFO ][o.e.c.m.MetadataIndexTemplateService] [la-test-elastic-2] adding template [.ml-anomalies-] for index patterns [.ml-anomalies-]
[2020-12-08T07:04:36,580][INFO ][o.e.c.m.MetadataIndexTemplateService] [la-test-elastic-2] adding component template [logs-mappings]
[2020-12-08T07:04:36,633][INFO ][o.e.c.m.MetadataIndexTemplateService] [la-test-elastic-2] adding component template [logs-settings]
[2020-12-08T07:04:36,703][INFO ][o.e.c.m.MetadataIndexTemplateService] [la-test-elastic-2] adding component template [metrics-settings]
[2020-12-08T07:04:36,760][INFO ][o.e.c.m.MetadataIndexTemplateService] [la-test-elastic-2] adding component template [synthetics-settings]
[2020-12-08T07:04:36,826][INFO ][o.e.c.m.MetadataIndexTemplateService] [la-test-elastic-2] adding component template [synthetics-mappings]
[2020-12-08T07:04:36,890][INFO ][o.e.c.m.MetadataIndexTemplateService] [la-test-elastic-2] adding component template [metrics-mappings]
[2020-12-08T07:04:37,060][INFO ][o.e.c.m.MetadataIndexTemplateService] [la-test-elastic-2] adding index template [.watch-history-12] for index patterns [.watcher-history-12
]
[2020-12-08T07:04:37,138][INFO ][o.e.c.m.MetadataIndexTemplateService] [la-test-elastic-2] adding index template [.triggered_watches] for index patterns [.triggered_watches*]
[2020-12-08T07:04:37,215][INFO ][o.e.c.m.MetadataIndexTemplateService] [la-test-elastic-2] adding index template [.watches] for index patterns [.watches*]
[2020-12-08T07:04:37,280][INFO ][o.e.c.m.MetadataIndexTemplateService] [la-test-elastic-2] adding index template [ilm-history] for index patterns [ilm-history-3*]
[2020-12-08T07:04:37,338][INFO ][o.e.c.m.MetadataIndexTemplateService] [la-test-elastic-2] adding index template [.slm-history] for index patterns [.slm-history-3*]
[2020-12-08T07:04:37,388][INFO ][o.e.c.m.MetadataIndexTemplateService] [la-test-elastic-2] adding template [.monitoring-alerts-7] for index patterns [.monitoring-alerts-7]
[2020-12-08T07:04:37,458][INFO ][o.e.c.m.MetadataIndexTemplateService] [la-test-elastic-2] adding template [.monitoring-es] for index patterns [.monitoring-es-7-]
[2020-12-08T07:04:37,519][INFO ][o.e.c.m.MetadataIndexTemplateService] [la-test-elastic-2] adding template [.monitoring-kibana] for index patterns [.monitoring-kibana-7-
]
[2020-12-08T07:04:37,594][INFO ][o.e.c.m.MetadataIndexTemplateService] [la-test-elastic-2] adding template [.monitoring-logstash] for index patterns [.monitoring-logstash-7-]
[2020-12-08T07:04:37,676][INFO ][o.e.c.m.MetadataIndexTemplateService] [la-test-elastic-2] adding template [.monitoring-beats] for index patterns [.monitoring-beats-7-
]
[2020-12-08T07:04:37,757][INFO ][o.e.c.m.MetadataIndexTemplateService] [la-test-elastic-2] adding index template [logs] for index patterns [logs--]
[2020-12-08T07:04:37,829][INFO ][o.e.c.m.MetadataIndexTemplateService] [la-test-elastic-2] adding index template [synthetics] for index patterns [synthetics--]
[2020-12-08T07:04:37,907][INFO ][o.e.c.m.MetadataIndexTemplateService] [la-test-elastic-2] adding index template [metrics] for index patterns [metrics--]
[2020-12-08T07:04:37,963][INFO ][o.e.x.i.a.TransportPutLifecycleAction] [la-test-elastic-2] adding index lifecycle policy [ml-size-based-ilm-policy]
[2020-12-08T07:04:38,055][INFO ][o.e.x.i.a.TransportPutLifecycleAction] [la-test-elastic-2] adding index lifecycle policy [metrics]
[2020-12-08T07:04:38,103][INFO ][o.e.x.i.a.TransportPutLifecycleAction] [la-test-elastic-2] adding index lifecycle policy [logs]
[2020-12-08T07:04:38,154][INFO ][o.e.x.i.a.TransportPutLifecycleAction] [la-test-elastic-2] adding index lifecycle policy [synthetics]
[2020-12-08T07:04:38,200][INFO ][o.e.x.i.a.TransportPutLifecycleAction] [la-test-elastic-2] adding index lifecycle policy [watch-history-ilm-policy]
[2020-12-08T07:04:38,263][INFO ][o.e.x.i.a.TransportPutLifecycleAction] [la-test-elastic-2] adding index lifecycle policy [ilm-history-ilm-policy]
[2020-12-08T07:04:38,309][INFO ][o.e.x.i.a.TransportPutLifecycleAction] [la-test-elastic-2] adding index lifecycle policy [slm-history-ilm-policy]
[2020-12-08T07:04:38,475][INFO ][o.e.l.LicenseService ] [la-test-elastic-2] license [e20e7697-37f4-4b7d-b244-a343f168bcf8] mode [basic] - valid
[2020-12-08T07:04:38,477][INFO ][o.e.x.s.s.SecurityStatusChangeListener] [la-test-elastic-2] Active license is now [BASIC]; Security is disabled
[2020-12-08T07:08:06,214][INFO ][o.e.n.Node ] [la-test-elastic-2] stopping ...
[2020-12-08T07:08:06,219][INFO ][o.e.x.w.WatcherService ] [la-test-elastic-2] stopping watch service, reason [shutdown initiated]
[2020-12-08T07:08:06,220][INFO ][o.e.x.w.WatcherLifeCycleService] [la-test-elastic-2] watcher has stopped and shutdown
[2020-12-08T07:08:06,219][INFO ][o.e.x.m.p.l.CppLogMessageHandler] [la-test-elastic-2] [controller/3350] [Main.cc@154] ML controller exiting
[2020-12-08T07:08:06,222][INFO ][o.e.x.m.p.NativeController] [la-test-elastic-2] Native controller process has stopped - no new native processes can be started
[2020-12-08T07:08:06,624][INFO ][o.e.n.Node ] [la-test-elastic-2] stopped
[2020-12-08T07:08:06,625][INFO ][o.e.n.Node ] [la-test-elastic-2] closing ...
[2020-12-08T07:08:06,645][INFO ][o.e.n.Node ] [la-test-elastic-2] closed

Elasticsearch is only listening to localhost, so you will not be able to connect to it from another host.

You need to look at the network.host setting - Network settings | Elasticsearch Guide [7.10] | Elastic

@warkolm : here elasticsearch.yml file setting

---------------------------------- Cluster -----------------------------------

Use a descriptive name for your cluster:

cluster.name: la-test-elastic-2

------------------------------------ Node ------------------------------------

Use a descriptive name for the node:

node.name: node-1

Add custom attributes to the node:

#node.attr.rack: r1

----------------------------------- Paths ------------------------------------

Path to directory where to store the data (separate multiple locations by comma):

path.data: /var/lib/elasticsearch

Path to log files:

path.logs: /var/log/elasticsearch

----------------------------------- Memory -----------------------------------

Lock the memory on startup:

#bootstrap.memory_lock: true

Make sure that the heap size is set to about half the memory available

on the system and that the owner of the process is allowed to use this

limit.

Elasticsearch performs poorly when the system is swapping the memory.

---------------------------------- Network -----------------------------------

Set the bind address to a specific IP (IPv4 or IPv6):

network.host: 0.0.0.0

Set a custom port for HTTP:

http.port: 9200

For more information, consult the network module documentation.

--------------------------------- Discovery ----------------------------------

Pass an initial list of hosts to perform discovery when this node is started:

The default list of hosts is ["127.0.0.1", "[::1]"]

discovery.seed_hosts: xx.xx.x.xx
xpack.security.enabled: true
xpack.security.transport.ssl.enabled: true
xpack.security.transport.ssl.verification_mode: certificate
xpack.security.transport.ssl.keystore.path: elastic-certificates.p12
xpack.security.transport.ssl.truststore.path: elastic-certificates.p12

Please format your code/logs/config using the </> button, or markdown style back ticks. It helps to make things easy to read which helps us help you :slight_smile:

we have connect Elasticsearch for another host using Transport client.Below code run from another host using 9300 port and its connect Elasticsearch.

            Settings settings = Settings.builder().put("cluster.name", clusterName).build();
            ESclient = new PreBuiltTransportClient(settings);
            String[] hosts = elasticHost.split(",");
            for (String host : hosts) {
                ESclient.addTransportAddress(new TransportAddress(InetAddress.getByName(host.trim()), elasticPort));
            }

But now we want to connect secure Elastic search using xpack.so we use Rest Client to connect secure Elasticsearch. Below code working fine in same instance.but when we try to estabish connection from another host its showing connection closed issue.

final CredentialsProvider credsProvider = new BasicCredentialsProvider();
         credsProvider.setCredentials(AuthScope.ANY, new UsernamePasswordCredentials("elastic", "elastic"));

    // 3. Changes for add multiple IP address
    String[] hosts = elasticHost.split(",");
    
    HttpHost[] httpHosts = Arrays.stream(hosts)
            .map(host -> new HttpHost(host.trim(), elasticPort, "http"))
            .collect(Collectors.toList())
            .toArray(new HttpHost[hosts.length]);
    
    // 4. Build the low-level client
    RestClientBuilder builder = RestClient.builder(httpHosts)
            .setHttpClientConfigCallback(new HttpClientConfigCallback() {
                @Override
                public HttpAsyncClientBuilder customizeHttpClient(
                        HttpAsyncClientBuilder httpClientBuilder) {

                            // set Basic credentials
                            httpClientBuilder.setDefaultCredentialsProvider(credsProvider);
                            // set SSL context
                            return httpClientBuilder;//.setSSLContext(sslContext);
                        }
            });
    // System.out.println("builder :" + builder.build().getNodes().toString());
    // 5. Build the high-level client
    client = new RestHighLevelClient(builder);

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