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
This commit is contained in:
Stéphane Nicoll
2025-11-18 23:03:25 +01:00
parent b1d78efbf3
commit ae4bf3e501
4 changed files with 21 additions and 20 deletions
@@ -341,7 +341,7 @@ To take full control over the client's configuration, define a javadoc:co.elasti
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.
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.
You can further tune how javadoc:co.elastic.clients.transport.rest5_client.low_level.sniffer.Sniffer[] is configured, as shown in the following example:
[configprops,yaml]
@@ -350,12 +350,11 @@ spring:
elasticsearch:
restclient:
sniffer:
enabled: true
interval: "10m"
delay-after-failure: "30s"
----
To disable auto-configuration of the Sniffer, set configprop:spring.elasticsearch.restclient.sniffer.enabled[] to `false`.
[[data.nosql.elasticsearch.connecting-using-rest.javaapiclient]]
@@ -163,7 +163,7 @@ public class ElasticsearchProperties {
/**
* Whether the sniffer is enabled.
*/
private boolean enabled = true;
private boolean enabled;
/**
* Interval between consecutive ordinary sniff executions.
@@ -139,7 +139,7 @@ class ElasticsearchRestClientConfigurations {
@Configuration(proxyBeanMethods = false)
@ConditionalOnClass(Sniffer.class)
@ConditionalOnSingleCandidate(Rest5Client.class)
@ConditionalOnProperty(name = "spring.elasticsearch.restclient.sniffer.enabled", matchIfMissing = true)
@ConditionalOnProperty(name = "spring.elasticsearch.restclient.sniffer.enabled")
static class RestClientSnifferConfiguration {
@Bean
@@ -257,8 +257,13 @@ class ElasticsearchRestClientAutoConfigurationTests {
}
@Test
void configureShouldCreateSnifferUsingRest5Client() {
this.contextRunner.run((context) -> {
void configureShouldNotCreateSnifferUsingRest5ClientByDefault() {
this.contextRunner.run((context) -> assertThat(context).doesNotHaveBean(Sniffer.class));
}
@Test
void configureWithSnifferEnabled() {
this.contextRunner.withPropertyValues("spring.elasticsearch.restclient.sniffer.enabled=true").run((context) -> {
assertThat(context).hasSingleBean(Sniffer.class);
assertThat(context.getBean(Sniffer.class)).hasFieldOrPropertyWithValue("restClient",
context.getBean(Rest5Client.class));
@@ -272,7 +277,8 @@ class ElasticsearchRestClientAutoConfigurationTests {
@Test
void configureWithCustomSnifferSettings() {
this.contextRunner
.withPropertyValues("spring.elasticsearch.restclient.sniffer.interval=180s",
.withPropertyValues("spring.elasticsearch.restclient.sniffer.enabled=true",
"spring.elasticsearch.restclient.sniffer.interval=180s",
"spring.elasticsearch.restclient.sniffer.delay-after-failure=30s")
.run((context) -> {
assertThat(context).hasSingleBean(Sniffer.class);
@@ -287,18 +293,14 @@ class ElasticsearchRestClientAutoConfigurationTests {
@Test
void configureWhenCustomSnifferShouldBackOff() {
Sniffer customSniffer = mock(Sniffer.class);
this.contextRunner.withBean(Sniffer.class, () -> customSniffer).run((context) -> {
assertThat(context).hasSingleBean(Sniffer.class);
Sniffer sniffer = context.getBean(Sniffer.class);
assertThat(sniffer).isSameAs(customSniffer);
then(customSniffer).shouldHaveNoInteractions();
});
}
@Test
void configureWithSnifferDisabled() {
this.contextRunner.withPropertyValues("spring.elasticsearch.restclient.sniffer.enabled=false")
.run((context) -> assertThat(context).doesNotHaveBean(Sniffer.class));
this.contextRunner.withPropertyValues("spring.elasticsearch.restclient.sniffer.enabled=true")
.withBean(Sniffer.class, () -> customSniffer)
.run((context) -> {
assertThat(context).hasSingleBean(Sniffer.class);
Sniffer sniffer = context.getBean(Sniffer.class);
assertThat(sniffer).isSameAs(customSniffer);
then(customSniffer).shouldHaveNoInteractions();
});
}
@Test