# RestHighLevel Client : Failed to connect to localhost port 9200: Connection refused

**URL:** <https://discuss.elastic.co/t/resthighlevel-client-failed-to-connect-to-localhost-port-9200-connection-refused/230310>\
**Category:** Elasticsearch\
**Created:** [April 29, 2020, 6:23am UTC](https://discuss.elastic.co/t/resthighlevel-client-failed-to-connect-to-localhost-port-9200-connection-refused/230310 "2020-04-29T06:23:40Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![Amit\_Porwal](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/amit_porwal/32/67342_2.png) [@Amit\_Porwal](https://discuss.elastic.co/u/Amit_Porwal)\
**Post date:** [April 29, 2020, 6:23am UTC](https://discuss.elastic.co/t/resthighlevel-client-failed-to-connect-to-localhost-port-9200-connection-refused/230310/1 "2020-04-29T06:23:41Z")

</div>

Learning ELK stack.I had spent ample of time on it, haven't find any solution yet.Any help would be greatly appreciated.

I just referred to this article to create this basic demo.  
[https://www.journaldev.com/18148/spring-boot-elasticsearch](https://www.journaldev.com/18148/spring-boot-elasticsearch)

```auto
 2020-04-29 11:32:49.752 INFO 13276 --- [main] c.j.elasticsearch.Elastic6Application : Starting Elastic6Application on YP1009833LT with PID 13276 (D:\ELK\spring-boot-elasticsearch\spring-boot-elasticsearch\target\classes started by amit.porwal in D:\ELK\spring-boot-elasticsearch\spring-boot-elasticsearch)
2020-04-29 11:32:49.757 INFO 13276 --- [main] c.j.elasticsearch.Elastic6Application : No active profile set, falling back to default profiles: default
2020-04-29 11:32:49.842 INFO 13276 --- [main] ationConfigEmbeddedWebApplicationContext : Refreshing org.springframework.boot.context.embedded.AnnotationConfigEmbeddedWebApplicationContext@80ec1f8: startup date [Wed Apr 29 11:32:49 IST 2020]; root of context hierarchy
2020-04-29 11:32:51.088 INFO 13276 --- [main] s.b.c.e.t.TomcatEmbeddedServletContainer : Tomcat initialized with port(s): 8080 (http)
2020-04-29 11:32:51.099 INFO 13276 --- [main] o.apache.catalina.core.StandardService : Starting service [Tomcat]
2020-04-29 11:32:51.100 INFO 13276 --- [main] org.apache.catalina.core.StandardEngine : Starting Servlet Engine: Apache Tomcat/8.5.23
2020-04-29 11:32:51.224 INFO 13276 --- [ost-startStop-1] o.a.c.c.C.[Tomcat].[localhost].[/] : Initializing Spring embedded WebApplicationContext
2020-04-29 11:32:51.224 INFO 13276 --- [ost-startStop-1] o.s.web.context.ContextLoader : Root WebApplicationContext: initialization completed in 1387 ms
2020-04-29 11:32:51.374 INFO 13276 --- [ost-startStop-1] o.s.b.w.servlet.ServletRegistrationBean : Mapping servlet: 'dispatcherServlet' to [/]
2020-04-29 11:32:51.377 INFO 13276 --- [ost-startStop-1] o.s.b.w.servlet.FilterRegistrationBean : Mapping filter: 'characterEncodingFilter' to: [/*]
2020-04-29 11:32:51.377 INFO 13276 --- [ost-startStop-1] o.s.b.w.servlet.FilterRegistrationBean : Mapping filter: 'hiddenHttpMethodFilter' to: [/*]
2020-04-29 11:32:51.377 INFO 13276 --- [ost-startStop-1] o.s.b.w.servlet.FilterRegistrationBean : Mapping filter: 'httpPutFormContentFilter' to: [/*]
2020-04-29 11:32:51.377 INFO 13276 --- [ost-startStop-1] o.s.b.w.servlet.FilterRegistrationBean : Mapping filter: 'requestContextFilter' to: [/*]
2020-04-29 11:32:51.969 INFO 13276 --- [main] s.w.s.m.m.a.RequestMappingHandlerAdapter : Looking for @ControllerAdvice: org.springframework.boot.context.embedded.AnnotationConfigEmbeddedWebApplicationContext@80ec1f8: startup date [Wed Apr 29 11:32:49 IST 2020]; root of context hierarchy
2020-04-29 11:32:52.029 INFO 13276 --- [main] s.w.s.m.m.a.RequestMappingHandlerMapping : Mapped "{[/books],methods=[POST]}" onto public com.journaldev.elasticsearch.bean.Book com.journaldev.elasticsearch.controller.BookController.insertBook(com.journaldev.elasticsearch.bean.Book) throws java.lang.Exception
2020-04-29 11:32:52.029 INFO 13276 --- [main] s.w.s.m.m.a.RequestMappingHandlerMapping : Mapped "{[/books/{id}],methods=[PUT]}" onto public java.util.Map<java.lang.String, java.lang.Object> com.journaldev.elasticsearch.controller.BookController.updateBookById(com.journaldev.elasticsearch.bean.Book,java.lang.String)
2020-04-29 11:32:52.029 INFO 13276 --- [main] s.w.s.m.m.a.RequestMappingHandlerMapping : Mapped "{[/books/{id}],methods=[GET]}" onto public java.util.Map<java.lang.String, java.lang.Object> com.journaldev.elasticsearch.controller.BookController.getBookById(java.lang.String)
2020-04-29 11:32:52.030 INFO 13276 --- [main] s.w.s.m.m.a.RequestMappingHandlerMapping : Mapped "{[/books/{id}],methods=[DELETE]}" onto public void com.journaldev.elasticsearch.controller.BookController.deleteBookById(java.lang.String)
2020-04-29 11:32:52.031 INFO 13276 --- [main] s.w.s.m.m.a.RequestMappingHandlerMapping : Mapped "{[/error]}" onto public org.springframework.http.ResponseEntity<java.util.Map<java.lang.String, java.lang.Object>> org.springframework.boot.autoconfigure.web.BasicErrorController.error(javax.servlet.http.HttpServletRequest)
2020-04-29 11:32:52.032 INFO 13276 --- [main] s.w.s.m.m.a.RequestMappingHandlerMapping : Mapped "{[/error],produces=[text/html]}" onto public org.springframework.web.servlet.ModelAndView org.springframework.boot.autoconfigure.web.BasicErrorController.errorHtml(javax.servlet.http.HttpServletRequest,javax.servlet.http.HttpServletResponse)
2020-04-29 11:32:52.063 INFO 13276 --- [main] o.s.w.s.handler.SimpleUrlHandlerMapping : Mapped URL path [/webjars/**] onto handler of type [class org.springframework.web.servlet.resource.ResourceHttpRequestHandler]
2020-04-29 11:32:52.063 INFO 13276 --- [main] o.s.w.s.handler.SimpleUrlHandlerMapping : Mapped URL path [/**] onto handler of type [class org.springframework.web.servlet.resource.ResourceHttpRequestHandler]
2020-04-29 11:32:52.102 INFO 13276 --- [main] o.s.w.s.handler.SimpleUrlHandlerMapping : Mapped URL path [/**/favicon.ico] onto handler of type [class org.springframework.web.servlet.resource.ResourceHttpRequestHandler]
2020-04-29 11:32:52.233 INFO 13276 --- [main] o.s.j.e.a.AnnotationMBeanExporter : Registering beans for JMX exposure on startup
2020-04-29 11:32:52.265 INFO 13276 --- [main] s.b.c.e.t.TomcatEmbeddedServletContainer : Tomcat started on port(s): 8080 (http)
2020-04-29 11:32:52.268 INFO 13276 --- [main] c.j.elasticsearch.Elastic6Application : Started Elastic6Application in 2.747 seconds (JVM running for 3.079)

```

Below is my pom.xml

```auto
    <?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
	<modelVersion>4.0.0</modelVersion>

	<groupId>com.journaldev.spring</groupId>
	<artifactId>spring-boot-elasticsearch</artifactId>
	<version>0.0.1-SNAPSHOT</version>
	<packaging>jar</packaging>

	<name>spring-boot-elasticsearch</name>
	<description>Spring Boot ElasticSearch Project</description>

	<parent>
		<groupId>org.springframework.boot</groupId>
		<artifactId>spring-boot-starter-parent</artifactId>
		<version>1.5.9.RELEASE</version>
		<relativePath/>
	</parent>

	<properties>
		<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
		<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
		<java.version>1.8</java.version>
	</properties>

	<dependencies>
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter</artifactId>
		</dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-test</artifactId>
			<scope>test</scope>
		</dependency>

		<!-- Elasticsearch Dependencies -->

		<dependency>
			<groupId>org.elasticsearch</groupId>
			<artifactId>elasticsearch</artifactId>
			<version>6.1.2</version>
		</dependency>

		<dependency>
			<groupId>org.elasticsearch.client</groupId>
			<artifactId>elasticsearch-rest-high-level-client</artifactId>
			<version>6.1.2</version>
		</dependency>

		<dependency>
			<groupId>org.apache.logging.log4j</groupId>
			<artifactId>log4j-api</artifactId>
			<version>2.8.2</version>
		</dependency>

		<dependency>
			<groupId>org.apache.logging.log4j</groupId>
			<artifactId>log4j-core</artifactId>
			<version>2.8.2</version>
		</dependency>

        <dependency>
            <groupId>org.elasticsearch.client</groupId>
            <artifactId>elasticsearch-rest-client</artifactId>
            <version>6.1.2</version>
        </dependency>

        <dependency>
            <groupId>org.elasticsearch.client</groupId>
            <artifactId>elasticsearch-rest-client-sniffer</artifactId>
            <version>6.1.2</version>
        </dependency>

	</dependencies>

	<build>
		<plugins>
			<plugin>
				<groupId>org.springframework.boot</groupId>
				<artifactId>spring-boot-maven-plugin</artifactId>
			</plugin>
		</plugins>
	</build>
</project>

```

also my elasticconfig file

```auto
    package com.journaldev.elasticsearch.config;

import org.apache.http.HttpHost;
import org.elasticsearch.client.RestClient;
import org.elasticsearch.client.RestHighLevelClient;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.beans.factory.config.AbstractFactoryBean;
import org.springframework.context.annotation.Configuration;

@Configuration
public class ElasticSearchConfiguration extends AbstractFactoryBean<RestHighLevelClient> {

    private static final Logger logger = LoggerFactory.getLogger(ElasticSearchConfiguration.class);
    @Value("${spring.data.elasticsearch.cluster-nodes}")
    private String clusterNodes;
    @Value("${spring.data.elasticsearch.cluster-name}")
    private String clusterName;
    private RestHighLevelClient restHighLevelClient;

    
    @Override
    public void destroy() {
        try {
            if (restHighLevelClient != null) {
                restHighLevelClient.close();
            }
        } catch (final Exception e) {
            logger.error("Error closing ElasticSearch client: ", e);
        }
    }

    @Override
    public Class<RestHighLevelClient> getObjectType() {
        return RestHighLevelClient.class;
    }

    @Override
    public boolean isSingleton() {
        return false;
    }

    @Override
    public RestHighLevelClient createInstance() {
        return buildClient();
    }

    private RestHighLevelClient buildClient() {
        try {
            restHighLevelClient = new RestHighLevelClient(
                    RestClient.builder(
                            new HttpHost("localhost", 9200, "http"),
                            new HttpHost("localhost", 9201, "http")));
        } catch (Exception e) {
            logger.error(e.getMessage());
        }
        return restHighLevelClient;
    }

}

```

My Dao service

```auto
    public Book insertBook(Book book){
    	book.setId(book.getId());
        Map<String, Object> dataMap = objectMapper.convertValue(book, Map.class);
        IndexRequest indexRequest = new IndexRequest(INDEX, TYPE, book.getId())
                .source(dataMap);
        try {
            IndexResponse response = restHighLevelClient.index(indexRequest);
        } catch(ElasticsearchException e) {
        	System.out.println(e.getDetailedMessage());
            
        } catch (java.io.IOException ex){
            System.out.println(ex.getLocalizedMessage());
            System.out.println(ex);
        }
        return book;
    }

```

---

<div class="post-metadata">

**Author:** ![dadoonet](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/dadoonet/32/137187_2.png) [@dadoonet](https://discuss.elastic.co/u/dadoonet)\
**Post date:** [April 29, 2020, 6:57am UTC](https://discuss.elastic.co/t/resthighlevel-client-failed-to-connect-to-localhost-port-9200-connection-refused/230310/2 "2020-04-29T06:57:39Z")

</div>

What are elasticsearch logs?

---

<div class="post-metadata">

**Author:** ![Amit\_Porwal](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/amit_porwal/32/67342_2.png) [@Amit\_Porwal](https://discuss.elastic.co/u/Amit_Porwal)\
**Post date:** [April 29, 2020, 8:18am UTC](https://discuss.elastic.co/t/resthighlevel-client-failed-to-connect-to-localhost-port-9200-connection-refused/230310/3 "2020-04-29T08:18:18Z")

</div>

ElasticSearch Logs ??? I may sound naive but do I also need to download the elastic search project from [here](https://www.elastic.co/downloads/elasticsearch) and separately and run the .bat file to start the server . I think the dependency in pom will take care of it ?

P.S - I really appreciate your quick respsonse.

---

<div class="post-metadata">

**Author:** ![dadoonet](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/dadoonet/32/137187_2.png) [@dadoonet](https://discuss.elastic.co/u/dadoonet)\
**Post date:** [April 29, 2020, 10:15am UTC](https://discuss.elastic.co/t/resthighlevel-client-failed-to-connect-to-localhost-port-9200-connection-refused/230310/4 "2020-04-29T10:15:57Z")

</div>

Elasticsearch is a server. If you don't start it the same way you would start MySQL for example, the client won't be able to connect to it.

You can also start easily an elastic cluster from [cloud.elastic.co](http://cloud.elastic.co) (14 days for free).

---

<div class="post-metadata">

**Author:** ![Amit\_Porwal](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/amit_porwal/32/67342_2.png) [@Amit\_Porwal](https://discuss.elastic.co/u/Amit_Porwal)\
**Post date:** [April 30, 2020, 6:58am UTC](https://discuss.elastic.co/t/resthighlevel-client-failed-to-connect-to-localhost-port-9200-connection-refused/230310/5 "2020-04-30T06:58:00Z")

</div>

Hey, that solved my problem.Thanks for that.Hope you are doing good amid this pandemic ?

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/elastic/original/3X/1/a/1ac57faf039f6b580b3f104ef42a2a89e41014de.png) [@system](https://discuss.elastic.co/u/system)\
**Post date:** [May 28, 2020, 6:58am UTC](https://discuss.elastic.co/t/resthighlevel-client-failed-to-connect-to-localhost-port-9200-connection-refused/230310/6 "2020-05-28T06:58:07Z")

</div>

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