From bb60c8fcaaeb8c4cc6f1c0e33c8e84a4b4b77ecb Mon Sep 17 00:00:00 2001 From: Chris Bono Date: Sun, 22 Sep 2024 13:17:35 -0500 Subject: [PATCH] Simplify server auto-configuration This simplifies the server-side auto-configuration as follows: - delete svc discover abstraction (use ObjectProvider for now) - use single AC class --- .../server/lifecycle/GrpcServerLifecycle.java | 17 +++-- .../server/DefaultGrpcServerFactoryTests.java | 30 ++++++++ ....java => GrpcServerAutoConfiguration.java} | 51 +++++++++----- .../server/GrpcServiceAutoConfiguration.java | 69 ------------------- ...ot.autoconfigure.AutoConfiguration.imports | 3 +- 5 files changed, 74 insertions(+), 96 deletions(-) create mode 100644 spring-grpc-core/src/test/java/org/springframework/grpc/server/DefaultGrpcServerFactoryTests.java rename spring-grpc-spring-boot-autoconfigure/src/main/java/org/springframework/grpc/autoconfigure/server/{GrpcServerFactoryAutoConfiguration.java => GrpcServerAutoConfiguration.java} (51%) delete mode 100644 spring-grpc-spring-boot-autoconfigure/src/main/java/org/springframework/grpc/autoconfigure/server/GrpcServiceAutoConfiguration.java diff --git a/spring-grpc-core/src/main/java/org/springframework/grpc/server/lifecycle/GrpcServerLifecycle.java b/spring-grpc-core/src/main/java/org/springframework/grpc/server/lifecycle/GrpcServerLifecycle.java index fc03266..60e3369 100644 --- a/spring-grpc-core/src/main/java/org/springframework/grpc/server/lifecycle/GrpcServerLifecycle.java +++ b/spring-grpc-core/src/main/java/org/springframework/grpc/server/lifecycle/GrpcServerLifecycle.java @@ -28,6 +28,7 @@ import java.util.concurrent.atomic.AtomicInteger; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; import org.springframework.context.ApplicationEventPublisher; +import org.springframework.context.ApplicationEventPublisherAware; import org.springframework.context.SmartLifecycle; import org.springframework.grpc.server.GrpcServerFactory; @@ -38,8 +39,9 @@ import io.grpc.Server; * * @author Michael (yidongnan@gmail.com) * @author Dave Syer + * @author Chris Bono */ -public class GrpcServerLifecycle implements SmartLifecycle { +public class GrpcServerLifecycle implements SmartLifecycle, ApplicationEventPublisherAware { private static final Log logger = LogFactory.getLog(GrpcServerLifecycle.class); @@ -49,7 +51,7 @@ public class GrpcServerLifecycle implements SmartLifecycle { private final Duration shutdownGracePeriod; - private final ApplicationEventPublisher eventPublisher; + private ApplicationEventPublisher eventPublisher; private Server server; @@ -57,14 +59,10 @@ public class GrpcServerLifecycle implements SmartLifecycle { * Creates a new GrpcServerLifecycle * @param factory The server factory to use. * @param shutdownGracePeriod The time to wait for the server to gracefully shut down. - * @param eventPublisher The event publisher to use. */ - public GrpcServerLifecycle(final GrpcServerFactory factory, final Duration shutdownGracePeriod, - final ApplicationEventPublisher eventPublisher) { - + public GrpcServerLifecycle(final GrpcServerFactory factory, final Duration shutdownGracePeriod) { this.factory = requireNonNull(factory, "factory"); this.shutdownGracePeriod = requireNonNull(shutdownGracePeriod, "shutdownGracePeriod"); - this.eventPublisher = eventPublisher; } @Override @@ -107,6 +105,11 @@ public class GrpcServerLifecycle implements SmartLifecycle { return this.server == null ? 0 : this.server.getPort(); } + @Override + public void setApplicationEventPublisher(ApplicationEventPublisher eventPublisher) { + this.eventPublisher = eventPublisher; + } + /** * Creates and starts the grpc server. * @throws IOException If the server is unable to bind the port. diff --git a/spring-grpc-core/src/test/java/org/springframework/grpc/server/DefaultGrpcServerFactoryTests.java b/spring-grpc-core/src/test/java/org/springframework/grpc/server/DefaultGrpcServerFactoryTests.java new file mode 100644 index 0000000..db9f0ad --- /dev/null +++ b/spring-grpc-core/src/test/java/org/springframework/grpc/server/DefaultGrpcServerFactoryTests.java @@ -0,0 +1,30 @@ +/* + * Copyright 2024-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.server; + +import org.junit.jupiter.api.Test; + +/** + * Tests for {@link DefaultGrpcServerFactory}/ + */ +class DefaultGrpcServerFactoryTests { + + @Test + void placeholderTest() { + } + +} diff --git a/spring-grpc-spring-boot-autoconfigure/src/main/java/org/springframework/grpc/autoconfigure/server/GrpcServerFactoryAutoConfiguration.java b/spring-grpc-spring-boot-autoconfigure/src/main/java/org/springframework/grpc/autoconfigure/server/GrpcServerAutoConfiguration.java similarity index 51% rename from spring-grpc-spring-boot-autoconfigure/src/main/java/org/springframework/grpc/autoconfigure/server/GrpcServerFactoryAutoConfiguration.java rename to spring-grpc-spring-boot-autoconfigure/src/main/java/org/springframework/grpc/autoconfigure/server/GrpcServerAutoConfiguration.java index 234c9ab..696d9c1 100644 --- a/spring-grpc-spring-boot-autoconfigure/src/main/java/org/springframework/grpc/autoconfigure/server/GrpcServerFactoryAutoConfiguration.java +++ b/spring-grpc-spring-boot-autoconfigure/src/main/java/org/springframework/grpc/autoconfigure/server/GrpcServerAutoConfiguration.java @@ -17,36 +17,51 @@ package org.springframework.grpc.autoconfigure.server; import java.util.List; -import org.springframework.boot.autoconfigure.AutoConfigureBefore; -import org.springframework.boot.autoconfigure.condition.ConditionalOnBean; +import io.grpc.BindableService; + +import org.springframework.beans.factory.ObjectProvider; +import org.springframework.boot.autoconfigure.AutoConfiguration; +import org.springframework.boot.autoconfigure.EnableAutoConfiguration; +import org.springframework.boot.autoconfigure.condition.ConditionalOnClass; import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean; import org.springframework.boot.context.properties.EnableConfigurationProperties; import org.springframework.context.annotation.Bean; -import org.springframework.context.annotation.Configuration; import org.springframework.grpc.server.DefaultGrpcServerFactory; import org.springframework.grpc.server.GrpcServerConfigurer; import org.springframework.grpc.server.GrpcServerFactory; +import org.springframework.grpc.server.lifecycle.GrpcServerLifecycle; -import io.grpc.BindableService; -import io.grpc.ServerServiceDefinition; - -@Configuration(proxyBeanMethods = false) -@ConditionalOnMissingBean(GrpcServerFactory.class) -@ConditionalOnBean(BindableService.class) -@AutoConfigureBefore(GrpcServiceAutoConfiguration.class) +/** + * {@link EnableAutoConfiguration Auto-configuration} for gRPC server-side components. + * + * @author David Syer + * @author Chris Bono + */ +@AutoConfiguration +@ConditionalOnClass(BindableService.class) @EnableConfigurationProperties(GrpcServerProperties.class) -public class GrpcServerFactoryAutoConfiguration { +public class GrpcServerAutoConfiguration { + + private final GrpcServerProperties properties; + + GrpcServerAutoConfiguration(GrpcServerProperties properties) { + this.properties = properties; + } @ConditionalOnMissingBean(GrpcServerFactory.class) @Bean - public DefaultGrpcServerFactory defaultGrpcServerFactory(final GrpcServerProperties properties, - final GrpcServiceDiscoverer serviceDiscoverer, final List serverConfigurers) { - final DefaultGrpcServerFactory factory = new DefaultGrpcServerFactory<>(properties.getAddress(), - properties.getPort(), serverConfigurers); - for (final ServerServiceDefinition service : serviceDiscoverer.findGrpcServices()) { - factory.addService(service); - } + DefaultGrpcServerFactory defaultGrpcServerFactory(ObjectProvider grpcServicesProvider, + List serverConfigurers) { + DefaultGrpcServerFactory factory = new DefaultGrpcServerFactory<>(this.properties.getAddress(), + this.properties.getPort(), serverConfigurers); + grpcServicesProvider.orderedStream().map(BindableService::bindService).forEach(factory::addService); return factory; } + @ConditionalOnMissingBean + @Bean + GrpcServerLifecycle grpcServerLifecycle(GrpcServerFactory factory) { + return new GrpcServerLifecycle(factory, this.properties.getShutdownGracePeriod()); + } + } diff --git a/spring-grpc-spring-boot-autoconfigure/src/main/java/org/springframework/grpc/autoconfigure/server/GrpcServiceAutoConfiguration.java b/spring-grpc-spring-boot-autoconfigure/src/main/java/org/springframework/grpc/autoconfigure/server/GrpcServiceAutoConfiguration.java deleted file mode 100644 index 79ca3e5..0000000 --- a/spring-grpc-spring-boot-autoconfigure/src/main/java/org/springframework/grpc/autoconfigure/server/GrpcServiceAutoConfiguration.java +++ /dev/null @@ -1,69 +0,0 @@ -/* - * Copyright 2024-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.server; - -import java.util.Collection; -import java.util.stream.Collectors; - -import org.springframework.boot.autoconfigure.condition.ConditionalOnBean; -import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean; -import org.springframework.context.ApplicationContext; -import org.springframework.context.ApplicationEventPublisher; -import org.springframework.context.annotation.Bean; -import org.springframework.context.annotation.Configuration; -import org.springframework.grpc.server.GrpcServerFactory; -import org.springframework.grpc.server.lifecycle.GrpcServerLifecycle; - -import io.grpc.BindableService; -import io.grpc.ServerServiceDefinition; - -@Configuration(proxyBeanMethods = false) -@ConditionalOnBean(GrpcServerFactory.class) -public class GrpcServiceAutoConfiguration { - - @ConditionalOnMissingBean - @Bean - public GrpcServiceDiscoverer defaultGrpcServiceDiscoverer(ApplicationContext applicationContext) { - return new DefaultGrpcServiceDiscoverer(applicationContext); - } - - @ConditionalOnMissingBean - @Bean - public GrpcServerLifecycle grpcServerLifecycle(final GrpcServerFactory factory, - final GrpcServerProperties properties, final ApplicationEventPublisher eventPublisher) { - return new GrpcServerLifecycle(factory, properties.getShutdownGracePeriod(), eventPublisher); - } - -} - -class DefaultGrpcServiceDiscoverer implements GrpcServiceDiscoverer { - - private final ApplicationContext applicationContext; - - public DefaultGrpcServiceDiscoverer(ApplicationContext applicationContext) { - this.applicationContext = applicationContext; - } - - @Override - public Collection findGrpcServices() { - return this.applicationContext.getBeansOfType(BindableService.class) - .values() - .stream() - .map(bean -> bean.bindService()) - .collect(Collectors.toList()); - } - -} diff --git a/spring-grpc-spring-boot-autoconfigure/src/main/resources/META-INF/spring/org.springframework.boot.autoconfigure.AutoConfiguration.imports b/spring-grpc-spring-boot-autoconfigure/src/main/resources/META-INF/spring/org.springframework.boot.autoconfigure.AutoConfiguration.imports index 171088e..80bec31 100644 --- a/spring-grpc-spring-boot-autoconfigure/src/main/resources/META-INF/spring/org.springframework.boot.autoconfigure.AutoConfiguration.imports +++ b/spring-grpc-spring-boot-autoconfigure/src/main/resources/META-INF/spring/org.springframework.boot.autoconfigure.AutoConfiguration.imports @@ -1,3 +1,2 @@ -org.springframework.grpc.autoconfigure.server.GrpcServiceAutoConfiguration -org.springframework.grpc.autoconfigure.server.GrpcServerFactoryAutoConfiguration +org.springframework.grpc.autoconfigure.server.GrpcServerAutoConfiguration org.springframework.grpc.autoconfigure.client.GrpcClientAutoConfiguration