Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -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<ServerServiceDefinition> findServices();

}
Original file line number Diff line number Diff line change
@@ -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<BindableService> grpcServicesProvider;

DefaultGrpcServiceDiscoverer(ObjectProvider<BindableService> grpcServicesProvider) {
this.grpcServicesProvider = grpcServicesProvider;
}

@Override
public List<ServerServiceDefinition> findServices() {
return grpcServicesProvider.orderedStream().map(BindableService::bindService).toList();
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -69,4 +70,10 @@ ServerBuilderCustomizers serverBuilderCustomizers(ObjectProvider<ServerBuilderCu
return new ServerBuilderCustomizers(customizers.orderedStream().toList());
}

@ConditionalOnMissingBean
@Bean
GrpcServiceDiscoverer grpcServiceDiscoverer(ObjectProvider<BindableService> bindableServicesProvider) {
return new DefaultGrpcServiceDiscoverer(bindableServicesProvider);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand All @@ -54,7 +53,7 @@ static class ShadedNettyServerFactoryConfiguration {

@Bean
ShadedNettyGrpcServerFactory shadedNettyGrpcServerFactory(GrpcServerProperties properties,
ObjectProvider<BindableService> grpcServicesProvider, ServerBuilderCustomizers serverBuilderCustomizers,
GrpcServiceDiscoverer grpcServicesDiscoverer, ServerBuilderCustomizers serverBuilderCustomizers,
SslBundles bundles) {
ShadedNettyServerFactoryPropertyMapper mapper = new ShadedNettyServerFactoryPropertyMapper(properties);
List<ServerBuilderCustomizer<io.grpc.netty.shaded.io.grpc.netty.NettyServerBuilder>> builderCustomizers = List
Expand All @@ -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;
}

Expand Down Expand Up @@ -94,7 +93,7 @@ static class NettyServerFactoryConfiguration {

@Bean
NettyGrpcServerFactory nettyGrpcServerFactory(GrpcServerProperties properties,
ObjectProvider<BindableService> grpcServicesProvider, ServerBuilderCustomizers serverBuilderCustomizers,
GrpcServiceDiscoverer grpcServicesDiscoverer, ServerBuilderCustomizers serverBuilderCustomizers,
SslBundles bundles) {
NettyServerFactoryPropertyMapper mapper = new NettyServerFactoryPropertyMapper(properties);
List<ServerBuilderCustomizer<NettyServerBuilder>> builderCustomizers = List
Expand All @@ -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;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand All @@ -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,
Expand All @@ -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,
Expand Down Expand Up @@ -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);
Expand Down