Skip to content

Commit

Permalink
Fix for CB-EXT to accept multiple ES IPs and Ports (#174)
Browse files Browse the repository at this point in the history
Added handling for multiple IPs for Elastic Search
  • Loading branch information
sreeragksgh authored Feb 15, 2023
1 parent 2cc5c07 commit 6b5b503
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 22 deletions.
24 changes: 12 additions & 12 deletions src/main/java/org/sunbird/common/util/CbExtServerProperties.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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() {
Expand Down Expand Up @@ -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() {
Expand Down
24 changes: 18 additions & 6 deletions src/main/java/org/sunbird/core/config/EsConfig.java
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand Down
6 changes: 2 additions & 4 deletions src/main/resources/application.properties
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 6b5b503

Please sign in to comment.