Asynchronous Microservices with ZIO-gRPC and Scala

Functional Ops

(λx.x)eranga
Effectz.AI
2 min readOct 31, 2021

--

Background

In my previous post I have discussed detailed information about integrating gRPC and Scala using ScalaPB library. In this post I’m gonna discuss about integrating gRPC with Scala using ZIO-gRPC functional library. All the source codes which related to this post available in gitlab. Please clone the repo and continue the post.

ZIO and ZIO-gRPC

ZIO is a purely functional, type-safe, and composable library for asynchronous, concurrent programming in Scala. ZIO supports for developing highly scalable, resilient, and reactive applications using simple, testable, and composable code. It powered by highly-scalable, non-blocking fibers that never waste or leak resources. ZIO-gRPC is a functional gRPC library built on top of ZIO. It enables writing purely functional gRPC servers and clients with using ZIO functionality.

Sbt dependency

First I need to add sbt-protoc compiler plugin and zio-grpc-codegen plugin dependencies to the plugins.sbt file. ScalaPB is a protocol buffer compiler(protoc) plugin for Scala. It will generate Scala case classes, parsers and serializers for your protocol buffers. zio-grpc-codegen plugin used to generate ZIO-gRPC code from Protobuf files. Following is the plugins.sbt file.

Then need to update the build.sbt with gRPC and other dependencies. build.sbt needs to define the instructions to compile Protobuf definitions into Scala codes and ZIO-gRPC codes. It will generates Scala codes and ZIO-gRPC codes from Protobuf file when compiling the project with sbt compile. Following is the build.sbt file.

Protobuf spec

Following is the Protobuf spec file(document.proto) I have used. Create a directory named protobuf inside src/main and add the file document.proto. The gRPC DocumentService contains four main functions. 1) createDocument(unary API), 2) createDocuments(client stream API), 3) getDocuments(server stream API), 4) streamDocuments(bi-directional stream API).

When compiling the project, it will generates the gRPC Scala codes in the target/scala-2.13/src_managed/main/com/rahasak/proto/document/ directory. It will contains ZDocumentService and ZioDocument object with ZIO-gRPC implementations.

ZIO-gRPC Service

Next I have implemented the ZIO-gRPC service definitions in the ZDocumentService. Following is the service implementation in ZDocumentServiceImpl.scala. It implemented all four gRPC service functions defined in the ZDocumentService.

ZIO-gRPC Server

The gRPC server(ZDocumentServiceApp.scala) registers the services implemented in the ZDocumentServiceImpl.scala using ZIO-gRPC. It exposes the TCP port 9000 to serve the service APIs.

ZIO-gRPC Client

To consume the functions implemented in the ZIO-gRPC server I have created the ZIO-gRPC client(ZDocumentClientApp.scala). It consumes the four functions implemented in the ZDocumentServiceApp asynchronously using ZIO-gRPC.

Reference

  1. https://medium.com/rahasak/reactive-microservices-with-grpc-and-scala-e4767ca2d34a
  2. https://scalapb.github.io/docs/grpc/
  3. https://scalac.io/blog/why-developers-should-pay-attention-to-zio-in-2021/
  4. https://scalapb.github.io/zio-grpc/docs/
  5. https://scalac.io/blog/streaming-microservices-with-zio-and-kafka/

--

--