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
This commit is contained in:
Chris Bono
2024-09-22 13:17:35 -05:00
committed by Dave Syer
parent 32286cfbed
commit bb60c8fcaa
5 changed files with 74 additions and 96 deletions

View File

@@ -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.

View File

@@ -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() {
}
}

View File

@@ -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<GrpcServerConfigurer> serverConfigurers) {
final DefaultGrpcServerFactory<?> factory = new DefaultGrpcServerFactory<>(properties.getAddress(),
properties.getPort(), serverConfigurers);
for (final ServerServiceDefinition service : serviceDiscoverer.findGrpcServices()) {
factory.addService(service);
}
DefaultGrpcServerFactory<?> defaultGrpcServerFactory(ObjectProvider<BindableService> grpcServicesProvider,
List<GrpcServerConfigurer> 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());
}
}

View File

@@ -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<ServerServiceDefinition> findGrpcServices() {
return this.applicationContext.getBeansOfType(BindableService.class)
.values()
.stream()
.map(bean -> bean.bindService())
.collect(Collectors.toList());
}
}

View File

@@ -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