Skip to content

Commit ae4bf3e

Browse files
committed
Disable Elasticsearch client's sniffer by default
This commit switches the auto-configuration of Elasticsearch to not contribute a client's sniffer by default. This matches the best practices as describe in https://www.elastic.co/blog/elasticsearch-sniffing-best-practices-what-when-why-how Closes gh-48155
1 parent b1d78ef commit ae4bf3e

File tree

4 files changed

+21
-20
lines changed

4 files changed

+21
-20
lines changed

documentation/spring-boot-docs/src/docs/antora/modules/reference/pages/data/nosql.adoc

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -341,7 +341,7 @@ To take full control over the client's configuration, define a javadoc:co.elasti
341341

342342

343343

344-
Additionally, a javadoc:co.elastic.clients.transport.rest5_client.low_level.sniffer.Sniffer[] is auto-configured to automatically discover nodes from a running Elasticsearch cluster and set them on the javadoc:co.elastic.clients.transport.rest5_client.low_level.Rest5Client[] bean.
344+
Additionally, a javadoc:co.elastic.clients.transport.rest5_client.low_level.sniffer.Sniffer[] can be auto-configured to automatically discover nodes from a running Elasticsearch cluster and set them on the javadoc:co.elastic.clients.transport.rest5_client.low_level.Rest5Client[] bean.
345345
You can further tune how javadoc:co.elastic.clients.transport.rest5_client.low_level.sniffer.Sniffer[] is configured, as shown in the following example:
346346

347347
[configprops,yaml]
@@ -350,12 +350,11 @@ spring:
350350
elasticsearch:
351351
restclient:
352352
sniffer:
353+
enabled: true
353354
interval: "10m"
354355
delay-after-failure: "30s"
355356
----
356357

357-
To disable auto-configuration of the Sniffer, set configprop:spring.elasticsearch.restclient.sniffer.enabled[] to `false`.
358-
359358

360359

361360
[[data.nosql.elasticsearch.connecting-using-rest.javaapiclient]]

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -163,7 +163,7 @@ public static class Sniffer {
163163
/**
164164
* Whether the sniffer is enabled.
165165
*/
166-
private boolean enabled = true;
166+
private boolean enabled;
167167

168168
/**
169169
* Interval between consecutive ordinary sniff executions.

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -139,7 +139,7 @@ Rest5Client elasticsearchRestClient(Rest5ClientBuilder restClientBuilder) {
139139
@Configuration(proxyBeanMethods = false)
140140
@ConditionalOnClass(Sniffer.class)
141141
@ConditionalOnSingleCandidate(Rest5Client.class)
142-
@ConditionalOnProperty(name = "spring.elasticsearch.restclient.sniffer.enabled", matchIfMissing = true)
142+
@ConditionalOnProperty(name = "spring.elasticsearch.restclient.sniffer.enabled")
143143
static class RestClientSnifferConfiguration {
144144

145145
@Bean

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

Lines changed: 17 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -257,8 +257,13 @@ void configureWithCustomSocketKeepAlive() {
257257
}
258258

259259
@Test
260-
void configureShouldCreateSnifferUsingRest5Client() {
261-
this.contextRunner.run((context) -> {
260+
void configureShouldNotCreateSnifferUsingRest5ClientByDefault() {
261+
this.contextRunner.run((context) -> assertThat(context).doesNotHaveBean(Sniffer.class));
262+
}
263+
264+
@Test
265+
void configureWithSnifferEnabled() {
266+
this.contextRunner.withPropertyValues("spring.elasticsearch.restclient.sniffer.enabled=true").run((context) -> {
262267
assertThat(context).hasSingleBean(Sniffer.class);
263268
assertThat(context.getBean(Sniffer.class)).hasFieldOrPropertyWithValue("restClient",
264269
context.getBean(Rest5Client.class));
@@ -272,7 +277,8 @@ void configureShouldCreateSnifferUsingRest5Client() {
272277
@Test
273278
void configureWithCustomSnifferSettings() {
274279
this.contextRunner
275-
.withPropertyValues("spring.elasticsearch.restclient.sniffer.interval=180s",
280+
.withPropertyValues("spring.elasticsearch.restclient.sniffer.enabled=true",
281+
"spring.elasticsearch.restclient.sniffer.interval=180s",
276282
"spring.elasticsearch.restclient.sniffer.delay-after-failure=30s")
277283
.run((context) -> {
278284
assertThat(context).hasSingleBean(Sniffer.class);
@@ -287,18 +293,14 @@ void configureWithCustomSnifferSettings() {
287293
@Test
288294
void configureWhenCustomSnifferShouldBackOff() {
289295
Sniffer customSniffer = mock(Sniffer.class);
290-
this.contextRunner.withBean(Sniffer.class, () -> customSniffer).run((context) -> {
291-
assertThat(context).hasSingleBean(Sniffer.class);
292-
Sniffer sniffer = context.getBean(Sniffer.class);
293-
assertThat(sniffer).isSameAs(customSniffer);
294-
then(customSniffer).shouldHaveNoInteractions();
295-
});
296-
}
297-
298-
@Test
299-
void configureWithSnifferDisabled() {
300-
this.contextRunner.withPropertyValues("spring.elasticsearch.restclient.sniffer.enabled=false")
301-
.run((context) -> assertThat(context).doesNotHaveBean(Sniffer.class));
296+
this.contextRunner.withPropertyValues("spring.elasticsearch.restclient.sniffer.enabled=true")
297+
.withBean(Sniffer.class, () -> customSniffer)
298+
.run((context) -> {
299+
assertThat(context).hasSingleBean(Sniffer.class);
300+
Sniffer sniffer = context.getBean(Sniffer.class);
301+
assertThat(sniffer).isSameAs(customSniffer);
302+
then(customSniffer).shouldHaveNoInteractions();
303+
});
302304
}
303305

304306
@Test

0 commit comments

Comments
 (0)