Skip to content

Commit

Permalink
Add capacity to use multiple URIs
Browse files Browse the repository at this point in the history
  • Loading branch information
tuser4198 authored and VanRoy committed May 17, 2019
1 parent 71b3421 commit 47fd3ed
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import java.util.concurrent.TimeUnit;
import java.util.stream.Collectors;

import com.google.common.collect.Lists;
import io.searchbox.client.JestClient;
import io.searchbox.client.JestClientFactory;
import io.searchbox.client.config.HttpClientConfig;
Expand Down Expand Up @@ -69,19 +70,19 @@ public class ElasticsearchJestAutoConfiguration implements DisposableBean {
@ConditionalOnMissingBean(JestClient.class)
@ConditionalOnMissingClass("org.elasticsearch.node.Node")
public JestClient client() {
return createJestClient(properties.getUri());
return createJestClient(properties.getUris());
}

@Bean
@ConditionalOnMissingBean(JestClient.class)
@ConditionalOnClass(Node.class)
public JestClient testClient() throws NodeValidationException {

if (StringUtils.isEmpty(properties.getUri())) {
if (CollectionUtils.isEmpty(properties.getUris())) {
int httpPort = createInternalNode();
return createJestClient("http://localhost:" + httpPort);
return createJestClient(Lists.newArrayList("http://localhost:" + httpPort));
} else {
return createJestClient(properties.getUri());
return createJestClient(properties.getUris());
}
}

Expand All @@ -103,12 +104,12 @@ public void destroy() throws Exception {

/**
* Create Jest client with URI
* @param uri URI of Elasticsearch
* @param uris URI list of Elasticsearch
* @return JestClient
*/
private JestClient createJestClient(String uri) {
private JestClient createJestClient(List<String> uris) {

HttpClientConfig.Builder builder = new HttpClientConfig.Builder(uri)
HttpClientConfig.Builder builder = new HttpClientConfig.Builder(uris)
.maxTotalConnection(properties.getMaxTotalConnection())
.defaultMaxTotalConnectionPerRoute(properties.getDefaultMaxTotalConnectionPerRoute())
.maxConnectionIdleTime(properties.getMaxConnectionIdleTime(), TimeUnit.MILLISECONDS)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,17 @@

import org.springframework.boot.context.properties.ConfigurationProperties;

import java.util.Collections;
import java.util.List;

/**
* Jest Elasticsearch properties.
* @author Julien Roy
*/
@ConfigurationProperties(prefix = "spring.data.jest")
public class ElasticsearchJestProperties {

private String uri;
private List<String> uris;
private String username;
private String password;

Expand All @@ -30,12 +33,16 @@ public Proxy getProxy() {
return this.proxy;
}

public String getUri() {
return uri;
public List<String> getUris() {
return uris;
}

public void setUris(List<String> uris) {
this.uris = uris;
}

public void setUri(String uri) {
this.uri = uri;
this.uris = Collections.singletonList(uri);
}

public String getUsername() {
Expand Down

0 comments on commit 47fd3ed

Please sign in to comment.