Akka streamlet utilities
GrpcServerLogic
Use case
An GrpcServerLogic
can be used to handle gRPC requests.
You need to extend your streamlet from AkkaServerStreamlet
so that Cloudflow will expose an HTTP endpoint in Kubernetes.
Additionally, you need to enable HTTP/2 by adding the following to your configuration:
akka.http.server.preview.enable-http2 = on
Example
gRPC is a contract-first technology, so you start by adding
the protobuf definition for the service you want to expose
to src/main/protobuf
. In this example we will implement a
simple request-response service:
syntax = "proto3";
option java_multiple_files = true;
option java_package = "sensordata.grpc";
option java_outer_classname = "SensorDataProto";
package sensordata;
service SensorDataService {
rpc Provide (SensorData) returns (SensorReply) {}
}
message SensorData {
string payload = 1;
}
message SensorReply {
string message = 1;
}
Because the Akka gRPC plugin is embedded in
Cloudflow, the code for the service will automatically be generated when you
compile this service. In this case, a sensordata.grpc.SensorDataService
will be
generated that you can implement.
To expose your implementation, extend AkkaServerStreamlet
and use GrpcServerLogic
to add your gRPC handlers:
- Scala
-
override def createLogic = new GrpcServerLogic(this) { override def handlers() = List(SensorDataServiceHandler.partial(new SensorDataServiceImpl(sinkRef(out))), ServerReflection.partial(List(SensorDataService))) }
- Java
-
public AkkaStreamletLogic createLogic() { return new GrpcServerLogic(this, getContext()) { public List<Function<HttpRequest, CompletionStage<HttpResponse>>> handlers() { return List.of( SensorDataServiceHandlerFactory.partial(new SensorDataServiceImpl(), SensorDataService.name, system()), ServerReflection.create(List.of(SensorDataService.description), system())); } }; }
Exposing through an ingress
gRPC services use HTTP/2 as a transport mechanism. Cloudflow supports both HTTP and HTTP/2 on the port exposed by the pod.
When you want to expose your gRPC service to external clients, you will need to expose it as a HTTP/2 service to the outside. Configuring an ingress to support HTTP/2 might be possible with the correct additional configuration, but is currently not officially supported, and might even require code changes.