From 6b5b50342df4f98bfb07b983be635e5458248199 Mon Sep 17 00:00:00 2001 From: Sreerag K S <58926794+sreeragksgh@users.noreply.github.com> Date: Wed, 15 Feb 2023 17:26:39 +0530 Subject: [PATCH] Fix for CB-EXT to accept multiple ES IPs and Ports (#174) Added handling for multiple IPs for Elastic Search --- .../common/util/CbExtServerProperties.java | 24 +++++++++---------- .../org/sunbird/core/config/EsConfig.java | 24 ++++++++++++++----- src/main/resources/application.properties | 6 ++--- 3 files changed, 32 insertions(+), 22 deletions(-) diff --git a/src/main/java/org/sunbird/common/util/CbExtServerProperties.java b/src/main/java/org/sunbird/common/util/CbExtServerProperties.java index c1c669e45..53873b28b 100644 --- a/src/main/java/org/sunbird/common/util/CbExtServerProperties.java +++ b/src/main/java/org/sunbird/common/util/CbExtServerProperties.java @@ -62,8 +62,8 @@ public class CbExtServerProperties { @Value("${sb.api.key}") private String sbApiKey; - @Value("${es.host}") - private String esHost; + @Value("${es.host.list}") + private String esHostList; @Value("${es.port}") private String esPort; @@ -344,8 +344,8 @@ public class CbExtServerProperties { @Value("${sb.data.sync.path}") private String lmsDataSyncPath; - @Value("${sb.es.host}") - private String sbEsHost; + @Value("${sb.es.host.list}") + private String sbEsHostList; @Value("${sb.es.port}") private String sbEsPort; @@ -598,12 +598,12 @@ public void setSbApiKey(String sbApiKey) { this.sbApiKey = sbApiKey; } - public String getEsHost() { - return esHost; + public String[] getEsHostList() { + return esHostList.split(",", -1); } - public void setEsHost(String esHost) { - this.esHost = esHost; + public void setEsHostList(String esHostList) { + this.esHostList = esHostList; } public String getEsPort() { @@ -1353,12 +1353,12 @@ public void setEsAutoCompleteIncludeFields(String esAutoCompleteIncludeFields) { this.esAutoCompleteIncludeFields = esAutoCompleteIncludeFields; } - public String getSbEsHost() { - return sbEsHost; + public String[] getSbEsHostList() { + return sbEsHostList.split(",", -1); } - public void setSbEsHost(String sbEsHost) { - this.sbEsHost = sbEsHost; + public void setSbEsHostList(String sbEsHost) { + this.sbEsHostList = sbEsHostList; } public String getSbEsPort() { diff --git a/src/main/java/org/sunbird/core/config/EsConfig.java b/src/main/java/org/sunbird/core/config/EsConfig.java index 1bdbedaf9..2d1d27ad9 100644 --- a/src/main/java/org/sunbird/core/config/EsConfig.java +++ b/src/main/java/org/sunbird/core/config/EsConfig.java @@ -12,31 +12,43 @@ import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.sunbird.common.util.CbExtServerProperties; +import org.springframework.util.StringUtils; +import org.sunbird.core.logger.CbExtLogger; + +import java.util.List; + @Configuration public class EsConfig { + private CbExtLogger logger = new CbExtLogger(getClass().getName()); @Autowired CbExtServerProperties configuration; @Bean(name = "esClient", destroyMethod = "close") public RestHighLevelClient getCbEsRestClient(CbExtServerProperties configuration) { - return createRestClient(configuration.getEsHost(), configuration.getEsPort(), configuration.getEsUser(), + return createRestClient(configuration.getEsHostList(), configuration.getEsUser(), configuration.getEsPassword()); } @Bean(name = "sbEsClient", destroyMethod = "close") public RestHighLevelClient getSbESRestClient(CbExtServerProperties configuration) { - return createRestClient(configuration.getSbEsHost(), configuration.getSbEsPort(), configuration.getSbEsUser(), + return createRestClient(configuration.getSbEsHostList(), configuration.getSbEsUser(), configuration.getSbEsPassword()); } - private RestHighLevelClient createRestClient(String host, String port, String user, String password) { + private RestHighLevelClient createRestClient(String[] hosts, String user, String password) { final CredentialsProvider credentialsProvider = new BasicCredentialsProvider(); credentialsProvider.setCredentials(AuthScope.ANY, new UsernamePasswordCredentials(user, password)); - RestClientBuilder builder = RestClient.builder(new HttpHost(host, Integer.parseInt(port))) - .setHttpClientConfigCallback( - httpClientBuilder -> httpClientBuilder.setDefaultCredentialsProvider(credentialsProvider)); + HttpHost[] httpHosts = new HttpHost[hosts.length]; + for (int i = 0; i < httpHosts.length; i++) { + String hostIp = hosts[i].split(":")[0]; + String hostPort = hosts[i].split(":")[1]; + httpHosts[i] = new HttpHost(hostIp, Integer.parseInt(hostPort)); + } + + RestClientBuilder builder = RestClient.builder(httpHosts).setHttpClientConfigCallback( + httpClientBuilder -> httpClientBuilder.setDefaultCredentialsProvider(credentialsProvider)); return new RestHighLevelClient(builder); } diff --git a/src/main/resources/application.properties b/src/main/resources/application.properties index a009db28d..754725fcd 100644 --- a/src/main/resources/application.properties +++ b/src/main/resources/application.properties @@ -43,14 +43,12 @@ sb.api.key=apiKey #Elastic search config es.auth.enabled=false -es.host=localhost -es.port=9200 +es.host.list=localhost:9200 es.username= es.password= #Sunbird Elasitc Search -sb.es.host=localhost -sb.es.port=9200 +sb.es.host.list=localhost:9200 sb.es.username= sb.es.password= sb.es.user.profile.index=user_alias