Home > front end >  "connection refused" error when java application and elasticsearch are on the same docker
"connection refused" error when java application and elasticsearch are on the same docker

Time:10-25

there.

when my java application is running on my local computer and working with elasticsearch docker, my java application can connect to elasticsearche, but my java application and elasticsearch running to same docker network and when my java app try connect to elasticsearch then launch the error "connection refused", can you help me?

I made a small java application to explain my problem. https://github.com/semihshn/elastic-search-example

CodePudding user response:

Edit: We had a call with the creator of the post and saw that the problem was the Configuration file of elasticsearch.

The application was using the default RestHighLevelClient instead of the object we have created. The object we created was not a bean. After fixing this issue, the application worked as expected.

The code for configuration is gone from this;

@Data
@Component
@Slf4j
public class ElasticClient {

    private final RestHighLevelClient elasticSearchClient;

    public ElasticClient(@Value("${elastic.search.domain}") String elasticSearchDomain) {

        log.info(String.format("elastichsearch host: %s", elasticSearchDomain));

        this.elasticSearchClient = new RestHighLevelClient(
                RestClient.builder(
                        new HttpHost(elasticSearchDomain, 9200, "http"),
                        new HttpHost(elasticSearchDomain, 9201, "http")
                ));

    }
}

To this;

@Getter
@Setter
@Configuration
@ConfigurationProperties("elastic.search")
public class ElasticClient {

    private String domain;

    @Bean
    public RestHighLevelClient restHighLevelClient() {
        return new RestHighLevelClient(
            RestClient.builder(
                    new HttpHost(domain, 9200, "http")
            ));
    }
}

Also special thanks to @David Maze for giving information about hostname usage.

  • Related