diff --git a/spring-grpc-core/src/main/java/org/springframework/grpc/server/GrpcServiceDiscoverer.java b/spring-grpc-core/src/main/java/org/springframework/grpc/server/GrpcServiceDiscoverer.java new file mode 100644 index 00000000..8d825421 --- /dev/null +++ b/spring-grpc-core/src/main/java/org/springframework/grpc/server/GrpcServiceDiscoverer.java @@ -0,0 +1,38 @@ +/* + * Copyright (c) 2024-2024 The gRPC-Spring 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 + * + * http://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.server; + +import java.util.List; + +import io.grpc.ServerServiceDefinition; + +/** + * Discovers {@link ServerServiceDefinition gRPC services} to be provided by the server. + * + * @author Michael (yidongnan@gmail.com) + * @author Chris Bono + */ +@FunctionalInterface +public interface GrpcServiceDiscoverer { + + /** + * Find gRPC services for the server to provide. + * @return list of services to add to the server - empty when no services available + */ + List findServices(); + +} diff --git a/spring-grpc-spring-boot-autoconfigure/src/main/java/org/springframework/grpc/autoconfigure/server/DefaultGrpcServiceDiscoverer.java b/spring-grpc-spring-boot-autoconfigure/src/main/java/org/springframework/grpc/autoconfigure/server/DefaultGrpcServiceDiscoverer.java new file mode 100644 index 00000000..a3e00952 --- /dev/null +++ b/spring-grpc-spring-boot-autoconfigure/src/main/java/org/springframework/grpc/autoconfigure/server/DefaultGrpcServiceDiscoverer.java @@ -0,0 +1,47 @@ +/* + * Copyright (c) 2016-2023 The gRPC-Spring 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 + * + * http://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.server; + +import java.util.List; + +import io.grpc.BindableService; +import io.grpc.ServerServiceDefinition; + +import org.springframework.beans.factory.ObjectProvider; +import org.springframework.grpc.server.GrpcServiceDiscoverer; + +/** + * The default {@link GrpcServiceDiscoverer} that finds all {@link BindableService} beans + * and configures and binds them. + * + * @author Michael (yidongnan@gmail.com) + * @author Chris Bono + */ +class DefaultGrpcServiceDiscoverer implements GrpcServiceDiscoverer { + + private final ObjectProvider grpcServicesProvider; + + DefaultGrpcServiceDiscoverer(ObjectProvider grpcServicesProvider) { + this.grpcServicesProvider = grpcServicesProvider; + } + + @Override + public List findServices() { + return grpcServicesProvider.orderedStream().map(BindableService::bindService).toList(); + } + +} diff --git a/spring-grpc-spring-boot-autoconfigure/src/main/java/org/springframework/grpc/autoconfigure/server/GrpcServerAutoConfiguration.java b/spring-grpc-spring-boot-autoconfigure/src/main/java/org/springframework/grpc/autoconfigure/server/GrpcServerAutoConfiguration.java index 103ec97b..70bbbc56 100644 --- a/spring-grpc-spring-boot-autoconfigure/src/main/java/org/springframework/grpc/autoconfigure/server/GrpcServerAutoConfiguration.java +++ b/spring-grpc-spring-boot-autoconfigure/src/main/java/org/springframework/grpc/autoconfigure/server/GrpcServerAutoConfiguration.java @@ -28,6 +28,7 @@ import org.springframework.context.annotation.Import; import org.springframework.grpc.autoconfigure.common.codec.GrpcCodecConfiguration; import org.springframework.grpc.server.GrpcServerFactory; +import org.springframework.grpc.server.GrpcServiceDiscoverer; import org.springframework.grpc.server.ServerBuilderCustomizer; import org.springframework.grpc.server.lifecycle.GrpcServerLifecycle; @@ -69,4 +70,10 @@ ServerBuilderCustomizers serverBuilderCustomizers(ObjectProvider bindableServicesProvider) { + return new DefaultGrpcServiceDiscoverer(bindableServicesProvider); + } + } diff --git a/spring-grpc-spring-boot-autoconfigure/src/main/java/org/springframework/grpc/autoconfigure/server/GrpcServerFactoryConfigurations.java b/spring-grpc-spring-boot-autoconfigure/src/main/java/org/springframework/grpc/autoconfigure/server/GrpcServerFactoryConfigurations.java index 72f5937c..9466a609 100644 --- a/spring-grpc-spring-boot-autoconfigure/src/main/java/org/springframework/grpc/autoconfigure/server/GrpcServerFactoryConfigurations.java +++ b/spring-grpc-spring-boot-autoconfigure/src/main/java/org/springframework/grpc/autoconfigure/server/GrpcServerFactoryConfigurations.java @@ -20,7 +20,6 @@ import javax.net.ssl.KeyManagerFactory; -import org.springframework.beans.factory.ObjectProvider; import org.springframework.boot.autoconfigure.condition.ConditionalOnBean; import org.springframework.boot.autoconfigure.condition.ConditionalOnClass; import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean; @@ -30,11 +29,11 @@ import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.grpc.server.GrpcServerFactory; +import org.springframework.grpc.server.GrpcServiceDiscoverer; import org.springframework.grpc.server.NettyGrpcServerFactory; import org.springframework.grpc.server.ServerBuilderCustomizer; import org.springframework.grpc.server.ShadedNettyGrpcServerFactory; -import io.grpc.BindableService; import io.grpc.CompressorRegistry; import io.grpc.DecompressorRegistry; import io.grpc.netty.NettyServerBuilder; @@ -54,7 +53,7 @@ static class ShadedNettyServerFactoryConfiguration { @Bean ShadedNettyGrpcServerFactory shadedNettyGrpcServerFactory(GrpcServerProperties properties, - ObjectProvider grpcServicesProvider, ServerBuilderCustomizers serverBuilderCustomizers, + GrpcServiceDiscoverer grpcServicesDiscoverer, ServerBuilderCustomizers serverBuilderCustomizers, SslBundles bundles) { ShadedNettyServerFactoryPropertyMapper mapper = new ShadedNettyServerFactoryPropertyMapper(properties); List> builderCustomizers = List @@ -66,7 +65,7 @@ ShadedNettyGrpcServerFactory shadedNettyGrpcServerFactory(GrpcServerProperties p } ShadedNettyGrpcServerFactory factory = new ShadedNettyGrpcServerFactory(properties.getAddress(), keyManager, builderCustomizers); - grpcServicesProvider.orderedStream().map(BindableService::bindService).forEach(factory::addService); + grpcServicesDiscoverer.findServices().forEach(factory::addService); return factory; } @@ -94,7 +93,7 @@ static class NettyServerFactoryConfiguration { @Bean NettyGrpcServerFactory nettyGrpcServerFactory(GrpcServerProperties properties, - ObjectProvider grpcServicesProvider, ServerBuilderCustomizers serverBuilderCustomizers, + GrpcServiceDiscoverer grpcServicesDiscoverer, ServerBuilderCustomizers serverBuilderCustomizers, SslBundles bundles) { NettyServerFactoryPropertyMapper mapper = new NettyServerFactoryPropertyMapper(properties); List> builderCustomizers = List @@ -106,7 +105,7 @@ NettyGrpcServerFactory nettyGrpcServerFactory(GrpcServerProperties properties, } NettyGrpcServerFactory factory = new NettyGrpcServerFactory(properties.getAddress(), keyManager, builderCustomizers); - grpcServicesProvider.orderedStream().map(BindableService::bindService).forEach(factory::addService); + grpcServicesDiscoverer.findServices().forEach(factory::addService); return factory; } diff --git a/spring-grpc-spring-boot-autoconfigure/src/test/java/org/springframework/grpc/autoconfigure/server/GrpcServerAutoConfigurationTests.java b/spring-grpc-spring-boot-autoconfigure/src/test/java/org/springframework/grpc/autoconfigure/server/GrpcServerAutoConfigurationTests.java index 312bfaff..300c3402 100644 --- a/spring-grpc-spring-boot-autoconfigure/src/test/java/org/springframework/grpc/autoconfigure/server/GrpcServerAutoConfigurationTests.java +++ b/spring-grpc-spring-boot-autoconfigure/src/test/java/org/springframework/grpc/autoconfigure/server/GrpcServerAutoConfigurationTests.java @@ -28,6 +28,7 @@ import java.util.concurrent.TimeUnit; import org.assertj.core.api.InstanceOfAssertFactories; +import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; import org.mockito.InOrder; import org.mockito.MockedStatic; @@ -41,6 +42,7 @@ import org.springframework.context.annotation.Configuration; import org.springframework.core.annotation.Order; import org.springframework.grpc.server.GrpcServerFactory; +import org.springframework.grpc.server.GrpcServiceDiscoverer; import org.springframework.grpc.server.NettyGrpcServerFactory; import org.springframework.grpc.server.ServerBuilderCustomizer; import org.springframework.grpc.server.ShadedNettyGrpcServerFactory; @@ -60,10 +62,16 @@ */ class GrpcServerAutoConfigurationTests { - private ApplicationContextRunner contextRunner() { - BindableService service = mock(); - ServerServiceDefinition serviceDefinition = ServerServiceDefinition.builder("my-service").build(); + private final BindableService service = mock(); + + private final ServerServiceDefinition serviceDefinition = ServerServiceDefinition.builder("my-service").build(); + + @BeforeEach + void prepareForTest() { when(service.bindService()).thenReturn(serviceDefinition); + } + + private ApplicationContextRunner contextRunner() { // NOTE: we use noop server lifecycle to avoid startup return new ApplicationContextRunner() .withConfiguration(AutoConfigurations.of(GrpcServerAutoConfiguration.class, @@ -73,9 +81,6 @@ private ApplicationContextRunner contextRunner() { } private ApplicationContextRunner contextRunnerWithLifecyle() { - BindableService service = mock(); - ServerServiceDefinition serviceDefinition = ServerServiceDefinition.builder("my-service").build(); - when(service.bindService()).thenReturn(serviceDefinition); // NOTE: we use noop server lifecycle to avoid startup return new ApplicationContextRunner() .withConfiguration(AutoConfigurations.of(GrpcServerAutoConfiguration.class, @@ -111,6 +116,24 @@ void serverLifecycleAutoConfiguredAsExpected() { .hasFieldOrPropertyWithValue("factory", context.getBean(GrpcServerFactory.class))); } + @Test + void whenHasUserDefinedGrpcServiceDiscovererDoesNotAutoConfigureBean() { + GrpcServiceDiscoverer customGrpcServiceDiscoverer = mock(GrpcServiceDiscoverer.class); + this.contextRunnerWithLifecyle() + .withBean("customGrpcServiceDiscoverer", GrpcServiceDiscoverer.class, () -> customGrpcServiceDiscoverer) + .run((context) -> assertThat(context).getBean(GrpcServiceDiscoverer.class) + .isSameAs(customGrpcServiceDiscoverer)); + } + + @Test + void grpcServiceDiscovererAutoConfiguredAsExpected() { + this.contextRunnerWithLifecyle() + .run((context) -> assertThat(context).getBean(GrpcServiceDiscoverer.class) + .extracting(GrpcServiceDiscoverer::findServices, + InstanceOfAssertFactories.list(ServerServiceDefinition.class)) + .containsExactly(this.serviceDefinition)); + } + @Test void whenHasUserDefinedServerBuilderCustomizersDoesNotAutoConfigureBean() { ServerBuilderCustomizers customCustomizers = mock(ServerBuilderCustomizers.class);