Auto Configuration
Dependencies
- Gradle
- Maven
implementation("io.github.danielliu1123:grpc-server-boot-starter")
<dependency>
<groupId>io.github.danielliu1123</groupId>
<artifactId>grpc-server-boot-starter</artifactId>
</dependency>
Register Services
After adding the dependencies, the application will start a gRPC server at 9090,
and it will automatically scan and register gRPC services.
You only need to add @Component based annotation to your service implementations.
@Component
public class SimpleServiceImpl extends SimpleServiceGrpc.SimpleServiceImplBase {
@Override
public void unaryRpc(SimpleRequest request, StreamObserver<SimpleResponse> responseObserver) {
var response = SimpleResponse.newBuilder().setResponseMessage("Hello " + request.getRequestMessage()).build();
responseObserver.onNext(response);
responseObserver.onCompleted();
}
}
The framework provides a @GrpcService annotation to better distinguish bean types.
But it is not necessary, you can use Spring's built-in @Component/@Service/@Controller instead.
In-process Server
gRPC server supports in-process transport, which is useful for testing scenarios.
grpc:
server:
in-process:
name: whatever
Build-in Services
io.grpc:grpc-services provides a series of built-in services.
Health
Health service is enabled by default.
Use the following configuration to disable it:
grpc:
server:
health:
enabled: false
You can customize the health check logic by implementing the HealthChecker interface.
There are some build-in HealthChecker implementations:
DataSourceHealthCheckeris enabled by default when thespring-boot-starter-jdbcis on the classpath.RedisHealthCheckeris enabled by default when thespring-boot-starter-data-redisis on the classpath.
Use the following configuration to disable them:
grpc:
server:
health:
datasource:
enabled: false
redis:
enabled: false
Starting from version 3.4.0, to better support Kubernetes Probes,
three special services were introduced: startup, readiness, and liveness.
For startup and readiness, the system will return the SERVING status only when all HealthCheckers pass their checks (return true), as they rely on the state of external services.
For liveness, it always returns the SERVING status, as it does not rely on the state of external services.
This is suitable for most applications.
If you need more customization, you can implement the health service yourself:
@Component
public class HealthService extends HealthGrpc.HealthImplBase {
@Override
public void check(HealthCheckRequest request, StreamObserver<HealthCheckResponse> responseObserver) {
// Your custom health check logic
var result = HealthCheckResponse.ServingStatus.SERVING;
responseObserver.onNext(
HealthCheckResponse.newBuilder().setStatus(result).build());
responseObserver.onCompleted();
}
}
Reflection
Reflection is disabled by default, it's often used in development and debugging scenarios.
Use the following configuration to enable it:
grpc:
server:
reflection:
enabled: true
Channelz
Channelz is disabled by default.
Use the following configuration to enable it:
grpc:
server:
channelz:
enabled: true
Virtual Threads Support
Starting from version 3.5.3.2, gRPC server supports virtual threads when using Java 21+.
When spring.threads.virtual.enabled is set to true, the gRPC server will automatically use virtual threads to handle requests, providing better performance for I/O-bound operations.
spring:
threads:
virtual:
enabled: true
Virtual threads are only available in Java 21+. This feature will be ignored on older Java versions.
Configurations
Full configuration is available in the GrpcServerProperties.