Move Redis health and metrics auto-config into spring-boot-data-redis

This commit is contained in:
Andy Wilkinson
2025-05-20 16:09:23 +01:00
committed by Phillip Webb
parent 3e72525828
commit 5d131754cc
15 changed files with 41 additions and 44 deletions

View File

@@ -0,0 +1,60 @@
/*
* Copyright 2012-2025 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.boot.data.redis.actuate.health.autoconfigure;
import org.springframework.beans.factory.config.ConfigurableListableBeanFactory;
import org.springframework.boot.actuate.autoconfigure.health.CompositeHealthContributorConfiguration;
import org.springframework.boot.actuate.autoconfigure.health.ConditionalOnEnabledHealthIndicator;
import org.springframework.boot.actuate.health.HealthContributor;
import org.springframework.boot.actuate.health.HealthIndicator;
import org.springframework.boot.autoconfigure.AutoConfiguration;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.boot.autoconfigure.condition.ConditionalOnBean;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.data.redis.actuate.health.RedisHealthIndicator;
import org.springframework.boot.data.redis.autoconfigure.RedisAutoConfiguration;
import org.springframework.context.annotation.Bean;
import org.springframework.data.redis.connection.RedisConnectionFactory;
/**
* {@link EnableAutoConfiguration Auto-configuration} for {@link RedisHealthIndicator}.
*
* @author Christian Dupuis
* @author Richard Santana
* @author Stephane Nicoll
* @author Mark Paluch
* @since 4.0.0
*/
@AutoConfiguration(after = { RedisAutoConfiguration.class, RedisReactiveHealthContributorAutoConfiguration.class })
@ConditionalOnClass({ RedisConnectionFactory.class, HealthIndicator.class, ConditionalOnEnabledHealthIndicator.class })
@ConditionalOnBean(RedisConnectionFactory.class)
@ConditionalOnEnabledHealthIndicator("redis")
public class RedisHealthContributorAutoConfiguration
extends CompositeHealthContributorConfiguration<RedisHealthIndicator, RedisConnectionFactory> {
RedisHealthContributorAutoConfiguration() {
super(RedisHealthIndicator::new);
}
@Bean
@ConditionalOnMissingBean(name = { "redisHealthIndicator", "redisHealthContributor" })
public HealthContributor redisHealthContributor(ConfigurableListableBeanFactory beanFactory) {
return createContributor(beanFactory, RedisConnectionFactory.class);
}
}

View File

@@ -0,0 +1,64 @@
/*
* Copyright 2012-2025 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.boot.data.redis.actuate.health.autoconfigure;
import reactor.core.publisher.Flux;
import org.springframework.beans.factory.config.ConfigurableListableBeanFactory;
import org.springframework.boot.actuate.autoconfigure.health.CompositeReactiveHealthContributorConfiguration;
import org.springframework.boot.actuate.autoconfigure.health.ConditionalOnEnabledHealthIndicator;
import org.springframework.boot.actuate.health.ReactiveHealthContributor;
import org.springframework.boot.actuate.health.ReactiveHealthIndicator;
import org.springframework.boot.autoconfigure.AutoConfiguration;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.boot.autoconfigure.condition.ConditionalOnBean;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.data.redis.actuate.health.RedisReactiveHealthIndicator;
import org.springframework.boot.data.redis.autoconfigure.RedisReactiveAutoConfiguration;
import org.springframework.context.annotation.Bean;
import org.springframework.data.redis.connection.ReactiveRedisConnectionFactory;
/**
* {@link EnableAutoConfiguration Auto-configuration} for
* {@link RedisReactiveHealthIndicator}.
*
* @author Christian Dupuis
* @author Richard Santana
* @author Stephane Nicoll
* @author Mark Paluch
* @since 4.0.0
*/
@AutoConfiguration(after = RedisReactiveAutoConfiguration.class)
@ConditionalOnClass({ ReactiveRedisConnectionFactory.class, Flux.class, ReactiveHealthIndicator.class,
ConditionalOnEnabledHealthIndicator.class })
@ConditionalOnBean(ReactiveRedisConnectionFactory.class)
@ConditionalOnEnabledHealthIndicator("redis")
public class RedisReactiveHealthContributorAutoConfiguration extends
CompositeReactiveHealthContributorConfiguration<RedisReactiveHealthIndicator, ReactiveRedisConnectionFactory> {
RedisReactiveHealthContributorAutoConfiguration() {
super(RedisReactiveHealthIndicator::new);
}
@Bean
@ConditionalOnMissingBean(name = { "redisHealthIndicator", "redisHealthContributor" })
public ReactiveHealthContributor redisHealthContributor(ConfigurableListableBeanFactory beanFactory) {
return createContributor(beanFactory, ReactiveRedisConnectionFactory.class);
}
}

