gRPC
#
gRPC Libraries for ScalaPBThis page covers ScalaPB's gRPC support. This support is a thin wrapper around
grpc-java, and provides you with an interface that is based on Scala's
standard library Future
, while streaming is based on the Observer pattern.
There are additional gRPC libraries built on top of ScalaPB that provide integration with other concurrency frameworks and effect systems:
- ZIO gRPC enables you to build gRPC servers and clients using ZIO.
- fs2-grpc provides gGRPC support for FS2 and Cats Effect.
- Akka gRPC provides support for building streaming gRPC servers and clients on top of Akka Streams and Akka HTTP.
- Pekko gRPC provides support for building streaming gRPC servers and clients on top of Pekko Streams and Pekko HTTP.
- http4s-grpc enables you to build gRPC clients and servers with Cats Effect and FS2. It is implemented on top of http4s Ember and runs on JVM, Node.js, and Scala Native.
#
Project SetupInstall ScalaPB as usual. Add the following to your build.sbt
:
#
Creating a serviceWhen you run sbt compile
, ScalaPB will generate code for your servers. This
includes an asynchronous client, a blocking and a base trait you can extend to
implement the server side.
For example, if your proto file in src/main/protobuf/hello.proto
looks like
this:
The generated code will look like this (simplified and commented):
#
Using the clientCreating a channel:
Blocking call:
Async call:
#
Writing a serverImplement the service:
See complete example server here.
#
Streaming clients, streaming servers, bidiScalapb-grpc supports both client and server streaming. The Scala API follows
closely the offical grpc-java
API. Example project coming soon.
#
grpc-netty issuesIn certain situations (for example when you have a fat jar), you may see the following exception:
To work around this issue, try the following solutions:
- Create a
NettyServer
explicitly usingio.grpc.netty.NettyServerBuilder
.
Example:
- If using SBT, try the following merge conflict strategy: