Skip to content

Commit 21edd46

Browse files
author
liuhaibo
committed
Add keepalive configure for elasticsearch autoconfig.
1 parent adcd3ac commit 21edd46

File tree

3 files changed

+26
-0
lines changed

3 files changed

+26
-0
lines changed

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

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,11 @@ public class ElasticsearchProperties {
6262
*/
6363
private String pathPrefix;
6464

65+
/**
66+
* Whether to enable keepalive between client and Elasticsearch.
67+
*/
68+
private boolean keepalive = true;
69+
6570
private final Restclient restclient = new Restclient();
6671

6772
public List<String> getUris() {
@@ -112,6 +117,14 @@ public void setPathPrefix(String pathPrefix) {
112117
this.pathPrefix = pathPrefix;
113118
}
114119

120+
public boolean isKeepalive() {
121+
return this.keepalive;
122+
}
123+
124+
public void setKeepalive(boolean keepalive) {
125+
this.keepalive = keepalive;
126+
}
127+
115128
public Restclient getRestclient() {
116129
return this.restclient;
117130
}

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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
import org.apache.http.client.config.RequestConfig;
2828
import org.apache.http.impl.client.BasicCredentialsProvider;
2929
import org.apache.http.impl.nio.client.HttpAsyncClientBuilder;
30+
import org.apache.http.impl.nio.reactor.IOReactorConfig;
3031
import org.elasticsearch.client.RestClient;
3132
import org.elasticsearch.client.RestClientBuilder;
3233
import org.elasticsearch.client.sniff.Sniffer;
@@ -155,6 +156,8 @@ public void customize(RestClientBuilder builder) {
155156
@Override
156157
public void customize(HttpAsyncClientBuilder builder) {
157158
builder.setDefaultCredentialsProvider(new PropertiesCredentialsProvider(this.properties));
159+
map.from(this.properties::isKeepalive).whenTrue()
160+
.to(keepalive -> builder.setDefaultIOReactorConfig(IOReactorConfig.custom().setSoKeepAlive(keepalive).build()));
158161
}
159162

160163
@Override

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

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -183,6 +183,16 @@ void configureWithCustomPathPrefix() {
183183
});
184184
}
185185

186+
@Test
187+
void configureWithKeepalive() {
188+
this.contextRunner.withPropertyValues("spring.elasticsearch.keepalive=true").run(
189+
context -> {
190+
RestClient client = context.getBean(RestClient.class);
191+
assertThat(client.getHttpClient()).extracting("connmgr.ioReactor.config.soKeepAlive").isEqualTo(Boolean.TRUE);
192+
}
193+
);
194+
}
195+
186196
@Test
187197
void configureWithoutSnifferLibraryShouldNotCreateSniffer() {
188198
this.contextRunner.withClassLoader(new FilteredClassLoader("org.elasticsearch.client.sniff"))

0 commit comments

Comments
 (0)