Skip to main content

gRPC

Availability

Ships in the release after 0.8.0.

ligero-grpc serves gRPC services (grpc-java) on their own port, next to the HTTP server, with the same lifecycle.

implementation 'com.ligeroframework:ligero-grpc:<version>'

Generate the service classes from your .proto files as usual (the com.google.protobuf Gradle plugin with protoc-gen-grpc-java), then:

public final class GreeterService extends GreeterGrpc.GreeterImplBase {
@Override
public void sayHello(HelloRequest request, StreamObserver<HelloReply> reply) {
reply.onNext(HelloReply.newBuilder().setMessage("Hello, " + request.getName()).build());
reply.onCompleted();
}
}

GrpcServer grpc = GrpcServer.builder(9090)
.service(new GreeterService())
.health() // grpc.health.v1 for load balancers and Kubernetes
.build();
grpc.attach(app); // starts with app.start(), drains on app.stop()

Each call runs on its own virtual thread, so service code can block — JDBC, other services — exactly like an HTTP handler.

Errors​

Throw the same exceptions as in HTTP handlers:

ThrowngRPC status
NotFoundException (404)NOT_FOUND with the message
BadRequestException (400)INVALID_ARGUMENT
UnauthorizedException / ForbiddenExceptionUNAUTHENTICATED / PERMISSION_DENIED
HttpException(409)ABORTED
TooManyRequestsExceptionRESOURCE_EXHAUSTED
HttpException(503)UNAVAILABLE
StatusRuntimeExceptionas is
anything elseUNKNOWN, no detail (logged)

Limits and options​

SettingDefault
maxMessageBytes4 MiB
maxMetadataBytes8 KiB
maxConcurrentCallsPerConnection100
handshakeTimeout20 s
maxConnectionIdle5 min
client keep-alive pingsat most every 30 s
shutdownGrace10 s
  • .tls(certChainPem, privateKeyPem) or .credentials(...) for mutual TLS.
  • .interceptor(...) for authentication or logging around every call.
  • .reflection() lets tools such as grpcurl discover the services. Use it in development: it also shows anyone who can connect what your API looks like.
  • grpc.setHealth("my.Service", false) reports NOT_SERVING, e.g. while a dependency is down. While the server drains on stop, health checks already answer NOT_SERVING.