Skip to content

Commit f9b3fad

Browse files
Provides a demo extension to combine different services (#14)
1 parent 57c1493 commit f9b3fad

File tree

7 files changed

+83
-6
lines changed

7 files changed

+83
-6
lines changed
+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
syntax = "proto3";
2+
3+
import "echo_messages.proto";
4+
5+
package freestyle.rpc.demo;
6+
7+
service EchoService {
8+
rpc Echo (EchoRequest) returns (EchoResponse);
9+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
syntax = "proto3";
2+
3+
package freestyle.rpc.demo;
4+
5+
message EchoRequest {
6+
string message = 1;
7+
}
8+
9+
message EchoResponse {
10+
string message = 1;
11+
}

demo/greeting/src/main/scala/greeting/GreetingClientApp.scala

+23
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,14 @@
1717
package freestyle.rpc.demo
1818
package greeting
1919

20+
import freestyle.rpc.demo.echo.EchoServiceGrpc
21+
import freestyle.rpc.demo.echo.EchoServiceGrpc.EchoServiceStub
22+
import freestyle.rpc.demo.echo_messages.EchoRequest
23+
import io.grpc.ManagedChannelBuilder
24+
25+
import scala.concurrent.Await
26+
import scala.concurrent.duration.Duration
27+
2028
object GreetingClientApp {
2129

2230
def main(args: Array[String]): Unit = {
@@ -51,6 +59,21 @@ object GreetingClientApp {
5159

5260
client.biStreamingDemo()
5361

62+
// EchoDemo using the same server where the greeting service is deployed.
63+
echoDemo(EchoRequest("echo..."))
64+
5465
(): Unit
5566
}
67+
68+
def echoDemo(request: EchoRequest): Unit = {
69+
70+
val channel =
71+
ManagedChannelBuilder.forAddress(host, portNode1).usePlaintext(true).build
72+
73+
val asyncEchoClient: EchoServiceStub = EchoServiceGrpc.stub(channel)
74+
75+
println("")
76+
println(s"Received -> ${Await.result(asyncEchoClient.echo(request), Duration.Inf)}")
77+
println("")
78+
}
5679
}

demo/greeting/src/main/scala/greeting/runtime/implicits.scala

+4-1
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,9 @@ package greeting.runtime
1919

2020
import freestyle._
2121
import freestyle.implicits._
22+
import freestyle.rpc.demo.echo.EchoServiceGrpc
2223
import freestyle.rpc.demo.greeting._
24+
import freestyle.rpc.demo.greeting.service._
2325
import freestyle.rpc.server._
2426
import freestyle.rpc.server.implicits._
2527
import freestyle.rpc.server.handlers._
@@ -32,7 +34,8 @@ object implicits {
3234
implicit val ec: ExecutionContext = ExecutionContext.Implicits.global
3335

3436
implicit val grpcConfigs: List[GrpcConfig] = List(
35-
AddService(GreeterGrpc.bindService(new GreetingService, ec))
37+
AddService(GreeterGrpc.bindService(new GreetingService, ec)),
38+
AddService(EchoServiceGrpc.bindService(new EchoService, ec))
3639
)
3740

3841
implicit val grpcServerHandler =
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
/*
2+
* Copyright 2017 47 Degrees, LLC. <http://www.47deg.com>
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package freestyle.rpc.demo
18+
package greeting
19+
package service
20+
21+
import freestyle.rpc.demo.echo.EchoServiceGrpc
22+
import freestyle.rpc.demo.echo_messages.{EchoRequest, EchoResponse}
23+
24+
import scala.concurrent.Future
25+
26+
class EchoService extends EchoServiceGrpc.EchoService {
27+
28+
override def echo(request: EchoRequest): Future[EchoResponse] = {
29+
println(s"Echo request ${request.message}")
30+
Future.successful(EchoResponse("Server Echo!"))
31+
}
32+
33+
}

demo/greeting/src/main/scala/greeting/GreetingService.scala renamed to demo/greeting/src/main/scala/greeting/service/GreetingService.scala

+1
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616

1717
package freestyle.rpc.demo
1818
package greeting
19+
package service
1920

2021
import java.util.concurrent.{Executors, TimeUnit}
2122
import java.util.concurrent.atomic.AtomicInteger

rpc/src/main/scala/server/package.scala

+2-5
Original file line numberDiff line numberDiff line change
@@ -27,9 +27,8 @@ package object server {
2727
class GrpcConfigInterpreter[F[_]](implicit initConfig: Config, configList: List[GrpcConfig])
2828
extends (Kleisli[F, Server, ?] ~> F) {
2929

30-
private[this] def interpret(configOptions: List[GrpcConfig])(
31-
implicit initConfig: Config): Server =
32-
configOptions
30+
private[this] def build(configList: List[GrpcConfig]): Server =
31+
configList
3332
.foldLeft[ServerBuilder[_]](ServerBuilder.forPort(initConfig.port))((acc, option) =>
3433
(option match {
3534
case DirectExecutor => acc.directExecutor()
@@ -45,8 +44,6 @@ package object server {
4544
}).asInstanceOf[ServerBuilder[_]])
4645
.build()
4746

48-
private[this] def build(configList: List[GrpcConfig]): Server = interpret(configList)
49-
5047
override def apply[B](fa: Kleisli[F, Server, B]): F[B] =
5148
fa(build(configList))
5249

0 commit comments

Comments
 (0)