Browse Source

Merge pull request #32051 from puppylpg

* pr/32051:
  Polish "Add socketKeepAlive configuration property for Elasticsearch"
  Add socketKeepAlive configuration property for Elasticsearch

Closes gh-32051
pull/32112/head
Stephane Nicoll 4 years ago
parent
commit
bb07eec4ed
  1. 13
      spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/elasticsearch/ElasticsearchProperties.java
  2. 3
      spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/elasticsearch/ElasticsearchRestClientConfigurations.java
  3. 16
      spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/elasticsearch/ElasticsearchRestClientAutoConfigurationTests.java

13
spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/elasticsearch/ElasticsearchProperties.java

@ -57,6 +57,11 @@ public class ElasticsearchProperties { @@ -57,6 +57,11 @@ public class ElasticsearchProperties {
*/
private Duration socketTimeout = Duration.ofSeconds(30);
/**
* Whether to enable socket keep alive between client and Elasticsearch.
*/
private boolean socketKeepAlive = false;
/**
* Prefix added to the path of every request sent to Elasticsearch.
*/
@ -104,6 +109,14 @@ public class ElasticsearchProperties { @@ -104,6 +109,14 @@ public class ElasticsearchProperties {
this.socketTimeout = socketTimeout;
}
public boolean isSocketKeepAlive() {
return this.socketKeepAlive;
}
public void setSocketKeepAlive(boolean socketKeepAlive) {
this.socketKeepAlive = socketKeepAlive;
}
public String getPathPrefix() {
return this.pathPrefix;
}

3
spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/elasticsearch/ElasticsearchRestClientConfigurations.java

@ -27,6 +27,7 @@ import org.apache.http.auth.UsernamePasswordCredentials; @@ -27,6 +27,7 @@ import org.apache.http.auth.UsernamePasswordCredentials;
import org.apache.http.client.config.RequestConfig;
import org.apache.http.impl.client.BasicCredentialsProvider;
import org.apache.http.impl.nio.client.HttpAsyncClientBuilder;
import org.apache.http.impl.nio.reactor.IOReactorConfig;
import org.elasticsearch.client.RestClient;
import org.elasticsearch.client.RestClientBuilder;
import org.elasticsearch.client.sniff.Sniffer;
@ -155,6 +156,8 @@ class ElasticsearchRestClientConfigurations { @@ -155,6 +156,8 @@ class ElasticsearchRestClientConfigurations {
@Override
public void customize(HttpAsyncClientBuilder builder) {
builder.setDefaultCredentialsProvider(new PropertiesCredentialsProvider(this.properties));
map.from(this.properties::isSocketKeepAlive).to((keepAlive) -> builder
.setDefaultIOReactorConfig(IOReactorConfig.custom().setSoKeepAlive(keepAlive).build()));
}
@Override

16
spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/elasticsearch/ElasticsearchRestClientAutoConfigurationTests.java

@ -183,6 +183,22 @@ class ElasticsearchRestClientAutoConfigurationTests { @@ -183,6 +183,22 @@ class ElasticsearchRestClientAutoConfigurationTests {
});
}
@Test
void configureWithNoSocketKeepAliveApplyDefault() {
RestClient client = RestClient.builder(new HttpHost("localhost", 9201, "http")).build();
assertThat(client.getHttpClient()).extracting("connmgr.ioReactor.config.soKeepAlive").isEqualTo(Boolean.FALSE);
}
@Test
void configureWithCustomSocketKeepAlive() {
this.contextRunner.withPropertyValues("spring.elasticsearch.socket-keep-alive=true").run((context) -> {
assertThat(context).hasSingleBean(RestClient.class);
RestClient client = context.getBean(RestClient.class);
assertThat(client.getHttpClient()).extracting("connmgr.ioReactor.config.soKeepAlive")
.isEqualTo(Boolean.TRUE);
});
}
@Test
void configureWithoutSnifferLibraryShouldNotCreateSniffer() {
this.contextRunner.withClassLoader(new FilteredClassLoader("org.elasticsearch.client.sniff"))

Loading…
Cancel
Save