View File

@@ -0,0 +1,20 @@
/*
* Copyright 2012-2025 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
/**
* Auto-configuration for Redis health integration using Spring Data Redis.
*/
package org.springframework.boot.data.redis.actuate.health.autoconfigure;

View File

@@ -0,0 +1,56 @@
/*
* Copyright 2012-2025 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.boot.data.redis.actuate.metrics.autoconfigure;
import io.lettuce.core.RedisClient;
import io.lettuce.core.metrics.MicrometerCommandLatencyRecorder;
import io.lettuce.core.metrics.MicrometerOptions;
import io.micrometer.core.instrument.MeterRegistry;
import org.springframework.boot.autoconfigure.AutoConfiguration;
import org.springframework.boot.autoconfigure.condition.ConditionalOnBean;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.data.redis.autoconfigure.ClientResourcesBuilderCustomizer;
import org.springframework.boot.data.redis.autoconfigure.RedisAutoConfiguration;
import org.springframework.context.annotation.Bean;
/**
* Auto-configuration for Lettuce metrics.
*
* @author Antonin Arquey
* @author Yanming Zhou
* @since 4.0.0
*/
@AutoConfiguration(before = RedisAutoConfiguration.class,
afterName = "org.springframework.boot.metrics.autoconfigure.CompositeMeterRegistryAutoConfiguration")
@ConditionalOnClass({ RedisClient.class, MicrometerCommandLatencyRecorder.class, MeterRegistry.class })
@ConditionalOnBean(MeterRegistry.class)
public class LettuceMetricsAutoConfiguration {
@Bean
@ConditionalOnMissingBean
MicrometerOptions micrometerOptions() {
return MicrometerOptions.create();
}
@Bean
ClientResourcesBuilderCustomizer lettuceMetrics(MeterRegistry meterRegistry, MicrometerOptions options) {
return (client) -> client.commandLatencyRecorder(new MicrometerCommandLatencyRecorder(meterRegistry, options));
}
}

View File

@@ -0,0 +1,20 @@
/*
* Copyright 2012-2025 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
/**
* Auto-configuration for Redis metrics.
*/
package org.springframework.boot.data.redis.actuate.metrics.autoconfigure;

View File

