Add ConditionalOnGrpcClientEnabled flag

This adds a coarse-grained conditional guard that will disable the
client autoconfiguration if `spring.grpc.client.enabled` is set to
false or the `io.grpc:grpc-stub` module is not on the classpath.

Signed-off-by: Chris Bono <chris.bono@gmail.com>
This commit is contained in:
Chris Bono
2025-02-18 18:10:37 -06:00
committed by Dave Syer
parent 1b782256b1
commit 2790a8a027
7 changed files with 106 additions and 1 deletions

View File

@@ -18,6 +18,7 @@
|spring.grpc.client.default-channel.ssl.bundle | | SSL bundle name.
|spring.grpc.client.default-channel.ssl.enabled | | Whether to enable SSL support. Enabled automatically if "bundle" is provided unless specified otherwise.
|spring.grpc.client.default-channel.user-agent | | The custom User-Agent for the channel.
|spring.grpc.client.enabled | `+++true+++` | Whether to enable client autoconfiguration.
|spring.grpc.client.observations.enabled | `+++true+++` | Whether to enable Observations on the client.
|spring.grpc.server.address | | The address to bind to. could be a host:port combination or a pseudo URL like static://host:port. Can not be set if host or port are set independently.
|spring.grpc.server.exception-handling.enabled | `+++true+++` | Whether to enable user-defined global exception handling on the gRPC server.

View File

@@ -0,0 +1,41 @@
/*
* Copyright 2023-2024 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.grpc.autoconfigure.client;
import io.grpc.stub.AbstractStub;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.context.annotation.Conditional;
/**
* {@link Conditional @Conditional} that only matches when the {@code io.grpc:grpc-stub}
* module is in the classpath and the {@code spring.grpc.client.enabled} property is not
* explicitly set to {@code false}.
*
* @author Freeman
* @author Chris Bono
*/
@Retention(RetentionPolicy.RUNTIME)
@Target({ ElementType.TYPE, ElementType.METHOD })
@ConditionalOnClass(AbstractStub.class)
@ConditionalOnProperty(prefix = "spring.grpc.client", name = "enabled", matchIfMissing = true)
public @interface ConditionalOnGrpcClientEnabled {
}

View File

@@ -32,6 +32,7 @@ import io.grpc.DecompressorRegistry;
import io.grpc.ManagedChannelBuilder;
@AutoConfiguration
@ConditionalOnGrpcClientEnabled
@EnableConfigurationProperties(GrpcClientProperties.class)
@Import({ GrpcCodecConfiguration.class, ClientInterceptorsConfiguration.class,
GrpcChannelFactoryConfigurations.ShadedNettyChannelFactoryConfiguration.class,

View File

@@ -26,6 +26,7 @@ import org.springframework.grpc.client.GlobalClientInterceptor;
@AutoConfiguration(
afterName = "org.springframework.boot.actuate.autoconfigure.observation.ObservationAutoConfiguration")
@ConditionalOnGrpcClientEnabled
@ConditionalOnClass({ ObservationRegistry.class, ObservationGrpcClientInterceptor.class })
@ConditionalOnBean(ObservationRegistry.class)
@ConditionalOnProperty(name = "spring.grpc.client.observation.enabled", havingValue = "true", matchIfMissing = true)

View File

@@ -35,6 +35,12 @@
"description": "Whether to enable Observations on the server.",
"defaultValue": true
},
{
"name": "spring.grpc.client.enabled",
"type": "java.lang.Boolean",
"description": "Whether to enable client autoconfiguration.",
"defaultValue": true
},
{
"name": "spring.grpc.client.observations.enabled",
"type": "java.lang.Boolean",

View File

@@ -51,6 +51,7 @@ import io.grpc.CompressorRegistry;
import io.grpc.DecompressorRegistry;
import io.grpc.ManagedChannelBuilder;
import io.grpc.netty.NettyChannelBuilder;
import io.grpc.stub.AbstractStub;
/**
* Tests for {@link GrpcClientAutoConfiguration}.
@@ -65,6 +66,32 @@ class GrpcClientAutoConfigurationTests {
.withConfiguration(AutoConfigurations.of(GrpcClientAutoConfiguration.class, SslAutoConfiguration.class));
}
@Test
void whenGrpcStubNotOnClasspathThenAutoConfigurationIsSkipped() {
this.contextRunner()
.withClassLoader(new FilteredClassLoader(AbstractStub.class))
.run((context) -> assertThat(context).doesNotHaveBean(GrpcClientAutoConfiguration.class));
}
@Test
void whenClientEnabledPropertySetFalseThenAutoConfigurationIsSkipped() {
this.contextRunner()
.withPropertyValues("spring.grpc.client.enabled=false")
.run((context) -> assertThat(context).doesNotHaveBean(GrpcClientAutoConfiguration.class));
}
@Test
void whenClientEnabledPropertyNotSetThenAutoConfigurationIsNotSkipped() {
this.contextRunner().run((context) -> assertThat(context).hasSingleBean(GrpcClientAutoConfiguration.class));
}
@Test
void whenClientEnabledPropertySetTrueThenAutoConfigurationIsNotSkipped() {
this.contextRunner()
.withPropertyValues("spring.grpc.client.enabled=true")
.run((context) -> assertThat(context).hasSingleBean(GrpcClientAutoConfiguration.class));
}
@Test
void whenHasUserDefinedCredentialsProviderDoesNotAutoConfigureBean() {
ChannelCredentialsProvider customCredentialsProvider = mock(ChannelCredentialsProvider.class);

View File

@@ -16,6 +16,7 @@
package org.springframework.grpc.autoconfigure.client;
import io.grpc.stub.AbstractStub;
import io.micrometer.core.instrument.binder.grpc.ObservationGrpcClientInterceptor;
import io.micrometer.observation.ObservationRegistry;
import org.junit.jupiter.api.Test;
@@ -75,6 +76,33 @@ class GrpcClientObservationAutoConfigurationTests {
.run(context -> assertThat(context).doesNotHaveBean(GrpcClientObservationAutoConfiguration.class));
}
@Test
void whenClientEnabledPropertyNotSetThenAutoConfigNotSkipped() {
this.validContextRunner()
.run(context -> assertThat(context).hasSingleBean(GrpcClientObservationAutoConfiguration.class));
}
@Test
void whenClientEnabledPropertySetTrueThenAutoConfigIsNotSkipped() {
this.validContextRunner()
.withPropertyValues("spring.grpc.client.enabled=true")
.run(context -> assertThat(context).hasSingleBean(GrpcClientObservationAutoConfiguration.class));
}
@Test
void whenClientEnabledPropertySetFalseThenAutoConfigIsSkipped() {
this.validContextRunner()
.withPropertyValues("spring.grpc.client.enabled=false")
.run(context -> assertThat(context).doesNotHaveBean(GrpcClientObservationAutoConfiguration.class));
}
@Test
void whenGrpcStubNotOnClasspathThenAutoConfigIsSkipped() {
this.validContextRunner()
.withClassLoader(new FilteredClassLoader(AbstractStub.class))
.run(context -> assertThat(context).doesNotHaveBean(GrpcClientObservationAutoConfiguration.class));
}
@Test
void whenAllConditionsAreMetThenInterceptorConfiguredAsExpected() {
this.validContextRunner().run((context) -> {
@@ -86,4 +114,4 @@ class GrpcClientObservationAutoConfigurationTests {
});
}
}
}