Skip to content

Commit 4daec6a

Browse files
onobcdsyer
authored andcommitted
Add docs for service filtering
See #207 Signed-off-by: Chris Bono <[email protected]>
1 parent 2a3d6ee commit 4daec6a

File tree

2 files changed

+32
-2
lines changed

2 files changed

+32
-2
lines changed

spring-grpc-docs/src/main/antora/modules/ROOT/pages/server.adoc

Lines changed: 32 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,37 @@ There are some `BindableServices` available off the shelf that you could include
1212
Very commonly, you will create your own `BindableService` by extending the generated service implementation from your Protobuf file.
1313
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.
1414

15+
[[service-filtering]]
16+
=== Service Filtering
17+
All available `BindableService` beans are bound to all running gRPC servers.
18+
However, you can register a `ServerServiceDefinitionFilter` bean to decide which services are bound to which server factories.
19+
20+
The following example prevents the `my-unwanted-service` service from being applied to any servers created by the server factory that the filter is applied to.
21+
22+
[source,java]
23+
----
24+
@Bean
25+
ServerServiceDefinitionFilter myServiceFilter() {
26+
return (serviceDefinition, __) ->
27+
!(serviceDefinition.getServiceDescriptor().getName().equals("my-unwanted-service"));
28+
}
29+
----
30+
31+
The `InProcessGrpcServerFactory` picks up the `ServerServiceDefinitionFilter` automatically.
32+
Any other server factory will require you to provide a `GrpcServerFactoryCustomizer` in which you can modify the factory by adding a filter, as shown in the following example:
33+
34+
[source,java]
35+
----
36+
@Bean
37+
GrpcServerFactoryCustomizer myServerFactoryCustomizer(ServerServiceDefinitionFilter myServiceFilter) {
38+
return factory -> {
39+
if (factory instanceof NettyGrpcServerFactory nettyServerFactory) {
40+
nettyServerFactory.setServiceFilter(myServiceFilter);
41+
}
42+
};
43+
}
44+
----
45+
1546
== Netty Server
1647

1748
If you use the `spring-grpc-spring-boot-starter` dependency on its own, the `Server` is a Netty-based implementation.
@@ -124,7 +155,7 @@ ServerInterceptor myGlobalLoggingInterceptor() {
124155
All global interceptors are applied to all created services by default.
125156
However, you can register a `ServerInterceptorFilter` bean to decide which interceptors are applied to which server factories.
126157

127-
The following example prevents the `ExtraThingsInterceptor` interceptor from being applied to any servers created by the `InProcessGrpcServerFactory` server factory.
158+
The following example prevents the `ExtraThingsInterceptor` interceptor from being applied to any servers created by the server factory that the filter is applied to.
128159

129160
[source,java]
130161
----

spring-grpc-spring-boot-autoconfigure/src/main/java/org/springframework/grpc/autoconfigure/server/GrpcServerFactoryConfigurations.java

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,6 @@
4242
import org.springframework.grpc.server.service.GrpcServiceConfigurer;
4343
import org.springframework.grpc.server.service.GrpcServiceDiscoverer;
4444
import org.springframework.grpc.server.service.ServerInterceptorFilter;
45-
import org.springframework.lang.Nullable;
4645

4746
import io.grpc.inprocess.InProcessServerBuilder;
4847
import io.grpc.netty.NettyServerBuilder;

0 commit comments

Comments
 (0)