@@ -1,6 +1,12 @@
{
"groups": [],
"properties": [
{
"name": "management.health.redis.enabled",
"type": "java.lang.Boolean",
"description": "Whether to enable Redis health check.",
"defaultValue": true
},
{
"name": "spring.data.redis.repositories.enabled",
"type": "java.lang.Boolean",

View File

@@ -1,3 +1,6 @@
org.springframework.boot.data.redis.actuate.health.autoconfigure.RedisHealthContributorAutoConfiguration
org.springframework.boot.data.redis.actuate.health.autoconfigure.RedisReactiveHealthContributorAutoConfiguration
org.springframework.boot.data.redis.actuate.metrics.autoconfigure.LettuceMetricsAutoConfiguration
org.springframework.boot.data.redis.autoconfigure.RedisAutoConfiguration
org.springframework.boot.data.redis.autoconfigure.RedisReactiveAutoConfiguration
org.springframework.boot.data.redis.autoconfigure.RedisRepositoriesAutoConfiguration

View File

@@ -0,0 +1,56 @@
/*
* Copyright 2012-2025 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.boot.data.redis.actuate.health.autoconfigure;
import org.junit.jupiter.api.Test;
import org.springframework.boot.actuate.autoconfigure.health.HealthContributorAutoConfiguration;
import org.springframework.boot.autoconfigure.AutoConfigurations;
import org.springframework.boot.data.redis.actuate.health.RedisHealthIndicator;
import org.springframework.boot.data.redis.actuate.health.RedisReactiveHealthIndicator;
import org.springframework.boot.data.redis.autoconfigure.RedisAutoConfiguration;
import org.springframework.boot.test.context.runner.ApplicationContextRunner;
import org.springframework.boot.testsupport.classpath.ClassPathExclusions;
import static org.assertj.core.api.Assertions.assertThat;
/**
* Tests for {@link RedisHealthContributorAutoConfiguration}.
*
* @author Phillip Webb
*/
@ClassPathExclusions({ "reactor-core*.jar", "lettuce-core*.jar" })
class RedisHealthContributorAutoConfigurationTests {
private final ApplicationContextRunner contextRunner = new ApplicationContextRunner()
.withConfiguration(AutoConfigurations.of(RedisAutoConfiguration.class,
RedisHealthContributorAutoConfiguration.class, HealthContributorAutoConfiguration.class));
@Test
void runShouldCreateIndicator() {
this.contextRunner.run((context) -> assertThat(context).hasSingleBean(RedisHealthIndicator.class)
.doesNotHaveBean(RedisReactiveHealthIndicator.class));
}
@Test
void runWhenDisabledShouldNotCreateIndicator() {
this.contextRunner.withPropertyValues("management.health.redis.enabled:false")
.run((context) -> assertThat(context).doesNotHaveBean(RedisHealthIndicator.class)
.doesNotHaveBean(RedisReactiveHealthIndicator.class));
}
}

View File

@@ -0,0 +1,62 @@
/*
* Copyright 2012-2025 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.boot.data.redis.actuate.health.autoconfigure;
import org.junit.jupiter.api.Test;
import org.springframework.boot.actuate.autoconfigure.health.HealthContributorAutoConfiguration;
import org.springframework.boot.autoconfigure.AutoConfigurations;
import org.springframework.boot.data.redis.actuate.health.RedisHealthIndicator;
import org.springframework.boot.data.redis.actuate.health.RedisReactiveHealthIndicator;
import org.springframework.boot.data.redis.autoconfigure.RedisAutoConfiguration;
import org.springframework.boot.test.context.runner.ApplicationContextRunner;
import static org.assertj.core.api.Assertions.assertThat;
/**
* Tests for {@link RedisReactiveHealthContributorAutoConfiguration}.
*
* @author Phillip Webb
*/
class RedisReactiveHealthContributorAutoConfigurationTests {
private final ApplicationContextRunner contextRunner = new ApplicationContextRunner()
.withConfiguration(AutoConfigurations.of(RedisAutoConfiguration.class,
RedisReactiveHealthContributorAutoConfiguration.class, HealthContributorAutoConfiguration.class));
@Test
void runShouldCreateIndicator() {
this.contextRunner.run((context) -> assertThat(context).hasSingleBean(RedisReactiveHealthIndicator.class)
.hasBean("redisHealthContributor"));
}
@Test
void runWithRegularIndicatorShouldOnlyCreateReactiveIndicator() {
this.contextRunner.withConfiguration(AutoConfigurations.of(RedisHealthContributorAutoConfiguration.class))
.run((context) -> assertThat(context).hasSingleBean(RedisReactiveHealthIndicator.class)
.hasBean("redisHealthContributor")
.doesNotHaveBean(RedisHealthIndicator.class));
}
@Test
void runWhenDisabledShouldNotCreateIndicator() {
this.contextRunner.withPropertyValues("management.health.redis.enabled:false")
.run((context) -> assertThat(context).doesNotHaveBean(RedisReactiveHealthIndicator.class)
.doesNotHaveBean("redisHealthContributor"));
}
}

View File

@@ -0,0 +1,102 @@
/*
* Copyright 2012-2025 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.boot.data.redis.actuate.metrics.autoconfigure;
import io.lettuce.core.metrics.MicrometerCommandLatencyRecorder;
import io.lettuce.core.metrics.MicrometerOptions;
import io.lettuce.core.resource.ClientResources;
import io.micrometer.core.instrument.simple.SimpleMeterRegistry;
import org.junit.jupiter.api.Test;
import org.springframework.boot.autoconfigure.AutoConfigurations;
import org.springframework.boot.data.redis.autoconfigure.RedisAutoConfiguration;
import org.springframework.boot.test.context.runner.ApplicationContextRunner;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.connection.lettuce.LettuceConnectionFactory;
import static org.assertj.core.api.Assertions.assertThat;
/**
* Tests for {@link LettuceMetricsAutoConfiguration}.
*
* @author Antonin Arquey
*/
class LettuceMetricsAutoConfigurationTests {
private final ApplicationContextRunner contextRunner = new ApplicationContextRunner()
.withConfiguration(AutoConfigurations.of(LettuceMetricsAutoConfiguration.class));
@Test
void whenThereIsAMeterRegistryThenCommandLatencyRecorderIsAdded() {
this.contextRunner.withBean(SimpleMeterRegistry.class)
.withConfiguration(AutoConfigurations.of(RedisAutoConfiguration.class))
.run((context) -> {
ClientResources clientResources = context.getBean(LettuceConnectionFactory.class).getClientResources();
assertThat(clientResources.commandLatencyRecorder())
.isInstanceOf(MicrometerCommandLatencyRecorder.class);
});
}
@Test
void autoConfiguredMicrometerOptionsUsesLettucesDefaults() {
this.contextRunner.withBean(SimpleMeterRegistry.class)
.withConfiguration(AutoConfigurations.of(RedisAutoConfiguration.class))
.run((context) -> {
MicrometerOptions micrometerOptions = context.getBean(MicrometerOptions.class);
assertThat(micrometerOptions.isEnabled()).isTrue();
assertThat(micrometerOptions.isHistogram()).isFalse();
assertThat(micrometerOptions.localDistinction()).isFalse();
assertThat(micrometerOptions.maxLatency()).isEqualTo(MicrometerOptions.DEFAULT_MAX_LATENCY);
assertThat(micrometerOptions.minLatency()).isEqualTo(MicrometerOptions.DEFAULT_MIN_LATENCY);
});
}
@Test
void whenUserDefinesAMicrometerOptionsBeanThenCommandLatencyRecorderUsesIt() {
this.contextRunner.withBean(SimpleMeterRegistry.class)
.withConfiguration(AutoConfigurations.of(RedisAutoConfiguration.class))
.withUserConfiguration(CustomMicrometerOptionsConfiguration.class)
.run((context) -> {
ClientResources clientResources = context.getBean(LettuceConnectionFactory.class).getClientResources();
assertThat(clientResources.commandLatencyRecorder())
.isInstanceOf(MicrometerCommandLatencyRecorder.class);
assertThat(clientResources.commandLatencyRecorder()).hasFieldOrPropertyWithValue("options",
context.getBean("customMicrometerOptions"));
});
}
@Test
void whenThereIsNoMeterRegistryThenClientResourcesCustomizationBacksOff() {
this.contextRunner.withConfiguration(AutoConfigurations.of(RedisAutoConfiguration.class)).run((context) -> {
ClientResources clientResources = context.getBean(LettuceConnectionFactory.class).getClientResources();
assertThat(clientResources.commandLatencyRecorder())
.isNotInstanceOf(MicrometerCommandLatencyRecorder.class);
});
}
@Configuration(proxyBeanMethods = false)
static class CustomMicrometerOptionsConfiguration {
@Bean
MicrometerOptions customMicrometerOptions() {
return MicrometerOptions.create();
}
}
}