Revert ServerServiceDefinition bean provider

In retrospect I think users should not have to create
ServerServiceDefinitions. It doesn't help anybody and
it seems like quite a deep internal detail.
This commit is contained in:
Dave Syer
2024-11-06 17:14:52 +00:00
parent 80e703448c
commit 33483bb500
4 changed files with 4 additions and 25 deletions

View File

@@ -7,13 +7,10 @@ The `Server` is the gRPC server that listens for incoming requests and routes th
== Create a gRPC Service
To create a gRPC server, you need to provide one or more beans of type `BindableService` or `ServerServiceDefinition`.
To create a gRPC server, you need to provide one or more beans of type `BindableService`.
There are some `BindableServices` available off the shelf that you could include in your application (an example is the reflection service from the `grpc-services` artifact which allows clients to browse the metadata of your services and download the Portobuf files).
Very commonly, you will create your own `BindableService` by extending the generated service implementation from your Protobuf file.
The easiest way to activate it is to simply add a Spring `@Service` annotation to the implementation class and have it picked up by the `@ComponentScan` in your Spring Boot application.
Alternatively, you could create an instance of it in a `@Configuration` class and return it as a bean, or return it's `ServerServiceDefinition`.
The last option is useful if you need to add specific behaviour, like an interceptor, to the service.
It is an error to have more than one instance of the same service name, so don't create both a `BindableService` and a `ServerServiceDefinition` bean for the same service.
== Netty Server

View File

@@ -36,19 +36,14 @@ class DefaultGrpcServiceDiscoverer implements GrpcServiceDiscoverer {
private final ObjectProvider<BindableService> grpcServicesProvider;
private ObjectProvider<ServerServiceDefinition> grpcServiceDefinitionsProvider;
DefaultGrpcServiceDiscoverer(ObjectProvider<BindableService> grpcServicesProvider,
ObjectProvider<ServerServiceDefinition> grpcServiceDefinitionsProvider) {
DefaultGrpcServiceDiscoverer(ObjectProvider<BindableService> grpcServicesProvider) {
this.grpcServicesProvider = grpcServicesProvider;
this.grpcServiceDefinitionsProvider = grpcServiceDefinitionsProvider;
}
@Override
public List<ServerServiceDefinition> findServices() {
List<ServerServiceDefinition> list = new ArrayList<>(
grpcServicesProvider.orderedStream().map(BindableService::bindService).toList());
list.addAll(grpcServiceDefinitionsProvider.orderedStream().toList());
return list;
}

View File

@@ -36,7 +36,6 @@ import io.grpc.BindableService;
import io.grpc.CompressorRegistry;
import io.grpc.DecompressorRegistry;
import io.grpc.ServerBuilder;
import io.grpc.ServerServiceDefinition;
/**
* {@link EnableAutoConfiguration Auto-configuration} for gRPC server-side components.
@@ -76,9 +75,8 @@ public class GrpcServerAutoConfiguration {
@ConditionalOnMissingBean
@Bean
GrpcServiceDiscoverer grpcServiceDiscoverer(ObjectProvider<BindableService> bindableServicesProvider,
ObjectProvider<ServerServiceDefinition> serviceDefinitionsProvider) {
return new DefaultGrpcServiceDiscoverer(bindableServicesProvider, serviceDefinitionsProvider);
GrpcServiceDiscoverer grpcServiceDiscoverer(ObjectProvider<BindableService> bindableServicesProvider) {
return new DefaultGrpcServiceDiscoverer(bindableServicesProvider);
}
@ConditionalOnBean(CompressorRegistry.class)

View File

@@ -134,17 +134,6 @@ class GrpcServerAutoConfigurationTests {
.containsExactly(this.serviceDefinition));
}
@Test
void grpcServiceDiscovererWithServiceDefinitionAsExpected() {
ServerServiceDefinition serviceDefinition = ServerServiceDefinition.builder("my-other-service").build();
this.contextRunnerWithLifecyle()
.withBean(ServerServiceDefinition.class, () -> serviceDefinition)
.run((context) -> assertThat(context).getBean(GrpcServiceDiscoverer.class)
.extracting(GrpcServiceDiscoverer::findServices,
InstanceOfAssertFactories.list(ServerServiceDefinition.class))
.containsExactly(this.serviceDefinition, serviceDefinition));
}
@Test
void whenHasUserDefinedServerBuilderCustomizersDoesNotAutoConfigureBean() {
ServerBuilderCustomizers customCustomizers = mock(ServerBuilderCustomizers.class);