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:
DataSourceHealthChecker
is enabled by default when thespring-boot-starter-jdbc
is on the classpath.RedisHealthChecker
is enabled by default when thespring-boot-starter-data-redis
is 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 HealthChecker
s 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
Configurations
Full configuration is available in the GrpcServerProperties
.