Configuration
This section describes how you can configure your grpc-spring-boot-starter application.
Table of Contents
Additional topics
- Getting Started
- Configuration
- Exception Handling
- Contextual Data / Scoped Beans
- Testing the Service
- Server Events
- Security
Configuration via Properties
grpc-spring-boot-starter can be
configured via
spring’s @ConfigurationProperties
mechanism.
You can find all build-in configuration properties here:
If you prefer to read the sources instead, you can do so here.
The properties for the server are all prefixed with grpc.server.
and grpc.server.security.
respectively.
Changing the Server Port
If you wish to change the grpc server port from the default (9090
) to a different port you can do so
using:
grpc.server.port=80
Set the port to 0
to use a free random port. This feature is intended for deployments with a discovery service and
concurrent tests.
Please make sure that you won’t run into conflicts with other applications or other endpoints such as
spring-web
.
The SSL
/TLS
and other security relevant configuration is explained on the Server Security page.
Enabling the InProcessServer
Sometimes, you might want to consume your own grpc-service in your own application. You can do so like any other grpc server, but you can save the network overhead by using grpc’s InProcessServer
.
You can turn it on using the following property:
grpc.server.in-process-name=<SomeName>
# Optional: Turn off the external grpc-server
#grpc.server.port=-1
This allows clients to connect to the server from within the same application using the following configuration:
grpc.client.inProcess.address=in-process:<SomeName>
This is especially useful for tests as they don’t need to open a specific port and thus can run concurrently (on a build server).
Using Unix’s Domain Sockets
On Unix based systems you can also use domain sockets to locally communicate between server and clients.
Simply configure the address like this:
grpc.server.address=unix:/run/grpc-server
Clients can then connect to the server using the same address.
If you are using grpc-netty
you also need the netty-transport-native-epoll
dependency.
grpc-netty-shaded
already contains that dependency, so there is no need to add anything for it to work.
Configuration via Beans
While this library intents to provide most of the features as configuration option, sometimes the overhead for adding it is too high and thus we didn’t add it, yet. If you feel like it is an important feature, feel free to open a feature request.
If you want to change the application beyond what you can do through the properties, then you can use the existing extension points that exist in this library.
First of all most of the beans can be replaced by custom ones, that you can configure in every way you want.
If you don’t wish to go that far, you can use classes such as GrpcServerConfigurer
to configure the server and other
components without losing the features provided by this library.
ServerInterceptor
There are three ways to add a ServerInterceptor
to your server.
- Define the
ServerInterceptor
as a global interceptor using either the@GrpcGlobalServerInterceptor
annotation, or aGlobalServerInterceptorConfigurer
- Explicitly list them in the
@GrpcService#interceptors
or@GrpcService#interceptorNames
field - Use a
GrpcServerConfigurer
and callserverBuilder.intercept(ServerInterceptor interceptor)
GrpcServerConfigurer
The grpc server configurer allows you to add your custom configuration to grpc’s ServerBuilder
s.
@Bean
public GrpcServerConfigurer keepAliveServerConfigurer() {
return serverBuilder -> {
if (serverBuilder instanceof NettyServerBuilder) {
((NettyServerBuilder) serverBuilder)
.keepAliveTime(30, TimeUnit.SECONDS)
.keepAliveTimeout(5, TimeUnit.SECONDS)
.permitKeepAliveWithoutCalls(true);
}
};
}
Be aware that depending on your configuration there might be different types of
ServerBuilder
s in the application context (e.g. theInProcessServerBuilder
).
Additional Topics
- Getting Started
- Configuration
- Exception Handling
- Contextual Data / Scoped Beans
- Testing the Service
- Server Events
- Security