Installing ZIO gRPC

Determining the right version#

The version of zio-grpc needs to be compatible with the version of ScalaPB in order to avoid unintended evictions and ensure binary compatibility:


Installation using SBT (Recommended)#

If you are building with sbt, add the following to your project/plugins.sbt:

addSbtPlugin("com.thesamet" % "sbt-protoc" % "1.0.2")
libraryDependencies +=
"com.thesamet.scalapb.zio-grpc" %% "zio-grpc-codegen" % "0.5.0"

Then, add the following lines to your build.sbt:

PB.targets in Compile := Seq(
scalapb.gen(grpc = true) -> (sourceManaged in Compile).value / "scalapb",
scalapb.zio_grpc.ZioCodeGenerator -> (sourceManaged in Compile).value / "scalapb"
libraryDependencies ++= Seq(
"io.grpc" % "grpc-netty" % "1.40.1",
"com.thesamet.scalapb" %% "scalapb-runtime-grpc" % scalapb.compiler.Version.scalapbVersion

This configuration will set up the ScalaPB code generator alongside the ZIO gRPC code generator. Upon compilation, the source generator will process all proto files under src/main/protobuf. The ScalaPB generator will generate case classes for all messages as well as methods to serialize and deserialize those messages. The ZIO gRPC code generator will generate code as described in the generated code section.

Generating code using ScalaPBC (CLI)#

If you are using ScalaPBC to generate Scala code from the CLI, you can invoke the zio code generator like this:

scalapbc \
-- e2e/src/main/protobuf/service.proto --zio_out=/tmp/out --scala_out=grpc:/tmp/out \
-Ie2e/src/main/protobuf -Ithird_party -Iprotobuf

You will need to add to your project the following libraries:

  • com.thesamet.scalapb::scalapb-runtime-grpc:0.11.5
  • com.thesamet.scalapb.zio-grpc:zio-grpc-core:0.5.0
  • io.grpc:grpc-netty:1.40.1