Skip to content

Commit

Permalink
[3.0.x] Update Pekko/Play gRPC
Browse files Browse the repository at this point in the history
  • Loading branch information
ihostage committed Dec 27, 2023
1 parent 300a4ff commit d711d0d
Show file tree
Hide file tree
Showing 14 changed files with 53 additions and 49 deletions.
2 changes: 1 addition & 1 deletion play-java-grpc-example/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ This example is described in the [Play Java gRPC Example site](https://developer

This is an example application that shows how to use Pekko gRPC to both expose and use gRPC services inside an Play application.

For detailed documentation refer to https://www.playframework.com/documentation/latest/Home and https://developer.lightbend.com/docs/akka-grpc/current/ .
For detailed documentation refer to https://www.playframework.com/documentation/latest/Home and https://pekko.apache.org/docs/pekko-grpc/current/.


## Sample license
Expand Down
9 changes: 4 additions & 5 deletions play-java-grpc-example/app/routers/HelloWorldRouter.java
Original file line number Diff line number Diff line change
@@ -1,21 +1,20 @@
package routers;

import org.apache.pekko.actor.ActorSystem;
import org.apache.pekko.stream.Materializer;
import example.myapp.helloworld.grpc.HelloReply;
import example.myapp.helloworld.grpc.HelloRequest;

import javax.inject.Inject;
import javax.inject.Singleton;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.CompletionStage;
import javax.inject.Inject;
import javax.inject.Singleton;

@Singleton
public class HelloWorldRouter extends example.myapp.helloworld.grpc.AbstractGreeterServiceRouter {

@Inject
public HelloWorldRouter(Materializer mat, ActorSystem system) {
super(mat, system);
public HelloWorldRouter(ActorSystem system) {
super(system);
}

@Override
Expand Down
21 changes: 12 additions & 9 deletions play-java-grpc-example/build.sbt
Original file line number Diff line number Diff line change
@@ -1,28 +1,30 @@
import play.core.PlayVersion.pekkoVersion
import play.core.PlayVersion.pekkoHttpVersion
import play.grpc.gen.javadsl.{ PlayJavaClientCodeGenerator, PlayJavaServerCodeGenerator }
import com.typesafe.sbt.packager.docker.{ Cmd, CmdLike, DockerAlias, ExecCmd }
import play.grpc.gen.javadsl.{PlayJavaClientCodeGenerator, PlayJavaServerCodeGenerator}
import com.typesafe.sbt.packager.docker.{Cmd, CmdLike, DockerAlias, ExecCmd}
import play.java.grpc.sample.BuildInfo

name := "play-java-grpc-example"
version := "1.0-SNAPSHOT"

ThisBuild / resolvers += Resolver.ApacheMavenSnapshotsRepo

// #grpc_play_plugins
// build.sbt
lazy val `play-java-grpc-example` = (project in file("."))
.enablePlugins(PlayJava)
.enablePlugins(AkkaGrpcPlugin) // enables source generation for gRPC
.enablePlugins(PekkoGrpcPlugin) // enables source generation for gRPC
.enablePlugins(PlayPekkoHttp2Support) // enables serving HTTP/2 and gRPC
// #grpc_play_plugins
.settings(
akkaGrpcGeneratedLanguages := Seq(AkkaGrpc.Java),
pekkoGrpcGeneratedLanguages := Seq(PekkoGrpc.Java),
// #grpc_client_generators
// build.sbt
akkaGrpcExtraGenerators += PlayJavaClientCodeGenerator,
pekkoGrpcExtraGenerators += PlayJavaClientCodeGenerator,
// #grpc_client_generators
// #grpc_server_generators
// build.sbt
akkaGrpcExtraGenerators += PlayJavaServerCodeGenerator,
pekkoGrpcExtraGenerators += PlayJavaServerCodeGenerator,
Test / javaOptions += "-Dtestserver.httpsport=9443",
// #grpc_server_generators
PlayKeys.devSettings ++= Seq(
Expand All @@ -49,8 +51,9 @@ lazy val `play-java-grpc-example` = (project in file("."))
.settings(
libraryDependencies ++= CompileDeps ++ TestDeps
)

scalaVersion := "2.13.12"
crossScalaVersions := Seq("2.13.12", "3.3.1")
scalacOptions ++= List("-encoding", "utf8", "-deprecation", "-feature", "-unchecked")
javacOptions ++= List("-Xlint:unchecked", "-Xlint:deprecation")
// Needed for ssl-config to create self signed certificated under Java 17
Expand All @@ -59,7 +62,7 @@ Test / javaOptions ++= List("--add-exports=java.base/sun.security.x509=ALL-UNNAM
val CompileDeps = Seq(
guice,
javaWs,
"com.lightbend.play" %% "play-grpc-runtime" % BuildInfo.playGrpcVersion,
"org.playframework" %% "play-grpc-runtime" % BuildInfo.playGrpcVersion,
"org.apache.pekko" %% "pekko-discovery" % pekkoVersion,
"org.apache.pekko" %% "pekko-http" % pekkoHttpVersion,
"org.apache.pekko" %% "pekko-http-spray-json" % pekkoHttpVersion,
Expand All @@ -69,7 +72,7 @@ val CompileDeps = Seq(

val TestDeps = Seq(
// used in tests
"com.lightbend.play" %% "play-grpc-testkit" % BuildInfo.playGrpcVersion % Test
"org.playframework" %% "play-grpc-testkit" % BuildInfo.playGrpcVersion % Test
)

// Make verbose tests
Expand Down
2 changes: 1 addition & 1 deletion play-java-grpc-example/conf/application.conf
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ play.http.secret.key = "default-value-used-locally-with-at-minimal-length"
play.modules {
# To enable Pekko gRPC clients to be @Injected
# This Module is generated by the Pekko gRPC sbt plugin. See your `target/scala-2.12/src_managed` folder.
enabled += example.myapp.helloworld.grpc.AkkaGrpcClientModule
enabled += example.myapp.helloworld.grpc.PekkoGrpcClientModule
disabled += "play.grpc.ClassicActorsystemProviderModule"
}
# #grpc_enable_client_module
Expand Down
8 changes: 4 additions & 4 deletions play-java-grpc-example/docs/src/main/paradox/code-details.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

Adding gRPC support to a vanilla Play application requires a few steps:

### 1. `sbt-akka-grpc`
### 1. `sbt-pekko-grpc`

Add the Pekko gRPC plugin on `project/plugins.sbt`

Expand All @@ -12,13 +12,13 @@ and enable it on your project (in `build.sbt`):

@@snip [build.sbt](../../../../build.sbt) { #grpc_play_plugins }

The `AkkaGrpcPlugin` locates the gRPC `.proto` files and generates source code from it. Remember to enable the plugin
The `PekkoGrpcPlugin` locates the gRPC `.proto` files and generates source code from it. Remember to enable the plugin
in all the projects of your build that want to use it.

Note how the `PlayPekkoHttp2Support` is also enabled. gRPC requires HTTP/2 transport and Play supports it only as an opt-in plugin.


### 2.a Serving (Akka) gRPC Services
### 2.a Serving (Pekko) gRPC Services

Have a look at the `conf/routes` file where you'll notice how to embed a gRPC router within a normal play application.
You can in fact mix normal Play routes with gRPC routers like this to offer a mixed service. You'll notice that we
Expand Down Expand Up @@ -51,4 +51,4 @@ Which in turn allows us to inject clients to any of the services defined in our

Since you may want to configure what service discovery or hardcoded location to use for each client, you may do so
as well in `conf/application.conf`, though we will not dive into this here. Refer to the documentation on
[using Pekko Discovery for endpoint discovery](https://developer.lightbend.com/docs/akka-grpc/current/client/configuration.html#using-akka-discovery-for-endpoint-discovery) for more details.
[using Pekko Discovery for endpoint discovery](https://pekko.apache.org/docs/pekko-grpc/current/client/configuration.html#using-pekko-discovery-for-endpoint-discovery) for more details.
4 changes: 2 additions & 2 deletions play-java-grpc-example/docs/src/main/paradox/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,10 @@ The [Play Framework](https://www.playframework.com/) combines productivity and p
scalable web applications with Java and Scala. Play is developer friendly with a "just hit refresh" workflow and
built-in testing support. With Play, applications scale predictably due to a stateless and non-blocking architecture.

[Akka gRPC](https://developer.lightbend.com/docs/akka-grpc/current/overview.html) is a toolkit for building streaming
[Pekko gRPC](https://pekko.apache.org/docs/pekko-grpc/current/) is a toolkit for building streaming
gRPC servers and clients on top of Pekko Streams.

For detailed documentation refer to https://www.playframework.com/documentation/latest/Home and https://developer.lightbend.com/docs/akka-grpc/current/.
For detailed documentation refer to https://www.playframework.com/documentation/latest/Home and https://pekko.apache.org/docs/pekko-grpc/current/.

## Obtaining this example

Expand Down
8 changes: 5 additions & 3 deletions play-java-grpc-example/project/plugins.sbt
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
resolvers += Resolver.ApacheMavenSnapshotsRepo

enablePlugins(BuildInfoPlugin)
val playGrpcV = "0.9.1"
val playGrpcV = "0.12.0"
buildInfoKeys := Seq[BuildInfoKey]("playGrpcVersion" -> playGrpcV)
buildInfoPackage := "play.java.grpc.sample"

Expand All @@ -9,6 +11,6 @@ addSbtPlugin("com.lightbend.paradox" % "sbt-paradox" % "0.10.5")

// #grpc_sbt_plugin
// project/plugins.sbt
addSbtPlugin("com.lightbend.akka.grpc" %% "sbt-akka-grpc" % "1.0.2")
libraryDependencies += "com.lightbend.play" %% "play-grpc-generators" % playGrpcV
addSbtPlugin("org.apache.pekko" % "pekko-grpc-sbt-plugin" % "1.0.1-15-513a1c3f-SNAPSHOT")
libraryDependencies += "org.playframework" %% "play-grpc-generators" % playGrpcV
// #grpc_sbt_plugin
2 changes: 1 addition & 1 deletion play-scala-grpc-example/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ This example is described in the [Play Scala gRPC Example site](https://develope

This is an example application that shows how to use Pekko gRPC to both expose and use gRPC services inside an Play application.

For detailed documentation refer to https://www.playframework.com/documentation/latest/Home and https://developer.lightbend.com/docs/akka-grpc/current/ .
For detailed documentation refer to https://www.playframework.com/documentation/latest/Home and https://pekko.apache.org/docs/pekko-grpc/current/.


## Sample license
Expand Down
16 changes: 9 additions & 7 deletions play-scala-grpc-example/build.sbt
Original file line number Diff line number Diff line change
Expand Up @@ -7,23 +7,24 @@ import play.scala.grpc.sample.BuildInfo
name := "play-scala-grpc-example"
version := "1.0-SNAPSHOT"

ThisBuild / resolvers += Resolver.ApacheMavenSnapshotsRepo

// #grpc_play_plugins
// build.sbt
lazy val `play-scala-grpc-example` = (project in file("."))
.enablePlugins(PlayScala)
.enablePlugins(AkkaGrpcPlugin) // enables source generation for gRPC
.enablePlugins(PekkoGrpcPlugin) // enables source generation for gRPC
.enablePlugins(PlayPekkoHttp2Support) // enables serving HTTP/2 and gRPC
// #grpc_play_plugins
.settings(
akkaGrpcGeneratedLanguages := Seq(AkkaGrpc.Scala),
pekkoGrpcGeneratedLanguages := Seq(PekkoGrpc.Scala),
// #grpc_client_generators
// build.sbt
akkaGrpcExtraGenerators += PlayScalaClientCodeGenerator,
pekkoGrpcExtraGenerators += PlayScalaClientCodeGenerator,
// #grpc_client_generators
// #grpc_server_generators
// build.sbt
akkaGrpcExtraGenerators += PlayScalaServerCodeGenerator,
pekkoGrpcExtraGenerators += PlayScalaServerCodeGenerator,
Test / javaOptions += "-Dtestserver.httpsport=0",
// #grpc_server_generators
PlayKeys.devSettings ++= Seq(
Expand Down Expand Up @@ -53,7 +54,7 @@ lazy val `play-scala-grpc-example` = (project in file("."))

val CompileDeps = Seq(
guice,
"com.lightbend.play" %% "play-grpc-runtime" % BuildInfo.playGrpcVersion,
"org.playframework" %% "play-grpc-runtime" % BuildInfo.playGrpcVersion,
"org.apache.pekko" %% "pekko-discovery" % pekkoVersion,
"org.apache.pekko" %% "pekko-http" % pekkoHttpVersion,
"org.apache.pekko" %% "pekko-http-spray-json" % pekkoHttpVersion,
Expand All @@ -63,14 +64,15 @@ val CompileDeps = Seq(

val playVersion = play.core.PlayVersion.current
val TestDeps = Seq(
"com.lightbend.play" %% "play-grpc-scalatest" % BuildInfo.playGrpcVersion % Test,
"com.lightbend.play" %% "play-grpc-specs2" % BuildInfo.playGrpcVersion % Test,
"org.playframework" %% "play-grpc-scalatest" % BuildInfo.playGrpcVersion % Test,
"org.playframework" %% "play-grpc-specs2" % BuildInfo.playGrpcVersion % Test,
"org.playframework" %% "play-test" % playVersion % Test,
"org.playframework" %% "play-specs2" % playVersion % Test,
"org.scalatestplus.play" %% "scalatestplus-play" % "7.0.0" % Test,
)

scalaVersion := "2.13.12"
crossScalaVersions := Seq("2.13.12", "3.3.1")
scalacOptions ++= List("-encoding", "utf8", "-deprecation", "-feature", "-unchecked")
// Needed for ssl-config to create self signed certificated under Java 17
Test / javaOptions ++= List("--add-exports=java.base/sun.security.x509=ALL-UNNAMED")
Expand Down
2 changes: 1 addition & 1 deletion play-scala-grpc-example/conf/application.conf
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ play.http.secret.key = "default-value-used-locally-with-at-minimal-length"
play.modules {
# To enable Pekko gRPC clients to be @Injected
# This Module is generated by the Pekko gRPC sbt plugin. See your `target/scala-2.12/src_managed` folder.
enabled += example.myapp.helloworld.grpc.AkkaGrpcClientModule
enabled += example.myapp.helloworld.grpc.PekkoGrpcClientModule
disabled += "play.grpc.ClassicActorsystemProviderModule"
}
# #grpc_enable_client_module
Expand Down
8 changes: 4 additions & 4 deletions play-scala-grpc-example/docs/src/main/paradox/code-details.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

Adding gRPC support to a vanilla Play application requires a few steps:

### 1. `sbt-akka-grpc`
### 1. `sbt-pekko-grpc`

Add the Pekko gRPC plugin on `project/plugins.sbt`

Expand All @@ -12,13 +12,13 @@ and enable it on your project (in `build.sbt`):

@@snip [build.sbt](../../../../build.sbt) { #grpc_play_plugins }

The `AkkaGrpcPlugin` locates the gRPC `.proto` files and generates source code from it. Remember to enable the plugin
The `PekkoGrpcPlugin` locates the gRPC `.proto` files and generates source code from it. Remember to enable the plugin
in all the projects of your build that want to use it.

Note how the `PlayPekkoHttp2Support` is also enabled. gRPC requires HTTP/2 transport and Play supports it only as an opt-in plugin.


### 2.a Serving (Akka) gRPC Services
### 2.a Serving (Pekko) gRPC Services

Have a look at the `conf/routes` file where you'll notice how to embed a gRPC router within a normal play application.
You can in fact mix normal Play routes with gRPC routers like this to offer a mixed service. You'll notice that we
Expand Down Expand Up @@ -51,4 +51,4 @@ Which in turn allows us to inject clients to any of the services defined in our

Since you may want to configure what service discovery or hardcoded location to use for each client, you may do so
as well in `conf/application.conf`, though we will not dive into this here. Refer to the documentation on
[using Pekko Discovery for endpoint discovery](https://developer.lightbend.com/docs/akka-grpc/current/client/configuration.html#using-akka-discovery-for-endpoint-discovery) for more details.
[using Pekko Discovery for endpoint discovery](https://pekko.apache.org/docs/pekko-grpc/current/client/configuration.html#using-pekko-discovery-for-endpoint-discovery) for more details.
4 changes: 2 additions & 2 deletions play-scala-grpc-example/docs/src/main/paradox/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,10 @@ The [Play Framework](https://www.playframework.com/) combines productivity and p
scalable web applications with Java and Scala. Play is developer friendly with a "just hit refresh" workflow and
built-in testing support. With Play, applications scale predictably due to a stateless and non-blocking architecture.

[Akka gRPC](https://developer.lightbend.com/docs/akka-grpc/current/overview.html) is a toolkit for building streaming
[Pekko gRPC](https://pekko.apache.org/docs/pekko-grpc/current/) is a toolkit for building streaming
gRPC servers and clients on top of Pekko Streams.

For detailed documentation refer to https://www.playframework.com/documentation/latest/Home and https://developer.lightbend.com/docs/akka-grpc/current/.
For detailed documentation refer to https://www.playframework.com/documentation/latest/Home and https://pekko.apache.org/docs/pekko-grpc/current/.

## Obtaining this example

Expand Down
8 changes: 5 additions & 3 deletions play-scala-grpc-example/project/plugins.sbt
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
resolvers += Resolver.ApacheMavenSnapshotsRepo

enablePlugins(BuildInfoPlugin)
val playGrpcV = "0.9.1"
val playGrpcV = "0.12.0"
buildInfoKeys := Seq[BuildInfoKey]("playGrpcVersion" -> playGrpcV)
buildInfoPackage := "play.scala.grpc.sample"

Expand All @@ -10,6 +12,6 @@ addSbtPlugin("com.lightbend.paradox" % "sbt-paradox" % "0.10.5")

// #grpc_sbt_plugin
// project/plugins.sbt
addSbtPlugin("com.lightbend.akka.grpc" %% "sbt-akka-grpc" % "1.0.2")
libraryDependencies += "com.lightbend.play" %% "play-grpc-generators" % playGrpcV
addSbtPlugin("org.apache.pekko" % "pekko-grpc-sbt-plugin" % "1.0.1-15-513a1c3f-SNAPSHOT")
libraryDependencies += "org.playframework" %% "play-grpc-generators" % playGrpcV
// #grpc_sbt_plugin
8 changes: 2 additions & 6 deletions test.sh
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,7 @@ pushd play-java-dagger2-example && scripts/test-sbt && popd
pushd play-java-ebean-example && scripts/test-sbt && popd
pushd play-java-fileupload-example && scripts/test-sbt && popd
pushd play-java-forms-example && scripts/test-sbt && popd
#if [ "$MATRIX_SCALA" != "3.x" ]; then
# pushd play-java-grpc-example && scripts/test-sbt && popd
#fi
pushd play-java-grpc-example && scripts/test-sbt && popd
pushd play-java-hello-world-tutorial && scripts/test-sbt && popd
pushd play-java-jpa-example && scripts/test-sbt && popd
pushd play-java-rest-api-example && scripts/test-sbt && popd
Expand All @@ -24,9 +22,7 @@ pushd play-scala-chatroom-example && scripts/test-sbt && popd
pushd play-scala-compile-di-example && scripts/test-sbt && popd
pushd play-scala-fileupload-example && scripts/test-sbt && popd
pushd play-scala-forms-example && scripts/test-sbt && popd
#if [ "$MATRIX_SCALA" != "3.x" ]; then
# pushd play-scala-grpc-example && scripts/test-sbt && popd
#fi
pushd play-scala-grpc-example && scripts/test-sbt && popd
pushd play-scala-hello-world-tutorial && scripts/test-sbt && popd
if [ "$MATRIX_SCALA" != "3.x" ]; then
pushd play-scala-isolated-slick-example && scripts/test-sbt && popd
Expand Down

0 comments on commit d711d0d

Please sign in to comment.