From b1188992c3ca40a7ca2cfb9ac2e450c727def539 Mon Sep 17 00:00:00 2001 From: LE SAULNIER Kevin Date: Fri, 6 Dec 2024 09:03:26 +0100 Subject: [PATCH] fix: allow multiple hosts for ES Signed-off-by: LE SAULNIER Kevin --- .../server/elasticsearch/ESConfig.java | 54 ------------------- src/main/resources/application-local.yml | 14 ++--- .../elasticsearch/EmbeddedElasticsearch.java | 2 +- 3 files changed, 9 insertions(+), 61 deletions(-) delete mode 100644 src/main/java/com/powsybl/network/conversion/server/elasticsearch/ESConfig.java diff --git a/src/main/java/com/powsybl/network/conversion/server/elasticsearch/ESConfig.java b/src/main/java/com/powsybl/network/conversion/server/elasticsearch/ESConfig.java deleted file mode 100644 index 6e03951e..00000000 --- a/src/main/java/com/powsybl/network/conversion/server/elasticsearch/ESConfig.java +++ /dev/null @@ -1,54 +0,0 @@ -/* - Copyright (c) 2021, RTE (http://www.rte-france.com) - This Source Code Form is subject to the terms of the Mozilla Public - License, v. 2.0. If a copy of the MPL was not distributed with this - file, You can obtain one at http://mozilla.org/MPL/2.0/. - */ -package com.powsybl.network.conversion.server.elasticsearch; - -import org.springframework.beans.factory.annotation.Value; -import org.springframework.context.annotation.Configuration; -import org.springframework.data.elasticsearch.client.ClientConfiguration; -import org.springframework.data.elasticsearch.client.elc.ElasticsearchConfiguration; -import org.springframework.data.elasticsearch.repository.config.EnableElasticsearchRepositories; - -import java.util.Optional; - -/** - * A class to configure DB elasticsearch client for indexation - * - * @author Slimane Amar - */ - -@Configuration -@EnableElasticsearchRepositories -public class ESConfig extends ElasticsearchConfiguration { - - @Value("#{'${spring.data.elasticsearch.embedded:false}' ? 'localhost' : '${spring.data.elasticsearch.host}'}") - private String esHost; - - @Value("#{'${spring.data.elasticsearch.embedded:false}' ? '${spring.data.elasticsearch.embedded.port:}' : '${spring.data.elasticsearch.port}'}") - private int esPort; - - @Value("${spring.data.elasticsearch.client.timeout:60}") - int timeout; - - @Value("${spring.data.elasticsearch.username:#{null}}") - private Optional username; - - @Value("${spring.data.elasticsearch.password:#{null}}") - private Optional password; - - @Override - public ClientConfiguration clientConfiguration() { - var clientConfiguration = ClientConfiguration.builder() - .connectedTo(esHost + ":" + esPort) - .withConnectTimeout(timeout * 1000L).withSocketTimeout(timeout * 1000L); - - if (username.isPresent() && password.isPresent()) { - clientConfiguration.withBasicAuth(username.get(), password.get()); - } - - return clientConfiguration.build(); - } -} diff --git a/src/main/resources/application-local.yml b/src/main/resources/application-local.yml index f01c8887..52f2cc2b 100644 --- a/src/main/resources/application-local.yml +++ b/src/main/resources/application-local.yml @@ -4,12 +4,14 @@ server: spring: rabbitmq: addresses: localhost - data: - elasticsearch: - host: localhost - ## to fill if authentication is needed - # user: - # password: + elasticsearch: + uris: + - http://localhost:9200 + connection-timeout: 60s + socket-timeout: 60s + ## to fill if authentication is needed + # username: + # password: powsybl: services: network-store-server: diff --git a/src/test/java/com/powsybl/network/conversion/server/elasticsearch/EmbeddedElasticsearch.java b/src/test/java/com/powsybl/network/conversion/server/elasticsearch/EmbeddedElasticsearch.java index c6eda7da..51b98e76 100644 --- a/src/test/java/com/powsybl/network/conversion/server/elasticsearch/EmbeddedElasticsearch.java +++ b/src/test/java/com/powsybl/network/conversion/server/elasticsearch/EmbeddedElasticsearch.java @@ -44,7 +44,7 @@ public void postConstruct() { elasticsearchContainer.start(); System.setProperty("spring.data.elasticsearch.embedded", Boolean.toString(true)); - System.setProperty("spring.data.elasticsearch.embedded.port", Integer.toString(elasticsearchContainer.getMappedPort(9200))); + System.setProperty("spring.elasticsearch.uris", "localhost:".concat(Integer.toString(elasticsearchContainer.getMappedPort(9200)))); } @PreDestroy