Akka gRPC provides support for building streaming gRPC servers and clients on top of Akka Streams and Akka Http.

Features of Akka-gRPC

  1. A generator, that starts from a protobuf service definitions, for:
  2. Model classes
  3. The service API as a Scala trait using Akka Stream Sources
  4. On the server side code to create an Akka HTTP route based on your implementation of the service
  5. On the client side, a client for the service
  6. gRPC Runtime implementation that uses
  • Akka Http/2 support or the server side and
  • grpc-netty-shaded for the client side.

Steps for generated Code

  1. Define the protobuf file under src/main/protobuf folder . If we define file other than src main/protobuf folder the you we have to provide explicit path for .protobuf file under tag.
  2. Compile the .proto file
  3. Setup the server so that client send request to server
  4. Provide the implementation for generated code on server side
  5. Provide the implementation for generated code on client side
  6. Start the server for maven project
mvn compile dependency:properties exec:exec@server

7. Start the client for maven project

mvn compile dependency:properties exec:exec@client

Define Protobuf file

syntax = "proto3";

option java_multiple_files = true;
option java_package = "com.knoldus.helloworld";
option java_outer_classname = "HelloWorldKnoldus";

// The greeting service definition.
service GreeterService {
    // Sends a greeting
    rpc SayHello (HelloRequest) returns (HelloReply) {}
}
message HelloRequest {
    string name = 1;
}
message HelloReply {
    string message = 1;
}

After compiling .proto file , the code will be generated

Set-up Server

First, the GreeterServer main class creates an akka.actor.ActorSystem, a container in which Actors, Akka Streams and Akka HTTP run. Next, it defines a function from HttpRequest to Future[HttpResponse] using the GreeterServiceImpl. This function handles gRPC requests in the HTTP/2 with TLS server that is bound to port 8080 in this example.

#akka #akka-http #akka-streams #grpc #scala

Akka-gRPC,Features of Akka-gRPC
4.45 GEEK