File tree 7 files changed +83
-6
lines changed
rpc/src/main/scala/server
7 files changed +83
-6
lines changed Original file line number Diff line number Diff line change
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 number Diff line number Diff line change
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
+ }
Original file line number Diff line number Diff line change 17
17
package freestyle .rpc .demo
18
18
package greeting
19
19
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
+
20
28
object GreetingClientApp {
21
29
22
30
def main (args : Array [String ]): Unit = {
@@ -51,6 +59,21 @@ object GreetingClientApp {
51
59
52
60
client.biStreamingDemo()
53
61
62
+ // EchoDemo using the same server where the greeting service is deployed.
63
+ echoDemo(EchoRequest (" echo..." ))
64
+
54
65
(): Unit
55
66
}
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
+ }
56
79
}
Original file line number Diff line number Diff line change @@ -19,7 +19,9 @@ package greeting.runtime
19
19
20
20
import freestyle ._
21
21
import freestyle .implicits ._
22
+ import freestyle .rpc .demo .echo .EchoServiceGrpc
22
23
import freestyle .rpc .demo .greeting ._
24
+ import freestyle .rpc .demo .greeting .service ._
23
25
import freestyle .rpc .server ._
24
26
import freestyle .rpc .server .implicits ._
25
27
import freestyle .rpc .server .handlers ._
@@ -32,7 +34,8 @@ object implicits {
32
34
implicit val ec : ExecutionContext = ExecutionContext .Implicits .global
33
35
34
36
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))
36
39
)
37
40
38
41
implicit val grpcServerHandler =
Original file line number Diff line number Diff line change
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
+ }
Original file line number Diff line number Diff line change 16
16
17
17
package freestyle .rpc .demo
18
18
package greeting
19
+ package service
19
20
20
21
import java .util .concurrent .{Executors , TimeUnit }
21
22
import java .util .concurrent .atomic .AtomicInteger
Original file line number Diff line number Diff line change @@ -27,9 +27,8 @@ package object server {
27
27
class GrpcConfigInterpreter [F [_]](implicit initConfig : Config , configList : List [GrpcConfig ])
28
28
extends (Kleisli [F , Server , ? ] ~> F ) {
29
29
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
33
32
.foldLeft[ServerBuilder [_]](ServerBuilder .forPort(initConfig.port))((acc, option) =>
34
33
(option match {
35
34
case DirectExecutor => acc.directExecutor()
@@ -45,8 +44,6 @@ package object server {
45
44
}).asInstanceOf [ServerBuilder [_]])
46
45
.build()
47
46
48
- private [this ] def build (configList : List [GrpcConfig ]): Server = interpret(configList)
49
-
50
47
override def apply [B ](fa : Kleisli [F , Server , B ]): F [B ] =
51
48
fa(build(configList))
52
49
You can’t perform that action at this time.
0 commit comments